Skip to content

Implement game logic and alpha-beta algorithm for Awalé#1

Open
Flex1-tech wants to merge 1 commit into
mainfrom
alpha-beta
Open

Implement game logic and alpha-beta algorithm for Awalé#1
Flex1-tech wants to merge 1 commit into
mainfrom
alpha-beta

Conversation

@Flex1-tech

Copy link
Copy Markdown

Add the game rules and logic for Awalé, including the implementation of the alpha-beta algorithm to determine the best move. Introduce methods to manage game states and validate moves.

…ntation de l'algorithme alpha-beta pour le choix du meilleur coup. Création de nouvelles méthodes pour gérer les états du jeu et la validation des mouvements.
Copilot AI review requested due to automatic review settings June 22, 2026 03:09

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Cette PR ajoute une IA (alpha-bêta) et complète la logique de jeu Awalé côté moteur (coups valides avec règle de “nourrissage”, génération d’états enfants, détection de fin de partie et détermination du vainqueur), puis branche l’IA dans la boucle main.py.

Changes:

  • Ajout de Rules.get_valid_moves() pour intégrer la règle de “nourrissage” quand l’adversaire n’a plus de graines.
  • Ajout de méthodes Game.get_children(), Game.is_game_over() et Game.get_winner() + ajustements sur la capture.
  • Introduction d’un module engine/alpha_beta/elagage.py et intégration de l’IA dans main.py.

Reviewed changes

Copilot reviewed 6 out of 8 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
main.py Ajoute gestion fin de partie et tour IA (appel à best_move).
engine/rules.py Ajoute get_valid_moves() avec règle de nourrissage.
engine/game.py Utilise get_valid_moves() dans play_move, ajoute enfants/fin de partie/vainqueur.
engine/board.py Ajoute Board.copy() pour cloner l’état du plateau.
engine/alpha_beta/elagage.py Nouveau module alpha-bêta/negamax + évaluation et sélection du meilleur coup.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread engine/game.py
Comment on lines +138 to +148
for hole in range(12):
if Rules.is_valid_move(self.board, hole, self.current_player):
child = Game()
child.board = self.board.copy()
child.score_p1 = self.score_p1
child.score_p2 = self.score_p2
child.current_player = self.current_player

child.play_move(hole)

children.append(child)
Comment on lines +1 to +3
import numpy as np
from ..rules import Rules
from engine.game import Game
Comment on lines +63 to +77
children = state.get_children()
if depth == 0 or state.is_game_over():
return evaluate_for(state, current_player)

best = -np.inf
next_player = 2 if current_player == 1 else 1


for child in children:
eval = -alpha_beta(child, depth - 1, -beta, -alpha, next_player)
best = max(best, eval)
alpha = min(beta, eval)
if beta <= alpha:
break # Coupure alpha
return best
Comment on lines +51 to +62
def alpha_beta(state, depth, alpha, beta, current_player) -> int:
"""
Implémentation de l'algorithme alpha-beta pour le jeu Awélé.
Args:
state: l'état actuel du jeu (instance de Game).
depth: la profondeur maximale de recherche.
alpha: la valeur alpha pour la coupure.
beta: la valeur bêta pour la coupure.
maximizing_player: booléen indiquant si c'est au tour du joueur maximisant.
Returns:
Le score de la meilleure action pour le joueur maximisant.
"""
Comment on lines +80 to +99
def best_move(state, depth=100) -> int:
current_player = state.current_player
next_player = 2 if current_player == 1 else 1
best_score, best_mv = -np.inf, None


for hole in Rules.get_valid_moves(state.board, current_player):
# Simuler le coup dans un nouvel état enfant
child = Game()
child.board = state.board.copy()
child.score_p1 = state.score_p1
child.score_p2 = state.score_p2
child.current_player = current_player
child.play_move(hole)

s = -alpha_beta(child, depth - 1, -np.inf, np.inf, next_player)
if s > best_score:
best_score, best_mv = s, hole

return best_mv
Comment on lines +45 to +48
def evaluate_for(state, player) -> float:
"""Score relatif au joueur donné — les deux joueurs maximisent toujours."""
raw = evaluate(state)
return raw if player == 1 else -raw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants