Tic-Tac-Toe Game Functions in C
Classified in Computers
Written at on English with a size of 7.68 KB.
#include <stdio.h>
// Name: Riya Patel, UWin No: 105159179
void printMenu()
{
printf("\n\nPress 'p' to print the tic-tac-toe board\n");
printf("Press 'c' to create a tic-tac-toe with some x and o cells\n");
printf("Press 't' to test if a tic-tac-toe board is valid or invalid\n");
printf("Press 'w' to predict winning cell for player x or o\n");
printf("Press 'e' to exit\n");
printf("Enter your choice: ");
}
void InitializeBoard(int m, int n, char board[][n])
{
int c = 1;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
board[i][j] = c + '0';
c++;
}
}
}
void PrintBoard(int m, int n, char board[][n])
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; j++)
{
j != 2 ? printf("%c |", board[i][j]) : printf("%c", board[i][j]);
}
if (i != 2)
printf("\n---|---|---\n");
}
printf("\n");
}
void CreateBoard(int m, int n, char board[][n])
{
int cell;
while (1)
{
printf("Enter the number of the cell where you want to insert X or O or enter -1 to exit: ");
scanf("%d", &cell);
if (cell == -1)
{
return;
}
else if (cell >= 1 && cell <= 9)
{
cell--;
int rIn = cell / 3;
int cIn = cell % 3;
char ch;
printf("Type X or O: ");
scanf(" %c", &ch);
if (ch == 'x' || ch == 'X')
{
ch = 'X';
}
else if (ch == 'o' || ch == 'O')
{
ch = 'O';
}
else
{
printf("Please enter a valid character. X or O only.\n");
continue;
}
board[rIn][cIn] = ch;
PrintBoard(m, n, board);
}
else
{
printf("Please enter a valid position.\n");
continue;
}
}
}
int IsValidBoard(int m, int n, char board[][n])
{
int i, j, countX = 0, CountY = 0;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if (board[i][j] == 'y' || board[i][j] == 'Y')
{
CountY++;
}
if (board[i][j] == 'x' || board[i][j] == 'X')
{
countX++;
}
}
}
int difference = countX - CountY;
if (difference >= -1 && difference <= 1)
{
return 1;
}
else
{
return 0;
}
}
int checkRowCrossed(int n, char board[][n], char checkMe)
{
int i, j, count = 0, cell = 0, exists = 0;
for (i = 0; i < n; i++)
{
count = 0;
for (j = 0; j < n; j++)
{
if (board[i][j] == checkMe)
{
count++;
}
if (board[i][j] != 'O' && board[i][j] != 'X')
{
cell = i * 3 + j + 1;
}
}
if (count == 2 && cell != 0)
{
exists = 1;
printf(" Cell # %d", cell);
}
}
return (exists);
}
int checkColumnCrossed(int n, char board[][n], char checkMe)
{
int i, j, count = 0, cell = 0, exists = 0;
for (i = 0; i < n; i++)
{
count = 0;
for (j = 0; j < n; j++)
{
if (board[j][i] == checkMe)
{
count++;
}
if (board[j][i] != 'O' && board[j][i] != 'X')
{
cell = j * 3 + i + 1;
}
}
if (count == 2 && cell != 0)
{
exists = 1;
printf(" Cell # %d", cell);
}
}
return (exists);
}
int checkDiagonalCrossed(int n, char board[][n], char checkMe)
{
int i, count = 0, cell = 0, exists = 0;
for (i = 0; i < 3; i++)
{
if (board[i][i] == checkMe)
{
count++;
}
if (board[i][i] != 'O' && board[i][i] != 'X')
{
cell = i * 3 + i + 1;
}
}
if (count == 2 && cell != 0)
{
exists = 1;
printf(" Cell # %d", cell);
}
count = 0;
cell = 0;
for (i = 0; i < 3; i++)
{
if (board[i][2 - i] == checkMe)
{
count++;
}
if (board[i][2 - i] != 'O' && board[i][2 - i] != 'X')
{
cell = i * 3 - i + 1 + 2;
}
}
if (count == 2 && cell != 0)
{
exists = 1;
printf(" Cell # %d", cell);
}
return (exists);
}
void ListWinningCells(int m, int n, char board[][n])
{
char check = 'O';
int n1, n2, n3;
n1 = checkColumnCrossed(n, board, check);
n2 = checkRowCrossed(n, board, check);
n3 = checkDiagonalCrossed(n, board, check);
int fnd = 0;
if (n1 == 1 || n2 == 1 || n3 == 1)
{
fnd = 1;
printf(" winning cells for player %c\n", check);
}
check = 'X';
n1 = checkColumnCrossed(n, board, check);
n2 = checkRowCrossed(n, board, check);
n3 = checkDiagonalCrossed(n, board, check);
if (n1 == 1 || n2 == 1 || n3 == 1)
{
fnd = 1;
printf(" winning cells for player %c", check);
}
if (fnd == 0)
{
printf("No winning cell for player O or X");
}
}
int main()
{
int r = 3;
int c = 3;
char board[r][c];
InitializeBoard(r, c, board);
char choice;
while (1)
{
printMenu();
scanf(" %c", &choice);
if (choice == 'p')
{
PrintBoard(r, c, board);
}
else if (choice == 'c')
{
CreateBoard(r, c, board);
}
else if (choice == 't')
{
int checkMe = IsValidBoard(r, c, board);
if (checkMe == 0)
{
printf("Not a valid board");
}
else
{
printf("It is a valid board");
} }
else if (choice == 'w')
{
ListWinningCells(r, c, board);
}
else if (choice == 'e')
{
break;
}
}
return 0;
}