As we learned from the IBM PC AT, YouTube (see video), Wikipedia (see article), and Sesame Street:
The letter H
is the most merciless letter of the alphabet!
(Even when actually composed from two elements in Code Page 437. In fact, it's even MORE merciless that way.)
Like the Aliens in, uhm...er... Aliens, Beasts relentlessly pursue all who would dare come near their eggs. There is no reasoning with them. You must squash them if not to perish.
For this scenario we will assume you're down to your last life, and you've met plain Beasts in a terrain with no eggs (as in the Wikipedia screenshot). You don't have a numeric keypad and can only move directly up/down/left/right...but the beasts apparently have one, and can move diagonally on their turn.
A Beast's choice of move among its options will be the one that minimizes distance from the player. If the distances are equal then tie breaking is done favoring left+up over right+down, but here's the disambiguation matrix to be explicit about it...lowest number to tie-break:
1 3 4
2 H 5
6 8 7
A beast never sleeps, but they are fortunately a bit slower than the player. They move every other turn (giving the player a head start by beginning their alternations on the second turn). They must move if a move is possible, regardless of if that takes them further from the player.
You crush a beast if you move a train of movable walls where it was sitting in a closed gap. These plain beasts are worth 2 points a head.
Input
A pair of integers indicating a map's size in columns then rows.
Row count of lines of input, each of the column size...containing either a solid wall (
#
), a movable wall (~
), a beast (H
), the player (O
) or just a space.Input that will be either U, D, L, R indicating an attempted move by the player... or W to just wait. Note that attempting to push a movable wall which is blocked is legal input, it just will result in no action.
Output
aHHHH!
if the beasts killed the player...or nothing if the player won with no beasts remainingThe score
(Note: For debugging purposes and/or amusement, you'll probably want to be able to output the state at each step; but that's too long to post here.)
Clarifications
Maps are guaranteed to be bounded by solid walls.
The order of who moves in a turn matters for the outcome. Thus: The player always goes first, then the beasts are given an ordering based on their initial map position if you were sweeping across the screen top to bottom left to right. (A row 1 beast moves before a row 2 beast, and two beasts on the same row it would be the one with the lowest column number that would move before the other)
Diagonal moving beasts can move into any open adjacent diagonal space, regardless of if it requires squeezing between walls.
A player can push any number of movable walls in a line provided that there's a space or a beast on the other end. But trying to push a train of walls into a Beast that is not pinned between walls treats the Beast as a wall, and won't allow the move.
A Beast's move decision during a turn is based on the player's location at the beginning of the turn. Its desired optimization of "distance to player" is through an "as the crow flies" calculation. Any approximation which would give the same result as measured from the center of its square to the center of the player's square is fine.
If a Beast can't make what would have been its first preferred move in a turn because a higher priority Beast took its spot, it will take its next best choice as opposed to staying in place (if a move is still possible).
Sample Cases
Simple Crush
Input
5 3
#####
#O~H#
#####
R
Output
2
Preference Matrix -> Death
Input
5 5
#####
#O #
# ~ #
# H#
#####
WWDW
Output
aHHHH!
0
Preference Matrix -> Win
Input
5 5
#####
#O #
# ~ #
# H#
#####
WRD
Output
2
Waiting For the Reaper
Input
5 5
#####
#O #
# ~ #
# H#
#####
WWW
Output
aHHHH!
0
Successful Defeat in the Wikipedia Scenario
Input
40 23
########################################
#~ ~ ~~ ~ ~ ~~ ~ ~~ ~ ~ ~~ #
#~~ ~ ~~ ~ ~ ~~ ~ #
#~# ~~ ~~~~ ~ ~~~~ ~ ~~~ ~#
# ~ ~ ~ ~~ #~~ ~ #
#~~ ~~~ ~ ~ ~ ~ ~~ ~ ~ ~ ~ #
# ~~ ~ ~ ~ ~~~ H ~ #~ #
# O~ ~ # ~~~ ~ ~ ~~ ~ ~ ~~ #
# ~ ~H~~ ~~ ~ # ~~ ~ #
# ~~ ~ ~~~ ~~ ~~~~ ~ ~#
#~ ~ ~~~ ~ ~ ~ ~ ~~ ~~#
# ~ # ~ ~~ ~~~ ~ ~ ~ # ~#
#~ ~ ~~ ~ ~ H ~~ ~~ ~ ~ ~~~ #
# ~ ~ ~ ~ ~ ~~~ ~ #
# ~~ ~ ~ ~~ ~ ~ ~ ~ ~ #
# ~~ ~ ~ ~ ~ ~~ #
#~ ~ # ~~~~ ~ ~~~H # ~ #
# ~ ~ ~ ~ ~ ~~ ~ #
# ~ ~ #~ ~ ~~ ~ ~ ~#
# ~~ ~ ~ ~~ ~ ~ #
# ~~~ ~ ~~ ~ ~ ~ ~ #
# ~ ~ ~ ~~ ~ ~ ~ #
########################################
RRRUWWWRRRURWWWWRDRRWWRDWWWWD
Output
8
Map provided by me, moves and output by @bobbel, corroborated by myself and @Allbeert.
Winning Criteria
I think this is golfable, so I'll go with code golf rules unless people complain.
Extra Credit
Playable Unicode implementation with the double-wide characters to resemble the image!