-
Notifications
You must be signed in to change notification settings - Fork 1
/
min_max_alg.py
139 lines (137 loc) · 3.97 KB
/
min_max_alg.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
import copy
import random
import math
#### IMPORTANT
##### taken and adapted from https://stackoverflow.com/questions/61072185/minimax-algorithm-for-tictactoe-in-python ############
X = "X"
O = "O"
EMPTY = None
def initial_state():
return [[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY]]
def player(board):
count_x, count_o = count(board)
if count_o + count_x == 0:
return X
elif count_x > count_o and count_x + count_o != 9:
return O
elif count_x == count_o and count_x + count_o != 9:
return X
elif count_x + count_o == 9:
return X
def count(board):
count_x, count_o = (0, 0)
for i in range(3):
for j in range(3):
if board[i][j] == X:
count_x += 1
elif board[i][j] == O:
count_o += 1
return count_x, count_o
def actions(board):
action = []
for i in range(3):
for j in range(3):
if board[i][j] == EMPTY:
action.append((i, j))
return action
def result(board, action):
board_copy = copy.deepcopy(board)
if not action in actions(board):
raise Exception
else:
move = player(board_copy)
i, j = action
board_copy[i][j] = move
return board_copy
def utility(board):
if winner(board) == X:
return 1
elif winner(board) == O:
return -1
elif winner(board) == None:
return 0
def terminal(board):
count_x, count_o = count(board)
if count_x + count_o == 9 or winner(board) != None:
return True
else:
return False
def winner(board):
for i in range(3):
if (board[i][0] == board[i][1] == board[i][2] and board[i][0] != EMPTY):
return board[i][2]
elif (board[0][i] == board[1][i] == board[2][i] and board[0][i] != EMPTY):
return board[2][i]
if (board[0][0] == board[1][1] == board[2][2] and board[0][0] != EMPTY):
return board[0][0]
elif (board[0][2] == board[1][1] == board[2][0] and board[2][0] != EMPTY):
return board[0][2]
return None
def minimax(board):
if terminal(board):
return None
if player(board) == X:
vI = -math.inf
move = set()
for action in actions(board):
v = min_value(result(board,action))
if v > vI:
vI = v
move = action
elif player(board) == O:
vI = math.inf
move = set()
for action in actions(board):
v = max_value(result(board,action))
if v < vI:
vI = v
move = action
return move
def max_value(board):
if terminal(board):
return utility(board)
v = -math.inf
for action in actions(board):
v = max(v, min_value(result(board, action)))
return v
def min_value(board):
if terminal(board):
return utility(board)
v = math.inf
for action in actions(board):
v = min(v, max_value(result(board, action)))
return v
def convert(state):
return [state[0:3],state[3:6],state[6:9]]
def GetMove(state,tup = False):
conv= [[0,0,0],[0,0,0],[0,0,0]]
for i in range(9):
conv[i//3][i%3] = state[i]
for i in range(3):
for y in range(3):
if conv[i][y] == 0:
conv[i][y] = EMPTY
if conv[i][y] == 1:
conv[i][y] = X
if conv[i][y] == 2:
conv[i][y] = O
if tup == False:
a,b = (minimax(conv))
return(a*3 + b)
else:
return(minimax(conv))
# for i in range(100):
# state = [0]*9
# while 0 in state:
# state[GetMove(state, False)] = 1
# while True:
# rand = random.randint(0,8)
# if state[rand] ==0:
# state[rand] = 2
# break
# if winner(convert(state)) != None:
# print('Winner: '+str(winner(convert(state))))
# print(state)
# break