0% found this document useful (0 votes)
13 views5 pages

Tic-Tac-Toe Game Against Computer

The document outlines a project to create a Tic-Tac-Toe game in C where a player competes against a computer with three difficulty levels: Easy, Medium, and Hard. Key features include a 3x3 grid representation, user input validation, computer move logic based on difficulty, winner detection, and a game restart option. The game will utilize a simple text-based interface and implement the Minimax algorithm for optimal computer play in Hard mode.

Uploaded by

alwaysuse171311
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Tic-Tac-Toe Game Against Computer

The document outlines a project to create a Tic-Tac-Toe game in C where a player competes against a computer with three difficulty levels: Easy, Medium, and Hard. Key features include a 3x3 grid representation, user input validation, computer move logic based on difficulty, winner detection, and a game restart option. The game will utilize a simple text-based interface and implement the Minimax algorithm for optimal computer play in Hard mode.

Uploaded by

alwaysuse171311
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Tic-Tac-Toe Game Against

Computer
Tic-Tac-Toe Game Against Computer
Project Overview
The goal is to create a Tic-Tac-Toe game in C where a player can play against
the computer. The game will feature three difficulty levels:

Easy: The computer makes random moves.

Medium: The computer blocks winning moves and makes random moves.

Hard: The computer plays optimally using the Minimax algorithm.

Key Features
1. Game Board Representation:

The board will be represented as a 3x3 grid using a 1D array of size 9.

The player and computer will alternate turns, marked as ‘X’ for the
player and ‘O’ for the computer.

2. User Input:

The player will be prompted to enter a number between 1 and 9,


corresponding to the grid positions.

The program should validate user input to ensure it is within the valid
range and the chosen cell is unoccupied.

3. Computer Moves:

The computer should make moves based on the selected difficulty


level:

Easy: Randomly selects an unoccupied cell.

Medium: Blocks the player's potential winning moves and makes


random moves otherwise.

Hard: Uses the Minimax algorithm to calculate the best possible


move.

Tic-Tac-Toe Game Against Computer 1


4. Game Flow:

Display the current game board after every move.

Alternate turns between the player and the computer.

Check after each move if there is a winner or if the game is a draw.

End the game when there’s a winner or the board is full (draw).

5. Winner Detection:

After every move, the game will check the board for a winning
combination:

Horizontal, vertical, or diagonal lines of the same symbol (either ‘X’


or ‘O’) signify a win.

If the player or computer wins, the game should display a victory


message.

6. Game Restart Option:

After a win or a draw, prompt the player if they would like to play again.

Detailed Roadmap

1. Setting Up the Game Board


Data Structure:

Use a 1D array of size 9 to represent the 3x3 board (index 0


corresponds to the top-left cell, index 8 to the bottom-right cell).

Initialize the board with numbers 1 to 9 to indicate empty positions.

2. Game Functions
1. Display the Board ( printBoard )

Function to display the current state of the game board.

Each cell will show either 'X', 'O', or a number representing an empty
spot.

Print the grid after each player’s turn.

2. Player Move ( playerMove )

Prompt the player to enter a position (1-9).

Tic-Tac-Toe Game Against Computer 2


Validate the input: ensure it's within the valid range and that the chosen
position is unoccupied.

Update the board with the player's move.

3. Computer Move ( computerMove )

Based on the selected difficulty level:

Easy: Randomly pick an available position.

Medium: Block potential player wins or select randomly if no


immediate threat exists.

Hard: Use the Minimax algorithm to evaluate all possible moves and
choose the optimal one.

4. Check for a Winner ( checkWinner )

Check if there is a line (horizontal, vertical, or diagonal) consisting of


the same symbol (either ‘X’ or ‘O’).

Return the winner or a draw if the board is full.

5. Check for a Draw ( checkDraw )

If all positions are filled without a winner, the game ends in a draw.

6. Minimax Algorithm (for Hard Mode)

The core of the hard mode is the Minimax algorithm:

This function will simulate all possible moves for both players and
return the best possible outcome (maximize for 'O' and minimize for
'X').

Recursively evaluate board states and choose the optimal move


based on minimizing the computer's loss or maximizing its win.

3. Difficulty Levels
1. Easy Mode:

The computer randomly selects one of the available empty positions on


the board.

2. Medium Mode:

The computer first checks if the player can win on the next move and
blocks that move.

Tic-Tac-Toe Game Against Computer 3


If the player cannot win immediately, it makes a random move from the
available positions.

3. Hard Mode:

The computer uses the Minimax algorithm to find the best possible
move. It simulates every potential move, recursively evaluates possible
game outcomes, and selects the optimal move.

4. Game Loop
The game will run in a loop until there’s a winner or a draw.

The loop alternates between the player and the computer, asking for the
player's move and letting the computer make its move.

After each move, the program will display the board and check for a winner
or draw.

5. Game Restart
After each game ends, prompt the player to ask if they want to play again.

User Interface
Simple text-based interface.

Display the board after every move with clear marking of 'X' and 'O'.

Prompt the player with clear instructions and input options (1-9 for grid
positions).

Display messages like "Player wins!", "Computer wins!", and "It's a draw!"
based on the game outcome.

Sample Functions
1. void printBoard(char board[9])

Function to display the current board state.

2. int playerMove(char board[9])

Get the player's move, validate it, and update the board.

3. int computerMove(char board[9], int difficulty)

Get the computer's move based on difficulty level.

Tic-Tac-Toe Game Against Computer 4


4. int checkWinner(char board[9])

Check if there's a winner on the board.

5. int checkDraw(char board[9])

Check if the game is a draw (board is full).

Tic-Tac-Toe Game Against Computer 5

You might also like