-
Notifications
You must be signed in to change notification settings - Fork 0
/
fourInaRow.py
181 lines (152 loc) · 6.38 KB
/
fourInaRow.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
from pygame import *
from pygamehelper import *
from pygame.locals import *
from vec2d import *
from math import e, pi, cos, sin, sqrt
from random import uniform
import os
FPS = 40
STEP = 60
BOARD_CAPACITY = 63
WHITE = (255,255,255)
YELLOW = (235,235,0)
RED = (214,71,0)
BLUE = (0,102,153)
DARK_BLUE = (0,49,101)
LIGHT_BLUE = (185,231,255)
NICE_BLUE = (202,202,255)
BOARD = (190, 240)
COLUMNS = [190, 250, 310, 370, 430, 490, 550]
ROWS = [240, 300, 360, 420, 480, 540]
class Starter(PygameHelper):
def __init__(self):
self.w, self.h = 800, 600
self.col = -1
self.row = -1
self.move = 0
self.done = 0
self.header = 0
self.head_column = -1
self.turn = 0
self.free_space = 0
self.moving_Y = 0
self.winner = 0
self.game_status = 1
self.pulls = [[0 for y in range(len(COLUMNS))] for x in range(len(ROWS))]
self.Surface = pygame.display.set_mode((self.w, self.h))
PygameHelper.__init__(self, size=(self.w, self.h), fill=(LIGHT_BLUE))
self.background = pygame.image.load(os.path.join("pics", "background.png"))
self.below_board = pygame.image.load(os.path.join("pics", "below_board.png"))
self.board = pygame.image.load(os.path.join("pics", "board.png"))
self.red_pull = pygame.image.load(os.path.join("pics", "red_pull.png"))
self.yellow_pull = pygame.image.load(os.path.join("pics", "yellow_pull.png"))
self.empty = pygame.image.load(os.path.join("pics", "empty.png"))
self.player_header = [self.red_pull, self.yellow_pull]
self.player_falling = [self.yellow_pull, self.red_pull]
self.player = [self.empty, self.red_pull, self.yellow_pull]
def check_win(self):
'''
for c in range(len(ROWS)):
self.result_red = 0
self.result_yellow = 0
for x in range(2,9):
y = x - 2 + c
if self.pulls[x][y] == 1:
self.result_red += 1
elif self.pulls[x][y] == 2:
self.result_yellow += 1
'''
for each_row in self.pulls:
if self.winner == 0:
for i, pull in enumerate(each_row[:4]):
if pull > 0:
if pull == each_row[i + 1] == each_row[i + 2] == each_row[i + 3]:
self.winner = pull
break
for row in range(3):
if self.winner == 0:
for col in range(7):
if self.pulls[row][col] > 0:
if self.pulls[row][col] == self.pulls[row + 1][col] == self.pulls[row + 2][col] == self.pulls[row + 3][col]:
self.winner = self.pulls[row][col]
break
for c in range(6):
if self.winner == 0:
for x in range(3):
if self.winner == 0:
for y in range(4):
if y == x + 2 - c:
if self.pulls[x][y] and (self.pulls[x][y] == self.pulls[x + 1][y + 1] == self.pulls[x + 2][y + 2] == self.pulls[x + 3][y + 3]):
self.winner = self.pulls[x][y]
break
for c in range(6):
if self.winner == 0:
for x in range(3,6):
if self.winner == 0:
for y in range(4):
if y == 8 - x - c:
if self.pulls[x][y] and (self.pulls[x][y] == self.pulls[x - 1][y + 1] == self.pulls[x - 2][y + 2] == self.pulls[x - 3][y + 3]):
self.winner = self.pulls[x][y]
break
print("winner: ", self.winner)
print("_____________________")
def update(self):
while self.moving_Y < ROWS[self.row]:
self.moving_Y += 1
def keyUp(self, key):
pass
def mouseUp(self, button, pos):
x, y = pos[0], pos[1]
if button == 1 and self.game_status == 1:
self.moving_Y = BOARD[1] - STEP
self.move = 0
self.free_space = 0
for column in COLUMNS:
if column < x < column + STEP:
self.col = COLUMNS.index(column)
print("col index:", self.col)
self.move = 1
break
if self.move == 1:
for each_row in self.pulls:
if each_row[self.col] == 0:
self.free_space += 1
print("free space: ", self.free_space)
if self.free_space > 0:
self.row = self.free_space - 1
print("row index:", self.row)
self.pulls[self.row][self.col] = 1 + self.turn
print("Board:")
print(self.pulls)
print("_____________________")
self.turn = not(self.turn)
self.done = 1
self.check_win()
if self.winner:
self.game_status = 2
def mouseMotion(self, buttons, pos, rel):
x, y = pos[0], pos[1]
if self.game_status == 1 and x in range(BOARD[0],BOARD[0] + len(COLUMNS) * STEP):
self.header = 1
for column in COLUMNS:
if column < x < column + STEP:
self.head_column = COLUMNS.index(column)
else:
self.header = 0
def draw(self):
self.screen.fill(NICE_BLUE)
self.screen.blit(self.below_board, BOARD)
if self.game_status == 1:
if self.header == 1 and sum([sum(pull) for pull in self.pulls]) < BOARD_CAPACITY:
self.screen.blit(self.player_header[self.turn], (BOARD[0] + self.head_column * STEP, BOARD[1] - STEP))
if self.move == 1:
self.screen.blit(self.player_falling[self.turn], (BOARD[0] + self.col * STEP, self.moving_Y))
for col in range(len(COLUMNS)):
for row in range(len(ROWS)):
if self.pulls[row][col] > 0:
self.screen.blit(self.player[self.pulls[row][col]], (COLUMNS[col], ROWS[row]))
if self.game_status == 2:
pass
self.screen.blit(self.board, BOARD)
s = Starter()
s.mainLoop(FPS)