Question

In: Computer Science

I am using C++ Write a program that allows two players to play a game of...

I am using C++

Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following:

  • 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 numbers.
  • Determine if player 1 has won or a tie has occurred.
  • Allows player 2 to select a location on the board for an 0. The program should ask the user to enter the row and column numbers.
  • Determine if player 2 has won or a tie has occurred.

If a player wins, the program should immediately declare that player the winner and end. If a tie has occurred, the program should display an appropriate message and end. (You probably already know this, but just in case. 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 when there are three Os in a row on the game board. The Os can appear in a row, in a column, or diagonally across the board. A tie occurs when all of the locations on the board are full, but there is no winner.)

Note: Do not use global variables, except for global constants! You must create and use at least two functions, but are encouraged to modularize as much as possible. Each function must be named appropriately and obvious to what it does. The main function should have little more than an array for the board and call to the various functions. You should also define constants for the number of rows and columns and use them throughout the program. Make sure to check for valid input and ask again if necessary. Keep it simple and keep it clean! Here are some functions (not a complete list!) that I would suggest.

void displayBoard(char board[][COLS]);
void playerTurn(char board[][COLS], char player);
bool checkForWinner(char board[][COLS], char player);
bool isBoardFull(char board[][COLS]);

The output should look something like this -- user inputs are in bold blue type:
       Columns
        1 2 3
Row 1:  * * *
Row 2:  * * *
Row 3:  * * *
Player X's turn.
Enter a row and column to place an X.
Row: 1
Column: 2

       Columns
        1 2 3
Row 1:  * X *
Row 2:  * * *
Row 3:  * * *
Player O's turn.
Enter a row and column to place an O.
Row: 1
Column: 2
That location is not available. Select another location.
Row: 4
Invalid Row!
Row: 2
Column: 3

       Columns
        1 2 3
Row 1:  * X *
Row 2:  * * O
Row 3:  * * *
Player X's turn.
Enter a row and column to place an X.
Row: 3
Column: 1

       Columns
        1 2 3
Row 1:  * X *
Row 2:  * * O
Row 3:  X * *
Player O's turn.
Enter a row and column to place an O.
Row: 2
Column: 1

       Columns
        1 2 3
Row 1:  * X *
Row 2:  O * O
Row 3:  X * *
Player X's turn.
Enter a row and column to place an X.
Row: 2
Column: 2

       Columns
        1 2 3
Row 1:  * X *
Row 2:  O X O
Row 3:  X * *
Player O's turn.
Enter a row and column to place an O.
Row: 1
Column: 3

       Columns
        1 2 3
Row 1:  * X O
Row 2:  O X O
Row 3:  X * *
Player X's turn.
Enter a row and column to place an X.
Row: 3
Column: 2

       Columns
        1 2 3
Row 1:  * X O
Row 2:  O X O
Row 3:  X X *
Player 1 (X) WINS!!!!!


Compile, run, and test your program. Make sure there are no errors and it functions properly. You can hit [Ctrl or command]+[Shift]+B to compile and run the program. Your program must run without errors to get full credit! Commit and push (upload) your repo, including your cpp file to GitHub for grading. It must be named correctly and saved as a readable cpp file with the proper extension.

Solutions

Expert Solution

#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 = '*';

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

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

board[i][j]= 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(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";

}

}

=====================================
SEE OUTPUT

PLEASE COMMENT if there is any concern.

==============================


Related Solutions

Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional...
Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: 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...
Write a program using Python that allows the user to play a guessing game (please provide...
Write a program using Python that allows the user to play a guessing game (please provide a picture of code so it is easier to read). The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: Normally, we would have the program select a random number as the "secret number". However, for the purpose of testing your program (as well as grading it), simply use an assignment...
Design and implement a Python program which will allow two players to play the game of...
Design and implement a Python program which will allow two players to play the game of Tic-Tac-Toe in a 4x4 grid! X | O | X | O -------------- O | O | X | O -------------- X | X | O | X -------------- X | X | O | X The rules for this game is the same as the classic, 3x3, game – Each cell can hold one of the following three strings: "X", "O", or "...
IN C++ Write a program to play the Card Guessing Game. Your program must give the...
IN C++ Write a program to play the Card Guessing Game. Your program must give the user the following choices: - Guess only the face value of the card. -Guess only the suit of the card. -Guess both the face value and suit of the card. Before the start of the game, create a deck of cards. Before each guess, use the function random_shuffle to randomly shuffle the deck.
Tony Gaddis C++ Tic-Tac-Toe Write a program that allows two players (player X and player O)...
Tony Gaddis C++ Tic-Tac-Toe Write a program that allows two players (player X and player O) to play a game of tic-tac-toe. Use a two- dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The players take turns making moves and the program keeps track of whose turn it is. Player X moves first. The program should run a loop that: Displays the contents...
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...
Subjects: Write a Python program that allows users to play the popular rock-paper-scissor game against the...
Subjects: Write a Python program that allows users to play the popular rock-paper-scissor game against the computer multiple times. This program assesses your knowledge of decision structures, repetition structures, and functions. Requirements: 1.Define a function named as play_game. This function receives two parameters representing the player’s and computer’s choices, and it returns an integer representing the game result from the player’s perspective. Specifically, it returns 1 if the player wins and 0 if the player does not win. Hint: you...
PYTHON: Write the code to play a card game called Battle. Two players each have a...
PYTHON: Write the code to play a card game called Battle. Two players each have a card deck consisting of the following cards: two, three, four, … jack, queen, king, ace, in increasing order. One card deck could be represented by a list such as: cardsPlayer1 = ["two", "three", "four"..."jack", "queen", "king", "ace"] Both players have a card randomly selected. When a card is selected, remove it from the player’s deck. The player that plays the higher of the two...
C program with functions Make a dice game with the following characteristics: must be two players...
C program with functions Make a dice game with the following characteristics: must be two players with two dice. when player 1 rolls the marker is annotated when player 2 rolls the marker is annotated in other words, for each roll the added scores must be shown. Example: Round 1: player 1 score = 6 player 2 score = 8 Round 2: player 1 score = 14 player 2 score = 20 Whoever reaches 35 points first wins. if a...
Two players A and B play a game of dice . They roll a pair of...
Two players A and B play a game of dice . They roll a pair of dice alternately . The player who rolls 7 first wins . If A starts then find the probability of B winning the game ?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT