-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe2.py
186 lines (169 loc) · 6.05 KB
/
TicTacToe2.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
### Start with X or O. Input moves as numbers 0-8
import random
example_code=[' ',' ',' ',' ',' ',' ',' ',' ',' ']
class Board:
def __init__(self,code):
self.code=code
def print_board(self,code):
print("Current board")
print(code[0],' | ',code[1],' | ',code[2])
print('---+-----+---')
print(code[3],' | ',code[4],' | ',code[5])
print('---+-----+---')
print(code[6],' | ',code[7],' | ',code[8])
def add_move(self,code,move,team):
code[int(move)]=str(team)
def check_end(self,code):
for x in 'XO':
if code[0]==x and code[1]==x and code[2]==x:
return True,x
elif code[3]==x and code[4]==x and code[5]==x:
return True,x
elif code[6]==x and code[7]==x and code[8]==x:
return True,x
elif code[0]==x and code[3]==x and code[6]==x:
return True,x
elif code[1]==x and code[4]==x and code[7]==x:
return True,x
elif code[2]==x and code[5]==x and code[8]==x:
return True,x
elif code[0]==x and code[4]==x and code[8]==x:
return True,x
elif code[2]==x and code[4]==x and code[6]==x:
return True,x
###Draw
elif code[0]!=' ' and code[1]!=' ' and code[2]!=' ' and code[3]!=' ' and code[4]!=' ' and code[5]!=' ' and code[6]!=' ' and code[7]!=' ' and code[8]!=' ':
return True,None
return False,x
def next_move_moderate(code,ai_team):
board=Board(code)
def dfs(code,team,node):
over,winner=board.check_end(code)
if over:
if winner==ai_team:
return node,1
elif winner==None:
return node,0
else:
return node,-1
##########
##New team
if team=='O':
next_team='X'
else:
next_team='O'
####
max_score=None
best_move=None
for node in range(9):
if code[node]==' ':
new_code=code.copy()
new_code[node]=next_team
move,score=dfs(new_code,next_team,node)
if next_team==ai_team:
if max_score==None:
max_score=score
best_move=move
if score<max_score:
max_score=score
best_move=move
else:
if max_score==None:
max_score=score
best_move=move
if score>max_score:
max_score=score
best_move=move
###Tally scores here and find which move you want?
print ('returning',best_move,max_score)
return best_move,max_score
##########
best_move,max_score=dfs(code,team,None)
return best_move
def next_move(code,ai_team):
board=Board(code)
total=set()
def dfs(code,team,node,top=True):
over,winner=board.check_end(code)
if over:
s=''.join(str(x) for x in code)
total.add((s,winner,node))
if winner==ai_team:
return 1
elif winner==None:
return 0
else:
return -1
##########
##New team
if team=='O':
next_team='X'
else:
next_team='O'
####
max_score=None
best_move=None
for node in range(9):
if code[node]==' ':
new_code=code.copy()
new_code[node]=next_team
score=dfs(new_code,next_team,node,False)
if next_team==ai_team:
if max_score==None:
max_score=score
best_move=node
if score==max_score:
x=random.random()
if x>0.5:
best_move=node
if score>max_score:
max_score=score
best_move=node
else:
if max_score==None:
max_score=score
best_move=node
if score==max_score:
x=random.random()
if x>0.5:
best_move=node
if score<max_score:
max_score=score
best_move=node
###Tally scores here and find which move you want?
if top==True:
return best_move,max_score
return max_score
##########
best_move,max_score=dfs(code,team,None)
return best_move
board=Board(example_code)
board.print_board(example_code)
over=False
team=input('X or O?')
if team=='O':
ai_team='X'
move= next_move(example_code,ai_team)
board.add_move(example_code,move,ai_team)
board.print_board(example_code)
over,winner=board.check_end(example_code)
else:
ai_team='O'
while over==False:
move=input('Enter a move: ')
if example_code[int(move)]!=' ':
print('invalid move')
continue
board.add_move(example_code,move,team)
board.print_board(example_code)
over,winner=board.check_end(example_code)
if over:
break
move= next_move(example_code,ai_team)
board.add_move(example_code,move,ai_team)
board.print_board(example_code)
over,winner=board.check_end(example_code)
if over==True and winner:
print("Game Over!", winner,'wins!')
if over==True and winner==None:
print("Game over! It's a draw!")