-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
104 lines (80 loc) · 3.19 KB
/
utils.py
File metadata and controls
104 lines (80 loc) · 3.19 KB
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
from collections import namedtuple
GameState = namedtuple('GameState', 'to_move, utility, board, moves')
class Game:
def actions(self, state):
"""Return a list of the allowable moves at this point."""
raise NotImplementedError
def result(self, state, move):
"""Return the state that results from making a move from a state."""
raise NotImplementedError
def utility(self, state, player):
"""Return the value of this final state to player."""
raise NotImplementedError
def terminal_test(self, state):
"""Return True if this is a final state for the game."""
return not self.actions(state)
def to_move(self, state):
"""Return the player whose move it is in this state."""
return state.to_move
def display(self, state):
"""Print or otherwise display the state."""
print(state)
def __repr__(self):
return '<{}>'.format(self.__class__.__name__)
def play_game(self, *players):
"""Play an n-person, move-alternating game."""
state = self.initial
while True:
for player in players:
move = player(self, state)
state = self.result(state, move)
if self.terminal_test(state):
# self.display(state)
return self.utility(state, self.to_move(self.initial))
class GameOfNim(Game):
def __init__(self, board):
possible_moves = [(r, n) for r in range(0, len(board)) for n in range(1, board[r] + 1)]
# to_move: C = comp, P = player
self.initial = GameState(to_move='C', utility=0, board=board, moves=possible_moves)
# returns new state reached from given state and given move
def result(self, state, move):
if move not in state.moves:
return state
newBoard = state.board.copy()
newBoard[move[0]] = newBoard[move[0]] - move[1]
newMoves = [(r, n) for r in range(0, len(newBoard)) for n in range(1, newBoard[r] + 1)]
newPlyr = 'P' if state.to_move == 'C' else 'C'
return GameState(to_move=newPlyr,
utility=self.compute_utility(newBoard, newPlyr),
board=newBoard, moves=newMoves)
# returns a list of valid actions in the given state
def actions(self, state):
return state.moves
# returns True if given state represents end of game
def terminal_test(self, state):
for idx in state.board:
if idx != 0:
return False
return True
# returns +1 if the Player wins or -1 if Computer wins
def utility(self, state, player):
if player == 'C':
return state.utility
else:
return -state.utility
def compute_utility(self, board, player):
if sum(board) == 0:
if player == 'C':
return 1
else:
return -1
return 0
'''
# added under player_query
def display_board(self, rockList, randPile):
# display board
print("-" * 25)
for i in range(0, randPile):
print('Pile {}: {}'.format(i + 1, 'O' * rockList[i]))
print("-" * 25)
'''