Tic Tac Toe Program
Tic Tac Toe Program
h>
#include <stdlib.h>
int main() {
char board[3][3] = { //Declares board with numbers
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}
};
int currentPlayer = 1; // Player 1's turn
int moves = 0; // Looping variable
int choice; // Board position choice
char mark;
displayBoard(board);
// Loop until the number of moves is less than 9
// (i.e., there are still empty cells on the game board)
while (moves < 9) {
mark = (currentPlayer == 1) ? 'X' : 'O'; //If currentPlayer is equal to 1, assign 'X' to mark; otherwise,
assign 'O' to mark."
printf("\nPlayer %d, enter a number (1-9) to place %c: ", currentPlayer, mark);
scanf("%d", &choice);
board[row][col] = mark;
displayBoard(board);
if (checkWin(board, mark)) {
printf("\nPlayer %d wins!\n", currentPlayer);
break;
}
currentPlayer = (currentPlayer == 1) ? 2 : 1;
moves++;
}
if (moves == 9) {
printf("\nIt's a tie!\n");
}
return 0;
}