Repository navigation
Expand file tree
/
Copy pathConnect_Four_2.py
More file actions
179 lines (135 loc) · 5.33 KB
/
Copy pathConnect_Four_2.py
File metadata and controls
179 lines (135 loc) · 5.33 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
#==================================================================
# Global Variables
#==================================================================
# this module lets the "coins" be colorful
from colorama import Fore
# this is the list containing the full board
board = [
["_", "_", "_", "_", "_", "_", "_"],
["_", "_", "_", "_", "_", "_", "_"],
["_", "_", "_", "_", "_", "_", "_"],
["_", "_", "_", "_", "_", "_", "_"],
["_", "_", "_", "_", "_", "_", "_"],
["_", "_", "_", "_", "_", "_", "_"]
]
player = ""
#==================================================================
# Functions
#==================================================================
# If the player is R then the color will be red. If the character isn't, then the color will be yellow
def set_color(player):
return Fore.RED if player == 'R' else Fore.YELLOW
# this will display the board in the terminal
def display_board(board):
for row in board:
row_str = "', '".join([f"{set_color(i)}{i}{Fore.RESET}" if i != "_" else i for i in row ])
print(f"[' {row_str} ']")
def get_column(player):
while True:
try:
column = int(input(f"Player '{set_color(player)}{player}{Fore.RESET}' - Choose a column to drop your coin in [0-6]: "))
if not 0 <= column <= 6:
print("ERROR: Invalid input")
else:
return column
except ValueError:
print("ERROR: Invalid input")
# check if column is full
def check_column_full(board, column):
if board[0][column] != "_":
print(f"ERROR: Column '{column}' is full")
return True
else:
return False
def check_board_full(board):
isFull = True
for row in board:
if "_" in row:
isFull = False
break
return isFull
def drop_coin(board, player):
# check if the board is full
if check_board_full(board):
# says it's a draw
print("It's a draw!")
exit()
# get valid column that is not full
column = get_column(player)
while check_column_full(board, column):
column = get_column(player)
# drop coin
for row in board[-1::-1]:
if row[column] == "_":
row[column] = player
break
# switch to next player
def next_player(player):
if player == "" or player == 'Y':
player = 'R'
else:
player = 'Y'
return player
def check_virtual_board_4x4(board_4x4):
# check rows
if board_4x4[0][0] != "_" and board_4x4[0][0] == board_4x4[0][1] == board_4x4[0][2] == board_4x4[0][3]:
return board_4x4[0][0]
elif board_4x4[1][0] != "_" and board_4x4[1][0] == board_4x4[1][1] == board_4x4[1][2] == board_4x4[1][3]:
return board_4x4[1][0]
elif board_4x4[2][0] != "_" and board_4x4[2][0] == board_4x4[2][1] == board_4x4[2][2] == board_4x4[2][3]:
return board_4x4[2][0]
elif board_4x4[3][0] != "_" and board_4x4[3][0] == board_4x4[3][1] == board_4x4[3][2] == board_4x4[3][3]:
return board_4x4[3][0]
# check columns
elif board_4x4[0][0] != "_" and board_4x4[0][0] == board_4x4[1][0] == board_4x4[2][0] == board_4x4[3][0]:
return board_4x4[0][0]
elif board_4x4[0][1] != "_" and board_4x4[0][1] == board_4x4[1][1] == board_4x4[2][1] == board_4x4[3][1]:
return board_4x4[0][1]
elif board_4x4[0][2] != "_" and board_4x4[0][2] == board_4x4[1][2] == board_4x4[2][2] == board_4x4[3][2]:
return board_4x4[0][2]
elif board_4x4[0][3] != "_" and board_4x4[0][3] == board_4x4[1][3] == board_4x4[2][3] == board_4x4[3][3]:
return board_4x4[0][3]
# check diagonals
elif board_4x4[0][0] != "_" and board_4x4[0][0] == board_4x4[1][1] == board_4x4[2][2] == board_4x4[3][3]:
return board_4x4[0][0]
elif board_4x4[3][0] != "_" and board_4x4[3][0] == board_4x4[2][1] == board_4x4[1][2] == board_4x4[0][3]:
return board_4x4[3][0]
else:
return None
def check_winner(board):
slice_row_start = 0
# process block of 4 rows
board_4x4 = []
while slice_row_start + 4 <= len(board):
# process block of 4 columns
slice_column_start = 0
while slice_column_start + 4 <= len(board[0]):
# process rows and create virtual board
for row in board[slice_row_start:slice_row_start + 4]:
board_4x4.append(row[slice_column_start:slice_column_start + 4])
# check virtual board for winner
winner = check_virtual_board_4x4(board_4x4)
if winner:
return winner
board_4x4 = []
# increment slice_column_start
slice_column_start += 1
# increment slice_row_start
slice_row_start += 1
return None
#==================================================================
# Main Program
#==================================================================
# display the board
display_board(board)
winner = check_winner(board)
while not winner:
# drop the red coin
player = next_player(player)
drop_coin(board, player)
# display the board
display_board(board)
winner = check_winner(board)
# display winner
if winner:
print(f"The winner is {set_color(player)}{player}{Fore.RESET}!")