Report of Mini Project
Report of Mini Project
1. Introduction
The Tic-Tac-Toe game is a classic 3x3 grid-based game played between two players, typically called
"X" and "O." The goal is to get three of one's marks in a row, column, or diagonal. The game ends
when a player wins or the grid is full with no winner, resulting in a draw.
In this implementation, the game allows a human player to compete against a computer opponent.
The human player can choose either the "X" or "O" symbol, and the computer randomly selects its
moves. The game checks for winning conditions after each move and announces the result (win,
draw, or continuation). The board is displayed after each turn, with colored output to distinguish
between the two players.
2. Objectives
1. Simulate a Tic-Tac-Toe game where a human player can play against a computer.
2. Use basic C++ features such as functions, loops, and conditional statements to implement
game logic.
3. Implement the game board as a 3x3 grid, with spaces, "X," and "O" as possible marks.
4. Validate user input and ensure that moves are placed in available spots on the board.
5. Implement random move generation for the computer, providing a basic AI that selects
random positions on the board.
6. Display the game board with color-coded output to differentiate the two players.
7. End the game with a win, draw, or continuation, based on the current state of the board.
3. Methodology
The Tic-Tac-Toe game is implemented using a class-based approach with the following key
components:
2. Game Flow:
o The game starts by prompting the human player to choose between "X" or "O."
o On each turn, the game board is printed with color-coded characters for "X" (red)
and "O" (yellow).
o The computer makes random moves on the board, and the game alternates turns
until a win or draw is detected.
REPORT
4. Code Explanation
The code uses a class TicTacToe to encapsulate the logic of the game. The primary functions in the
class handle different aspects of the game:
• printBoard(): The board is printed with row and column numbers for clarity. The marks "X"
and "O" are color-coded using ANSI escape codes (red for "X" and yellow for "O").
• placeMark(int row, int col): This function checks if the position on the board is empty (' '),
and if so, places the current player's mark at that position.
• checkWin(): The game checks whether the current player has completed any row, column, or
diagonal, which would declare them as the winner.
• checkDraw(): The game checks if all the spots on the board are filled and no one has won,
indicating a draw.
• switchPlayer(): This function alternates the turn between the two players.
• getRandomMove(): The computer randomly selects an empty spot on the board to make its
move.
• playGame(): The main game loop. It repeatedly displays the board, prompts the user for
their move, allows the computer to make its move, and checks the game state after each
move.
• Color-Coded Output: The program uses ANSI escape sequences to color the marks for player
X (in red) and player O (in yellow).
• Random AI: The computer's moves are selected randomly, making it a beginner-level
opponent.
• Input Validation: The game checks whether the player has chosen either "X" or "O" at the
start. It also validates the moves to ensure they are within the board's bounds and that the
selected spot is empty.
• End Conditions: The game properly handles win conditions (3 consecutive marks) and draw
conditions (all spaces filled with no winner).
• Conclusion
• This implementation of Tic-Tac-Toe provides a functional and straightforward game
where a human player competes against a simple computer opponent. It covers all the
basic requirements of the game and includes color-coded output to make the game
visually engaging. While the computer’s AI is currently rudimentary, it can be
improved with more advanced algorithms to make the game more challenging.
Overall, this program demonstrates good practice with basic C++ features and
provides a solid foundation for further development.