-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_board.py
171 lines (146 loc) · 4.19 KB
/
main_board.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
from p5 import *
import random
board = [
['', '', ''],
['', '', ''],
['', '', '']
]
players = ('X', 'O')
currentPlayer = random.randint(0,1)
ai = 1
human = 0
# 0 -> human ......... 1-> ai
width = 400
height = 400
w = width/3
h = height/3
def setup():
size(width, height)
scores = {'X': -10, 'O': 10, 'tie': 0}
def wincheck():
global win_index
res = 2
match = 'tie'
for i in range(3):
if board[0][i] == board[1][i] == board[2][i] != '':
win_index = [f'0{i}', f'1{i}', f'2{i}']
res = 1
match = board[0][i]
return res, match
if board[i][0] == board[i][1] == board[i][2] != '':
win_index = [f'{i}0', f'{i}1', f'{i}2']
res = 1
match = board[i][0]
return res, match
if board[0][0] == board[1][1] == board[2][2] != '':
win_index = ['00', '11', '22']
res = 1
match = board[0][0]
return res, match
if board[2][0] == board[1][1] == board[0][2] != '':
win_index = ['20', '11', '02']
res = 1
match = board[2][0]
return res, match
for i in range(3):
for j in range(3):
if board[i][j]=='':
res=0
match=None
break
# return res, match
return res, match
def minimax(depth, toMaxi):
global board
res, match = wincheck()
if res != 0:
return scores[match]-(depth/10)
if toMaxi:
bestscore = -10**9
for i in range(3):
for j in range(3):
if board[i][j] == '':
board[i][j] = players[ai]
score = minimax(depth+1, False)
board[i][j] = ''
bestscore = max(score, bestscore)
return bestscore
else:
bestscore = 10**9
for i in range(3):
for j in range(3):
if board[i][j] == '':
board[i][j] = players[human]
score = minimax(depth+1, True)
board[i][j] = ''
bestscore = min(score, bestscore)
return bestscore
def bestmove():
global board
'''
for every empty pos, put O their and call minimax and get score for that move
O paka au minimax pakeiki score update kare jadi seta higher
'''
bestscore = -10**9
for i in range(3):
for j in range(3):
if board[i][j] == '':
board[i][j] = players[ai]
score = minimax(0, False)
board[i][j] = ''
if score > bestscore:
bestscore = score
bi = i
bj = j
nextTurn(bi, bj)
def nextTurn(i, j):
global currentPlayer
if board[i][j] == "":
board[i][j] = players[currentPlayer]
currentPlayer = int(not currentPlayer)
win_index = []
def draw():
res, match = wincheck()
if res == 1 or res == 2:
if res == 1:
print(f"{match} wins")
else:
print("draw")
no_loop()
# return
if currentPlayer == human and res == 0:
if mouse_is_pressed:
i = floor(mouse_x/w)
j = floor(mouse_y/h)
nextTurn(j, i)
elif currentPlayer == ai and res == 0:
bestmove()
background(0)
if res==2:
stroke(Color(255,0,102))
else:
stroke(Color(0,255,153))
line((w, 0), (w, height))
line((w*2, 0), (w*2, height))
line((0, h), (width, h))
line((0, h*2), (width, h*2))
for i in range(3): # i should be j maybe
for j in range(3): # j sould be i maybe
spot = board[j][i]
x = w*i + w/2
y = h*j + h/2
stroke_weight(4)
# print(win_index)
if res == 1 and f'{j}{i}' in win_index:
stroke(Color(0, 100, 255))
else:
stroke(Color(0,255,153))
if spot == players[1]:
no_fill()
circle((x, y), w/2)
elif spot == players[0]:
xr = w/4
line((x-xr, y-xr), (x+xr, y+xr))
line((x+xr, y-xr), (x-xr, y+xr))
if __name__ == '__main__':
run()