Question

In: Computer Science

How to make a 2D array Tic Tac Toe game in C?

How to make a 2D array Tic Tac Toe game in C?

Solutions

Expert Solution

Below code demonstrates a Tic Tac Toe game in C language.
The variable and function names are self-explanatory:
For e.g. playersMove() function is for getting first player's i.e. user's move, while the computersMove()
function gets the second player's, i.e. computer's move.
The displayGame() method displays the results on the output screen.


Code starts below this line:

#include <stdio.h>
#include <stdlib.h>

char ticTacToe[3][3]; .

char findWinner(void);
void initializeGame(void);
void playersMove(void);
void computersMove(void);
void displayGame(void);

int main(void)
{
char gameOver;

printf("Tic Tac Toe.\n");
printf("Player 1: You\n");
printf("Player 2: Computer\n");
  
gameOver = ' ';
initializeGame();

do {
displayGame();
playersMove();
gameOver = findWinner();
if(gameOver!= ' ')
       break;
computersMove();
gameOver = findWinner();
} while(gameOver== ' ');

if(gameOver=='X') printf("Congrats!! You won the Game!\n");
else printf("Computer wins!! You Lose!!\n");
displayGame();

return 0;
}

void initializeGame(void)
{
int i, j;

for(i=0; i<3; i++)
for(j=0; j<3; j++) ticTacToe[i][j] = ' ';
}

void playersMove(void)
{
int x, y;

printf("Enter X,Y coordinates for your move: ");
scanf("%d%*c%d", &x, &y);

x--; y--;

if(ticTacToe[x][y]!= ' '){
printf("Not a valid move, play again.\n");
playersMove();
}
else ticTacToe[x][y] = 'X';
}

void computersMove(void)
{
int i, j;
for(i=0; i<3; i++){
for(j=0; j<3; j++)
if(ticTacToe[i][j]==' ') break;
if(ticTacToe[i][j]==' ') break;
}

if(i*j==9) {
printf("draw\n");
exit(0);
}
else
ticTacToe[i][j] = 'O';
}

void displayGame(void)
{
int t;

for(t=0; t<3; t++) {
printf(" %c | %c | %c ",ticTacToe[t][0],
ticTacToe[t][1], matrix [t][2]);
if(t!=2) printf("\n---|---|---\n");
}
printf("\n");
}

char findWinner(void)
{
int i;

for(i=0; i<3; i++)
if(ticTacToe[i][0]==ticTacToe[i][1] &&
ticTacToe[i][0]==ticTacToe[i][2]) return ticTacToe[i][0];

for(i=0; i<3; i++)
if(ticTacToe[0][i]==ticTacToe[1][i] &&
ticTacToe[0][i]==ticTacToe[2][i]) return ticTacToe[0][i];

if(ticTacToe[0][0]==ticTacToe[1][1] &&
ticTacToe[1][1]==ticTacToe[2][2])
return ticTacToe[0][0];

if(ticTacToe[0][2]==ticTacToe[1][1] &&
ticTacToe[1][1]==ticTacToe[2][0])
return ticTacToe[0][2];

return ' ';
}


Related Solutions

C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed. I never said what type of code I needed in a previous question. I apologize and I can't go back and change it so here is the same question with more information Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players...
How to make tic tac toe game in javascript with vue.js
How to make tic tac toe game in javascript with vue.js
Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes....
Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed (such as #include <stdio.h> etc). Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players (you and a friend) take turns placing X’s and O’s onto the board. After each turn, the current board configuration should be displayed, and once a player connects...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below: The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players...
- Make a 2-D array to represent a Tic-Tac-Toe game/grid. Place a couple of 'X's and...
- Make a 2-D array to represent a Tic-Tac-Toe game/grid. Place a couple of 'X's and 'O's in the grid and display it. Do some extra printing to make it look like a Tic-Tac-Toe board we are suppose to use rows and columns so just make it simple thanks!
The objective of this assignment is to implement the tic-tac-toe game with a C program. The...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with ‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of...
In a game of tic tac toe. How do you check if all the positions in...
In a game of tic tac toe. How do you check if all the positions in a double array are taken, the double arrays are used to store the x's and o's?
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications for a tic tac toe game object: class tictactoeGame { public: char boardConfig[3][3]; // two dimensional array stores current board configuration // Constructor: // set boardConfig[i][j] to be ' ' (the space character) // for 0<= i <= 2, 0<= j <= 2 tictactoeGame() { //fill this in } //put an 'X' character at the given location bool placeX(int x, int y) { //fill...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications for a tic tac toe game object: class tictactoeGame { public: char boardConfig[3][3]; // two dimensional array stores current board configuration // Constructor: // set boardConfig[i][j] to be ' ' (the space character) // for 0<= i <= 2, 0<= j <= 2 tictactoeGame() { //fill this in } //put an 'X' character at the given location bool placeX(int x, int y) { //fill...
explain a pseudocode for tic tac toe in c++ between a computer and a player in...
explain a pseudocode for tic tac toe in c++ between a computer and a player in words
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT