Implement game logic and alpha-beta algorithm for Awalé#1
Open
Flex1-tech wants to merge 1 commit into
Open
Conversation
…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.
There was a problem hiding this comment.
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()etGame.get_winner()+ ajustements sur la capture. - Introduction d’un module
engine/alpha_beta/elagage.pyet intégration de l’IA dansmain.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 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.