-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGipfGame.py
209 lines (177 loc) · 6.09 KB
/
GipfGame.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from __future__ import print_function
import sys
sys.path.append('..')
from Game import Game
from .GipfLogic import Board
import numpy as np
import string
class GipfGame(Game):
alpha = list(string.ascii_lowercase)
spot_content = {
-1: "X",
+0: " ",
+1: "O"
}
def __init__(self, n):
self.n = n
#self.refill()
def refill(self):
self.reserve = [1, None, 1] #normal: 12, mini: 5
def getInitBoard(self):
""" Returns the initial board (numpy board)
"""
b = Board(self.n)
return np.array(b.pieces)
def getBoardSize(self):
# (a,b) tuple
return (int(self.n//1.8), self.n)
def getActionSize(self):
# return number of actions
return round(self.n*2.34) #normal: 42, mini:30
def getCanonicalForm(self, board, player):
# return state if player==1, else return -state if player==-1
if player == -1:
board = board*player
r_tmp = board[0][0]
board[0][0] = abs(board[0][2])
board[0][2] = abs(r_tmp)
return board
def stringRepresentation(self, board):
return board.tostring()
def getSymmetries(self, board, pi):
# mirror, rotational
assert(len(pi) == 30)
b = Board(self.n)
b.pieces = np.copy(board)
syms = []
newB = board
newPi = pi
for i in range(1, 5):
if i % 2:
# flip board left<->right:
newB = np.fliplr(newB)
# flip pi left<->right:
newPi = self.fliplr_action(newPi)
else:
# flip board up<->down:
newB = np.flipud(newB)
# flip pi up<->down:
newPi = self.flipud_action(newPi)
# record the symmetry
syms += [(newB, list(newPi))]
return syms
def fliplr_action(self, pi):
"""
Input: first XOR second half of Pi
Returns: the half of pie in a reordered list that corresponds
to a left<--->right flip of the board
"""
assert (len(pi) == 30)
flip_indices = [0, 3, 4, 1, 2, 7, 8, 5, 6, 10, 9, 13, 14, 11, 12, 17,
18, 15, 16, 20, 19, 23, 24, 21, 22, 27, 28, 25, 26, 29]
pi_new = [pi[i] for i in flip_indices]
return pi_new
def flipud_action(self, pi):
"""
Input: first XOR second half of Pi
Returns: the half of pie in a reordered list that corresponds
to a left<--->right flip of the board
"""
assert (len(pi) == 30)
flip_indices = [29, 25, 26, 27, 28, 21, 22, 23, 24, 19, 20, 16, 15, 18,
17, 12, 11, 14, 13, 9, 10, 5, 6, 7, 8, 1, 2, 3, 4, 0]
pi_new = [pi[i] for i in flip_indices]
return pi_new
def getNextState(self, board, curPlayer, action):
"""Executes the given move and returns next (board, player)
action must be a valid move
"""
b = Board(self.n)
b.pieces = np.copy(board)
# Reserve um einen reduzieren
self.dec_reserve(b.pieces, curPlayer)
# Move ausführen
b.execute_move(action, curPlayer)
# Steine schlagen und Reserve auffüllen
add_reserve = b.remove_lines(curPlayer)
self.inc_reserve(b.pieces, add_reserve)
return (b.pieces, -curPlayer)
def getValidMoves(self, board):
"""Returns a vector with all possible moves
"""
b = Board(self.n)
b.pieces = np.copy(board)
legalMoves = b.get_legal_moves_binary()
return legalMoves
def getValidMovesHuman(self, board):
"""Returns a vector with all possible moves
"""
b = Board(self.n)
b.pieces = np.copy(board)
allMoves = b.get_all_moves()
valids = b.get_legal_moves_binary()
return allMoves, valids
#def hasPiecesLeft(self, player):
# """Returns True if the player still has pieces left in his reserve
# """
# if self.reserve(player+1) > 0:
# return True
# else:
# return False
def getGameEnded(self, board):
"""Returns 0 if not ended, 1 if player 1 won, -1 if player 1 lost
"""
"""
if self.reserve[0] == 0: # Player -1
return 1
elif self.reserve[2] == 0: # Player 1
return -1
else:
return 0
"""
if board[0][0] == 0: # Player -1
return 1
elif board[0][2] == 0: # Player 1
return -1
else:
return 0
def dec_reserve(self, board, player):
#self.reserve[player+1] -= 1
board[0][player+1] -= 1
def inc_reserve(self, board, add_reserve):
#self.reserve = np.add(self.reserve, add_reserve, where=[1,0,1])
board[0][0] += add_reserve[0]
board[0][2] += add_reserve[2]
def display(self, board):
"""Displays the current board
"""
b = Board(self.n)
n = board.shape
print("\nReserve Black (X): ", board[0][0])
# label top
print(" ", end="")
for y in range(n[1]):
if y < 4:
print(GipfGame.alpha[y]+(str(y+4)), end=" ")
else:
print(GipfGame.alpha[y]+(str(10-y)), end=" ")
print("")
# board
for x in range(n[0]):
print(" ", end="")
for y in range(n[1]):
if (x,y) in b.get_startingPoints():
print(" "+chr(9679), end=" ")
elif (x,y) in b.get_actBoard():
piece = board[x][y] # get the piece to print
print("("+GipfGame.spot_content[piece]+")", end="")
else:
print(" ", end="")
print("")
# label bottom
print(" ", end="")
for y in range(n[1]):
print(GipfGame.alpha[y]+"1", end=" ")
print("")
print("Reserve White (O): ", board[0][2])
print("")