In the previous project we saw a text based Tic-Tac-Toe. Now lets improve it by making a Graphic based Tic-Tac-Toe. It uses tkinter library to improve its User Interface. Making it graphically better . You may consider it a immediate up gradation of text based Tic-Tac-Toe

import tkinter as tk
from tkinter import messagebox

class TicTacToe:
    def __init__(self):
        self.window = tk.Tk()
        self.window.title("Tic-Tac-Toe")
        self.window.geometry("300x350")

        self.current_player = "X"
        self.board = [" " for _ in range(9)]

        self.buttons = [tk.Button(self.window, text=" ", font=('normal', 20), width=4, height=2, command=lambda i=i: self.make_move(i)) for i in range(9)]

        for i in range(9):
            row, col = divmod(i, 3)
            self.buttons[i].grid(row=row, column=col)

        self.reset_button = tk.Button(self.window, text="Restart", font=('normal', 12), command=self.reset)
        self.reset_button.grid(row=3, column=1)

    def make_move(self, index):
        if self.board[index] == " ":
            self.board[index] = self.current_player
            self.buttons[index]["text"] = self.current_player
            if self.check_win():
                self.show_message(f"Player {self.current_player} wins!")
            elif " " not in self.board:
                self.show_message("It's a draw!")
            else:
                self.current_player = "O" if self.current_player == "X" else "X"

    def check_win(self):
        win_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 a, b, c in win_combinations:
            if self.board[a] == self.board[b] == self.board[c] != " ":
                return True
        return False

    def show_message(self, message):
        messagebox.showinfo("Game Over", message)
        self.reset()

    def reset(self):
        self.current_player = "X"
        self.board = [" " for _ in range(9)]
        for i in range(9):
            self.buttons[i]["text"] = " "

    def run(self):
        self.window.mainloop()

if __name__ == "__main__":
    game = TicTacToe()
    game.run()

You can check our basic version of this project to understand it better.

Text based Tic-Tac-Toe

It can also be improved more . Consider these points for further enhancements:

  1. AI Opponent: Add an AI opponent that can play against the player. You can implement different AI levels, such as easy, medium, and hard, to make the game more challenging.
  2. Scorekeeping: Keep track of the score between different games. Display the number of wins for each player and the number of draws.
  3. Customizable Player Names: Allow the players to enter their names or choose from pre-defined names.
  4. Sound Effects: Add sound effects for actions like making a move, winning, and drawing. This can make the game more engaging.
  5. Animations: Implement animations for player moves and game outcomes. This can provide a more visually appealing experience.
  6. Responsive Design: Make the game window responsive to different screen sizes. Ensure that the game looks good and functions well on both small and large screens.
  7. Multiple Game Modes: Create different game modes, such as 4×4 or 5×5 boards, or variations of Tic-Tac-Toe like “Ultimate Tic-Tac-Toe.”
  8. Undo/Redo: Implement undo and redo functionality, allowing players to go back to previous moves.
  9. Settings Menu: Include a settings menu where players can adjust game settings, such as AI difficulty, sound preferences, or player names.
  10. Game Statistics: Display game statistics, such as the total number of games played, win percentages, and more.
  11. Multiplayer: Enable online multiplayer so that players can compete with friends or other users over the internet.
  12. Themes and Skins: Allow users to choose different themes or skins for the game board and pieces.
  13. Accessibility: Ensure the game is accessible to all players, including those with disabilities. Implement keyboard controls and ensure that the game is usable with screen readers.
  14. Mobile App: Develop a mobile version of the game that can be played on smartphones and tablets.
  15. AI Strategies: If you have implemented an AI opponent, consider enhancing the AI’s strategies for more challenging gameplay.
  16. Localization: Translate the game into multiple languages to reach a broader audience.
  17. Game Over Screens: Create more elaborate “Game Over” screens with options to restart or return to the main menu.
  18. Online Leaderboards: Implement online leaderboards to allow players to compare their performance with others.
  19. Tutorials and Help: Provide in-game tutorials or help sections to guide new players on how to play.
  20. Testing and Bug Fixes: Thoroughly test the game to identify and fix any bugs, glitches, or usability issues.

Remember that the scope of improvements depends on your goals and the resources you have. You can gradually implement these enhancements based on your development capacity and user feedback to make the game even more engaging and enjoyable.

One Response

Leave a Reply

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