Robots Game

Problem #355

Tags: lua games special c-1

Who solved this?

No translations... yet

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:

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...

Problem Statement

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

  1. Use print for output and io.read for input (they are connected to checker's code to exchange data).
  2. At start print the magic phrase 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.
  3. Generate the field randomly, according to description above. Now main game loop starts.
  4. At every iteration first print the field. Concatenate characters in every of 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.
  5. Read user input as a line of characters 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).
  6. Execute user's move, mind that if K is specified, charge is engaged before moving the player e.g. ULK means "kill robots, then move up-and-left.
  7. Perform robot moves, shifting each of them vertically, horizontally or diagonally 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!
  8. If the total number of robots left in the field is less than initial robot count, add one new robot at one of the four corners (pick randomly just make sure it is unoccupied).
  9. Repeat from the step 4.

Game End Conditions

Detect possible player's death immediately (and particularly don't print the next field's state):

Make 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.

You need to login to get test data and submit solution.