forked from Vanshikapandey30/Hacktoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColor Connect Clash
72 lines (61 loc) · 1.75 KB
/
Color Connect Clash
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
from turtle import *
from random import choice
from freegames import line
turns = {'blue': 'green', 'green': 'blue'}
state = {'player': 'green', 'rows': [0] * 8}
def grid():
"""Draw Color Connect Clash grid."""
bgcolor('light blue')
for x in range(-150, 200, 50):
line(x, -200, x, 200)
for x in range(-175, 200, 50):
for y in range(-175, 200, 50):
up()
goto(x, y)
begin_fill()
color('white')
for _ in range(4): # Draw squares
forward(50)
left(90)
end_fill()
update()
def check_full_row(rows):
"""Check if all rows are full."""
return all(count >= 8 for count in rows)
def random_computer_move():
"""Make a random move for the computer."""
rows = state['rows']
available_columns = [i for i, count in enumerate(rows) if count < 8]
return choice(available_columns)
def tap(x, y):
"""Draw blue or green square in tapped row or random computer move."""
player = state['player']
rows = state['rows']
if player == 'green':
row = int((x + 200) // 50)
else:
row = random_computer_move()
count = rows[row]
if count < 8: # Prevent overflow in columns
x = row * 50 - 200 + 25
y = count * 50 - 200 + 25
up()
goto(x, y)
begin_fill()
color(player)
for _ in range(4): # Draw square
forward(50)
left(90)
end_fill()
update()
rows[row] = count + 1
if check_full_row(rows):
print("Game over: All rows full!")
return
state['player'] = turns[player]
setup(420, 420, 370, 0)
hideturtle()
tracer(False)
grid()
onscreenclick(tap)
done()