-
Notifications
You must be signed in to change notification settings - Fork 16
/
tic_tac_toe_x.py
196 lines (157 loc) · 7.38 KB
/
tic_tac_toe_x.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
"""
Full code for running a game of tic-tac-toe on a board of any size with a specified number in a row for the win. This is
similar to tic_tac_toe.py but all relevent moves are paramiterized by board_size arg that sets how big the board is and
winning_length which determines how many in a row are needed to win. Defaults are 5 and 4. This allows you to play games
in a more complex environment than standard tic-tac-toe.
Two players take turns making moves on squares of the board, the first to get winning_length in a row, including
diagonals, wins. If there are no valid moves left to make the game ends a draw.
The main method to use here is play_game which simulates a game to the end using the function args it takes to determine
where each player plays.
The board is represented by a board_size x board_size tuple of ints. A 0 means no player has played in a space, 1 means
player one has played there, -1 means the seconds player has played there. The apply_move method can be used to return a
copy of a given state with a given move applied. This can be useful for doing min-max or monte carlo sampling.
"""
import random
import itertools
def _new_board(board_size):
"""Return a emprty tic-tac-toe board we can use for simulating a game.
Args:
board_size (int): The size of one side of the board, a board_size * board_size board is created
Returns:
board_size x board_size tuple of ints
"""
return tuple(tuple(0 for _ in range(board_size)) for _ in range(board_size))
def apply_move(board_state, move, side):
"""Returns a copy of the given board_state with the desired move applied.
Args:
board_state (2d tuple of int): The given board_state we want to apply the move to.
move (int, int): The position we want to make the move in.
side (int): The side we are making this move for, 1 for the first player, -1 for the second player.
Returns:
(2d tuple of int): A copy of the board_state with the given move applied for the given side.
"""
move_x, move_y = move
def get_tuples():
for x in range(len(board_state)):
if move_x == x:
temp = list(board_state[x])
temp[move_y] = side
yield tuple(temp)
else:
yield board_state[x]
return tuple(get_tuples())
def available_moves(board_state):
"""Get all legal moves for the current board_state. For Tic-tac-toe that is all positions that do not currently have
pieces played.
Args:
board_state: The board_state we want to check for valid moves.
Returns:
Generator of (int, int): All the valid moves that can be played in this position.
"""
for x, y in itertools.product(range(len(board_state)), range(len(board_state[0]))):
if board_state[x][y] == 0:
yield (x, y)
def _has_winning_line(line, winning_length):
count = 0
last_side = 0
for x in line:
if x == last_side:
count += 1
if count == winning_length:
return last_side
else:
count = 1
last_side = x
return 0
def has_winner(board_state, winning_length):
"""Determine if a player has won on the given board_state.
Args:
board_state (2d tuple of int): The current board_state we want to evaluate.
winning_length (int): The number of moves in a row needed for a win.
Returns:
int: 1 if player one has won, -1 if player 2 has won, otherwise 0.
"""
board_width = len(board_state)
board_height = len(board_state[0])
# check rows
for x in range(board_width):
winner = _has_winning_line(board_state[x], winning_length)
if winner != 0:
return winner
# check columns
for y in range(board_height):
winner = _has_winning_line((i[y] for i in board_state), winning_length)
if winner != 0:
return winner
# check diagonals
diagonals_start = -(board_width - winning_length)
diagonals_end = (board_width - winning_length)
for d in range(diagonals_start, diagonals_end+1):
winner = _has_winning_line(
(board_state[i][i + d] for i in range(max(-d, 0), min(board_width, board_height - d))),
winning_length)
if winner != 0:
return winner
for d in range(diagonals_start, diagonals_end+1):
winner = _has_winning_line(
(board_state[i][board_height - i - d - 1] for i in range(max(-d, 0), min(board_width, board_height - d))),
winning_length)
if winner != 0:
return winner
return 0 # no one has won, return 0 for a draw
def play_game(plus_player_func, minus_player_func, board_size=5, winning_length=4, log=False):
"""Run a single game of tic-tac-toe until the end, using the provided function args to determine the moves for each
player.
Args:
plus_player_func ((board_state(board_size by board_size tuple of int), side(int)) -> move((int, int))):
Function that takes the current board_state and side this player is playing, and returns the move the player
wants to play.
minus_player_func ((board_state(board_size by board_size tuple of int), side(int)) -> move((int, int))):
Function that takes the current board_state and side this player is playing, and returns the move the player
wants to play.
board_size (int): The size of a single side of the board. Game is played on a board_size*board_size sized board
winning_length (int): The number of pieces in a row needed to win a game.
log (bool): If True progress is logged to console, defaults to False
Returns:
int: 1 if the plus_player_func won, -1 if the minus_player_func won and 0 for a draw
"""
board_state = _new_board(board_size)
player_turn = 1
while True:
_available_moves = list(available_moves(board_state))
if len(_available_moves) == 0:
# draw
if log:
print("no moves left, game ended a draw")
return 0.
if player_turn > 0:
move = plus_player_func(board_state, 1)
else:
move = minus_player_func(board_state, -1)
if move not in _available_moves:
# if a player makes an invalid move the other player wins
if log:
print("illegal move ", move)
return -player_turn
board_state = apply_move(board_state, move, player_turn)
print(board_state)
winner = has_winner(board_state, winning_length)
if winner != 0:
if log:
print("we have a winner, side: %s" % player_turn)
return winner
player_turn = -player_turn
def random_player(board_state, _):
"""A player func that can be used in the play_game method. Given a board state it chooses a move randomly from the
valid moves in the current state.
Args:
board_state (2d tuple of int): The current state of the board
_: the side this player is playing, not used in this function because we are simply choosing the moves randomly
Returns:
(int, int): the move we want to play on the current board
"""
moves = list(available_moves(board_state))
return random.choice(moves)
if __name__ == '__main__':
# example of playing a game
play_game(random_player, random_player, log=True, board_size=10, winning_length=4)