
XO Battle — Jeu de Tic-Tac-Toe
Jeu de Tic-Tac-Toe développé en Python avec interface console interactive. Supporte deux joueurs avec noms personnalisés, validation des mouvements et système de rejouer.
Vue d'ensemble du projet
XO Battle est un jeu de Tic-Tac-Toe classique développé en Pythonavec une interface console interactive. Le jeu permet à deux joueurs de s'affronter en utilisant leurs propres noms et en suivant les règles traditionnelles du Morpion.
Le projet intègre un système de validation des mouvements, une détection automatique des conditions de victoire (lignes, colonnes, diagonales), et une gestion intelligente des égalités. L'interface console offre une expérience de jeu fluide avec affichage clair du plateau et instructions intuitives.
Le jeu propose également un système de rejouer permettant aux joueurs de faire plusieurs parties consécutives sans redémarrer l'application. L'architecture modulaire facilite la maintenance et l'extension des fonctionnalités futures.
Fonctionnalités clés
Implémentation du code
1. Affichage du plateau de jeu
def print_board(board):
print()
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]} ")
print()2. Détection des conditions de victoire
def check_win(board):
"""Checks if there's a winner on the board."""
win_conditions = [
[0, 1, 2], # Top row
[3, 4, 5], # Middle row
[6, 7, 8], # Bottom row
[0, 3, 6], # Left column
[1, 4, 7], # Middle column
[2, 5, 8], # Right column
[0, 4, 8], # Diagonal from top-left
[2, 4, 6] # Diagonal from top-right
]
for condition in win_conditions:
a, b, c = condition
if board[a] == board[b] == board[c] and board[a] != " ":
return board[a] # Return the winning marker ('X' or 'O')
return None3. Validation des mouvements
def get_move(board, marker, player_names):
"""Prompts the player for a move and validates the input."""
while True:
try:
move = int(input(f"{player_names[marker]} ({marker}), enter your move (1-9): "))
if move < 1 or move > 9:
print("Please choose a number between 1 and 9.")
continue
index = move - 1
if board[index] != " ":
print("That position is already taken. Choose another.")
continue
return index
except ValueError:
print("Invalid input. Please enter a number between 1 and 9.")4. Boucle principale du jeu
def tic_tac_toe(player_names):
"""Runs a single game of Tic Tac Toe."""
board = [" "] * 9 # Initialize an empty board
current_marker = "X" # Player 1 gets "X" and Player 2 gets "O"
move_count = 0
# Game loop
while move_count < 9:
print_board(board)
index = get_move(board, current_marker, player_names)
board[index] = current_marker
move_count += 1
winner = check_win(board)
if winner:
print_board(board)
print(f"Congratulations, {player_names[winner]} ({winner}) wins!")
break
# Switch markers between players
current_marker = "O" if current_marker == "X" else "X"
else:
# No winner after 9 moves means a tie.
print_board(board)
print("It's a tie!")5. Fonction principale avec rejouer
def main():
# Ask for the players' real names.
print("Enter the names for the players:")
player1 = input("Enter name for Player 1: ").strip()
player2 = input("Enter name for Player 2: ").strip()
# Assign markers: Player 1 gets "X" and Player 2 gets "O"
player_names = {"X": player1, "O": player2}
# Main game loop to allow restarting
while True:
tic_tac_toe(player_names)
replay = input("Do you want to play again? (y/n): ").strip().lower()
if replay != 'y':
print("Thanks for playing!")
breakTechnologies utilisées
Détails du projet
Client
Projet Personnel
Timeline
2025 – Présent
Rôle
Développeur Python
Fonctionnalités du jeu
Métriques de performance
Temps de réponse
Instantané
Plateau
3x3 cases
Interface
Console Python
Complexité
O(1) par mouvement
Autres projets

JobHub – Plateforme d'Emploi Intelligente avec Agent IA
Application Web Full-Stack

Email AI Platform
Intelligence Artificielle

Ovia
Intelligence Artificielle

MediBook Platform
Web Application (Healthcare SaaS)

MediBook Healthcare Passport
Web Application (Healthcare SaaS)

MediBook Pharma Platform
Web Application (Healthcare SaaS)
© 2026 Sina Ganji. All rights reserved.