Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exercise The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a

Exercise The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either X or O. The first player plays with X while the second player plays with O. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of the same type in a line (a line can be any row, any column or any diagonal). The first player who manages to place 5 markers in a line wins. The game is played until one of the players wins or until the board is full with no player having 5 markers in a line (i.e., the result of the game is a draw). Your program should do the following: 1) Ask at the beginning if the game is going to be played by two human players (i.e., human player vs. human player) or by one human player and the computer (i.e., human player vs. computer). In the latter case (i.e., human player vs. computer) there must be an option that allows the player to choose the first to play (either the computer or himself/herself). 2) Players will play in turns (i.e., the first player will place an X on the board, then the second player will place an O in the next move, and so on). a. When it is a human players turn, the selected board position must be introduced by the user by indicating the corresponding row and column numbers. Your program should display numbers around the board, b. When it is the computers turn (in human vs. computer mode), the computer should choose a valid empty position. A possible tactic to choose the position is suggested in point 6 (see below). 3) If a human player choses an illegal position (out of bounds or already taken) then a message should be displayed. The program should keep asking the player to introduce the position for his/her move until a valid position is chosen by the player. 4) After each move, the entire board containing all previous moves should be reprinted. 5) When there are 5 consecutive markers (either X or O) in line anywhere in the board (i.e., any row, column or diagonal) the game should finish and the program should display a message indicating who has won the game. The game should also finish when the board is full with no player having 5 markers in a line; in this case the program should display a message indicating that the final result of the game is a draw. 6) When the user chooses to play in human vs. computer mode, the following computer tactic may be employed in every computers turn: a. The computer first checks all possible valid moves and if there is any move that allows the computer to win immediately, that move is selected. b. If (a) is not possible, then the computer checks if the human player is about to win in the next move. If this is the case, then the computer blocks that move. c. If cases (a) and (b) are not relevant, then the computer can choose a random empty position. This can be done with the rand() function, which is declared in the C standard header file stdlib.h. The following statements: int x = rand() % 5; int y = rand() % 5; store in x and y random integer numbers between 0 and 4. This can be used to generate the row and column numbers until an empty position is found.

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

Step: 3

blur-text-image

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

Principles Of Multimedia Database Systems

Authors: V.S. Subrahmanian

1st Edition

1558604669, 978-1558604667

More Books

Students also viewed these Databases questions