-
Notifications
You must be signed in to change notification settings - Fork 34
/
game.py
77 lines (57 loc) · 1.81 KB
/
game.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
import numpy as np
class Connect2Game:
"""
A very, very simple game of ConnectX in which we have:
rows: 1
columns: 4
winNumber: 2
"""
def __init__(self):
self.columns = 4
self.win = 2
def get_init_board(self):
b = np.zeros((self.columns,), dtype=np.int)
return b
def get_board_size(self):
return self.columns
def get_action_size(self):
return self.columns
def get_next_state(self, board, player, action):
b = np.copy(board)
b[action] = player
# Return the new game, but
# change the perspective of the game with negative
return (b, -player)
def has_legal_moves(self, board):
for index in range(self.columns):
if board[index] == 0:
return True
return False
def get_valid_moves(self, board):
# All moves are invalid by default
valid_moves = [0] * self.get_action_size()
for index in range(self.columns):
if board[index] == 0:
valid_moves[index] = 1
return valid_moves
def is_win(self, board, player):
count = 0
for index in range(self.columns):
if board[index] == player:
count = count + 1
else:
count = 0
if count == self.win:
return True
return False
def get_reward_for_player(self, board, player):
# return None if not ended, 1 if player 1 wins, -1 if player 1 lost
if self.is_win(board, player):
return 1
if self.is_win(board, -player):
return -1
if self.has_legal_moves(board):
return None
return 0
def get_canonical_board(self, board, player):
return player * board