Sunday, February 2, 2025

Part 1: Tic Tac Toe game in C programing | Flowchart, Algorithm of Computer Player, and Data Structure

Previous Page                                                                                                              Next Page

Flowchart



Explanation of Flowchart:

Start: The game process begins here.

Display Game Menu: The user can choose to play first or second.

Initialize Game: Initialize the human or computer as the first or second player. Set up the Tic-Tac-Toe board and assign initial values to the players.

Draw Game Window: Render the game window and wait for user input.

Start Game: Begin an infinite game loop.

Set Current Player (Human or Computer): Designate the current player as either the human or the computer and obtain input from the player.

Get Input from Player and Draw X or O Based on Player: Draw an "X" for the first player and an "O" for the second player.

Check for Game Winner or Draw: Determine if a player has won the game or if the game has ended in a draw.

Display Game Winner or Draw Message: Show a message indicating whether a player has won or if the game is a draw.

Ask User to Restart Game or Exit: If the user presses any key, restart the game. If the user presses the ESC key, exit the game.

Exit Game: The game process ends.

Algorithm of computer player:

As this game article is target for beginner c programmer, I haven't used complex AI algorithm which required creating complex data structure. Tic Tac Toe game has very limited number of steps, so I have formulated series of best game steps from high rank to low rank. Computer will choose next move based on predefine series of game steps.

In every turn, computer will choose location starting from 1 to 11 steps.

1. Choose location to win the game by scanning columns, rows, first diagonal, and second diagonal in order. If there is no winning position, then go to 2 steps.
2. Choose location to draw the game by scanning columns, rows, first diagonal, and second diagonal in order. If there is no game draw position, then go to 3 steps.
3. Choose center location i.e. fifth location, if not available go to step 4.
4. Choose second location, if not available go to step 5.
5. Choose fourth location, if not available go to step 6.
6. Choose sixth location, if not available go to step 7.
7. Choose eighth location, if not available go to step 8.
8. Choose first location, if not available go to step 9.
9. Choose third location, if not available go to step 10.
10. Choose seventh location, if not available go to step 11.
11. Choose ninth location

In above algorithm, 1 and 2 steps will not come in effective until third turn of the game.

Data Structure:

This game is a 2D game played by two players on a 3x3 grid, totaling 9 boxes. There are three key elements/objects in this game: the player, the game board, and a 2D x-y coordinate system to represent the boxes. To model these elements, I have created three structure definitions: struct Point2D, struct BoardBox, and struct Player.

The Point2D structure is used to represent the center of each box, making it easier to draw an "X" or an "O" in the correct position. The BoardBox structure represents the nine boxes of the game. It contains the coordinates of the center of each box and a value assigned to it. The value is determined based on the player's choice: a value of 3 is assigned for the human player, and a value of 5 is assigned for the computer player.
//Structure definition to store window x,y coordinates
struct Point2D {
    int x;
    int y;
};

//Structure definition to define each game board boxes
struct BoardBox {
    struct Point2D center;
    int value;
};

//struct variable to store nine boxes of the board.
struct BoardBox box[numBox]; 
The Player structure represents both the human and computer players. It contains information about the current player, the next player, the box number selected by the current player, and the number of turns taken by the player. In total, there will be 9 turns to complete the game.
//structure definition to store player information
struct Player {
    int numOfTurn;
    int selectedBox;
    int currentPlayer;
    int nextPlayer;

};

//Global variable to represent player
struct Player p; 

No comments:

Post a Comment