-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
214 lines (179 loc) · 5.86 KB
/
game.py
File metadata and controls
214 lines (179 loc) · 5.86 KB
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
import random
import time
SIZE = 3
# This class manages the game board as a 2D character array
class GameBoard:
def __init__(self):
self.new_board()
# Initialize SIZE x SIZE matrix with ''
def new_board(self):
self.board = [['' for col in range(SIZE)] for row in range(SIZE)]
def update_board(self, row, col, val):
self.board[row][col] = val
def __str__(self):
out_str = " column\n"
out_str += "row | 1 2 3\n"
out_str += "----|-----------------\n"
for row in range(SIZE):
out_str += f" {row+1} | "
for col in range(SIZE):
if self.board[row][col] == 'o':
out_str += " ⭕ "
elif self.board[row][col] == 'x':
out_str += " ❌ "
else:
out_str += " "
if col != 2:
out_str += "|"
if row != 2:
out_str += "\n | ----+----+----\n"
return out_str + '\n'
# Must pass the 2D board as the argument
def check_win_lose(board):
# Check rows
for row in board:
if all(cell == 'x' for cell in row):
return 'x'
elif all(cell == 'o' for cell in row):
return 'o'
# Check columns
for col in zip(*board):
if all(cell == 'x' for cell in col):
return 'x'
elif all(cell == 'o' for cell in col):
return 'o'
# Check diagonals
diag1 = [board[i][i] for i in range(SIZE)]
diag2 = [board[i][2-i] for i in range(SIZE)]
if all(cell == 'x' for cell in diag1) or all(cell == 'col' for cell in diag2):
return 'x'
elif all(cell == 'o' for cell in diag1) or all(cell == 'o' for cell in diag2):
return 'o'
# Check for empty slots
for row in board:
for cell in row:
if cell == '':
return
# Assume board is full
return 'Tie'
# Gets user input and validates it for correctness
def get_input(board):
is_valid = False
user_input = ""
# Run loop until input is valid or until user quits
while is_valid == False:
user_input = input(
"Your move. Input coordinates 'row, col' e.g. '1 3': ")
# Quit condition
if any(x in user_input for x in ('q', 'quit')):
quit(0)
values = user_input.split()
try:
row = int(values[0])
col = int(values[1])
if 0 < row <= SIZE and 0 < col <= SIZE:
if board[row-1][col-1] != '':
print("This location is already marked...")
else:
is_valid = True
else:
print("Input not within the range (1-3)!")
except:
print("Invalid input...")
return row-1, col-1
# AI player algorithm
def minimax(board, depth, isMaximizing):
result = check_win_lose(board)
if result == ('x' if isMaximizing else 'o'):
return 1
elif result == ('o' if isMaximizing else 'x'):
return -1
elif result == "Tie":
return 0
bestMove = (-1, -1, '')
bestScore = -float('inf')
bestMoves = []
for i in range(SIZE):
for j in range(SIZE):
if board[i][j] == '':
board[i][j] = ('x' if isMaximizing else 'o')
score = minimax(board, depth+1, isMaximizing)
board[i][j] = ''
if isinstance(score, int) == False:
return score
elif score == bestScore:
bestMoves.append((i, j))
elif score > bestScore:
bestScore = score
bestMoves = [(i, j)]
if len(bestMoves) > 0:
# Add randomness by randomly choosing among the best moves
randomMove = random.choice(bestMoves)
bestMove = (randomMove[0], randomMove[1])
return bestMove
# Welcome message
print('''
<>=====================================<>
Welcome to Brandon's Tic-Tac-Toe!
Input coordinates to make a move
like '1 2' where 1 is the first
row and 2 is the second column
starting from top-left corner.
Coordinates range from 1 to 3
Input q to quit
<>=====================================<>
''')
# Game variables and setup
is_quit = False
user_turn = True
user_icon = 'x'
AI_icon = 'o'
game_board = GameBoard()
print(game_board)
# Game Loop
while is_quit == False:
# Get user input for user's turn
if user_turn:
user_in = get_input(game_board.board)
game_board.update_board(*user_in, user_icon)
user_turn = False
# Get AI input for AI's turn
else:
AI_is_x = (True if AI_icon == 'x' else False)
AI_in = minimax(game_board.board, 0, AI_is_x)
# Validate AI output
if isinstance(AI_in, int):
print('AI broke! Restart application.')
quit(1)
time.sleep(0.5)
game_board.update_board(*AI_in, AI_icon)
user_turn = True
print("AI Turn Over:")
print(game_board)
# Check win and lose conditions
result = check_win_lose(game_board.board)
if result != None:
if (result == 'Tie'):
print("Tie! Game over!")
elif result == 'x':
print("X wins!")
elif result == 'o':
print("o wins!")
# Game is over, create a new board for new game
print("Play again(Y/n)?")
again = input("> ")
if again == 'n' or again == 'N':
is_quit = True
else:
print("New game!")
game_board.new_board()
print(game_board)
if user_turn:
user_icon = 'x'
AI_icon = 'o'
print("You are ❌")
else:
user_icon = 'o'
AI_icon = 'x'
print("You are ⭕")
print("Program exiting...")