C Tic Tac Toe game ⭕

preview_player
Показать описание
C tic tac toe game for beginners tutorial example explained

#C #game #tictactoe

This is a tictactoe game written in C designed for beginners
(This doesn't contain the useof pointers or other advanced C topics)
Рекомендации по теме
Комментарии
Автор

//This is a tic tac toe game written in C designed for beginners
//(This doesn't contain the use of pointers or other more advanced C topics)
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>

char board[3][3];
const char PLAYER = 'X';
const char COMPUTER = 'O';

void resetBoard();
void printBoard();
int checkFreeSpaces();
void playerMove();
void computerMove();
char checkWinner();
void printWinner(char);

int main()
{
char winner = ' ';
char response = ' ';

do
{
winner = ' ';
response = ' ';
resetBoard();

while(winner == ' ' && checkFreeSpaces() != 0)
{
printBoard();

playerMove();
winner = checkWinner();
if(winner != ' ' || checkFreeSpaces() == 0)
{
break;
}

computerMove();
winner = checkWinner();
if(winner != ' ' || checkFreeSpaces() == 0)
{
break;
}
}

printBoard();
printWinner(winner);

printf("\nWould you like to play again? (Y/N): ");
scanf("%c");
scanf("%c", &response);
response = toupper(response);
} while (response == 'Y');

printf("Thanks for playing!");

return 0;
}

void resetBoard()
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
board[i][j] = ' ';
}
}
}
void printBoard()
{
printf(" %c | %c | %c ", board[0][0], board[0][1], board[0][2]);
printf("\n---|---|---\n");
printf(" %c | %c | %c ", board[1][0], board[1][1], board[1][2]);
printf("\n---|---|---\n");
printf(" %c | %c | %c ", board[2][0], board[2][1], board[2][2]);
printf("\n");
}
int checkFreeSpaces()
{
int freeSpaces = 9;

for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
if(board[i][j] != ' ')
{
freeSpaces--;
}
}
}
return freeSpaces;
}
void playerMove()
{
int x;
int y;

do
{
printf("Enter row #(1-3): ");
scanf("%d", &x);
x--;
printf("Enter column #(1-3): ");
scanf("%d", &y);
y--;

if(board[x][y] != ' ')
{
printf("Invalid move!\n");
}
else
{
board[x][y] = PLAYER;
break;
}
} while (board[x][y] != ' ');

}
void computerMove()
{
//creates a seed based on current time
srand(time(0));
int x;
int y;

if(checkFreeSpaces() > 0)
{
do
{
x = rand() % 3;
y = rand() % 3;
} while (board[x][y] != ' ');

board[x][y] = COMPUTER;
}
else
{
printWinner(' ');
}
}
char checkWinner()
{
//check rows
for(int i = 0; i < 3; i++)
{
if(board[i][0] == board[i][1] && board[i][0] == board[i][2])
{
return board[i][0];
}
}
//check columns
for(int i = 0; i < 3; i++)
{
if(board[0][i] == board[1][i] && board[0][i] == board[2][i])
{
return board[0][i];
}
}
//check diagonals
if(board[0][0] == board[1][1] && board[0][0] == board[2][2])
{
return board[0][0];
}
if(board[0][2] == board[1][1] && board[0][2] == board[2][0])
{
return board[0][2];
}

return ' ';
}
void printWinner(char winner)
{
if(winner == PLAYER)
{
printf("YOU WIN!");
}
else if(winner == COMPUTER)
{
printf("YOU LOSE!");
}
else{
printf("IT'S A TIE!");
}
}

BroCodez
Автор

this is the most elaborating way of programming that invokes all the materials that you study in c programming. Thank you

naboulsikhalid
Автор

Thank you for providing this information for free man, appreciate it! I am about to go into my second year as a CS major and did not know where to go after learning Java so these tutorials have been great for learning the basic syntax of another language

AZureSC
Автор

Watching this tutorial in 2024 and absolutely enjoying learning C from you. Followed your course from the first video till the last and now i feel very confident in C language.
This guy never disappoints🙌.
Respect man❤❤
Keep making such insightful and useful videos

muhammadshahzaib
Автор

From a student who's learning programming and runs out of time, thank you a lot Bro Code

beckbrook
Автор

I'm just beginning to learn and followed this example. It's great, thank you. So far I've added the ability to choose whether to be X or O, who goes first, and made it alternate who goes first after each game. I'm going to work on the AI now.

partykeeps
Автор

Yo! Very good informative video . Btw advance congrats for 200k . U deserve more

solozlegend
Автор

I love you bro keep it up I am learning a lot thanks to you!! <3

takashikumi
Автор

Thanks bro, nice approach for this problem
This video definitely deserves more views

OfficialSumanMahato
Автор

Yo Bro Code, u gotta hit us up with some videos on pointers. I appreciate all the hard work u put in here cuz it really translates to a learning experience for me and many others here.

AbhitejBokka
Автор

Thank you for this. I easily altered it to make it a two player game, add an option for player names and a nice little menu. In the words of Homer SImpson "GAAHHHHGGHHH! More, please!"

Palmieres
Автор

hi bro I have ended this tutorial right now, and I want to start c++ tutorial . I want to say thank u for teaching fast and very good . best regards

sabaabiri
Автор

"Sit back relax and enjoy the show" :) and i also finished the course, Thanks bruh

official-k.i.d.u-k
Автор

Completed the whole series and now I'm ready to ace my midterms

astudent
Автор

Thanks ! I modified the code to set pawn and a toss will be performed to decide who will go first

minecraftcookie
Автор

Correct me if I'm wrong. The checkFreespaces() function within the computerMove() function is unnecessary since the main function already contains a while loop checking for that. The printWinner(' '); statement in the computerMove() function is also pointless since the winner=checkWinner() is already doing the job in main function.

Rid-E
Автор

Thank you, awesome breakdown....One small improvement: if you add && board[i][0] != ' ' to the check rows if statement in the checkeWinner() function (and of course && board[0][j] != ' ' for columns etc) it will ensure that it doesn't prematurely return from that function if it sees that for instance the top row is still all ' ' even if you win in the bottom row. It doesn't come up that often, but it's irritating when it does, I made a 2 player version of this so I was able to force this corner case and it drove me crazy until I finally figured out how to solve it with he simple fix above.

bsykesbeats
Автор

My first game reloaded thank you❣️ love from 🇳🇵

MandipChhetri
Автор

Hey bro..first of all thanks a lot for all the free courses... I would like to know something, How can i prevent the player from winning? And How the computer can block the player winning move?
like, if player move 2 X horizontally or vertically, how the computer can prevent player from winning? you response would be very helpful... I'm eagerly waiting for your response.

MehediHasanu
Автор

now we just need a video on this but with the language C# :)

gitch