-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMini-max_Tic-Tac-Toe.py
More file actions
81 lines (67 loc) · 2.49 KB
/
Copy pathMini-max_Tic-Tac-Toe.py
File metadata and controls
81 lines (67 loc) · 2.49 KB
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
"""
Mini-max Tic-Tac-Toe Player
"""
import poc_ttt_gui
import poc_ttt_provided as provided
# Set timeout, as mini-max can take a long time
import codeskulptor
codeskulptor.set_timeout(60)
# SCORING VALUES - DO NOT MODIFY
SCORES = {provided.PLAYERX: 1,
provided.DRAW: 0,
provided.PLAYERO: -1}
PLAYERX = provided.PLAYERX
PLAYERO = provided.PLAYERO
EMPTY = provided.EMPTY
def mm_move(board, player):
"""
Make a move on the board.
Returns a tuple with two elements. The first element is the score
of the given board and the second element is the desired move as a
tuple, (row, col).
"""
winner = board.check_win()
if winner != None:
return SCORES[winner], (-1, -1)
else:
empty_squares = board.get_empty_squares()
# if len(empty_squares) > 4:
# return -1, (-1, -1)
scores = []
for square in empty_squares :
#create next board
next_board = board.clone()
next_board.move(square[0], square[1], player)
#find next score with recursion
next_score = mm_move(next_board, provided.switch_player(player))[0]
if next_score == SCORES[player]:
return SCORES[player], square
scores.append(next_score)
score = SCORES[provided.switch_player(player)]
square = empty_squares[0]
for idx in range(len(empty_squares)):
if scores[idx] == SCORES[provided.DRAW] :
square = empty_squares[idx]
score = scores[idx]
break
return score, square
# next_move = mm_move(clone_board, provided.switch_player(player))
# if next_move[0] == player:
# return
def move_wrapper(board, player, trials):
"""
Wrapper to allow the use of the same infrastructure that was used
for Monte Carlo Tic-Tac-Toe.
"""
move = mm_move(board, player)
assert move[1] != (-1, -1), "returned illegal move (-1, -1)"
return move[1]
# Test game with the console or the GUI.
# Uncomment whichever you prefer.
# Both should be commented out when you submit for
# testing to save time.
# provided.play_game(move_wrapper, 1, False)
# poc_ttt_gui.run_gui(3, provided.PLAYERO, move_wrapper, 1, False)
#game =provided.TTTBoard(3, False, [[PLAYERX, EMPTY, EMPTY], [PLAYERO, PLAYERO, PLAYERX], [EMPTY, PLAYERX, EMPTY]])
#print game
#print mm_move(game, provided.PLAYERX)