-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXOGame.py
256 lines (210 loc) · 6.09 KB
/
XOGame.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# Noughts and Crosses
import random
h1 = [0, 1, 2]
h2 = [3, 4, 5]
h3 = [6, 7, 8]
v1 = [0, 3, 6]
v2 = [1, 4, 7]
v3 = [2, 5, 8]
d1 = [0, 4, 8]
d2 = [2, 4, 6]
lines = [h1, h2, h3, v1, v2, v3, d1, d2]
empty_board = ['_', '_', '_', '_', '_', '_', '_', '_', '_']
def print_board(b):
for n, x in enumerate(b):
print(x, end='')
if (n+1) % 3 == 0:
print('')
def full(b):
if '_' not in b:
return True
else:
return False
def wins(p, b):
win = [p, p, p]
for l in lines:
bl = [b[x] for x in l] # list comprehension
if bl == win:
return True
return False
def random_move(b, p):
played = False
while not played:
r = random.randint(0, 8)
if b[r] == '_':
b[r] = p
played = True
def random_game(b):
while not full(b):
random_move(b, 'x')
print_board(b)
print('')
if wins('x', b):
print('x has won')
return
if full(b):
print("It's a draw!")
return
random_move(b, 'o')
print_board(b)
print('')
if wins('o', b):
print('o has won')
return
if full(b):
print("It's a draw!")
return
def human_move(b, p):
valid = False
while not valid:
pos = input(f'Player {p} enter a position from 0 to 8: ')
if not pos.isdigit():
print('Not a valid board position')
elif int(pos) > 8 or int(pos) < 0 :
print('Board position must be between 0 and 8')
elif b[int(pos)] == '_':
valid = True
b[int(pos)] = p
else:
print('Position taken')
def human_game(b):
while not full(b):
human_move(b, 'x')
print_board(b)
print('')
if wins('x', b):
print('x has won')
return
if full(b):
print("It's a draw!")
return
human_move(b, 'o')
print_board(b)
print('')
if wins('o', b):
print('o has won')
return
if full(b):
print("It's a draw!")
return
def try_to_take(b, ps):
for p in ps:
if b[p] == '_':
b[p] = 'x'
return True
return False
def tactic_play_centre(b):
return try_to_take(b, [4])
def win_or_block(b, piece):
for l in lines:
bl = [b[x] for x in l]
if bl.count('_') == 1 and bl.count(piece) == 2:
for x in l:
if b[x] == '_':
b[x] = 'x'
return True
return False
def tactic_win(b):
return win_or_block(b, 'x')
def tactic_block(b):
return win_or_block(b, 'o')
def tactic_empty_corner(b):
return try_to_take(b, [0, 2, 6, 8])
def tactic_empty_sides(b):
return try_to_take(b, [1, 3, 5, 7])
def tactic_opposite_corner(b):
l = []
if b[0] == 'o':
l.append(8)
if b[2] == 'o':
l.append(6)
if b[6] == 'o':
l.append(2)
if b[8] == 'o':
l.append(0)
return try_to_take(b, l)
def tactic_fork(b):
l = []
if h1.count('x') == 1 and h1.count('_') == 2:
if v1.count('x') == 1 and v1.count('_') == 2:
l.append(0)
if v3.count('x') == 1 and v3.count('_') == 2:
l.append(2)
if d1.count('x') == 1 and d1.count('_') == 2:
l.append(0)
if d2.count('x') == 1 and d2.count('_') == 2:
l.append(2)
if h3.count('x') == 1 and h3.count('_') == 2:
if v1.count('x') == 1 and v1.count('_') == 2:
l.append(6)
if v3.count('x') == 1 and v3.count('_') == 2:
l.append(8)
if d1.count('x') == 1 and d1.count('_') == 2:
l.append(8)
if d2.count('x') == 1 and d2.count('_') == 2:
l.append(6)
if d1.count('x') == 1 and d1.count('_') == 2:
if v1.count('x') == 1 and v1.count('_') == 2:
l.append(0)
if v3.count('x') == 1 and v3.count('_') == 2:
l.append(8)
if d2.count('x') == 1 and d2.count('_') == 2:
if v1.count('x') == 1 and v1.count('_') == 2:
l.append(2)
if v3.count('x') == 1 and v3.count('_') == 2:
l.append(6)
return try_to_take(b, l)
def computer_move(b):
print('Computer has played: ')
if tactic_win(b):
print('Used tactic_win')
return
if tactic_block(b):
print('Used tactic_block)')
return
if tactic_fork(b):
print('Used tactic_fork')
return
if tactic_play_centre(b):
print('Used tactic_centre')
return
if tactic_opposite_corner(b):
print('Used tactic_opposite_corner')
return
if tactic_empty_corner(b):
print('Used tactic_empty_corner')
return
if tactic_empty_sides(b):
print('Used tactic_empty_sides')
return
print('No tactic applied: error in tactic implementations')
def human_vs_computer_game(b):
s = 'ch' # so the starter is random every game
player = random.choice(s) # random.choice returns a single random element from a sequence
while not full(b):
if player == 'h':
human_move(b, 'o')
print_board(b)
print('')
if wins('o', b):
print('Human player has won')
return
if full(b):
print("It's a draw!")
return
player='c'
if player == 'c':
computer_move(b)
print_board(b)
print('')
if wins('x', b):
print('Computer has won')
return
if full(b):
print("It's a draw!")
return
player = 'h'
board = empty_board
# random_game(board)
# human_game(board)
human_vs_computer_game(board)