top of page

Create Your First Project

Start adding your projects to your portfolio. Click on "Manage Projects" to get started

Noughts and Crosses Python Game

Project type

Python-Based Console Game

Date

November 2022

Location

Wolverhampton, United Kingdom

Python Developer

Software Developer

Gameplay Showcase

Noughts and Crosses Game

This program implements the classic game of Noughts and Crosses (also known as Tic-Tac-Toe), where a human player competes against a computer opponent. The game is designed to provide a challenging and engaging experience, with several key features and functionalities.

The game is played on a 3x3 grid, represented in the code as a list of lists. Initially, all cells on the board are empty, represented by a space character (' '). The human player uses the mark 'X', while the computer opponent uses 'O'. The game begins with a welcome message that displays the empty board along with instructions on how to make a move.

During gameplay, the human player is prompted to choose a square on the board by entering a number between 1 and 9, corresponding to a specific cell on the grid. The program validates this input to ensure it is valid and that the chosen cell is unoccupied. If the input is invalid, the game will prompt the player until a valid selection is made. Once the player has made their move, the computer takes its turn. The computer is programmed to play strategically, prioritizing the middle cell first and selecting subsequent moves in a defined order to maximize its chances of winning.

After every move, the game updates and displays the current state of the board, showing both the player’s and the computer’s moves. The program checks for a win after each turn, declaring a winner if three identical marks are aligned in a row, column, or diagonal. The game also detects draw conditions if all cells are filled without either player achieving a win. In such cases, the game ends with a draw message.

The game alternates turns between the player and the computer until a win or draw condition is met. When the game concludes, it displays a message indicating the result—whether the player won, the computer won, or it ended in a draw. The program also tracks the player's score: a win adds 1 point, a loss deducts 1 point, and a draw leaves the score unchanged. Players can choose to save their score after the game by entering their name. This score, along with others, is stored in a text file and can be retrieved later. Players can also load and view the leaderboard, which displays all saved scores and corresponding names.

The game features a menu at the start, offering the following options: play the game, save the current score, load and display saved scores, or quit. This menu ensures easy navigation and allows players to enjoy the game or view their progress seamlessly.

To play the game, you need to download two files: noughtsandcrosses.py and main.py, from the provided GitHub repository. Save both files in the same directory on your computer. Ensure that Python 3.6 or higher is installed on your system, which you can verify by typing python --version or python3 --version into a terminal. If Python is installed, a version number will be displayed.

Once the files are saved, open a terminal and use the cd command to navigate to the directory containing the files. For example, if the files are saved on your desktop, type cd Desktop and press Enter. If the files are in a subdirectory, use cd followed by the subdirectory name. After navigating to the correct directory, type python main.py or python3 main.py and press Enter. This will execute the main.py file, which loads the noughtsandcrosses.py file. The game will then start in the terminal, displaying the welcome message, an empty game board, and the main menu.

To play, select option 1 from the menu. The game will display a numbered 3x3 grid (1–9), and you can choose a square by entering the corresponding number. The computer will then take its turn, and this alternation continues until a win or draw condition is met. After playing, you can save your score by selecting option 2 from the menu. You will be prompted to enter your name, and the game will save your score to a file named leaderboards.txt in the program's directory. If you wish to view saved scores, select option 3 to load and display them.

This game is entirely text-based and is controlled using the keyboard. It is designed to handle invalid input gracefully, ensuring a smooth and user-friendly experience. By following the provided steps, you can easily play and enjoy this challenging game of Noughts and Crosses.

bottom of page