This project is a graphical Tic Tac Toe game implemented in C++ using the SFML library. The game allows two players to compete, keeps track of their scores, and saves game statistics to a file.
- Two-Player Mode: Players can enter their names and take turns playing.
- Graphical Interface: The game uses images, sprites, and text to display the game board and status.
- Persistent Stats: Game statistics (total games, wins, and draws) are saved to a file and loaded at the start of the game.
- Reset Functionality: Players can reset the board after a game is completed.
- C++ Compiler: Ensure you have a C++ compiler installed that supports C++17 or later.
- SFML Library: Download and install the SFML library from https://www.sfml-dev.org/.
- Windows Environment: The program uses Windows-specific functions, so it may not work directly on other operating systems without modifications.
Ensure the following files are in the Resources directory:
background.png: The background image for the game.board.png: The Tic Tac Toe board image.reset.png: The reset button image.circle.png: The image representing Player 2's marker.cross.png: The image representing Player 1's marker.blank.png: The image representing empty board slots.font.ttf: The font file used for displaying text.
Additionally, the game writes and reads statistics from a game_stats.txt file in the same directory as the executable.
-
Clone the repository or copy the source code.
-
Place all required resources in the
Resourcesdirectory. -
Compile the code using a compiler that supports SFML. Below is an example command for compilation:
g++ -o TicTacToe main.cpp -lsfml-graphics -lsfml-window -lsfml-system
-
Run the compiled executable:
./TicTacToe
-
Mouse Left Click:
- Click on an empty slot to place your marker.
- Click the reset button to reset the board after a game.
-
Close Window: Close the game by clicking the window's close button.
-
game_assetsStructure:- Stores game assets like images, textures, and fonts.
-
game_spritesStructure:- Stores the sprites and text elements used in the game.
-
game_stateStructure:- Tracks the game state, including the current player, board status, and game statistics.
-
Main Functions:
load_assets: Loads all required assets.load_board: Initializes or resets the game board.check_win: Checks if the current player has won.check_draw: Checks if the game is a draw.key_press: Handles user interactions.load_game_statsandsave_game_stats: Handle reading and writing of game statistics.
- Takes player names as input.
- Loads game assets and statistics.
- Initializes the game board and launches a graphical window using SFML.
- Players alternate placing their markers (cross or circle) on the board.
- The game checks for wins or draws after each move.
- Once a game ends, the reset button becomes active to restart the board.
Game statistics are saved in the game_stats.txt file and include:
- Total games played.
- Games won by Player 1.
- Games won by Player 2.
- Number of draw games.
- Add a main menu for better navigation.
- Allow custom player markers.
- Support AI for single-player mode.
- Make the game cross-platform by removing Windows-specific dependencies.
- Assets Not Found:
Ensure the
Resourcesdirectory contains all required files. - SFML Errors: Verify the SFML library is installed correctly and linked during compilation.
- Statistics Not Saved:
Check file permissions for
game_stats.txt.
This project is free to use and modify for personal or educational purposes.
- SFML Library: For providing the graphical framework used in this game.
- Developers of various open-source projects for inspiration.