Write a program that allows two players to play a game of tic-tac-toe. Use a twodimensional char array with three rows and three columns as the game board. Each element in the array should be initialized with an asterisk (*). The program should run a loop that:
• Displays the contents of the board array.
• Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row and column number.
• Allows player 2 to select a location on the board for an O. The program should ask the user to enter the row and column number.
• Determines whether a player has won, or a tie has occurred. If a player has won, the program should declare that player the winner and end. If a tie has occurred, the program should say so and end. At game end, show the menu to Play the Game, Display the Game Stats, or Quit. Player 1 wins when there are three Xs in a row on the game board. The Xs can appear in a row, in a column, or diagonally across the board. Player 2 wins with the same conditions. A tie occurs when all of the locations on the board are full, but there is no winner.
• The user is allowed to play the game multiple times. Use a menu to allow the player to choose to Play the Game, Display the Game Stats, or Quit. • Track game stats as follows - total number of games played, how many times Player 1 wins, how many times Player 2 wins, and how many tie games.
utilize a 2D array for the gameboard, and to utilize structs, functions, parameters, loops, branching and good design.
do not use pointers
#include <stdio.h>
int main() {
int winner = 0, count = 0;
int data[9], index, sign, player, flag, i, k, j;
for (i = 0; i < 9; i++)
data[i] = ' ';
while (count < 9) {
flag = 0;
system("clear");
printf("\n\n");
printf("\t\t\t %c | %c | %c \n", data[0], data[1], data[2]);
printf("\t\t\t----+----+----\n");
printf("\t\t\t %c | %c | %c \n", data[3], data[4], data[5]);
printf("\t\t\t----+----+---\n");
printf("\t\t\t %c | %c | %c \n", data[6], data[7], data[8]);
if (count % 2 == 0) {
sign = 'X';
player = 1;
} else {
sign = 'O';
player = 2;
}
printf("Move for player%d(1-9):", player);
scanf("%d", &index);
if (index < 1 || index > 9) {
printf("Allowed index is 1 to 9!!\n");
continue;
}
if (data[index - 1] == 'X' || data[index - 1] == 'O') {
printf("Position Already occupied!!\n");
sleep(1);
continue;
}
data[index - 1] = sign;
count++;
for (i = 0; i < 9; i++) {
if (i % 3 == 0)
flag = 0;
if (data[i] == sign)
flag++;
if (flag == 3) {
winner = 1;
goto win;
}
}
flag = 0;
for (i = 0; i < 3; i++) {
for (k = i; k <= i + 6; k = k + 3) {
if (data[k] == sign)
flag++;
}
if (flag == 3) {
winner = 1;
goto win;
}
flag = 0;
}
if ((data[0] == sign && data[4] == sign && data[8]
== sign) ||
(data[2] == sign && data[4] == sign && data[6] ==
sign)) {
winner = 1;
goto win;
}
}
win:
system("clear");
printf("\n\n");
printf("\t\t\t %c | %c | %c \n", data[0], data[1], data[2]);
printf("\t\t\t----+----+----\n");
printf("\t\t\t %c | %c | %c \n", data[3], data[4], data[5]);
printf("\t\t\t----+----+---\n");
printf("\t\t\t %c | %c | %c \n\n", data[6], data[7],
data[8]);
if (winner) {
printf("Player %d is the winner. Congrats!!\n", player);
} else {
printf("Match draw.. Best of luck for both\n");
}
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.