-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_state.py
81 lines (66 loc) · 2.08 KB
/
game_state.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
"""
The GameState superclass.
NOTE: You do not have to run python-ta on this file.
"""
from typing import Any
class GameState:
"""
The state of a game at a certain point in time.
WIN - score if player is in a winning position
LOSE - score if player is in a losing position
DRAW - score if player is in a tied position
p1_turn - whether it is p1's turn or not
"""
WIN: int = 1
LOSE: int = -1
DRAW: int = 0
p1_turn: bool
def __init__(self, is_p1_turn: bool) -> None:
"""
Initialize this game state and set the current player based on
is_p1_turn.
"""
self.p1_turn = is_p1_turn
def __str__(self) -> str:
"""
Return a string representation of the current state of the game.
"""
raise NotImplementedError
def get_possible_moves(self) -> list:
"""
Return all possible moves that can be applied to this state.
"""
raise NotImplementedError
def get_current_player_name(self) -> str:
"""
Return 'p1' if the current player is Player 1, and 'p2' if the current
player is Player 2.
"""
if self.p1_turn:
return 'p1'
return 'p2'
def make_move(self, move: Any) -> 'GameState':
"""
Return the GameState that results from applying move to this GameState.
"""
raise NotImplementedError
def is_valid_move(self, move: Any) -> bool:
"""
Return whether move is a valid move for this GameState.
"""
return move in self.get_possible_moves()
def __repr__(self) -> Any:
"""
Return a representation of this state (which can be used for
equality testing).
"""
raise NotImplementedError
def rough_outcome(self) -> float:
"""
Return an estimate in interval [LOSE, WIN] of best outcome the current
player can guarantee from state self.
"""
raise NotImplementedError
if __name__ == "__main__":
from python_ta import check_all
check_all(config="a2_pyta.txt")