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 in 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 and 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 and column number.
  • 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 say so and end. At game end, show the menu to Play the Game, Display the Game Stats, or Quit.

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 with the same conditions. A tie occurs when all of the locations on the board are full, but there is no winner.

Part 1

  • The user is allowed to play the game multiple times. Use a menu to allow the player to choose to Play the Game, Display the Game Stats, or Quit.
  • The screen clears each time the user plays a new game.
  • Track game stats as follows - total number of games played, how many times Player 1 wins, how many times Player 2 wins, and how many tie games.

DESIGN: Use these functions as guidelines. Other designs are acceptable as long as the program is complete, logical and accurate -- and utilizes a 2D array for the gameboard, structs, functions, parameters and good design.

  • Outside of main in global space, define a struct Player with member data for name, symbol (X or O), wins, losses, ties
  • main
    • TTT is a 2 player game. Either create 2 Player variables or an array of Players with 2 elements. Pass the structs as parameters when needed.
    • Declare a 2D char array for the gameboard and initialize each element to contain asterisks.
    • getPlayerInfo function: Prompt the user to input a name for each player. Player 1 goes first. Randomly assign X or O to the players.
    • Display a menu with 3 options - Play Game, Show Stats, and Quit.
    • Play game - Create a loop to display the board, allow player 1 to take a turn, check for a win or tie, allow player 2 to take a turn (if not a win or tie), display the board, if not a win or tie then loop again. Once a win or tie has been determined, display the game board one last time and display a message about the winner and show the menu
  • Show Stats - display how many games have been played, display a table showing each player's name, symbol and number of wins, losses and ties.
  • Quit - end the program
  • display the board - displays the current game state in a 3 x 3 board. Display the column headings as 1, 2, 3 and the row headings as 1, 2, 3. Use your own judgment on how to best format and display the game board to the player.
  • player turn - used for both X and O player. Use two parameters, the game board and the player symbol. Use a bool variable to flag whether the space is available. Ask the user to input a row and column to indicate which space to choose. Validate the input (must be 1, 2, or 3). Check for availability of that space (using row and column). If that space is available, then place the player symbol in that space and end the function. If the space is not available, then display a user-friendly error message and repeat the loop asking for a row and column.
  • game over - a value returning function, which returns a bool and accepts the game board as an argument. This function checks for three possibilities - a winning game (game over is true), a game in which a win is still possible (game over is false) or a tie (game over is true). This function calls the player wins function.
  • player wins - a value returning function, which returns a bool and accepts the game board and a player symbol (X or O) as arguments. It returns true if the player has won.
  • can player win - a value returning function, which returns a bool and accepts the game board and a player symbol (X or O) as arguments. The can player win function returns true if the player (X or O) can still win, otherwise it returns false which means the game is a tie.
  • display the winner - a void returning function which accepts the game board as an argument. The display winner function displays the game board and a message about the winner
  • update game stats - Track game stats as follows - total number of games played, Player 1 wins, Player 1 losses, Player 2 wins, Player 2 losses, number of tie games.
  • show game stats - Display all game stats in a user-friendly format.

Part 2

Add an option to the menu for a 1 player game where the players plays against the computer. Your job is to add artificial intelligence for the computer to play against the player. For example, you may use logic such as this for the computer: If there is a winning move then take it. Else if the middle spot is open, take it. Else, randomly take an open spot. Feel free to improve upon this logic. This is just a suggestion for a simple Artificial Intelligence (AI).

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.