in gym/gym/envs/board_game/hex.py [0:0]
def game_finished(board):
# Returns 1 if player 1 wins, -1 if player 2 wins and 0 otherwise
d = board.shape[1]
inpath = set()
newset = set()
for i in range(d):
if board[0, 0, i] == 1:
newset.add(i)
while len(newset) > 0:
for i in range(len(newset)):
v = newset.pop()
inpath.add(v)
cx = v // d
cy = v % d
# Left
if cy > 0 and board[0, cx, cy - 1] == 1:
v = cx * d + cy - 1
if v not in inpath:
newset.add(v)
# Right
if cy + 1 < d and board[0, cx, cy + 1] == 1:
v = cx * d + cy + 1
if v not in inpath:
newset.add(v)
# Up
if cx > 0 and board[0, cx - 1, cy] == 1:
v = (cx - 1) * d + cy
if v not in inpath:
newset.add(v)
# Down
if cx + 1 < d and board[0, cx + 1, cy] == 1:
if cx + 1 == d - 1:
return 1
v = (cx + 1) * d + cy
if v not in inpath:
newset.add(v)
# Up Right
if cx > 0 and cy + 1 < d and board[0, cx - 1, cy + 1] == 1:
v = (cx - 1) * d + cy + 1
if v not in inpath:
newset.add(v)
# Down Left
if cx + 1 < d and cy > 0 and board[0, cx + 1, cy - 1] == 1:
if cx + 1 == d - 1:
return 1
v = (cx + 1) * d + cy - 1
if v not in inpath:
newset.add(v)
inpath.clear()
newset.clear()
for i in range(d):
if board[1, i, 0] == 1:
newset.add(i)
while len(newset) > 0:
for i in range(len(newset)):
v = newset.pop()
inpath.add(v)
cy = v // d
cx = v % d
# Left
if cy > 0 and board[1, cx, cy - 1] == 1:
v = (cy - 1) * d + cx
if v not in inpath:
newset.add(v)
# Right
if cy + 1 < d and board[1, cx, cy + 1] == 1:
if cy + 1 == d - 1:
return -1
v = (cy + 1) * d + cx
if v not in inpath:
newset.add(v)
# Up
if cx > 0 and board[1, cx - 1, cy] == 1:
v = cy * d + cx - 1
if v not in inpath:
newset.add(v)
# Down
if cx + 1 < d and board[1, cx + 1, cy] == 1:
v = cy * d + cx + 1
if v not in inpath:
newset.add(v)
# Up Right
if cx > 0 and cy + 1 < d and board[1, cx - 1, cy + 1] == 1:
if cy + 1 == d - 1:
return -1
v = (cy + 1) * d + cx - 1
if v not in inpath:
newset.add(v)
# Left Down
if cx + 1 < d and cy > 0 and board[1, cx + 1, cy - 1] == 1:
v = (cy - 1) * d + cx + 1
if v not in inpath:
newset.add(v)
return 0