Question: Problem 1 For this project you will write a program that implements the game of Tic-Tac-Toe where you can play against the computer (player 1
Problem 1
For this project you will write a program that implements the game of Tic-Tac-Toe where you can play against the computer (player 1 will be the user, player 2 will be the computer). Your program should go through the following steps:
Generate an empty Tic-Tac-Toe table (3x3 array) Run a loop as long as none of the players have placed three in a row (a player has won) and the table is not full (stalemate). This loop should: Display the current layout of the table blank squares are displayed as underscores O for player 1's moves (user) X for player 2's moves (computer) put spaces between the squares If it is player 1's turn (the user): ask the user to enter his/her selection (the location in the table where the X or O should be placed (row, col)). Check that player 1 has entered a legal option (an empty cell); if the selection is on a cell that is already occupied, the program should ask the user to enter the option again. If it is player 2's turn (the computer): randomly generate a move (row, col) in the table that is not currently occupied (if the computer selects an occupied cell, then skip it and generate a new move). Print out the selection of player 2 (the computer generated option). Hint: to generate the random move, generate two random integers (one for the row and one for the column), each between 0 and 2. Update the table with the provided option If the above loop ends because one of the players has placed three in a row, the program should print a winning message of that player (see below). Else, it should print exactly the following : Game over, no player wins.
The program should function as follows (items underlined are to be entered by the user):
This program plays the game of tic-tac-toe The current state of the game is: _ _ _ _ _ _ _ _ _
Player 1 enter your selection [row,col]: 1,2
The current state of the game is:
_ O _ _ _ _ _ _ _
Player 2 has entered [row,col]: 2,2
The current state of the game is: _ O _ _ X _ _ _ _
Player 1 enter your selection [row,col]: 1,3 The current state of the game is:
O O O X X _ O _ X
Congratulations, Player 1 wins!
Constraints: Do not use global variables Your program should implement and use the following functions:
clear_table: this function should take as parameter a 3x3 array and clear it out for the beginning of the game. You are free to chose the type of the array and how you store the values for empty, 0 or X. generate_player2_move: this function should take as parameter the 3x3 array, and pointers to two integers row and col, in which the function will return the valid play position for player 2. This function should make use of the check_legal_option function below to see that the randomly generated move is valid or not. check_table_full: this function takes as parameter the table array and returns true or false depending on whether all the cells are occupied or not check_three_in_a_row: this function takes as parameter the table array and returns 0 if no player has three in a row (on rows, columns, or diagonals) or the ID of the player (1 or 2) who has three in a row (three Os represent player 1, three Xs represent player 2) display_table: this function takes as input the table array and prints out the current status as shown above (the function should print an undescore _for an empty cell. check_legal_option: this function takes as input the table array and a possible move (row, column where the X or O should go) and returns true or false depending on whether the option is valid or not update_table: this function takes as input the table array and the move (row, column where the X or O should go) currently entered by the user and updates the table with the latest entered move. Your program should be saved in a file called tictactoe.c.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
