-
Notifications
You must be signed in to change notification settings - Fork 0
/
TowerGame.rpy
511 lines (406 loc) · 15.6 KB
/
TowerGame.rpy
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
image pole = "pole.png"
define texture1 = "texture1.png"
define texture2 = "texture2.png"
define texture3 = "texture3.png"
define texture4 = "texture4.png"
####
# main game screen
screen hanoi_game_screen(towers):
textbutton "Click me!" action Show("moves_count_scr") align (0.95, 0.05)
vbox:
align (0.05, 0.05)
textbutton "autosolve" action [ToggleVariable("autosolve", False, True), SetVariable("create_movements_list", True), Return("start_autosolving")]
textbutton "give up" action Jump("give_up")
text "Moves made: [player_moves]" size 35 align (0.5, 0.05)
for i, each_tower in enumerate(towers):
####
# pole and mark
vbox:
pos each_tower["tower_pos"] anchor(0.5, 1.0)
xalign 0.5
if each_tower["mark"] == "start":
text "Start" size 25 xalign 0.5
null height (block_size*1)
elif each_tower["mark"] == "finish":
text "Goal" size 25 xalign 0.5
null height (block_size*1)
else:
text "" size 25 xalign 0.5
null height (block_size*1)
add "pole"
####
# blocks
vbox:
pos each_tower["tower_pos"] anchor(0.5, 1.0)
xalign 0.5 yoffset -20
for each_block in each_tower["blocks"]:
frame:
xpadding 0 ypadding 0
xmargin 0 ymargin 0
background Frame(each_block["color"], 10, 10) # color
xminimum (block_size*(each_block["size"]+2)) xmaximum (block_size*(each_block["size"]+2)) # the width of block
yminimum block_size ymaximum block_size
xalign 0.5
####
# buttons to operate the game
if can_click:
####
# if player haven't decided yet what block to move
# and there is some blocks on the pole
if start_from_tower < 0 and len(each_tower["blocks"])>0:
button:
xminimum 125 xmaximum 125
pos each_tower["tower_pos"] anchor(0.5, 0.0) yoffset 10
text "↑" size 35 xalign 0.5
action SetVariable("start_from_tower", i)
####
# if player decided to move block from this pole
elif start_from_tower == i:
button:
xminimum 125 xmaximum 125
pos each_tower["tower_pos"] anchor(0.5, 0.0) yoffset 10
text "undo" size 35 xalign 0.5
action SetVariable("start_from_tower", -1)
####
# if block that player decided to move is smaller
# than top block on this pole
elif (start_from_tower >= 0) and (towers[i]["blocks"] == [] or towers[start_from_tower]["blocks"][0]["size"]<towers[i]["blocks"][0]["size"]):
button:
xminimum 125 xmaximum 125
pos each_tower["tower_pos"] anchor(0.5, 0.0) yoffset 10
text "[i] ↓" size 35 xalign 0.5
action [SetVariable("finish_to_tower", i), Return("move_done")]
####
# if block can't be moved on this pole
else:
button:
xminimum 125 xmaximum 125
pos each_tower["tower_pos"] anchor(0.5, 0.0) yoffset 10
text " " size 35 xalign 0.5
action [[]]
####
# if interactions disabled
else:
button:
xminimum 125 xmaximum 125
pos each_tower["tower_pos"] anchor(0.5, 0.0) yoffset 10
text " " size 35 xalign 0.5
action [[]]
screen moves_count_scr():
modal True
default i = 0
$ n = hanoi_moves_count(i)
frame:
align (0.5,0.5)
vbox:
text"Minimal number of moves to solve [i] blocks tower:"
text "[n]" size 35 xalign 0.5
null height 20
hbox:
xalign 0.5
textbutton "<" action SetScreenVariable("i", max(0, i-1))
null width 20
textbutton ">" action SetScreenVariable("i", i+1)
null height 20
textbutton "close" action Hide("moves_count_scr") xalign 0.5
init python:
def check_game_state():
'''
This function checks if all the blocks are in one
tower at the finish pole and return True or False.
'''
global towers, finish_tower, blocks_number
if len(towers[finish_tower]["blocks"]) == blocks_number:
return True
return False
def hanoi_moves_count(x):
'''
This function counts the number of moves one must proceed
to move the whole tower from start pole to finish one.
x - number of blocks in tower
'''
if x == 1:
return 1
elif x > 1:
return 1 + hanoi_moves_count(x-1) * 2
else:
return 0
def make_path_2(t, x, f):
'''
Use this function to get result of "hanoi_func_2"
t - towers description that should be a list
of 3 lists which contents the blocks numbers
x - number of the biggest block
f - number of the pole where tower should be moved to
This function will work even if some blocks was already moved
from start tower to another pole.
Note that this function will work correct only if blocks
in each tower placed correct (the small one over the large, like
[ [3], [1, 2, 4], [] ]), in case you will try to randomly
generate the game state.
'''
####
# the general list of moves
global res_2
res_2 = []
return hanoi_func_2(t, x, f)
def hanoi_func_2(t, x, f):
'''
t - towers description that should be a list
of 3 lists which contents the blocks numbers
x - number of the biggest block
f - number of the pole where tower should be moved to
The function returns the list of tuples that are
(start_pole, finish_pole) that describes moves that
should be made from last to first to win the game.
So when function will return that list, one should pop
tuples out of it one after another and move blocks
from "start_pole" position to "finish_pole" position.
'''
global res_2
####
# s - is the pole where the biggest block is located
if x in t[0]:
s = 0
elif x in t[1]:
s = 1
else:
s = 2
####
# if the biggest block is already on finish pole
# and this block's number is 1
# then we got nothing else to do
if (s == f) and (x == 1):
pass
####
# if the biggest block is already on finish pole
# and this block's number is not 1
# then we should move the tower of (x-1) blocks upon it
elif (s == f) and (x != 1):
hanoi_func_2(t, x-1, f)
####
# if the biggest block is not in finish pole
# and this block's number is 1
# then move it to finish pole
elif (s != f) and (x == 1):
res_2.append((s, f))
####
# otherwise we need to create a list of moves
# [to move (x-1) tower from free pole to finish one,
# to move x-block to the finish pole,
# to set (x-1) tower on the free pole from blocks on different poles]
# then we should make the moves from this list
# from last one to first.
#
# So to move (x-1) tower to finish pole
# we should use make_path_1 function,
# and to set all the blocks to (x-1) tower on the free pole
# we should use make_path_2 function
else:
####
# ss is the free pole that neither start nor finish one
# for current tower
ss = [0,1,2]
ss.remove(s)
ss.remove(f)
ss = ss[0]
make_path_1(x-1, ss, f)
res_2.append((s, f))
####
# ff is the free pole that neither start nor finish one
# for current tower
ff = [0,1,2]
ff.remove(s)
ff.remove(f)
ff = ff[0]
hanoi_func_2(t, x-1, ff)
return res_2
def make_path_1(x, s, f):
'''
Use this function to get result of "hanoi_func_1"
x - number of blocks in tower
s - number of pole where tower is located
f - number of pole where tower should be moved to
This function is used when all the blocks are in one tower.
'''
global res_1, res_2
res_1 = []
hanoi_func_1(x, s, f)
####
# adds all the moves to the general list of moves
#
for i in res_1:
res_2.append(i)
def hanoi_func_1(x, s, f):
'''
x - number of blocks in tower
s - number of pole where tower is located
f - number of pole where tower should be moved to
The function returns the list of tuples that are
(start_pole, finish_pole) that describes moves that
should be made from last to first to win the game.
So when function will return that list, one should pop
tuples out of it one after another and move blocks
from "start_pole" position to "finish_pole" position.
In general, to move n-block tower from start pole to finish pole,
one must move (n-1)-block tower to the pole
that is neither start nor finish one,
then move the n-block to the finish pole
and move (n-1)-block tower to the finish pole
'''
global res_1
if x == 1:
res_1.append((s, f))
else:
####
# ff is the free pole that neither start nor finish one
# for current tower
ff = [0,1,2]
ff.remove(s)
ff.remove(f)
ff = ff[0]
hanoi_func_1(x-1, ff, f)
res_1.append((s, f))
hanoi_func_1(x-1, s, ff)
return res_1
label hanoi_game(blocks_number=8): # sets the default number of blocks
####
# a list of colors for maximum number of blocks
# this can be colors or images,
# if you need all the blocks to be the same color
# just add this color to the list several times
$ block_colors = ["#c00", "#0c0", "#00c", "#cc0", "#c0c", "#0cc", "#930", "#ccc"]
$ block_colors = [texture1, texture2, texture3, texture4, texture1, texture2, texture3, texture4]
####
# number of blocks in tower shouldn't be greater
# then the number of colors in block_colors list
if blocks_number > len(block_colors):
$ blocks_number = len(block_colors)
####
# creates the blocks description
$ blocks_set = []
python:
for b in range(1, blocks_number+1):
blocks_set.append({"size": b, "color":block_colors[b-1]} )
####
# randomly sets the start and finish poles
$ a = [0,1,2]
$ start_tower = renpy.random.choice(a)
$ a.remove(start_tower)
$ finish_tower = renpy.random.choice(a)
####
# the height of the block in pixels
$ block_size = 20
####
# the positions for all 3 towers
$ towers_pos = [(0.25, 0.65), (0.5, 0.65), (0.75, 0.65)]
####
# main game variable - description of towers
$ towers = [ {"tower_pos":towers_pos[0],"blocks":[], "mark":None},
{"tower_pos":towers_pos[1],"blocks":[], "mark":None},
{"tower_pos":towers_pos[2],"blocks":[], "mark":None}
]
####
# put the blocks, start and finish marks on their places
$ towers[start_tower]["blocks"] = blocks_set
$ towers[start_tower]["mark"] = "start"
$ towers[finish_tower]["mark"] = "finish"
####
# flags that are used for autosolve function
$ autosolve = False
$ create_movements_list = False
####
# game variables
$ delay_time = 0.2
$ player_moves = 0
$ minimal_moves = hanoi_moves_count(blocks_number)
####
# let's show the game screen
show screen hanoi_game_screen(towers=towers)
####
# main game loop
label hanoi_loop:
####
# default values that shows that player haven't decided yet
# what move to do
$ start_from_tower = -1
$ finish_to_tower = -1
####
# wait for user interact
if not autosolve:
$ can_click = True
$ ui.interact()
####
# to prevent farther interactions
$ can_click = False
####
# autosolve function
if autosolve:
####
# create list of moves
if create_movements_list:
####
# we need to make this list only once
$ create_movements_list = False
####
# make_path_2 function needs "t" - that is list of 3 list
# that contains only blocks numbers
# so create it from main game variable "towers"
$ t = []
python:
for i in towers:
tt = []
for j in i["blocks"]:
tt.append(j["size"])
t.append(tt)
####
# and now, let's make the list of moves
$ movements_list = make_path_2(t, blocks_number, finish_tower)
####
# move to make (the last one in movements_list)
$ start_from_tower, finish_to_tower = movements_list.pop()
####
# move the block
$ block_to_move = towers[start_from_tower]["blocks"][0]
$ towers[start_from_tower]["blocks"] = towers[start_from_tower]["blocks"][1:]
$ towers[finish_to_tower]["blocks"].insert(0, block_to_move)
####
# add one "move" to counter
$ player_moves += 1
####
# check the game state
if check_game_state():
jump win
####
# make pause between moves if autosolve is enaible
if autosolve:
$ renpy.pause(delay_time)
####
# next move
jump hanoi_loop
label win:
####
# to prevent farther interactions
$ can_click = False
if player_moves > minimal_moves:
"You Win!\nIt takes [player_moves] moves."
else:
"Perfect Win!\nIt takes [player_moves] moves."
hide screen hanoi_game_screen
return
label give_up:
####
# to prevent farther interactions
$ can_click = False
####
# to stop autosolve
$ autosolve = False
"Good luck next time..."
hide screen hanoi_game_screen
return
# The game starts here.
label start:
scene black
"..."
call hanoi_game(blocks_number=8) # pass the desireble number of blocks
jump start