Question

In: Computer Science

I need a good pseudocode for C++ for a tic tac toe program.

I need a good pseudocode for C++ for a tic tac toe program.

Solutions

Expert Solution

Solution:

Pseudocode / program in c++ as follows:

#include <iostream>

using namespace std;

void initializeBoard(char board[][3]);

void displayBoard(char board[][3]);/*gets input from user returns character number of location chosen by user*/

void getInput(char board[][3], char marker,int& x,int& y);/* mark character marker of player on chosen character pos of board*/

void markBoard(char board[][3],int x,int y, char marker);/*checks to see if someone has won returns true or false*/

bool gameOver(char board[][3]); /*checks to see if someone won on rows of board*/

bool checkHorizontal(char board[][3], char marker);/*checks to see if someone won on columns of board*/

bool checkVertical(char board[][3], char marker);/*checks to see if someone won on diagonal of board*/

bool checkDiagonal(char board[][3], char marker);/*checks to see if players have tied*/

bool checkTie(char board[][3]);/*prints winner as marker or ties*/

void printWinner(char marker);/*checks to see if selected location is available returns false when location has already been taken or is an invalid number*/

bool validMove(char board[][3], int x,int y);

int main()

{

srand(time(0));

int start=rand()%2;

char board[3][3];

char exit;

char f,s;

int x,y;

f='X';

s='O';

initializeBoard(board);

displayBoard(board);

while(true)

{

f = '1';

getInput(board,f,x,y);

f = 'X';

markBoard(board, x,y, f);

displayBoard(board);

if (gameOver(board))

break;

s = '2';

getInput(board,s,x,y);

s='O';

markBoard(board, x,y, s);

displayBoard(board);

if (gameOver(board))

break;

}

system("pause");

return 0;

}

void initializeBoard(char board[][3])

{

char count = '1';

for (int i = 0; i<3; i++) {

for (int j = 0; j<3; j++){

board[i][j]= count;

count++;

}

}

}

bool validMove(char board[][3], int x, int y)

{if(x<0||x>2||y<0||y>2)

return false;

if(board[x][y]=='X'||board[x][y]=='O')

return false;

else

return true;

}

void displayBoard(char board[][3])

{ int t;

for(t=0; t<3; t++) {

cout<<" "<<board[t][0]<<" | "<< board[t][1]<<" | "<< board[t][2];

if(t!=2) cout<<"\n---|---|---\n";

}

cout<<"\n";

}

void getInput(char board[][3], char marker,int& x,int& y)

{for(;;)

{cout << "Player "<< marker << " Enter a Row and Column (between 1-3): ";

cin >> x>>y;

x--;

y--;

if (validMove(board,x,y))

return;

cout << "Invalid Move: Please Try Again\n\n";

}

}

void markBoard(char board[][3], int x,int y, char marker)

{ board[x][y]=marker;

}

bool gameOver(char board[][3])

{if (checkHorizontal(board,'X'))

{printWinner('X');

return true;

}

if (checkVertical(board, 'X'))

{printWinner('X');

return true;

}

if (checkDiagonal(board, 'X'))

{ printWinner('X');

return true;

}

if (checkHorizontal(board,'O'))

{printWinner('O');

return true;

}

if (checkVertical(board, 'O'))

{printWinner('O');

return true;

}

if (checkDiagonal(board, 'O'))

{printWinner('O');

return true;

}

if (checkTie(board))

{printWinner('T');

return true;

}

return false;

}

bool checkHorizontal(char board[][3], char marker)

{int i,j,count;

for(i=0; i<3; i++)

{count=0;

for(j=0;j<3;j++)

if(board[i][j]==marker)

count++;

if(count==3)

return true;

}

return false;

}

bool checkVertical(char board[][3], char marker)

{int i,j,count;

for(i=0; i<3; i++)

{count=0;

for(j=0;j<3;j++)

if(board[j][i]==marker)

count++;

if(count==3)

return true;

}

return false;

}

bool checkDiagonal(char board[][3], char marker)

{if(board[0][0]==board[1][1] && board[1][1]==board[2][2]&& board[0][0]==marker)

return true;

if(board[0][2]==board[1][1] && board[1][1]==board[2][0]&& board[0][2]==marker)

return true;

return false;

}

bool checkTie(char board[][3])

{int i,j;

for(i=0;i<3;i++)

for(j=0;j<3;j++)

if(isdigit(board[i][j]))

return false;

return true;

}

void printWinner(char marker)

{ if(marker=='T')

cout<<"TIE GAME!\n";

else {

if(marker =='X') {

marker = '1';

}else{

marker = '2';

}

cout<<"The winner is player "<<marker<<"!!!\n";

}

}

Please give thumbsup, if you like it. Thanks.


Related Solutions

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
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...
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 C# A Tic Tac Toe program that uses functions and arrays. The program should start...
In C# A Tic Tac Toe program that uses functions and arrays. The program should start by asking the player one for their name and then player two for their name. Then should print the board and ask the user what column and then what row they would like to put their x (or o, depending on the player) . Use the clear function to make it look as though the board is never repeating.. Thanks!
I am having an issue with the Java program with a tic tac toe. it isn't...
I am having an issue with the Java program with a tic tac toe. it isn't a game. user puts in the array and it prints out this. 1. Write a method print that will take a two dimensional character array as input, and print the content of the array. This two dimensional character array represents a Tic Tac Toe game. 2. Write a main method that creates several arrays and calls the print method. Below is an example of...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has...
Using C language Problem!! <3 x 3 tic tac toe> 2 players are doing tic tac...
Using C language Problem!! <3 x 3 tic tac toe> 2 players are doing tic tac toe alternatively. Anyone could win when they first make a bingo for any line(horizontal, vertical, diagonal) [Constraints] 1. Use 2dimensional Array(3 X 3) 2. Each elements are 1 or 2 so that can show their player1, player2's mark. 3. Inputs are 1 through 9 as you can see 1 2 3 4 5 6 7 8 9 4. No inputs are duplicated 5. if...
How to make a 2D array Tic Tac Toe game in C?
How to make a 2D array Tic Tac Toe game in C?
In the C++ programming language write a program capable of playing Tic-Tac-Toe against the user. Your...
In the C++ programming language write a program capable of playing Tic-Tac-Toe against the user. Your program should use OOP concepts in its design. You can use ASCII art to generate and display the 3x3 playing board. The program should randomly decide who goes first computer or user. Your program should know and inform the user if an illegal move was made (cell already occupied). The program should also announce if one of the players wins or if a draw...
Programming assignment (100 pts): In the C++ programming language write a program capable of playing Tic-Tac-Toe...
Programming assignment (100 pts): In the C++ programming language write a program capable of playing Tic-Tac-Toe against the user. Your program should use OOP concepts in its design. You can use ASCII art to generate and display the 3x3 playing board. The program should randomly decide who goes first computer or user. Your program should know and inform the user if an illegal move was made (cell already occupied). The program should also announce if one of the players wins...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT