-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connect Four.py
218 lines (185 loc) · 7.26 KB
/
Connect Four.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
# https://py2.codeskulptor.org/#user45_GlIniqqF8H_232.py
# 26/09/18
import simplegui
anti_ghost = False
chip = []
chip_X = 115
chip_Y = 50
chip_Y_destination = 700
image1 = simplegui.load_image('https://i.imgur.com/XaXQgGY.png')# Frame
turn = 0
gamestate = 1
winner = ""
def timer1_handler():
global anti_ghost, chip_X, chip_Y, turn
# Move chip to destination
if chip_Y != chip_Y_destination:
chip_Y += 10
else:
timer1.stop()
# Add chip into list
add_chip()
# Spawn next chip
chip_X = 115
chip_Y = 50
# Flip turn Red / Navy
if turn == 0:
turn = 1
elif turn == 1:
turn = 0
# Turn off anti_ghost
anti_ghost = False
# Sort chip
chip.sort()
# Check if column already have 6 chips
def check_column():
# Counting how many chips already in that column
chip_X_num = 0
a = len(chip)
my_list = range(0, a, 1)
for i in my_list:
if chip[i][0] == chip_X:
chip_X_num += 1
if chip_X_num == 6:
return False
else:
return True
def determine_chip_Y_destination():
# Declare Global Variables
global chip_Y_destination
# Count how many chips already in that column
chip_Y_num = 0
a = len(chip)
my_list1 = range(0, a, 1)
for i in my_list1:
if chip[i][0] == chip_X:
chip_Y_num += 1
chip_Y_destination = 700 - chip_Y_num * 100
def add_chip():
# Add new chip into list
if turn == 0:
chip_color = "Red"
elif turn == 1:
chip_color = "Navy"
chip.append([chip_X, chip_Y_destination, chip_color])
# Check for winner
print chip
check_win()
def check_win():
global gamestate, winner
# Draw
a = len(chip)
my_list = range(0, a, 1)
if a == 42:
winner = "Draw"
gamestate = 0
# Vertical Win
for i in my_list:
for j in my_list:
if chip[i][0] == chip[j][0] and chip[i][1] - 100 == chip[j][1] and chip[i][2] == chip[j][2]:
for k in my_list:
if chip[j][0] == chip[k][0] and chip[j][1] - 100 == chip[k][1] and chip[j][2] == chip[k][2]:
for l in my_list:
if chip[k][0] == chip[l][0] and chip[k][1] - 100 == chip[l][1] and chip[k][2] == chip[l][2]:
if chip[i][2] == "Red":
winner = "Red"
elif chip[i][2] == "Navy":
winner = "Navy"
gamestate = 0
# Horizontal Win
for i in my_list:
for j in my_list:
if chip[i][0] + 100 == chip[j][0] and chip[i][1] == chip[j][1] and chip[i][2] == chip[j][2]:
for k in my_list:
if chip[j][0] + 100 == chip[k][0] and chip[j][1] == chip[k][1] and chip[j][2] == chip[k][2]:
for l in my_list:
if chip[k][0] + 100 == chip[l][0] and chip[k][1] == chip[l][1] and chip[k][2] == chip[l][2]:
if chip[i][2] == "Red":
winner = "Red"
elif chip[i][2] == "Navy":
winner = "Navy"
gamestate = 0
# Diagonal Right Win
for i in my_list:
for j in my_list:
if chip[i][0] + 100 == chip[j][0] and chip[i][1] - 100 == chip[j][1] and chip[i][2] == chip[j][2]:
for k in my_list:
if chip[j][0] + 100 == chip[k][0] and chip[j][1] - 100 == chip[k][1] and chip[j][2] == chip[k][2]:
for l in my_list:
if chip[k][0] + 100 == chip[l][0] and chip[k][1] - 100 == chip[l][1] and chip[k][2] == chip[l][2]:
if chip[i][2] == "Red":
winner = "Red"
elif chip[i][2] == "Navy":
winner = "Navy"
gamestate = 0
# Diagonal Left Win
for i in my_list:
for j in my_list:
if chip[i][0] + 100 == chip[j][0] and chip[i][1] + 100 == chip[j][1] and chip[i][2] == chip[j][2]:
for k in my_list:
if chip[j][0] + 100 == chip[k][0] and chip[j][1] + 100 == chip[k][1] and chip[j][2] == chip[k][2]:
for l in my_list:
if chip[k][0] + 100 == chip[l][0] and chip[k][1] + 100 == chip[l][1] and chip[k][2] == chip[l][2]:
if chip[i][2] == "Red":
winner = "Red"
elif chip[i][2] == "Navy":
winner = "Navy"
gamestate = 0
class Objects:
def draw_chips(self, canvas):
a = len(chip)
my_list = range(0, a, 1)
for i in my_list:
self.chip_X = chip[i][0]
self.chip_Y = chip[i][1]
self.chip_color = chip[i][2]
canvas.draw_circle((self.chip_X, self.chip_Y), 45, 1, "Black", self.chip_color)
canvas.draw_circle((self.chip_X, self.chip_Y), 40, 1, "Black", self.chip_color)
def chip(self, canvas):
if turn == 0:
chip_color = "Red"
elif turn == 1:
chip_color = "Navy"
canvas.draw_circle((chip_X, chip_Y), 45, 1, "Black", chip_color)
canvas.draw_circle((chip_X, chip_Y), 40, 1, "Black", chip_color)
def draw_handler(canvas):
# Draw chip used for selection
if gamestate == 1:
Objects().chip(canvas)
# Draw chips
Objects().draw_chips(canvas)
# Draw Frame
canvas.draw_image(image1, (830 / 2, 780 / 2), (830, 780), (830 / 2, 780 / 2), (830, 780))
# Draw gameover
if gamestate == 0:
if winner == "Draw":
canvas.draw_text("Out of moves, Draw", (80, 50), 20, 'Black', 'serif')
else:
canvas.draw_text(str(winner) + " Wins", (80, 50), 20, 'Black', 'serif')
canvas.draw_text("Press space to start new game", (560, 50), 20, 'Black', 'serif')
def keydown_handler(key):
# Anti_ghost old method of preventing multiple keys refer to sonic for new format
global anti_ghost, gamestate, turn
global chip, chip_X
if key == simplegui.KEY_MAP["space"]:
if gamestate == 1:
if check_column() == True:
anti_ghost = True
determine_chip_Y_destination()
timer1.start()
elif gamestate == 0:
chip = []
gamestate = 1
turn = 0
elif key == simplegui.KEY_MAP["right"] and anti_ghost == False:
if chip_X < 715:
chip_X += 100
elif key == simplegui.KEY_MAP["left"] and anti_ghost == False:
if chip_X > 115:
chip_X -= 100
frame = simplegui.create_frame("Connect Four", 830, 780)
frame.set_canvas_background("White")
frame.set_draw_handler(draw_handler)
frame.set_keydown_handler(keydown_handler)
timer1 = simplegui.create_timer(1, timer1_handler)
frame.start()