-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnect4.py
159 lines (130 loc) · 3.75 KB
/
Connect4.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
import os,sys
Players = ["Yellow","Red"]
L = []
InProgress = True
EndGame = False
#True for Red, False for Yellow
Player = True
R = "🔴"
Y = "🟡"
E = "⚪"
def clear():
if os.name == "nt":
os.system("cls")
else:
InColab = 'google.colab' in sys.modules
if InColab is True:
from google.colab import output
output.clear()
else:
os.system("clear")
print("Rules:","1. Only 1 move is allowed per turn, and only 2 players can play. Red Player moves first, followed by the Yellow Player", "2. First player to connect 4 tokens horizontally, vertically or diagonally wins.", "3. When it is your turn, specify the column number to drop the token in from the left.", "4. A white dot indicates an empty space.","5. The current board will appear after each turn.", sep = "\n")
def init():
#Setup board
while True:
Width = input("Enter width: ")
try:
Width = int(Width)
if Width < 4 or Width > 20:
raise ValueError
except ValueError:
clear()
print("Must be between 4 and 20")
continue
break
while True:
Height = int(input("Enter height: "))
try:
Height = int(Height)
if Height < 4 or Height > 20:
raise ValueError
except ValueError:
clear()
print("Must be between 4 and 20")
continue
break
for i in range(Height):
L2 = []
for j in range(Width):
L2.append(E)
L.append(L2)
def board():
#Print board
for i in L:
for j in i:
print(j,end="")
print()
def check():
#Horizontal Check
for i in range(len(L)):
for j in range(0,len(L[i])-3):
if L[i][j] == L[i][j+1] == L[i][j+2] == L[i][j+3] and L[i][j] != E:
win()
return True
#Vertical Check
for i in range(len(L)-3):
for j in range(0,len(L[i])):
if L[i][j] == L[i+1][j] == L[i+2][j] == L[i+3][j] and L[i][j] != E:
win()
return True
#Diagonal Check
for i in range(len(L)-3):
for j in range(0,len(L[i])-3):
if L[i][j] == L[i+1][j+1] == L[i+2][j+2] == L[i+3][j+3] and L[i][j] != E or L[i][::-1][j] == L[i+1][::-1][j+1] == L[i+2][::-1][j+2] == L[i+3][::-1][j+3] and L[i][::-1][j] != E:
win()
return True
#Draw Check
if sum(i.count(E) for i in L) == 0:
win("Draw")
return True
def win(outcome="Not Draw"):
#Display Outcome
clear()
if outcome == "Draw":
print("-"*25,outcome,"-"*25)
else:
print("-"*25,Players[int(not Player)],"Player has won!","-"*25)
board()
def move():
#Get Player move
global Player
clear()
print("-"*25,Players[int(Player)],"Player's Turn","-"*25)
C = R if Player is True else Y
board()
while True:
N = input("Enter column: ")
try:
N = int(N)
if N < 1 or N > len(L[0]) or L[0][N-1] != E:
raise ValueError
except ValueError:
clear()
print("-"*25,Players[int(Player)],"Player's Turn","-"*25)
print("Illegal move, please try again.")
board()
continue
break
for i in range(len(L)-1,-1,-1):
if L[i][N-1] == E:
L[i][N-1] = C
Player = not Player
break
while EndGame is False:
#Start game
clear()
init()
while not check():
move()
while True:
N = input("New game? (Y/N): ").upper()
if N == "Y":
L = []
Player = True
break
if N == "N":
EndGame = True
break
clear()
win()
print("Invalid choice, try again.")