Skip to content

sikatikenmogne/tic-tac-toe-cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tic-Tac-Toe CLI

Python License wakatime Status

Classic user vs computer Tic-Tac-Toe game on CLI

Overview

This project is a simple implementation of the classic Tic-Tac-Toe game where the user plays against the computer. The computer makes random moves, and the game continues until there is a winner or a draw.

Game Rules

  • The computer plays using 'X's.
  • The user plays using 'O's.
  • The computer always makes the first move by placing an 'X' in the middle of the board.
  • The board squares are numbered from 1 to 9, starting from the top-left corner and moving row by row.
  • The user inputs their move by entering the number of the square they choose.
  • The game ends when there is a winner or a draw.

How to Run

  1. Ensure you have Python 3.x or higher installed on your machine.

  2. Clone this repository.

  3. Navigate to the project directory.

  4. Run the game using the following command:

    python main.py

How It Works

  1. Initialization:

    • The board is initialized as a 3x3 grid with numbers 1 to 9.
    • Flags and dictionaries are set up to track the game state, including whether the game is a draw and whether a player has won.
  2. Main Game Loop:

    • The game continues while there are free fields and no player has won.
    • The computer makes a move by placing 'X' on the board.
    • The game checks if the computer has won.
    • If there are still free fields and the computer hasn't won, the player makes a move by placing 'O' on the board.
    • The game checks if the player has won.
    • If either player has won, the game ends and the result is displayed.
  3. Functions:

    • display_board(board): Displays the current state of the board.
    • enter_move(board): Allows the player to enter their move and updates the board accordingly.
    • make_list_of_free_fields(board): Returns a list of coordinates of the free cells on the board.
    • victory_for(board, sign): Checks if the specified player has won the game.
    • draw_move(board): Makes a move for the computer and updates the board accordingly.

Example Session

Clik to expand
...
+-------+-------+-------+
|       |       |       |
|   1   |   2   |   3   |
|       |       |       |
+-------+-------+-------+
|       |       |       |
|   4   |   X   |   6   |
|       |       |       |
+-------+-------+-------+
|       |       |       |
|   7   |   8   |   9   |
|       |       |       |
+-------+-------+-------+
Enter your move: 1
+-------+-------+-------+
|       |       |       |
|   O   |   2   |   3   |
|       |       |       |
+-------+-------+-------+
|       |       |       |
|   4   |   X   |   6   |
|       |       |       |
+-------+-------+-------+
|       |       |       |
|   7   |   8   |   9   |
|       |       |       |
+-------+-------+-------+
+-------+-------+-------+
|       |       |       |
|   O   |   X   |   3   |
|       |       |       |
+-------+-------+-------+
|       |       |       |
|   4   |   X   |   6   |
|       |       |       |
+-------+-------+-------+
|       |       |       |
|   7   |   8   |   9   |
|       |       |       |
+-------+-------+-------+
...


Enter your move: 7
+-------+-------+-------+
|       |       |       |
|   O   |   X   |   X   |
|       |       |       |
+-------+-------+-------+
|       |       |       |
|   O   |   X   |   X   |
|       |       |       |
+-------+-------+-------+
|       |       |       |
|   O   |   O   |   9   |
|       |       |       |
+-------+-------+-------+
You won!

UML Sequence Diagram

Below is a UML sequence diagram that illustrates the interaction between the main components of the game.

Click to view diagram

sequence-diagram

Project Structure

  • main.py: The main Python script that implements the game logic.
  • README.md: This file, providing an overview of the project.
  • assesment.md: Contains the project description and instructions.

License

This project is licensed under the MIT License.