-
Notifications
You must be signed in to change notification settings - Fork 0
/
MiniMaxTTT.py
153 lines (132 loc) · 4.35 KB
/
MiniMaxTTT.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
#!/bin/python
import random
import copy
import time
start_time = time.time()
globvar = 0
class GameNode:
def __init__(self):
#Standard board
#self.board = ['_']*9
# Board in which X wins in the next move
self.board = ['X','O','X','X','O','_','_','_','O']
#self.board = ['X','_','_','_','_','Y','_','_','_']
self.value = 0
self.children = []
def checkWin(board,player):
if ( (board[0] == 'X' and board[1] == 'X' and board[2] == 'X') or #top row
(board[3] == 'X' and board[4] == 'X' and board[5] == 'X') or #middle row
(board[6] == 'X' and board[7] == 'X' and board[8] == 'X') or #bottom row
(board[0] == 'X' and board[3] == 'X' and board[6] == 'X') or #first column
(board[1] == 'X' and board[4] == 'X' and board[7] == 'X') or # middle column
(board[2] == 'X' and board[5] == 'X' and board[8] == 'X') or # last column
(board[0] == 'X' and board[4] == 'X' and board[8] == 'X') or # left-right diagonal column
(board[2] == 'X' and board[4] == 'X' and board[6] == 'X')):
return 'X' #right-left diagonal column
elif ( (board[0] == 'O' and board[1] == 'O' and board[2] == 'O') or #top row
(board[3] == 'O' and board[4] == 'O' and board[5] == 'O') or #middle row
(board[6] == 'O' and board[7] == 'O' and board[8] == 'O') or #bottom row
(board[0] == 'O' and board[3] == 'O' and board[6] == 'O') or #first column
(board[1] == 'O' and board[4] == 'O' and board[7] == 'O') or # middle column
(board[2] == 'O' and board[5] == 'O' and board[8] == 'O') or # last column
(board[0] == 'O' and board[4] == 'O' and board[8] == 'O') or # left-right diagonal column
(board[2] == 'O' and board[4] == 'O' and board[6] == 'O')):
return 'O'
else:
return None
def copyBoard(node):
tmp = GameNode()
for i in range(len(node.board)):
tmp.board[i] = node.board[i]
return tmp
def possibleMoves(board,player):
moves = []
for i in range(len(board)):
if board[i] == '_':
newNode = GameNode()
newNode.board = copy.copy(board)
newNode.board[i] = player
moves.append(newNode)
return moves
def checkTie(board):
if '_' in board:
return False
else:
return True
def minimax(node,player):
global globvar
#if we are at a goalnode return it's value
if checkWin(node.board, player) == 'X':
node.value = 10
return node.value
elif checkWin(node.board, player) == 'O':
node.value = -10
return node.value
if checkTie(node.board):
return node.value
#if our player is 'X' return max of minimax calls
if player == 'X':
node.children = possibleMoves(node.board,player)
vals = []
for c in node.children:
globvar += 1
vals.append(minimax(c,'O' if player == 'X' else 'X'))
return max(vals)
#if our player is 'Y' return min of minimax calls
if player == 'O':
node.children = possibleMoves(node.board,player)
vals = []
for c in node.children:
globvar += 1
vals.append(minimax(c,'O' if player == 'X' else 'X'))
return min(vals)
def setValue(player):
return 10 if player == 'X' else -10
def printBoard(board):
print "-------------"
print board[0], " | ", board[1]," | ", board[2]
print "-------------"
print board[3], " | ", board[4]," | ", board[5]
print "-------------"
print board[6], " | ", board[7]," | ", board[8]
print "-------------"
# Complete the function below to print 2 integers separated by a single space which will be your next move
def nextMove(player,board):
moves = possibleMoves(board,player)
values = []
for move in moves:
values.append(minimax(move,'O' if player == 'X' else 'X'))
if player == 'O':
indexToFind = values.index(min(values))
if player == 'X':
indexToFind = values.index(max(values))
print globvar
return findIndexOfDifferentBoardElement(board,moves[indexToFind].board)
def findIndexOfDifferentBoardElement(oldBoard,newBoard):
for i in range(len(oldBoard)):
if oldBoard[i] != newBoard[i]:
return mapMove(i)
def mapMove(index):
switcher = {
0: (0,0),
1: (0,1),
2: (0,2),
3: (1,0),
4: (1,1),
5: (1,2),
6: (2,0),
7: (2,1),
8: (2,2),
}
print switcher.get(index)[0], switcher.get(index)[1]
gn = GameNode()
#If player is X, I'm the first player.
#If player is O, I'm the second player.
player = raw_input()
#Read the board now. The board is a 3x3 array filled with X, O or _.
boardString = raw_input()
boardString += raw_input()
boardString += raw_input()
board = list(boardString)
nextMove(player,board);
print("--- %s seconds ---" % (time.time() - start_time))