-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game_model.py
219 lines (177 loc) · 7.22 KB
/
Game_model.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
219
# Game_model
# Jonathan Tang 85625237
from game_mechanic import Jewel,Faller,GameState
import random
TEST = False
AUTO_FALLER = True
def run_user_interface():
if TEST == True:
# field_height = ask_field_height()
# field_width = ask_field_width()
game_state =GameState(5,5)
create_empty_field(game_state)
new_jewel = Jewel('A',1,4,"FROZEN")
new_jewel2 = Jewel('B',2,5,"FALLING")
## game_state.place_jewel_on_field(new_jewel)
## game_state.drop_jewel(new_jewel)
## game_state.display_field()
## print("")
game_state = GameState(5,5)
game_state.place_jewel_on_field(new_jewel2)
game_state.add_row_jewels(0,'YXY X')
game_state.add_row_jewels(1,'YYY Y')
game_state.add_row_jewels(2,'BYY Y')
game_state.add_row_jewels(3,'BBB Y')
game_state.add_row_jewels(4,'BYB Y')
game_state.drop_all_jewels()
game_state.display_field()
## new_jewel2 = Jewel('B',0,2,"FROZEN")
## game_state.display_field()
##
## game_state.place_jewel_on_field(new_jewel2)
## game_state.display_field()
##
## game_state.drop_jewel_one_down(new_jewel2)
## game_state.display_field()
##
## game_state.drop_jewel_one_down(new_jewel2)
## game_state.display_field()
new_faller = Faller('J','Y','J',4)
game_state.place_faller_on_board(new_faller)
game_state.update_faller_state(new_faller)
game_state.display_field()
while game_state.empty_below_faller(new_faller):
game_state.drop_faller_one_down(new_faller)
game_state.update_faller_state(new_faller)
game_state.display_field()
game_state.update_faller_state(new_faller)
game_state.display_field()
game_state.check_diaganol_match()
game_state.check_vertical_match()
game_state.check_horizontal_match()
game_state.display_field()
game_state.remove_matched_jewels()
game_state.display_field()
game_state.drop_all_jewels()
game_state.display_field()
else:
field_height = int(ask_field_height())
field_width = int(ask_field_width())
game_state = GameState (field_height,field_width)
create_field(game_state)
while check_match(game_state):
pass
game_state.display_field()
running = True
while running:
if AUTO_FALLER == False:
faller_init = ask_input() #Want input of F
if faller_init == "Q":
break
running = False
if faller_init == None:
continue
elif faller_init[0] == "F":
faller_col = int(faller_init[2])
faller_top_color = faller_init[4]
faller_middle_color = faller_init[6]
faller_bottom_color = faller_init[8]
new_faller = Faller(faller_top_color,faller_middle_color,faller_bottom_color,faller_col)
else:
continue
if AUTO_FALLER == True:
new_faller = Faller("A","B","C",1)
while (new_faller.get_faller_state() != "FROZEN" and running == True):
game_state.place_faller_on_board(new_faller)
game_state.display_field()
faller_command = ask_input() #print("Put in > < or R")
if faller_command == "Q":
running = False
break
elif faller_command == ">":
game_state.move_faller_right(new_faller)
elif faller_command == "<":
game_state.move_faller_left(new_faller)
elif faller_command == "R":
game_state.rotate_faller(new_faller)
elif game_state.empty_below_faller(new_faller) and faller_command == "":
game_state.drop_faller_one_down(new_faller)
game_state.update_faller_state(new_faller)
elif faller_command == "":
game_state.update_faller_state(new_faller)
while check_match(game_state):
pass
game_state.move_faller_into_field(new_faller)
while check_match(game_state):
pass
game_state.display_field()
if game_state.not_frozen_or_faller_in_field(new_faller) != True:
print("GAME OVER")
running = False
def create_field(game_state:GameState) -> None:
#Creates a correctly sized GameState and creates
start_input = input () # EMPTY or CONTENTS "
if start_input == "EMPTY":
create_empty_field(game_state)
elif start_input == "CONTENTS":
create_filled_field(game_state)
def create_empty_field(game_state:GameState) -> None:
game_state.new_game_field()
def create_filled_field(game_state:GameState) -> None:
game_state.new_game_field()
for i in range (game_state.get_field_height()):
row_of_jewels = input("Row_of_Jewel "+ str(game_state.get_field_width())+" ")
game_state.add_row_jewels(i,row_of_jewels)
if check_match(game_state) != True:
game_state.drop_all_jewels()
def check_match(game_state:GameState):
# Returns True if there are matches, otherwise, returns false
bool_value = False
match1 = game_state.check_diaganol_match()
match2 = game_state.check_vertical_match()
match3 = game_state.check_horizontal_match()
if (match1 == True or match2 == True or match3 == True):
game_state.remove_matched_jewels()
game_state.drop_all_jewels()
bool_value = True
return bool_value
def update_faller(game_state: GameState, faller:Faller ):
game_state.place_faller_on_board(faller)
game_state.update_faller_state(faller)
def ask_input() ->str:
user_input = input()
if user_input == "":
return user_input
else:
return handle_input(user_input[0],user_input)
def random_faller(game_state:GameState) -> Faller:
jewel_colors = ['A','B','C','D','E','F','G']
field_columns = range(1,game_state.get_field_width() + 1)
top_color = random.choice(jewel_colors)
middle_color = random.choice(jewel_colors)
bottom_color = random.choice(jewel_colors)
random_col = random.choice(field_columns)
return Faller(top_color,middle_color,bottom_color,random_col)
def handle_input(user_command,user_input) -> str:
if user_command == "F":
return user_input
if user_command == "Q":
return user_input
if user_command == ">":
return user_input
if user_command == "<":
return user_input
if user_command == "R":
return user_input
else:
return None
def ask_field_height() -> int:
#Returns input height of the field
field_height = input() #"Field Height?"
return field_height
def ask_field_width() -> int:
#Returns input width of the field
field_width = input() #"Field Width?"
return field_width
if __name__ == "__main__":
run_user_interface()