-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxoxoxo-game.py
executable file
Β·255 lines (228 loc) Β· 8.34 KB
/
xoxoxo-game.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
#!/usr/bin/env python
GAME_NAME = "ββββββ"
ROWS = list("abcdefghij")
COLUMNS = [i for i in range(1,11)]
CELL_EMPTY = "E"
CELL_X = "X"
CELL_O = "O"
PLAYER_SYMBOLS = {1: 'X', 2: 'O'}
current_player = 1
winner = 0
board_state = dict()
def print_welcome_message():
print()
print("π Welcome to {}!".format(GAME_NAME))
print()
def draw_board():
global board_state
BOARD_HEAD_BORDER = "βββββ¬ββββ¬ββββ¬ββββ¬ββββ¬ββββ¬ββββ¬ββββ¬ββββ¬ββββ¬ββββ"
BOARD_SEPARATOR = "βββββΌββββΌββββΌββββΌββββΌββββΌββββΌββββΌββββΌββββΌββββ€"
BOARD_FOOT_BORDER = "βββββ΄ββββ΄ββββ΄ββββ΄ββββ΄ββββ΄ββββ΄ββββ΄ββββ΄ββββ΄ββββ"
BOARD_ROW = "β {} β β β β β β β β β β β"
BOARD_HEADER = BOARD_HEAD_BORDER + "\n" + \
"β β 1 β 2 β 3 β 4 β 5 β 6 β 7 β 8 β 9 β 10β\n" + \
BOARD_SEPARATOR
print(BOARD_HEADER)
for r_index, r in enumerate(ROWS):
print("β {} β".format(r), end='')
for c_index, c in enumerate(COLUMNS):
if c_index < len(COLUMNS) - 1:
print(" {} β".format(format_cell_content(board_state[r][c])), end = '')
else:
print(" {} β".format(format_cell_content(board_state[str(r)][c])))
if r_index < len(ROWS)-1:
print(BOARD_SEPARATOR)
print(BOARD_FOOT_BORDER)
print()
def format_cell_content(cell_content):
if cell_content == CELL_EMPTY:
return ' '
elif cell_content == CELL_O:
return 'O'
elif cell_content == CELL_X:
return 'X'
def print_players():
print("π Player 1: {}".format(get_player_symbol(1)))
print("π Player 2: {}".format(get_player_symbol(2)))
print("π Current player: {}".format(current_player))
print("π Select move example: 'a1' means row a, column 1. 'd7' means row d column 7.")
print("π Type 'X' and press enter to quit the game.")
print("π (Enter the input without the apostrophes.)")
def get_move():
return input("β Move for Player {} ({}): ".format(current_player, get_player_symbol(current_player)))
def is_int(val):
try:
num = int(val)
except ValueError:
return False
return True
def user_wants_to_exit(move):
if move == 'X' or move == 'x':
return True
return False
def validate_move(move):
move_list = list(move)
if len(move_list) < 2:
print("β Wrong format: length of '{}' is lower than 2".format(move))
return False
elif len(move_list) > 3:
print("β Wrong format: length of '{}' is larger than 3".format(move))
return False
elif move_list[0] not in ROWS:
print("β Wrong format: row '{}' does not exist".format(move_list[0]))
return False
else:
if len(move_list) == 2 and not is_int(move_list[1]):
print("β Wrong format: column '{}' is invalid".format(move_list[1]))
return False
elif len(move_list) == 2 and int(move_list[1]) not in COLUMNS:
print("β Wrong format: column '{}' does not exist".format(move_list[1]))
return False
elif len(move_list) == 3 and not is_int(''.join(move_list[1:3])):
print("β Wrong format: column '{}' is invalid".format(''.join(move_list[1:3])))
return False
elif len(move_list) == 3 and int(''.join(move_list[1:3])) not in COLUMNS:
print("β Wrong format: column '{}' does not exist".format(''.join(move_list[1:3])))
return False
else:
move_r = move_list[0]
if len(move_list) == 2:
move_c = int(move_list[1])
else:
move_c = int(''.join(move_list[1:3]))
if board_state[move_r][move_c] != CELL_EMPTY:
print("β Cell '{}' is already occupied".format(move))
else:
return True
def initialize_board():
for r in ROWS:
board_state[r] = dict()
for c in COLUMNS:
board_state[r][c] = CELL_EMPTY
def update_board(move):
global current_player
move_list = list(move)
if len(move_list) == 2:
board_state[move_list[0]][int(move_list[1])] = get_player_symbol(current_player)
else:
board_state[move_list[0]][int(''.join(move_list[1:3]))] = get_player_symbol(current_player)
return
def is_game_ended():
# Check player 1
if is_winner(1):
set_winner(1)
return True
# Check player 2
elif is_winner(2):
set_winner(2)
return True
elif is_board_full():
# Check if board is full
set_winner(0)
return True
else:
return False
def is_board_full():
for r in ROWS:
for c in COLUMNS:
if board_state[r][c] == CELL_EMPTY:
return False
return True
def is_winner(player):
player_symbol = get_player_symbol(player)
for r in ROWS:
subsequent_player_symbols = 0
for c in COLUMNS[:-4]:
subsequent_player_symbols = 0
for i in range(4,-1,-1):
if board_state[r][c+i] == player_symbol:
subsequent_player_symbols += 1
if subsequent_player_symbols == 5:
return True
for c in COLUMNS:
subsequent_player_symbols = 0
for r_index, r in enumerate(ROWS[:-4]):
subsequent_player_symbols = 0
for i in range(4,-1,-1):
if board_state[ROWS[r_index+i]][c] == player_symbol:
subsequent_player_symbols += 1
if subsequent_player_symbols == 5:
return True
for i in range(5,0,-1):
lzipped = list(zip(ROWS[:-i], COLUMNS[i:]))
if check_winner_by_coord_list(lzipped, player_symbol):
return True
lzipped = list(zip(ROWS, COLUMNS))
if check_winner_by_coord_list(lzipped, player_symbol):
return True
for i in range(1,6):
lzipped = list(zip(ROWS[i:], COLUMNS[:-i]))
if check_winner_by_coord_list(lzipped, player_symbol):
return True
for i in range(5,11):
lzipped = list(zip(ROWS[:i], COLUMNS[i-1::-1]))
if check_winner_by_coord_list(lzipped, player_symbol):
return True
for i in range(1,6):
lzipped = list(zip(ROWS[i:10], COLUMNS[10-1:i-1:-1]))
if check_winner_by_coord_list(lzipped, player_symbol):
return True
def check_winner_by_coord_list(coord_list, player_symbol):
subsequent_player_symbols = 0
first_found = False
for r_c in coord_list:
if not first_found:
if board_state[r_c[0]][r_c[1]] == player_symbol:
first_found = True
subsequent_player_symbols += 1
else:
if board_state[r_c[0]][r_c[1]] == player_symbol:
subsequent_player_symbols += 1
else:
return False
if subsequent_player_symbols >= 5:
return True
return False
def set_winner(w):
global winner
winner = w
def update_current_player():
global current_player
if current_player == 1:
current_player = 2
else:
current_player = 1
def get_player_symbol(player):
return PLAYER_SYMBOLS[player]
def print_end_message():
print("π Game is over.")
print()
print("πββββββββββββββββπ")
print("πβ Game Over βπ")
if winner in [1,2]:
print("πβ Winner is: {} βπ".format(winner))
else:
print("πβ Draw (tie) βπ")
print("πββββββββββββββββπ")
print()
def start_game():
print_welcome_message()
initialize_board()
while(not is_game_ended()):
draw_board()
print_players()
is_valid_move = False
while not is_valid_move:
move = get_move()
if user_wants_to_exit(move):
print("π Bye!")
return
is_valid_move = validate_move(move)
print()
update_board(move)
update_current_player()
draw_board()
print_end_message()
return
if __name__ == "__main__":
start_game()