Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a C++ 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

Write a C++ 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:

  • 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 number and then the 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 number and then the column number.
  • Validates the user's input:
  • Do not let the user input a row outside the range [1,3]
  • Do not let the user input a column outside the range [1,3]
  • Do not let the user input into a cell that already has an 'X' or 'O'
  • If the user enters an invalid input, just ask them for that input again.
  • This is best done using nested do-while loops. Below is some pseudo-code that will help with this:
  do { do { output "Row: " input row; }while(row is outside range); do { output "Column: "; input col; }while(col is outside range); }while(cell at row, col contains 'X' or 'O');  
  • 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 declare that and end.

  • These are the following end game outputs:

    • Player 1 wins!
    • Player 2 wins!
    • Tie!
  • 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. The same logic applies to player 2, but with Os. A tie occurs when all of the locations on the board are full, but there is no winner.

Below is a sample output where player 1 wins:

  1 2 3 1 * * * 2 * * * 3 * * * Player 1, Row: 1 Player 1, Column: 1 1 2 3 1 X * * 2 * * * 3 * * * Player 2, Row: 1 Player 2, Column: 2 1 2 3 1 X O * 2 * * * 3 * * * Player 1, Row: 2 Player 1, Column: 2 1 2 3 1 X O * 2 * X * 3 * * * Player 2, Row: 3 Player 2, Column: 1 1 2 3 1 X O * 2 * X * 3 O * * Player 1, Row: 3 Player 1, Column: 3 1 2 3 1 X O * 2 * X * 3 O * X Player 1 wins!  

Next is a sample output where player 2 wins after ignoring some invalid input:

  1 2 3 1 * * * 2 * * * 3 * * * Player 1, Row: 1 Player 1, Column: 1 1 2 3 1 X * * 2 * * * 3 * * * Player 2, Row: 3 Player 2, Column: 1 1 2 3 1 X * * 2 * * * 3 O * * Player 1, Row: 1 Player 1, Column: 2 1 2 3 1 X X * 2 * * * 3 O * * Player 2, Row: 1 Player 2, Column: 3 1 2 3 1 X X O 2 * * * 3 O * * Player 1, Row: 1 Player 1, Column: 3 Player 1, Row: 9 Player 1, Row: 9 Player 1, Row: 2 Player 1, Column: 1 1 2 3 1 X X O 2 X * * 3 O * * Player 2, Row: 2 Player 2, Column: 2 1 2 3 1 X X O 2 X O * 3 O * * Player 2 wins!  
  1 2 3 1 * * * 2 * * * 3 * * * Player 1, Row: 1 Player 1, Column: 1 1 2 3 1 X * * 2 * * * 3 * * * Player 2, Row: 2 Player 2, Column: 1 1 2 3 1 X * * 2 O * * 3 * * * Player 1, Row: 1 Player 1, Column: 2 1 2 3 1 X X * 2 O * * 3 * * * Player 2, Row: 2 Player 2, Column: 2 1 2 3 1 X X * 2 O O * 3 * * * Player 1, Row: 2 Player 1, Column: 3 1 2 3 1 X X * 2 O O X 3 * * * Player 2, Row: 1 Player 2, Column: 3 1 2 3 1 X X O 2 O O X 3 * * * Player 1, Row: 3 Player 1, Column: 1 1 2 3 1 X X O 2 O O X 3 X * * Player 2, Row: 3 Player 2, Column: 2 1 2 3 1 X X O 2 O O X 3 X O * Player 1, Row: 3 Player 1, Column: 3 1 2 3 1 X X O 2 O O X 3 X O X Tie! 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

6th Edition

1943153035, 978-1943153039

More Books

Students also viewed these Databases questions

Question

18 Impact of total rewards on recruitment and retention.

Answered: 1 week ago

Question

What is the basis for Security Concerns in Cloud Computing?

Answered: 1 week ago

Question

Describe the three main Cloud Computing Environments.

Answered: 1 week ago