We want you to make a Program that will allow two people to play Tic Tac Toe. Yo
ID: 3641055 • Letter: W
Question
We want you to make a Program that will allow two people to play Tic Tac Toe.You might recall that Tic Tac Toe is a game that is played on a 3x3 board. There are two players; X, who places Xs on the board, and O, who places Os on the board. They take turns, and the winner is the one who can place three of their symbols in a row.
You only need to worry about the case where two people are playing Tic Tac Toe against each other, not in the case where the computer attempts to play Tic Tac Toe against a person.
Some hints.
The board can be represented as a two dimensional array. (which code like char board[3][3]; or similar).
If X chooses a square, then O cannot choose it and vice versa.
If X chooses a square than X cannot choose the same square on a subsequent turn.
If a player has won, then they will have their symbols in every row in a column, every column in a row, or on a diagonal. Assuming the first subscript is x, and the second subscript is y then the board
_XO
_XO
_X_
is a win for x, because board[1][0] == board[1][1] == board[1][2] and board[1][0] == X.
Try to write functions for everything, breaking the task into individual functions (To draw the board on the screen, to take in input, to check for a winner, etc) will make the program easier to write.
Explanation / Answer
Simple TicTacToe Program in C.
#include<stdio.h>
int main()
{
int i = 0; // Loop counter
int player = 0; // Player number - 1 or 2
int go = 0; // Square selection number for turn
int row = 0; // Row index for a square
int column = 0; // Column index for a square
int line = 0; // Row or column index in checking loop
int winner = 0; // The winning player
char answer = 'n';
char board[3][3] =
{
{'1','2','3'},
{'4','5','6'},
{'7','8','9'}
};
// The main game loop. The game continues for up to 9 turns
// As long as there is no winner
for( i = 0; i<9 && winner==0; i++)
{
/* Display the board */
printf(" ");
printf(" %c | %c | %c ", board[0][0], board[0][1], board[0][2]);
printf("---+---+--- ");
printf(" %c | %c | %c ", board[1][0], board[1][1], board[1][2]);
printf("---+---+--- ");
printf(" %c | %c | %c ", board[2][0], board[2][1], board[2][2]);
player = i%2 + 1; // Select player
// Get valid player square selection
do
{
printf(" Player %d, please enter the number of the square "
"where you want to place your %c: ", player,(player==1)?'X':'O');
scanf("%d", &go);
row = --go/3; // Get row index of square
column = go%3; // Get column index of square
}
while(go<0 || go>9 || board[row][column]>'9');
board[row][column] = (player == 1) ? 'X' : 'O'; // Insert player symbol
// Check for a winning line - diagonals first
if((board[0][0] == board[1][1] && board[0][0] == board[2][2]) ||
(board[0][2] == board[1][1] && board[0][2] == board[2][0]))
winner = player;
else
// Check rows and columns for a winning line
for(line = 0; line <= 2; line ++)
if((board[line][0] == board[line][1] && board[line][0] == board[line][2])||
(board[0][line] == board[1][line] && board[0][line] == board[2][line]))
winner = player;
}
// Game is over so display the final board
printf(" ");
printf(" %c | %c | %c ", board[0][0], board[0][1], board[0][2]);
printf("---+---+--- ");
printf(" %c | %c | %c ", board[1][0], board[1][1], board[1][2]);
printf("---+---+--- ");
printf(" %c | %c | %c ", board[2][0], board[2][1], board[2][2]);
// Display result message
if(winner == 0)
printf("It's is a draw ");
else
printf("Congratulations, Player %d, YOU WON! ", winner);
printf("Would you like to play again? y/n :");
getchar();
scanf("%c", &answer);
while (answer == 'y');
system("Pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.