-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu-only.py
213 lines (188 loc) · 5.45 KB
/
cpu-only.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
import random
import time
game_state = True
draw = False
player_id = 0
player_item_type = {1: "", 2: ""}
board = [
["_", "_", "_"],
["_", "_", "_"],
["_", "_", "_"],
]
winning_states = [
[(0, 0), (0, 1), (0, 2)],
[(1, 0), (1, 1), (1, 2)],
[(2, 0), (2, 1), (2, 2)],
[(0, 0), (1, 0), (2, 0)],
[(0, 1), (1, 1), (2, 1)],
[(0, 2), (1, 2), (2, 2)],
[(0, 0), (1, 1), (2, 2)],
[(0, 2), (1, 1), (2, 0)],
]
move_sequence = []
move_count = {1: 0, 2: 0}
def initialise():
global game_state
global draw
global player_id
global player_item_type
global board
global winning_states
global move_sequence
global move_count
game_state = True
draw = False
player_id = 0
player_item_type = {1: "", 2: ""}
board = [
["_", "_", "_"],
["_", "_", "_"],
["_", "_", "_"],
]
winning_states = [
[(0, 0), (0, 1), (0, 2)],
[(1, 0), (1, 1), (1, 2)],
[(2, 0), (2, 1), (2, 2)],
[(0, 0), (1, 0), (2, 0)],
[(0, 1), (1, 1), (2, 1)],
[(0, 2), (1, 2), (2, 2)],
[(0, 0), (1, 1), (2, 2)],
[(0, 2), (1, 1), (2, 0)],
]
move_sequence = []
move_count = {1: 0, 2: 0}
def otherPlayer():
if player_id == 1:
return 2
elif player_id == 2:
return 1
else:
return 0
def otherPlayerItem():
if player_item_type[player_id] == "X":
return "O"
elif player_item_type[player_id] == "O":
return "X"
else:
return 0
def printBoard():
for i in board:
for j in i:
print(j, end=" ")
print()
print()
# Returns an integer from -6 (worst case) to 5 (best case)
def getWinningStates(player_id):
count_item = 0
player_winning_states = []
for x in winning_states:
temp = 0
for y in x:
row, col = y
if board[row][col] == player_item_type[player_id]:
temp += 2
elif board[row][col] == "_":
temp += 1
elif board[row][col] == player_item_type[otherPlayer()]:
temp -= 2
if temp > count_item:
count_item = temp
player_winning_states.clear()
player_winning_states.append(x)
elif temp == count_item:
count_item = temp
player_winning_states.append(x)
return count_item, player_winning_states
def aiPlay():
print("AI", player_id, "(" + player_item_type[player_id] + ") is thinking...")
player_count_item, player_winning_states = getWinningStates(otherPlayer())
ai_count_item, ai_winning_states = getWinningStates(player_id)
freeSlots = []
if ai_count_item >= 5:
# print("AI leading so trying to win")
for x in ai_winning_states:
for y in x:
a, b = y
if board[a][b] == "_":
freeSlots.append(y)
elif player_count_item >= 5:
# print("Player leading so trying to stop them from winning")
for x in player_winning_states:
for y in x:
a, b = y
if board[a][b] == "_":
freeSlots.append((a, b))
else:
for x in ai_winning_states:
for y in x:
a, b = y
if board[a][b] == "_":
freeSlots.append(y)
if len(freeSlots) == 0:
for x in range(len(board)):
for y in range(len(board[x])):
if board[x][y] == "_":
freeSlots.append((x, y))
row, col = random.choice(freeSlots)
board[row][col] = player_item_type[player_id]
move_sequence.append((row, col))
move_count[player_id] += 1
printBoard()
def play_game():
# Main game logic
global game_state
global player_id
while game_state:
if player_id == 1:
aiPlay()
elif player_id == 2:
aiPlay()
else:
print("Invalid player id")
game_state = False
quit()
# checking each time for game win logic
for x in winning_states:
count_x = 0
count_o = 0
for y in x:
a, b = y
if board[a][b] == "X":
count_x += 1
if board[a][b] == "O":
count_o += 1
if count_x == 3 or count_o == 3:
print("AI", player_id, "(" + player_item_type[player_id] + ")", "HAS WON THE GAME!")
game_state = False
break
# DRAW condition check game logic
for x in board:
if "_" in x:
draw = False
break
else:
draw = True
if draw:
print("GAME IS A DRAW!")
game_state = False
break
# Switching play chance to other player if the game is not over
if otherPlayer() != 0:
player_id = otherPlayer()
else:
print("Invalid player id")
game_state = False
quit()
print("Moves list:", move_sequence)
print("Moves count", move_count)
print()
while True:
initialise()
printBoard()
player_id = random.randint(1, 2)
player_item_type[player_id] = random.choice(["X", "O"])
player_item_type[otherPlayer()] = otherPlayerItem()
print("AI", player_id, " is starting first and has chosen " + player_item_type[player_id])
play_game()
break
print("TicTacToe by Hara <3")