This famous game emerged in 1970s and has a number of variants. The first task just provides training in implementing proper order of moves and serves as a preface to the more challenging "sequel".
Original author is not known. We'll use the description from the old Programming Games and Puzzles book by Jacques Arsac:
The year of 2387
. Space expedition reaches planet X
. One of the members just has found enormous decrepit building
and sneaked inside. The large hall is a dangerous place - the ground has many pits or rather crevasses. No live
soul inside. Obviously civilization here reached very advanced technology levels and created automatic factories which were
producing self-moving robots. Factories still function on their own, but not as well as they used to: robots
are created randomly and they do not work properly anymore... They move to attack intruders, but do it blindly -
if there is a pit on their path, they can't avoid it and fall into the abyss. Direction they take is well
determined - they move straight towards the explorer...
The game starts with the human player entering the hall and doors automatically close behind. The only exit is at the opposite wall. There are already several robots in the hall and new ones arrive via four corners.
Both player and robots can move in any of 8
directions. Player additionally can remain in place.
To get rid of the robots the player has two options:
also there are few disintegrating charges: when engaged, such a charge clears all neighboring cells of robots
. . . . . . 0 0 . . . . . . . 0 . . . . . . 0 . . . X . . .
. . . . . . X . . . . . . . . . . . X . . . . . . . 0 . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . 0 . 0 . . . . . . . . . . . . 0 . . . . . . . . . . . .
. . . . . . . . 0 . . . . . . . . 0 . . . . . . . . . . 0 0
. 0 . . . . . . . . . . . . . . 0 . . . . . 0 . . . . . 0 .
. . . . . . . 0 . . . . . . . . . . . . . . . . . . . . . .
0 0 . . . . . X . . . . . . . . . . X . . . . . . . . 0 . .
< 0 . . . . . . . . . . . . . . . . . . . . . . X . . . . @
. . 0 . . . . . . . . . . 0 . . . . . . . . . . . . X . . .
0 X . . . . . . . . . . X . . 0 . . . X . . . . . . 0 . 0 .
. . . . . . . . . X . . . . . . 0 . . . . . . . X . . . . .
. . 0 . . . . . . . . . . . . . X . . . . . . 0 . . 0 . . .
. . . . . . . . . . . . . . . 0 . . . X . . . 0 . . 0 . . 0
. . . X . . . . . . 0 . . . . . . . . . . . . . . . . X X .
. . . . . . . . . . . . . . . 0 . . . X . . . . 0 . . . . X
. . . . . . . . . . . X . . . 0 . . . . . . 0 . . . . . . .
Let's have a look at the game field. It is a rectangle W=30
by H=17
in this case. Pits are denoted with
0
and robots with X
. The player (marked with @
) starts in the middle of the right wall and the exit is
in the middle of the left wall (marked <
). It may be seen that if player makes two diagonal moves in up-left
direction the closest robot moves first up-right
and then straight up
into the pit. However the second
robot will be dangerously close already... Stepping back player forces the second one into the same pit... You'll
see, despite so trivial graphic the game is somewhat scary...
You are to implement the game and submit the code in Lua
. The checker will try to play several runs for
human and verify that you implemented robots (and player) moves correctly.
Try the Hurkle task if you haven't yet to get acquainted with Lua-based problems. Useful links: Quick Intro to Lua and online Lua sandbox.
Technical details
print
for output and io.read
for input (they are connected to checker's code to exchange data).INIT W H R P:
- then read 4
integers W
, H
, R
and P
- for width
and height of the field, amounts of robots and pits.H
lines using spaces for better visibility (so the line becomes 2*W-1
symbols long) - and simply
print those lines one by one. Then print MOVE:
as a prompt for player to enter command.U
, D
, L
, R
, K
or W
. First four serve for direction, K
means to use the charge to kill all robots in neighboring cells. W
means wait one move. If the input is
empty simply repeat it (you'll see that after reading initial parameters one empty read follows to devour
line end).K
is specified, charge is engaged before moving the player e.g. ULK
means "kill robots, then move up-and-left.1
cell to bring as much
closer to player as possible. If robot gets into the pit it disappears. If two (or more) robots move into
the same cell, they collide and only one of them remains active. It is important to move robots simultaneously!4
.Game End Conditions
Detect possible player's death immediately (and particularly don't print the next field's state):
LOST BY CAPTURE
and end the gameLOST BY HIT
and also finishLOST BY FALL
and also finishWIN
or something like this, but
the checker is not expected to play that farMake sure game ends by "falling through the code end" rather than calling os.exit
or halting with error
(because the checker code is added to yours) - also keep in mind to re-initialize all necessary variables at
beginning to correctly perform next runs.