-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_controller.py
196 lines (177 loc) · 6.6 KB
/
game_controller.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
from disk import Disk
from check_win import Check_Win
from winners import Winners
import random
# define constants
ZERO = 0
ONE = 1
TWO = 2
FOUR = 4
SEVEN = 7
TEN = 10
COUNTDOWN = 20
ROW = 6
COLUMN = 7
HALF_HUDR = 50
ONE_HUDR = 100
MAX_DROP_HEIGHT = 650
# define text constant
TEXT_FILL = 255
TEXT_SIZE = 50
class GameController:
def __init__(self, SPACE):
self.SPACE = SPACE
self.matrix = [[ZERO]*ROW for x in range(COLUMN)]
self.disk_ball = None
self.total_disk = ZERO
self.ai_side = False
self.winning = False
self.countdown = COUNTDOWN
def update(self):
'''display disks in the current matrix and check winning status'''
# display the disks in the list matrix
for column in self.matrix:
for disk in column:
if disk != ZERO:
disk.draw_me(disk.x, disk.y)
# check tie if the matrix is full of disks
if self.total_disk == COLUMN * ROW:
fill(TEXT_FILL)
textSize(TEXT_SIZE)
text("TIE!",
self.SPACE['w']/TWO - ONE_HUDR, self.SPACE['h']/TEN)
# end loop
noLoop()
# check result's status
result = Check_Win(self.matrix).check_win()
# if human side win:
if result == "R":
fill(TEXT_FILL)
textSize(TEXT_SIZE)
text("YOU WIN!",
self.SPACE['w']/TWO - ONE_HUDR, self.SPACE['h']/TEN)
# trigger to update winner's score.txt
self.winning = True
# if machine side win:
if result == "Y":
fill(TEXT_FILL)
textSize(TEXT_SIZE)
text("MACHINE WIN! YOU LOSE!",
self.SPACE['w']/FOUR - (ONE_HUDR + HALF_HUDR),
self.SPACE['h']/TEN)
# end loop
noLoop()
def place_ball(self, mouseX, mouseY):
'''get x and y coordinates of the mouse
and show the disk in the upper grey area'''
# get the column index of where the pressed mouse points to
col = self.get_column_index(mouseX)
# check if that column if full
if not self.is_row_full(col):
# create human side disk object
self.disk_ball = Disk("R")
# get the column index by passing mouse X location
# calculate where to draw the disk
# draw the disk
col_tem = self.get_column_index(mouseX)
draw_x = col_tem * ONE_HUDR + HALF_HUDR
draw_y = HALF_HUDR
self.disk_ball.draw_me(draw_x, draw_y)
def release_ball(self):
'''calculate x and y coordinates and drop the disk'''
drop_height_index = None
# get the column index
col = self.get_column_index(self.disk_ball.x)
# check if the column is full
if not self.is_row_full(col):
# get the row index of the targeted place for dropped disk
drop_height_index = self.matrix_update(col)
# calculate the targeted dropped height
drop_height = MAX_DROP_HEIGHT - drop_height_index * ONE_HUDR
# drop the disk
self.disk_ball.down(drop_height)
# total disks in the matrix add 1
self.total_disk += 1
# trigger AI's side
print("It's AI's turn:")
self.ai_side = True
def release_AI(self):
'''release disk in the AI side'''
# create AI side disk object
self.disk_ball = Disk("Y")
# randomly generate the column for dropping AI side disk
# check if the column can drop AI side disk
can_drop = True
while can_drop:
# column minus one as it's a closed range
col = random.randint(0, (COLUMN - ONE))
if not self.is_row_full(col):
drop_height_index = self.matrix_update(col)
# total disks in the matrix add 1
self.total_disk += 1
# end the while when can drop
can_drop = False
else:
continue
# pass x and y coordinates for AI side disk
draw_x = col * ONE_HUDR + HALF_HUDR
draw_y = HALF_HUDR
self.disk_ball.draw_me(draw_x, draw_y)
# calculate the targeted dropped height
drop_height = MAX_DROP_HEIGHT - drop_height_index * ONE_HUDR
# drop the disk
self.disk_ball.down(drop_height)
# trigger human side
print("It's your turn:")
self.countdown = COUNTDOWN
self.ai_side = False
def get_column_index(self, mouse_x):
'''get x coordinate of mouse and return the column index'''
col_index = int(mouse_x // ONE_HUDR)
return col_index
def is_row_full(self, col):
'''check if the specifc column is full'''
row_count = 0
for row in range(len(self.matrix[col])):
if self.matrix[col][row] != ZERO:
row_count += 1
# if row count == 7 or not
if row_count == ROW:
return True
else:
return False
def matrix_update(self, col):
'''check if there is blank space in the specific column,
add disk object to the matrix, return the row index'''
for row in range(len(self.matrix[col])):
if self.matrix[col][row] == ZERO:
self.matrix[col][row] = self.disk_ball
break
return row
def update_winners(self):
'''if human wins, get user's name, and update the score.txt'''
# delay for showing the complete winning texts
if self.winning:
self.countdown -= 1
if self.countdown == 0:
while self.winning:
# get user's name
answer = self.input("Please enter your name: ")
if answer:
print('Hi ' + answer.title() + "! You Win!")
self.winning = False # end while loop
win = Winners() # call class
filename = "score.txt" # input filename
win.winners_update(answer.title(), filename) # update dic
win.winners_rewrite(filename) # rewrite file
elif answer == '':
print('Please enter your name for records: ')
else:
print(answer) # Canceled dialog will print None
self.winning = False
# end loop
noLoop()
def input(self, message=''):
'''for user to input his/her name'''
from javax.swing import JOptionPane
return JOptionPane.showInputDialog(frame, message)