Question

Write a program that allows two players to play a game of tic-tac-toe. Use a twodimensional...

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

Homework Answers

Answer #1

#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;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
***JAVA*** Write a program that randomly populates a standard tic tac toe board, and then determines...
***JAVA*** Write a program that randomly populates a standard tic tac toe board, and then determines if there is a winner or if there is a tie (“cat”). Also the program should print out the randomly generated board to verify its findings. You may also assume that the board is fully populated before a winner is checked, and in the event that both players have a winning condition print out that both win. Example Dialog: Welcome to Random Tic Tac...
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to...
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. // A tic-tac-toe board looks like a table of three rows and three columns partially or completely filled with the characters X and O. // At any point, a cell of that table could be empty or could contain an X or an O. You should have one instance variable, a two-dimensional array of values representing the...
R-S-P Requirement: - write one C++ program that mimics the Rock-Scissor-Paper game. This program will ask...
R-S-P Requirement: - write one C++ program that mimics the Rock-Scissor-Paper game. This program will ask user to enter an input (out of R-S-P), and the computer will randomly pick one and print out the result (user wins / computer wins). - User's input along with computer's random pick will be encoded in the following format: -- user's rock: 10 -- user's scissor:          20 -- user's paper:            30 -- comp's rock:            1 -- comp's scissor:        2 -- comp's...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the computer through a user interface. The user will choose to throw Rock, Paper or Scissors and the computer will randomly select between the two. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should then reveal the computer's choice and print a statement indicating if the user won, the computer won, or if it was a tie. Allow...
python: write a two card poker game, the user and the computer will get two cards,...
python: write a two card poker game, the user and the computer will get two cards, cards are an integer value between 2-14 and are printed out as 2,3,4,5,6,7,8,9,10,J,Q,K,A. the winner is when: if both players have a pair then the pair with the highest numerical value wins, if only one player has a pair, then the pair wins, if neither the players has a pair, then the player with the highest card wins, if both players highest card is...
Python3, please wite the code with comments 2. Write a program to implement the Pokemon game...
Python3, please wite the code with comments 2. Write a program to implement the Pokemon game using your algorithm from It should ask the user to enter their type and compare it with a randomly chosen computer type and correctly display who wins/ties. Write an algorithm for resolving a battle among the classic Pokemon types. The user should select a type of either fire, water or grass. The computer should randomly select one of those three types as well.      ...
Write a Java program segment (not an entire program) that will summarize the season results for...
Write a Java program segment (not an entire program) that will summarize the season results for 5 players on one of TRU’s basketball teams. Each player played 10 games. Your program segment should ask the user for a player’s name, followed by the points that player scored in each of the 10 games (a total of 11 pieces of input). Your program will then produce the summary line shown below for that player.   Your program should get the data for...
in c++ : Problem Domain: The classic TicTacToe game alternatively place Xs and Os on a...
in c++ : Problem Domain: The classic TicTacToe game alternatively place Xs and Os on a 3x3 grid. The winner is the first player to place 3 consecutive marks in a horizontal, vertical or diagonal row. Understanding the Problem: In this assignment, you will: 1) Create a tic tac toe board data structure. 2) Check for game over. 3) Clear the tic tac toe game for each game. 4) Display the tic tac toe board to screen. 5) Simulate playing...
Your group is required to develop a simple Android BlackJack card game. Part B (35 Marks)...
Your group is required to develop a simple Android BlackJack card game. Part B – Group Submission (Do it on android studio) i need the full working code in android studio The program shall fulfil the following requirements: 1) There are total of 4 main features : User authentication/login, game setting, playing game and view history. User Login 2) Allow the player to register new user account or login using the existing account to play the game. Each user account...
You are coding a simple game called Pig. Players take turns rolling a die. The die...
You are coding a simple game called Pig. Players take turns rolling a die. The die determines how many points they get. You may get points each turn your roll (turn points), you also have points for the entire game (grand points). The first player with 100 grand points is the winner. The rules are as follows: Each turn, the active player faces a decision (roll or hold): Roll the die. If it’s is a: 1: You lose your turn,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT