-
Notifications
You must be signed in to change notification settings - Fork 1
/
project_v3.py
300 lines (277 loc) · 13.8 KB
/
project_v3.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import random2
from colorama import init, Style
from os import system, name
from IPython.display import clear_output
init() #[It generates the game state colors in windows terminal, not in Google Colab.]
# Screen clear function at restarting the game
def clear():
# for windows (here, os.name is 'nt')
if name == 'nt':
_ = system('cls')
# for mac, linux or Google Colab (here, os.name is 'posix')
else:
_ = system('clear')
clear_output()
# Random coordinate generator function
def Rand(start, end, length):
list = []
while len(list)<length:
res = []
for j in range(2):
res.append(random2.randint(start, end))
if res not in list:
list.append(res)
if res==[0,0] or res==[end, end]:
list.remove(res)
list = [i for i in list+obs if i in list and i not in obs]
list.sort()
return list
# Game dictionary
Game_dict = { 'Easy':{'Size':10, 'Life':5, 'Jump':5, 'Obstacle':10, 'Advantage':20, 'Obstacle_coordinates':[], 'Advantage_coordinates':[]},
'Medium':{'Size':20, 'Life':8, 'Jump':10,'Obstacle':15, 'Advantage':25, 'Obstacle_coordinates':[], 'Advantage_coordinates':[]},
'Hard':{'Size':25, 'Life':10, 'Jump':15, 'Obstacle':20, 'Advantage':35, 'Obstacle_coordinates':[], 'Advantage_coordinates':[]} }
# Game state sign function
def Game_State_Sign(x,y,level):
for i in range(0, size):
for j in range(0, size):
list_ij=[i,j]
if (i==x and j==y):
print("\033[0;37;44m s",end=' ')
elif (list_ij in p_xy):
print("\033[1;36;40m -",end=' ')
elif (list_ij in Obstacle_list):
print("\033[0;37;41m *",end=' ')
elif (list_ij in Advantage_list):
print("\033[0;37;42m +",end=' ')
elif (i==size-1 and j==size-1):
print("\033[0;37;45m e",end=' ')
else:
print("\033[0;30;47m -",end=' ')
#print('\033[ \n') #newline with default style [It works in Google Colab, but not in windows terminal. Why!?]
print(Style.RESET_ALL) #return to default style [It works both in windows terminal and Google Colab.]
# Game state coordinate function
def Game_State_Coordinate(x,y,level):
for i in range(0, size):
for j in range(0, size):
list_ij=[i,j]
if (i==x and j==y):
print("\033[0;37;44m s "+Style.RESET_ALL+' ',end='')
elif (list_ij in p_xy):
print("\033[1;36;40m{:2.0f},".format(i),j,Style.RESET_ALL+' ',end='')
elif (list_ij in Obstacle_list):
print("\033[0;37;41m{:2.0f},".format(i),j,Style.RESET_ALL+' ',end='')
elif (list_ij in Advantage_list):
print("\033[0;37;42m{:2.0f},".format(i),j,Style.RESET_ALL+' ',end='')
elif (i==size-1 and j==size-1):
print("\033[0;37;45m e "+Style.RESET_ALL+' ',end='')
else:
print("\033[0;30;47m{:2.0f},".format(i),j,Style.RESET_ALL+' ',end='')
#print('\033[ \n') #newline with default style [It works in Google Colab, but not in windows terminal. Why!?]
print(Style.RESET_ALL,"\n") #newline with to with default style [It works both in windows terminal and Google Colab.]
# Game state function
def Game_State(x,y,level):
print("\nYour current game state with sign:\n")
Game_State_Sign(x,y,level)
if level != 'Hard':
print("\nYour current game state with coordinate:\n")
Game_State_Coordinate(x,y,level)
print("\nYour current coordinate: {}, {}".format(x,y))
# Jump function
def Jump(command,x,y, jump):
if jump>0:
if command == '56': # Jump right
x = x
y = y+2
elif command == '54': # Jump left
x = x
y = y-2
elif command == '58': # Jump up
x = x-2
y = y
elif command == '52': # Jump down
x = x+2
y = y
jump = jump - 1
print("You have only",jump,"jumps left!")
else:
print("Sorry, you can't jump anymore.")
return x, y, jump
# Move function
def Move(command,x,y, jump):
if command == '6': # Move right
x = x
y = y+1
elif command == '4': # Move left
x = x
y = y-1
elif command == '8': # Move up
x = x-1
y = y
elif command == '2': # Move down
x = x+1
y = y
elif command in commands[-4:]:
x,y,jump = Jump(command,x,y,jump)
elif command not in commands:
print("Invalid command key\nThe valid command keys are:", end= " ")
print(*commands, sep = ", ")
return x, y, jump
# Undo function [It's mot much efficient. If you use it twice in a row to return to previous coordinates, it won't function the second time. #Because it stores only your last coordinates, not all your past ones.
def Undo(x_past,y_past):
x = x_past
y = y_past
return x,y
# Point function [ I tried to apply Game_Denoter concept. But in doing so, my Point function became complicated and was giving wrong output.]
def Points(x,y,level,life,jump,point,command):
if command in commands:
if command=='0':
Game_State(x,y,level)
life = life - 1
print("You have seen the game state again. Your life is reduced to", life)
elif command=='3':
life = life
point = point
print("You have returned to your previous coordinate by the undo command.\nSo Your lives or points haven't changed.")
else:
if [x,y] != [x_past,y_past] and [x,y] not in p_xy:
if [x,y] in Obstacle_list:
point = point - 5
print("It's an obstacle coordinate. Your point is reduced to",point)
elif [x,y] in Advantage_list:
point = point + 10
print("It's an advantage coordinate. Your point is increased to",point)
else:
if [x,y] not in list_xy:
life = life - 1
#print("Your current coordinate: {}, {}".format(x,y))
print("You are out of the game state boundary. You life is reduced to",life)
if life>0:
print("Press 3 to undo this move and return back inside the game state.")
else:
if command in commands[-4:]:
print("So, your coordinate is not changed.")
else:
print("You have come here before.\nSo it can't change your lives or points anymore.")
return life, point
while True: #restarting loop of the main program
#main program
print("\n\033[1;31mWelcome to the 'Memorize your move' game!\n"+Style.RESET_ALL)
print("This is a console game where you will start from the initial coordinate and need to reach the final coordinate. \nOn your way to the ending coordinate, you will find some obstacles and advantages.\n")
print("\033[1;31mGame Features:"+Style.RESET_ALL+"\n# The game has three levels - 1) Easy, 2) Medium and 3) Hard.")
print("# For each level, you will have fixed game state, lives and jumps. \n# Obstacles will reduce your point and Advantages will increase your point.")
print("# You can jump over the obstacles also, but only for fixed times. \n# You can see your present coordinate anytime. But for each time of seeing, you will lose one of your life.")
print("# If you go out of the bound, you will lose one of your life.")
print("# The effect of advantage/obstacle on each coordinate works the first time only. \n That means your lives/points won't change when you undo your move or go back to any of your past coordinates.")
print("# So, you need to memorize your coordinate, obstacles, advantages. And move or jump according to that.\n")
print("\033[1;31mChoose your level:"+Style.RESET_ALL+" \nPress 1 for Easy \nPress 2 for Medium \nPress 3 for Hard")
while True:
gl = input("> ",)
if gl == '1':
level = 'Easy'
break
elif gl == '2':
level = 'Medium'
break
elif gl == '3':
level = 'Hard'
break
else:
print("Invalid input. Try again. Choose 1, 2 or 3.")
continue
obs = []
Game_dict[level]['Obstacle_coordinates'] = Rand(0, Game_dict[level]['Size']-1, Game_dict[level]['Obstacle'])
obs = Game_dict[level]['Obstacle_coordinates']
Game_dict[level]['Advantage_coordinates'] = Rand(0, Game_dict[level]['Size']-1, Game_dict[level]['Advantage'])
adv = Game_dict[level]['Advantage_coordinates']
size = Game_dict[level]['Size']
life = Game_dict[level]['Life']
jump = Game_dict[level]['Jump']
Obstacle_list = Game_dict[level]['Obstacle_coordinates']
Advantage_list = Game_dict[level]['Advantage_coordinates']
print("There are two game modes: \n (a) Simple Mode: All your commands and outputs will remain visible until the game ends.\n (b) Complex Mode: The output of your previous command will remain visible until giving the next command.")
print("\n\033[1;31mChoose your mode:"+Style.RESET_ALL+" \nPress a for Simple \nPress b for Complex")
while True:
gm = input("> ",)
if gm == 'a':
mode = 'Simple'
break
elif gm == 'b':
mode = 'Complex'
break
else:
print("Invalid input. Try again. Choose a or b.")
continue
print("\nWelcome to - Level: "+level+" ("+mode+" Mode)")
commands = ['0', '1', '2', '3', '4', '6', '8', '52', '54', '56', '58']
list_xy = []
for i in range(0, size):
for j in range(0, size):
list_xy.append([i,j]) #This list contains all the cordinates of the game state of the selected level.
#Starting conditions:
x=0
y=0
point=0
p_xy = [[x,y]] #This list will keep all the coordinates that the player will go through.
Game_State(x,y, level)
print("Here, you have {} lives".format(life), end=" ")
print("and {} jumps.".format(jump))
print("There are",len(obs),"obstacles and",len(adv),"advantages in this game state.\n\n\033[1;31mYour target is to avoid every obstacle and go through all the advantages.\nBy doing so, you can achieve",10*(len(adv)),"points maximum before reaching the ending coordinate."+Style.RESET_ALL)
print("\n\033[1;31mHow to play:"+Style.RESET_ALL+"\nPress 8 to move up \nPress 2 to move down \nPress 4 to move left \nPress 6 to move right")
print("Press 5 and any of the move commands to jump at a specific direction:\n Press 58 to jump up\n Press 52 to jump down\n Press 54 to jump left\n Press 56 to jump right")
print("Press 0 to see the game state and your current coordinate \nPress 3 to undo your last move\nPress 1 to exit the game")
if mode=='Complex':
print("\n\033[1;31mAs you have selected the Complex Mode, all your previous command outputs (including the game state) are gonna disappper when you give the next command. \nSo, before giving the first command, carefully read the game rules and memorize your coordinate, obstacles, advantages."+Style.RESET_ALL)
print("\nIf you are ready.....")
else:
print("\nMemorize your coordinate, obstacles, advantages.")
print("\nStart making moves by pressing the command keys which are listed above.")
while (life > 0):
command = input("\ncommand: ")
if mode=='Complex':
clear()
if command=='0':
life,point = Points(x,y,level,life,jump,point,command)
print("Your current point:", point)
if life==0:
print("You lost with",jump,"jumps.\n\033[1;31mGAME OVER"+Style.RESET_ALL)
break
elif command=='1':
print("\nYou have exited the game.\n \033[1;31m-- END --"+Style.RESET_ALL)
break
elif command=='3':
x,y = Undo(x_past, y_past)
#print("Your current coordinate: {}, {}".format(x,y))
life,point = Points(x,y,level,life,jump,point,command)
#print("Your current point:", point)
else:
x_past = x
y_past = y
x,y,jump = Move(command,x,y,jump)
#print("Your current coordinate: {}, {}".format(x,y))
life,point = Points(x,y,level,life,jump,point,command)
#print("Your current point:", point)
if [x,y] not in p_xy:
p_xy.append([x,y])
#print(p_xy)
if life==0 and x==size-1 and y==size-1:
print("You have reached the end. But you have no life left.\n\033[1;31mGAME OVER"+Style.RESET_ALL)
break
elif life==0:
print("You lost with",point,"points and",jump,"jumps.\n\033[1;31mGAME OVER"+Style.RESET_ALL)
break
elif (x==size-1 and y==size-1):
Game_State(x,y, level)
print("You have reached the end. \nYou won with ",point,"points (out of",10*(len(adv)),"points),",life,"lives and",jump,"jumps. Congrats!\n \033[1;31m-- END --"+Style.RESET_ALL)
break
while True:
print("\n*********************************************************************************\n")
answer = input("Wanna play again? \n(y/n): ")
if answer in ('y', 'n'):
break
print("Invalid input. Type y or n")
if answer == 'y':
clear()
continue
else:
print("\n ********Goodbye********")
break