Koppelingen in nieuw tabblad openen
  1. Creating a Tic Tac Toe game using Object-Oriented Programming (OOP) in Python ensures a modular, maintainable, and extensible codebase. Below is an implementation that adheres to OOP principles.

    Code Implementation

    class Board:
    def __init__(self):
    self.board = [" " for _ in range(9)] # 3x3 board represented as a list
    self.current_winner = None # Track the winner

    def display(self):
    for row in [self.board[i * 3:(i + 1) * 3] for i in range(3)]:
    print("| " + " | ".join(row) + " |")

    def make_move(self, position, player):
    if self.board[position] == " ":
    self.board[position] = player
    if self.check_winner(position, player):
    self.current_winner = player
    return True
    return False

    def check_winner(self, position, player):
    # Check rows, columns, and diagonals
    row_index = position // 3
    row = self.board[row_index * 3:(row_index + 1) * 3]
    if all([spot == player for spot in row]):
    return True

    col_index = position % 3
    column = [self.board[col_index + i * 3] for i in range(3)]
    if all([spot == player for spot in column]):
    return True

    if position % 2 == 0: # Check diagonals
    diagonal1 = [self.board[i] for i in [0, 4, 8]]
    diagonal2 = [self.board[i] for i in [2, 4, 6]]
    if all([spot == player for spot in diagonal1]) or all([spot == player for spot in diagonal2]):
    return True

    return False

    def is_full(self):
    return " " not in self.board


    class Player:
    def __init__(self, symbol):
    self.symbol = symbol


    class Game:
    def __init__(self):
    self.board = Board()
    self.players = [Player("X"), Player("O")]

    def play(self):
    print("Welcome to Tic Tac Toe!")
    self.board.display()

    current_player_index = 0
    while not self.board.is_full():
    player = self.players[current_player_index]
    move = int(input(f"Player {player.symbol}, enter your move (0-8): "))

    if move < 0 or move > 8:
    print("Invalid move. Try again.")
    continue

    if self.board.make_move(move, player.symbol):
    self.board.display()

    if self.board.current_winner:
    print(f"Player {player.symbol} wins!")
    return
    current_player_index = (current_player_index + 1) % 2
    else:
    print("Spot already taken. Try again.")

    print("It's a tie!")


    if __name__ == "__main__":
    game = Game()
    game.play()
    Gekopieerd.
  1. Tic Tac Toe - Object-Oriented Python Game | Academic

    2 jul. 2025 · Welcome to the classic game of Tic Tac Toe, implemented using Object-Oriented Programming in Python. This project is ideal for learning how …

  2. hammoda711/tic-tac-toe-game-oop-solid-py - GitHub

    Tic Tac Toe Game Overview This project is a simple implementation of the classic Tic Tac Toe game using Python, with a focus on Object-Oriented Programming …

  3. Non-GUI-based Tic-tac-toe Game using Python OOP

    16 mei 2023 · I hope you understood the entire logic behind coding the basic Non-GUI-based Tic-tac-toe game. If you find anything that I could do better, please let …

  4. [Python] 4. Advanced Tic-Tac-Toe Game Ver.4 (with Classes!)

    4 sep. 2024 · Now that you’re ready to introduce object-oriented programming (OOP) into your Tic-Tac-Toe game, it’s the perfect opportunity to learn about classes and inheritance in Python.

  5. How to Make Tic Tac Toe Game in Python | Step-by-Step …

    Learn to build your first Tic Tac Toe game in Python with this easy step-by-step guide. Perfect for beginners! Complete code + examples included.

  6. Build Your First Python Game - Tic Tac Toe (Object-Oriented)

    Our Tic-Tac-Toe game is built using Object-Oriented Programming (OOP), which organizes code into classes and objects. Think of classes as blueprints and objects as the actual things built from those …

  7. Mensen vragen ook naar
  8. Build a Python Tic-Tac-Toe Game with PyQt (Step-by …

    13 feb. 2025 · In this tutorial, we will build a simple Tic-Tac-Toe game using Python and PyQt5. This project will help you understand Object-Oriented Programming …

  9. Build a Tic-Tac-Toe Game With Python and Tkinter

    1 feb. 2025 · In this step-by-step project, you'll learn how to create a tic-tac-toe game using Python and the Tkinter GUI framework. Tkinter is cross-platform and …

  10. Tic Tac Toe in Python: A Comprehensive Guide - CodeRivers

    7 apr. 2025 · In Python, creating a Tic Tac Toe game allows us to explore fundamental programming concepts such as data structures, control flow, and user input handling. This blog will walk you …

  11. Tic Tac Toe GUI In Python using PyGame - GeeksforGeeks

    12 jul. 2025 · This article will guide you and give you a basic idea of designing a game Tic Tac Toe using pygame library of Python. Pygame is a cross-platform …