Here we will make a python project to make a Text based Tic-Tac-Toe. We will go through the steps and create project. We will also discuss the further improvement in the game.

Steps to complete the Tic-Tac-Toe

  1. Create a Board UI
  2. Get Player input
  3. Check Winning condition
  4. Play the game in loop
# make the display of Gameboard
board = [ " " for _ in range(9)]
def display_board():

    print(f'{board[0]} | {board[1]} | {board[2]}')
    print("--+---+--")
    print(f'{board[3]} | {board[4]} | {board[5]}')
    print("--+---+--")
    print(f'{board[6]} | {board[7]} | {board[8]}')

# input player input
def get_player_move(player):
    while True:
        try:
            move = int(input(f'Please {player} enter your move (1-9)'))-1
            if 0 <= move < 9 and board[move]==" ":
                return move
            else:
                print("Invalid Move . Please Try Again")
        except ValueError:
            print("Invalid input. Enter a number (1-9).")


# check for win
def check_win(player):
    winning_combinations = [(0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6)]
    for comb in winning_combinations:
        if (all(board[pos]== player for pos in comb)):
            return True
    return False

# play the game in a loop
def game():
    player = 'X'
    print("Welcome to the Game . Let's play")
    while True:
        display_board()
        move = get_player_move(player)
        board[move]= player
        if check_win(player):
            display_board()
            print(f'Player {player} wins! ')
            break
        if " " not in board:
            display_board()
            print("Its a draw")
            break
        player = "O" if player == "X" else "X"


if __name__ == '__main__':
    game()

Future Improvements

Our Tic-Tac-Toe game logic is working, but there are several ways to make it better and more user-friendly. Here are some improvements:

  1. Use a Game Loop: It’s good to wrap your game logic in a loop, but it’s a bit more organized to have a dedicated game loop function that handles the game’s flow. This makes it easier to restart the game.
  2. Clear the Console: In your current implementation, the board gets printed over and over, creating a scrolling effect. Instead, you can clear the console screen before printing the updated board. You can use libraries like os (for Windows) and sys (for Linux/macOS) for this.
  3. Player Names: Allow players to enter their names at the beginning, and let them choose “X” or “O.” This personalizes the game and makes it more engaging.
  4. GUI Interface: Consider creating a graphical user interface (GUI) for the game. Python has libraries like tkinter for building simple graphical interfaces. This would make your game more visually appealing.
  5. Restart Option: After the game ends, ask the players if they want to play again. This keeps them engaged without needing to rerun the script.
  6. Comments and Documentation: Add comments to explain your code. Consider adding a docstring to your functions, explaining their purpose and usage.
  7. Error Handling: Improve error handling. If a player enters an invalid move (e.g., a letter or a number outside the range), provide clear and friendly error messages.
  8. Test and Refactor: Test your code rigorously with different scenarios to ensure it handles all possible cases. Refactor your code to make it more modular and easier to understand.
  9. Winning Animation: Implement a winning animation to highlight the three squares that make a winning combination. You can briefly change their background color or print them differently to indicate the win.
  10. Draw Message: Make the “It’s a draw” message more explicit and engaging. For example, “The game ends in a draw! Try again.”

By making these improvements, Our game will become more user-friendly and enjoyable to play.

This is our Graphic based Tic-Tac-Toe-Python Project- Graphic based Tic-Tac-Toe

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *