-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconnect_4.py
More file actions
455 lines (347 loc) · 19.1 KB
/
Copy pathconnect_4.py
File metadata and controls
455 lines (347 loc) · 19.1 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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
"""
Project: Game Connect-4 in Terminal
File: connect_four.py
Author: Ivaylo Stoyanov - Devihem
Description: This is a basic project that recreates the game Connect 4. It is played in the terminal.
The script give the user options to choose a gameplay mode: default mode with two players and a standard board,
or a custom mode with two to six players and a custom board. In the custom mode, the user can choose the number of rows,
columns, and players. Each player can also choose a name and select a color.
Players take turns placing their tokens on the board by selecting a column.
If a player has four or more tokens in a row, column, or diagonal connected together the player wins.
If no player wins and the board is full, the game is considered a draw.
"""
import re
import random
import copy
# First user input possible for selecting game mode. The function return 'P' or 'Any input'
def choosing_game_play_mode():
game_modes_list = ["AI", "N", "P"]
while True:
user_choice = input('\n┌─-----------------Choose-a-gameplay-mode----------------─┐'
'\n│ Versus AI - One Player, Standard Board, AI │'
'\n│ Normal - Two Players, Standard Board │'
'\n│ Party - Two - Six Players, Custom Board │'
'\n└─-------------------------------------------------------─┘\n'
'\n Type "AI" to play against Bot, type "N" for normal match or'
' "P" for Party match and Press ENTER to continue :'
'\n => : ')
if user_choice.upper() in game_modes_list:
break
print("\n\n\n\n\n\n\n\n\n\n")
return user_choice.upper()
# Receiving user input for the game mode. Return how many rows, columns and players will teh game have.
def game_mod(user_mode_input: str):
# default values (Grid 6 x 7 , 2 Players)
custom_rows, custom_cols, custom_players = 6, 7, 2
# if the selected mode is Party the user choose one by one the parameters.
if user_mode_input.upper() == 'P':
# Rows input with try/except. If input is incorrect the error is raised and the input is repeated.
while True:
try:
custom_rows = int(input('\nHow many ROWS [ 1 - 99 ] ?'
'\nRecommended ROWS [ 4 to 10 ] => : '))
if 0 < custom_rows < 100:
break
else:
raise ValueError
except ValueError:
print('\nIncorrect input !\nExpected input - integer number in the given range [ 1 - 99 ]')
continue
# Columns input with try/except. If input is incorrect the error is raised and the input is repeated.
while True:
try:
custom_cols = int(input('\nHow many COLUMNS [ 1 - 99 ] ?'
'\nRecommended COLUMNS [ 4 to 16 ] => : '))
if 0 < custom_cols < 100:
break
else:
raise ValueError
except ValueError:
print('\nIncorrect input !\nExpected input - integer number in the given range [ 1 - 99 ]')
continue
# Player count input with try/except. If input is incorrect the error is raised and the input is repeated.
while True:
try:
custom_players = int(input('\nHow many PLAYERS [ 1 - 6 ] ?'
'\nRecommended PLAYERS [ 2 to 4 ] => : '))
if 0 < custom_players < 7:
break
else:
raise ValueError
except ValueError:
print('\nIncorrect input !\nExpected input - integer number in the given range [ 1 - 6 ]')
continue
# Return custom parameters
return custom_rows, custom_cols, custom_players
else:
# Return default parameters
return custom_rows, custom_cols, custom_players
# If game mode is Party function return players name and color selected by users , otherwise return default values
def players_name_and_color(new_players_count: int, user_mode_input: str):
players_dictionary = {}
# All colors available
colors_list = ['\033[1;31m██\033[0m', '\033[1;32m██\033[0m', '\033[1;33m██\033[0m',
'\033[1;34m██\033[0m', '\033[1;35m██\033[0m', '\033[1;37m██\033[0m']
# If Custom mode is selected
if user_mode_input.upper() == 'P':
# While loop until the selected players count is the same as the dictionary keys ( Players )
while len(players_dictionary.keys()) < new_players_count:
new_player_name = input('\nWelcome, enter your name, between [4-20] characters from [ a-z, A-Z, 0-9 , _ ]'
'\nEnter Your name => : ')
# Username validation in 4 steps - Valid format 4-20 - characters [a-z, A-Z, 0-9, _] and to be unique:
# Check_1 - if name is empty
if not new_player_name:
print('\nPlayer name cannot be empty !')
continue
# Check_2 - if name is not within correct length
if len(new_player_name) < 4 or len(new_player_name) > 20:
print('\nPlayer name length not in range [ 4 - 20 ] !')
continue
# Check_3 - if name contain not allowed characters, (Regex check for all symbols except [^_a-zA-Z0-9] )
if re.findall(r'\W+', new_player_name):
print('\nPlayer name contains not allowed characters')
continue
# Check_4 if name already exist return user to the player name-input, until correct name is written.
if new_player_name in players_dictionary.keys():
# For better visualisation print the error and the usernames that already exist
print(f'\nThis Player name already exist !'
f'\nPlayers names that already used: {", ".join(players_dictionary.keys())}')
continue
# Player choose his color , with int index , repeat until correct index is typed
while True:
# Print Colors and their Index underneath
print('\n', *colors_list)
print(''.join((f' {color_index}' for color_index in range(1, len(colors_list) + 1))))
# Color index input - try/except. If input is incorrect the error is raised and the input is repeated.
try:
player_color = int(input('Please select your color !\nColor number => : '))
if 0 < player_color <= len(colors_list):
players_dictionary[new_player_name] = colors_list.pop(player_color - 1)
print(f'\n{players_dictionary[new_player_name][0:7]} {new_player_name}\033[0m '
f'You are set and ready !')
break
else:
raise ValueError
except ValueError:
print(f'Incorrect input ! '
f'Expected input - integer number in the given range [ 1 - {len(colors_list)} ]')
continue
elif user_mode_input.upper() == 'N':
# Return Default Player 1 and Player 2 information for quick games.
players_dictionary['Player_1'] = colors_list.pop(0) # Red Current Index
players_dictionary['Player_2'] = colors_list.pop(2) # BLue Current Index
elif user_mode_input.upper() == 'AI':
# Return Default Player 1 and Player 2 - AI information for quick games.
players_dictionary['Player_1'] = colors_list.pop(0) # Red Current Index
players_dictionary['Player_2_AI'] = colors_list.pop(2) # BLue Current Index
# When all the data is fill out correctly return dictionary in format (Player-name : [Color_code + symbol])
return players_dictionary
# Receiving matrix row and col and Creating empty board/matrix , also create columns indexes list and return them.
def board_creating(matrix_rows: int, matrix_cols: int):
board_matrix = [[' ' for _ in range(matrix_cols)] for _ in range(matrix_rows)]
columns_index_print = [' ' + str(x) if x < 10 else str(x) for x in range(1, number_of_cols + 1)]
return board_matrix, columns_index_print
# Making check for possible places to put the token. Returning free columns and flag.
def check_free_columns(matrix_board: list, matrix_cols: int):
free_cols = [(index_col + 1) for index_col in range(matrix_cols) if matrix_board[0][index_col] == ' ']
return free_cols
# Take the user input and check it if is valid, return correct index for token place
def player_token_placement(p_symbol: str, p_name: str, free_columns_index: list, matrix):
# Repeating the player input if it's incorrect or the index is wrong
if p_name == "Player_2_AI":
return monte_carlo_ai_placement(matrix)
while True:
try:
column_index_place = int(input(f'\nWhere you want to place your token'
f' {p_symbol[0:9]} {p_name}\033[0m ?\n => : ')) - 1
# If index is incorrect raise error
if column_index_place + 1 not in free_columns_index:
raise ValueError
# stop the loop and return correct index for placing the player symbol
return column_index_place
# if error occurred print error message and show the free columns for placement
except ValueError:
print(f'\nIncorrect input !'
f'\nExpected input - Integer number. One, from the free columns indexes {free_columns_index}')
# Placing the token, checking from the lowest to the upper floor/level for empty cell / box
def place_token(board_matrix: list, matrix_rows: int, col_to_place: int, p_symbol: str):
# Starting form the bottom of the board/matrix
for current_row in range(matrix_rows - 1, -1, -1):
# There is no else case, there is at least one free space guaranteed from check_free_columns().
if board_matrix[current_row][col_to_place] == ' ':
board_matrix[current_row][col_to_place] = p_symbol
# Creating variable for where was placed the last token.
last_row_col_placement = (current_row, col_to_place)
# Return updated matrix/board and the coordinates of the last token placed
return board_matrix, last_row_col_placement
# Function to check if there is a winner after every placement on board
def winner_check(matrix_board: list, p_symbol: str, last_r_c_token_placed: tuple, matrix_rows: int, matrix_cols: int):
row, col = last_r_c_token_placed
# Win pattern, going in both direction in the matrix with positive or negative step( -1 , +1 ).
directions = (
# (R, C) # First direction | Second direction
(1, 0), # Bottom - Top
(0, 1), # Right - Left
(1, 1), # Prime Diagonal
(1, -1) # Secondary Diagonal
)
# Checking every possible direction for least 4 connected/same blocks
for dir_r, dir_c in directions:
# The last token itself is already 1 of 4 blocks , counter = 1
counter = 1
# Using positive or negative step to check in both direction of a row , column or diagonal
for direction_step_pos_or_neg in [1, -1]:
# Checking maximum tree blocks in direction is the limit for longest chain , or maximum 7 blocks connected.
for dir_step in range(1, 4):
# Token index row/col + ( direction pattern * dir_step (1,2,3) * step (+1 , -1 )
new_r = row + dir_r * dir_step * direction_step_pos_or_neg
new_c = col + dir_c * dir_step * direction_step_pos_or_neg
# check if the new created indexes are in range of the matrix/board
if 0 <= new_r < matrix_rows and 0 <= new_c < matrix_cols:
# if there is the same symbol there as the current player counter add 1
if matrix_board[new_r][new_c] == p_symbol:
counter += 1
# else stop checking in this direction
else:
break
# if counter is at least 4 = four or more connected same elements , return True ( current player is Winner )
if counter >= 4:
return True
# return False there is still no winner
return False
# Taking input from user and return boolean statement for new_game_flag
def another_game():
# The loop is repeated until correct input yes or no.
while True:
do_you_want_new_game = input('\n\nDo you want to play again [YES/NO] ? \n => : ')
if do_you_want_new_game.upper() == 'YES':
return True
elif do_you_want_new_game.upper() == 'NO':
return False
else:
print(f'\nIncorrect input !')
# Simple - MCTS - Model
def monte_carlo_ai_placement(matrix):
player_token = '\033[1;31m██\033[0m'
ai_token = '\033[1;34m██\033[0m'
all_first_moves_dict = {}
first_move_loc = int()
# Number of random played games - simulation
for game_number in range(100):
mc_matrix = copy.deepcopy(matrix)
for counter in range(0, 100):
free_cols = check_free_columns(mc_matrix, number_of_cols)
random_place = random.choice(free_cols) - 1
if counter == 0:
if random_place not in all_first_moves_dict:
all_first_moves_dict[random_place] = 0
first_move_loc = random_place
if counter % 2 == 0:
player_symbol = ai_token
else:
player_symbol = player_token
# AI Place random token
mc_matrix, mc_r_c_last_token = place_token(mc_matrix, number_of_rows, random_place, player_symbol)
# Flags for Winner or Draw Game
mc_winner_flag = winner_check(mc_matrix, player_symbol, mc_r_c_last_token, number_of_rows, number_of_cols)
mc_end_game_flag = False if len(check_free_columns(mc_matrix, number_of_cols)) > 0 else True
# if any of the flag is raised stop the loop
if mc_winner_flag or mc_end_game_flag:
# Rewarding system
# State Winner
if mc_winner_flag:
if player_symbol == ai_token:
all_first_moves_dict[first_move_loc] += 6
elif player_symbol == player_token:
all_first_moves_dict[first_move_loc] -= 4
# State Draw
elif mc_end_game_flag:
all_first_moves_dict[first_move_loc] -= 1
break
sorted_all_moves = (sorted(all_first_moves_dict.items(), key=lambda k: (-k[1], k[0])))
return sorted_all_moves[0][0]
# Print - WELCOME
def starting_print():
print(' _______ _ _ '
'\n(_______) _ | | (_)'
'\n _ ___ ____ ____ _____ ____ _| |_ | |_____ '
'\n| | / _ \\| _ \\| _ \\| ___ |/ ___|_ _) |_____ |'
'\n| |____| |_| | | | | | | | ____( (___ | |_ | |'
'\n \\______)___/|_| |_|_| |_|_____)\\____) \\__) |_|'
'\n')
# Print - GAME BOARD
def board_print(matrix_board: list, columns_print: list, matrix_cols: int):
# Spacing from other prints
print('\n\n\n\n\n\n\n')
# Top frame
print('┌─' + '───┬─' * (matrix_cols - 1) + '───┐')
# Columns with numbers
print('│', ' │ '.join(columns_print), '│')
# Every mid row and matrix row.
[print('├─' + '───┼─' * (matrix_cols - 1) + '───┤\n' + '│ ' + ' │ '.join(x), end=' │\n') for x in matrix_board]
# Bottom frame
print('└─' + '───┴─' * (matrix_cols - 1) + '───┘\n')
def winner_print(p_symbol: str, p_name: str):
print(
f'\n┌─---------CONGRATULATION-----------┐'
f'\n {p_symbol[0:9]} {p_name}\033[0m '
f'\n└─-------------YOU-WIN--------------┘'
)
def draw_print():
print(
f'\n┌─-----------GOOD-GAME-----------┐'
f'\n THIS ROUND IS '
f'\n└─-------------DRAW--------------┘'
)
# Welcome, Print with script name.
starting_print()
# User choose - Gameplay mode , Custom or Default
gameplay_mode = choosing_game_play_mode()
# Common variables for creating the game board and saving players' info.
number_of_rows, number_of_cols, players = game_mod(gameplay_mode)
# All players are added in dictionary with their personal name and color
players_dict = players_name_and_color(players, gameplay_mode)
# Board creating
board, columns_print_for_representation = board_creating(number_of_rows, number_of_cols)
# Flags for end game. First scenario - Player Win , Second scenario - Players are Draw or New game can be selected.
winner_flag = False
end_game_flag = False
new_game_flag = False
# While one of two condition is met [Winner] or [No more moves].
while not winner_flag and not end_game_flag:
# Using for-loop to rotate players turns.
for player_name, players_symbol in players_dict.items():
# Printing the gaming board
board_print(board, columns_print_for_representation, number_of_cols)
# Checking witch columns have at least one empty space ( checking only the top row )
free_columns = check_free_columns(board, number_of_cols)
# Player choose where to place the token ( in witch column )
column_to_place = player_token_placement(players_symbol, player_name, free_columns, board)
# Placing the color token in the board
board, r_c_last_token = place_token(board, number_of_rows, column_to_place, players_symbol)
# Check if there is a winner ( based on last placed token )
winner_flag = winner_check(board, players_symbol, r_c_last_token, number_of_rows, number_of_cols)
# Check if there is more empty spaces
end_game_flag = False if len(check_free_columns(board, number_of_cols)) > 0 else True
# if any of the flag is raised stop the loop
if winner_flag or end_game_flag:
# Print the final state of the board
board_print(board, columns_print_for_representation, number_of_cols)
# Print Winner
if winner_flag:
winner_print(players_symbol, player_name)
# Print Draw
elif end_game_flag:
draw_print()
# Option for new round available only in the end of the game
new_game_flag = another_game()
break
# If new round is selected the game continue after the players name and color selection.
if new_game_flag:
# Reset all flags and create new empty board.
winner_flag = False
end_game_flag = False
new_game_flag = False
board, columns_print_for_representation = board_creating(number_of_rows, number_of_cols)
continue
print('\n\nThank you for playing Connect-4 made by me! Goodbye!')