This is a simple console-based Tic Tac Toe game written in C. The game allows two players to play against each other, taking turns marking positions on a 3x3 board. The first player to align three of their symbols (either 'X' or 'O') in a row, column, or diagonal wins the game. If the board is filled without any player meeting the winning condition, the game ends in a tie.
- The game starts with an empty 3x3 board. Each player takes turns to mark a position on the board.
- Player 1 will use the symbol 'X', while Player 2 will use the symbol 'O'.
- To make a move, a player enters a number between 1 and 9 corresponding to the position they want to mark on the board:
1
: Top-left2
: Top-center3
: Top-right4
: Middle-left5
: Center6
: Middle-right7
: Bottom-left8
: Bottom-center9
: Bottom-right
- The game checks if the position is already taken or if the input is valid. If not, the player is prompted to make another choice.
- The game will continue until one player wins or the board is full (resulting in a tie).
- Turn-based gameplay: Players alternate between turns to mark their symbol on the board.
- Input validation: The game checks whether the player's input is valid (between 1 and 9) and if the selected position is available.
- Win detection: The game checks for any winning condition after each move. If a player has aligned three symbols in a row, column, or diagonal, they win.
- Tie condition: If all positions are filled and no one wins, the game declares a tie.
The game board is represented as follows:
1 | 2 | 3
--+---+--
4 | 5 | 6
--+---+--
7 | 8 | 9
Players will input the number corresponding to the position they wish to mark.
- When the game starts, you will be prompted with the instructions and asked to press any key to begin.
- The board will be displayed after every valid move, showing the current state of the game.
- The game will display a message declaring the winner, or if the game ends in a tie.
main()
: Initializes the game and controls the game loop.instructions()
: Displays the game's instructions at the start.printBoard()
: Prints the current state of the Tic Tac Toe board.updateBoard()
: Clears the screen and prints the updated board after each valid move.isPositionTaken()
: Checks if a position on the board is already taken.checkWin()
: Checks if any player has won by analyzing the board for winning patterns.
- Compile the code using any C compiler:
gcc tic_tac_toe.c -o tic_tac_toe
- Run the game:
./tic_tac_toe
Enjoy playing Tic Tac Toe!