16
3
Some text-based rogue-likes don't let you walk into walls, and give you your move back if you try to. Why do that when you can make the player move in the closest valid direction?
The challenge
Write a program of function that, given a direction and a 3 by 3 grid of characters, outputs the same grid after the player has taken a step.
For example,
9
#..
.@#
#.#
becomes
#.@
..#
#.#
Input
The direction is given by a single digit from 1 to 9, each corresponding to 8 cardinal directions and standing still. This is derived from the relative locations of the numbers on a keypad:
NW N NE ..\ | / ...7 8 9 W- 4 5 6 -E ...1 2 3 ../ | \ SW S SE
However, you may also use the numbers 123, 456, 789 instead of 789, 456, 123. In other words, you may swap the top and bottom 2 rows or numbers if you so choose. These are the only 2 acceptable index combinations.The 3 by 3 grid will consist of 3 distinct, printable ASCII characters representing walkable floor, walls, and the player. (In the test cases,
.
is used for floor,#
s are walls, and@
is the player)- You may choose what characters your program uses, but you must state them in your answer and they must be consistent over multiple trials.
- The character representing the character will always be in the middle of the 3 by 3 grid, and the direction will always be between 1 and 9 (incl.)
- You may take input in any order
- The 3 by 3 grid can be input as a char array, a string array, a 9 length string, or another reasonable alternative.
Output
- Return from a function, or output to StdOut or closest alternative
- Trailing spaces and newlines are allowed
- You must use the same character representations as the input
- The same formats allowed for inputs are allowed for outputs
How the player moves
If the specified direction is blocked by a wall (e.g. if the above example had direction 6), then look at the 2 closest directions:
- If one (and only one) direction is free, move the player in that direction.
- If no directions are free, look at the next closest 2 directions (excluding direction 5). If you have wrapped all the way around and found no open direction (player surrounded by walls), do not move the player
- If both of the directions are open, pick one to move to at random (although not necessarily uniformly).
If the given direction is a 5, do not move the player
Test cases
(#
= wall, .
= floor, @
= player)
Input: 9 #.. .@# #.# Output: #.@ ..# #.# Input: 3 #.. .@# #.# Output: #.. ..# #@# Input: 7 ##. #@# ..# Output: ##@ ##. #.# or #.# ..# @.# Input: 5 ... .@. ... Output: ... .@. ... Input: 2 ### #@# ### Output: ### #@# ###
Scoring
This is code-golf, so the shortest answer in bytes wins.
Not using
yuhjklbn
? RIP. – Rɪᴋᴇʀ – 2017-05-03T01:16:18.310Also, I don't really like the "pick an alternative direction" part as much. It's kinda different from the other part of the challenge, which is "move the player in a specified direction and output.". That's just personal preference though. – Rɪᴋᴇʀ – 2017-05-03T01:18:44.187
3 line string falls under "reasonable alternative", and is acceptable. Also fixed the returning from a function bit. – MildlyMilquetoast – 2017-05-03T01:21:31.203
1This part is confusing: "If no directions are free, look at the next closest 2 directions." – Leaky Nun – 2017-05-03T02:38:07.717
1Can we assign a number instead of an ASCII character for each element, then take input as a 2-dimensional list? Or do they have to be strings? – Scott Milner – 2017-05-03T03:09:27.310
2Can I use the directions
123;456;789
instead of789;456;123
? – Leaky Nun – 2017-05-03T03:14:57.893@LeakyNun Sure. – MildlyMilquetoast – 2017-05-03T04:40:42.497
If the random generation does not have to be uniform, can I just always pick the one counterclockwise to it? – HyperNeutrino – 2017-05-03T05:50:45.920
@HyperNeutrino No, there must be a >0 chance of each option – MildlyMilquetoast – 2017-05-03T05:51:56.503
Okay. Thanks for clarifying. – HyperNeutrino – 2017-05-03T06:03:29.653