Question: Tic-Tac-Toe Game 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

Tic-Tac-Toe Game

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: 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 F5 to compile and run the program. Your program must run without errors to get full credit! Upload (attach!) your cpp file here for grading. It must be named correctly and saved as a readable cpp file with the proper extension.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!