-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
83 lines (66 loc) · 2.73 KB
/
test.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
from copy import deepcopy
xdim, ydim = 3, 2
class GameState:
def __init__(self):
self.board = [[0 for x in range(xdim)] for y in range(ydim)]
self.board[-1][-1] = 1
self.whose_turn = 0
self.player_locations = [None, None]
def forecast_move(self, move):
""" Return a new board object with the specified move
applied to the current game state.
Parameters
----------
move: tuple
The target position for the active player's next move
"""
if move not in get_legal_moves(self):
raise RuntimeError("Attempted forecast of illegal move")
newGameState = deepcopy(self)
newGameState[move[0]][move[1]] = 1
newGameState.player_locations[whose_turn] = move
newGameState.whose_turn ^= 1
return newGameState
def get_legal_moves(self):
""" Return a list of all legal moves available to the
active player. Each player should get a list of all
empty spaces on the board on their first move, and
otherwise they should get a list of all open spaces
in a straight line along any row, column or diagonal
from their current position. (Players CANNOT move
through obstacles or blocked squares.) Moves should
be a pair of integers in (column, row) order specifying
the zero-indexed coordinates on the board.
"""
moves = []
if self.player_locations[self.whose_turn] is None:
return self.get_blank_locations()
matrix = [(1,0), (-1,0), (0,1), (0,-1), (1,1), (1,-1), (-1, 1), (-1,-1)]
for dx, dy in matrix:
x,y = self.player_locations[self.whose_turn]
while x+dx <= xdim and x+dx >= 0 and y+dy <= ydim and y+dy >= 0:
x = x+dx
y = y+dx
if self.board[x][y] : break
moves.append((x,y))
return moves
def get_blank_locations(self):
print("\n\n\n\n#######\n\n")
for y in range(ydim): print(y)
print("\n\n\n\n#######\n\n")
return [(x,y) for y in range(ydim) for x in range(xdim)
if (self.board[x][y] == 0)]
print("Creating empty game board...")
g = GameState()
print("Getting legal moves for player 1...")
p1_empty_moves = g.get_legal_moves()
print("Found {} legal moves.".format(len(p1_empty_moves or [])))
print("Applying move (0, 0) for player 1...")
g1 = g.forecast_move((0, 0))
print("Getting legal moves for player 2...")
p2_empty_moves = g1.get_legal_moves()
if (0, 0) in set(p2_empty_moves):
print("Failed\n Uh oh! (0, 0) was not blocked properly when " +
"player 1 moved there.")
else:
print("Everything looks good!")