-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamazons.py
114 lines (98 loc) · 3.91 KB
/
amazons.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from board import Board
from exceptions import InvalidFormatError
from players import *
from const import *
import random
def extract_positions(line):
"""
Récupère la liste de positions dans line
Args:
line (str): string sous la forme '<pos1>,<pos2>,<pos3>,...,<posn>'
Returns:
list: liste d'instances de Pos2D
Raises:
InvalidFormatError: si la ligne est vide
"""
if line == '':
raise InvalidFormatError('Liste de positions vide')
else:
return line.strip().split(',')
def read_file(path):
"""
Récupère les informations stockées dans le fichier donné
Args:
path (str): chemin vers un fichier de format de plateau
Returns:
tuple: (size, pos_black, pos_white, pos_arrows)
Raises:
InvalidFormatError: si le format du fichier est invalide
"""
with open(path, 'r') as f:
try:
size = int(f.readline().strip())
except ValueError: # Si la première ligne n'est pas un entier
raise InvalidFormatError('La taille du plateau n\'est pas donnée')
pos_black = extract_positions(f.readline())
pos_white = extract_positions(f.readline())
# on récupère la liste des positions des flèches
try:
pos_arrows = extract_positions(f.readline())
except InvalidFormatError:
pos_arrows = []
if f.readline() != '': # S'il reste du texte dans le fichier
raise InvalidFormatError('Format invalide: informations après les flèches')
return size, pos_black, pos_white, pos_arrows
class Amazons:
"""
La classe Amazons représente une instance du jeu des amazones au complet.
Attributes:
board (Board): le plateau de jeu
players (tuple): tuple des joueurs (instances de Player)
current_player_idx (int): indice du joueur dont c'est le tour
status (EndOfGameStatus): état de la fin de partie
"""
def __init__(self, path, P1isHuman,P2isHuman, timeLimit = 2):
"""
Args:
path (str): chemin vers le fichier représentant le plateau
"""
self.board = Board(*read_file(path))
if P1isHuman and P2isHuman:
self.players = (HumanPlayer(self.board, PLAYER_1), HumanPlayer(self.board, PLAYER_2,))
if not P1isHuman and not P2isHuman:
self.players = (AIPlayer(self.board, PLAYER_1,timeLimit), AIPlayer(self.board, PLAYER_2,timeLimit))
if not P1isHuman and P2isHuman:
self.players = (AIPlayer(self.board, PLAYER_1,timeLimit), HumanPlayer(self.board, PLAYER_2))
if P1isHuman and not P2isHuman:
self.players = (HumanPlayer(self.board, PLAYER_1), AIPlayer(self.board, PLAYER_2,timeLimit))
print(self.players)
self.current_player_idx = 0
self.status = None
random.seed(0xCAFE)
def play(self , action=None):
"""
Joue une partie du jeu des amazones
"""
print(self.board) # on affiche l'état actuel du plateau
# Le joueur actuel joue son coup
if action != None:
self.players[self.current_player_idx].play(action)
else:
"ACTION = NONE"
self.minimaxPlay = self.players[self.current_player_idx].play()
# On passe au joueur suivant
self.current_player_idx = 1-self.current_player_idx
print(self.board) # On affiche le plateau après le dernier coup
#self.show_winner() # On montre qui a gagné
def is_over(self):
"""
Détermine si la partie est terminée
Returns:
bool: True si la partie est finie et False sinon
"""
self.status = self.board.status
return self.status.over
def show_winner(self):
winner = '1' if self.status.winner == PLAYER_1 else '2'
print(f'Player {winner} won: {self.status.winner_score} vs {self.status.loser_score}!')
return winner