-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetMove.py
80 lines (72 loc) · 2.13 KB
/
getMove.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
##Rose McDonald##
def isWin(b, m):
return((b[0][0] == m and b[0][1] == m and b[0][2] == m) or
(b[1][0] == m and b[1][1] == m and b[1][2] == m) or
(b[2][0] == m and b[2][1] == m and b[2][2] == m) or
(b[0][0] == m and b[1][1] == m and b[2][2] == m) or
(b[2][0] == m and b[1][1] == m and b[0][2] == m) or
(b[0][0] == m and b[1][0] == m and b[2][0] == m) or
(b[0][1] == m and b[1][1] == m and b[2][1] == m) or
(b[0][2] == m and b[1][2] == m and b[2][2] == m))
def isDraw(b):
return ' ' not in b
def copyBoard(b):
tempBoard = [[0]*3]*3
tempBoard = [[b[i][j] for j in range(3)] for i in range(3)]
return tempBoard
def isWinMove(b, m, i, j):
#modify a temp board
bTemp = copyBoard(b)
bTemp[i][j] = m
return isWin(b, m)
def isForkMove(b, m, i, j):
bTemp = copyBoard(b)
bTemp[i][j] = m
wins = 0
rows = 3
columns = 3
for i in range(rows):
for j in range(columns):
if bTemp[i][j] == ' ' and isWinMove(bTemp, m, i, j):
wins += 1
return wins > 1
#return isWin(b, m)
#get gameboard, return next move
def getNextMove(board, markPlayer, markRobot):
#markPlayer = 'X'
#markRobot = 'O'
rows = 3
columns = 3
#check robot win
for i in range(rows):
for j in range(columns):
if board[i][j] == ' ' and isWinMove(board, markRobot, i, j):
return [i,j]
#check player win
for i in range(rows):
for j in range(columns):
if board[i][j] == ' ' and isWinMove(board, markPlayer, i, j):
return [i,j]
#check robot fork
for i in range(rows):
for j in range(columns):
if board[i][j] == ' ' and isForkMove(board, markRobot, i, j):
return [i,j]
#check player fork
for i in range(rows):
for j in range(columns):
if board[i][j] == ' ' and isForkMove(board, markPlayer, i, j):
return [i,j]
#play center
if board[1][1] == ' ':
return [1,1]
#play corner
for i in [0,2]:
for j in [0,2]:
if board[i][j] == ' ':
return [i,j]
#play side
for i in range(rows):
for j in range(columns):
if (i != 1 and j != 1) and board[i][j] == ' ':
return [i,j]