Minimal NetHack

64

10

NetHack is a roguelike game where a player must retrieve the Amulet of Yendor from the lowest level of the dungeon. Commonly played via telnet, the entire game is represented with ASCII graphics. The game is extremely challenging and requires knowledge of many game mechanics in order to succeed.

For the purposes of this challenge, assume that the entire dungeon is a single level and only 5×16 characters. Furthermore, assume that this is a "safe" dungeon or that you are only implementing a prototype—there will be no monsters, concerns about hunger, etc. In fact, you must only track the location of the character and the amulet and the game will effectively end when the player arrives at the same location as the amulet.

Challenge requirements

  • There will be a 5×16 dungeon (single level).
  • Give the player a starting location (optionally random) and the amulet a separate random (different each time the program is run) starting square inside the dungeon. That is, the amulet is not allowed to start on the same square as the player.
  • Accept four input keys which move the player one square at a time (four cardinal directions). Reading/processing other input is allowed (a readline() function that requires pressing 'enter', etc).
  • Travelling outside the bounds of the dungeon is not allowed. E.g., if the player is on the right edge of the dungeon pressing right should do nothing.
  • After initial generation and after each movement, print the state of the game. As this is code golf and printing is rather uninteresting, ignore the character count for the print function and function call assuming no state changes. Empty cells should be shown as period (.), amulet as double quote (") and character as at symbol (@).
  • The game is over when the player "discovers" the amulet (arrives at the same square)

Winning

This is a code golf challenege, the shortest code to meet the requirements one week from today will be declared winner.

Example

Here is an example solution in C# (ungolfed) to show basic requirements and sample output.

using System;

namespace nh
{
    class Program
    {
        static Random random = new Random();

        // player x/y, amulet x/y
        static int px, py, ax, ay;

        static void Main(string[] args)
        {
            px = random.Next(0, 16);
            py = random.Next(0, 5);

            // amulet starts on a position different from the player
            do { ax = random.Next(0, 16); } while (px == ax);
            do { ay = random.Next(0, 5); } while (py == ay); 

            print();

            do
            {
                // reads a single keypress (no need to press enter)
                // result is cast to int to compare with character literals
                var m = (int)Console.ReadKey(true).Key;

                // Move the player. Here standard WASD keys are used.
                // Boundary checks for edge of dungeon as well.
                if (m == 'W')
                    py = (py > 0) ? py - 1 : py;
                if (m == 'S')
                    py = (py < 5) ? py + 1 : py;
                if (m == 'A')
                    px = (px > 0) ? px - 1 : px;
                if (m == 'D')
                    px = (px < 16) ? px + 1 : px;

                // print state after each keypress. If the player doesn't
                // move this is redundant but oh well.
                print();

            // game ends when player is on same square as amulet
            } while (px != ax || py != ay);
        }

        static void print()
        {
            Console.Write('\n');
            for (int y=0; y<5; y++)
            {
                for (int x = 0; x < 16; x++)
                {
                    if (x == px && y == py)
                        Console.Write('@');
                    else if (x == ax && y == ay)
                        Console.Write('"');
                    else
                        Console.Write('.');
                }
                Console.Write('\n');
            }
        }
    }
}

Total character count is 1474, but ignoring calls to the print function and its definition the final character count is 896.

Output when the program is run:

................
...."...........
..........@.....
................
................

Output (including above) after the 'a' key is pressed twice:

................
...."...........
..........@.....
................
................

................
...."...........
.........@......
................
................

................
...."...........
........@.......
................
................

user21677

Posted 2015-07-02T17:04:39.110

Reputation:

The spec says the amulet should be given a separate square, but the example doesn't enforce that. Is the example valid, or should it be enforced? – Geobits – 2015-07-02T17:11:45.677

10I have a feeling this will be of interest to @Doorknob. – Alex A. – 2015-07-02T17:15:32.390

Whoops, good catch, updated. There should be a check to make sure the amulet doesn't spawn on the player. – None – 2015-07-02T17:15:33.253

1What about buffering on stdin? A "non-blocking getchar" is very easy in some languages/environments and very hard in others. Is "press w and then enter" also OK? – Lynn – 2015-07-02T17:20:26.033

Should the position of the amulet be uniform over the non-player board locations? Or just "there must be a non-zero chance of it landing on any non-player tile"? Right now, "random" could be interpreted as "randomly on one of two squares", which might be easier to implement. – Lynn – 2015-07-02T17:25:23.790

Yes, that's fine. I was using Console.Read() but that was cluttering up my C# example by having to ignore newlines. – None – 2015-07-02T17:26:16.480

As far as random, the intent is that it's different every time you run the game, not necessary some strict uniform distribution. Pulling bits from the current time would work just as well. – None – 2015-07-02T17:28:12.860

Are we allowed to take input as a string of moves, e.g. "WSSAAWD"? Should we allow for the player running off an edge and compensate by sending them to the opposite side, or just prevent movement? – Kade – 2015-07-02T17:56:58.900

@Vioz- I don't see why a string of input shouldn't be allowed. If the character were to run out of bounds that should just be prevented, e.g., on the right edge pressing right should do nothing. – None – 2015-07-02T18:04:36.340

@tolos: Would you mind taking a look at my Julia answer and letting me know whether my scoring method is valid? – Alex A. – 2015-07-02T18:41:43.547

10Rogue is the original roguelike game where a player must retrieve the Amulet of Yendor from the lowest level of the dungeon. Why not call this a minimal Rogue? – Gilles 'SO- stop being evil' – 2015-07-02T19:10:45.487

@Gilles I've never played Rogue but I've spent a few hours with NetHack – None – 2015-07-02T19:29:11.203

5@Gilles Why not call this a minimal snake? – Casey Kuball – 2015-07-02T22:03:13.317

26Psshh, no diagonal movement? No yubnhjkl? Not even an up staircase to climb after getting the amulet? :P (furiously upvotes anyway) – Doorknob – 2015-07-02T22:43:47.910

2

@tolos: I'm still unclear what counts as random here. It's not really possible to fulfill the different each time the program is run requirement if the program is run 80 times... Specifically, in this answer, the amulet can occupy only 9 of all possible 79 locations. Does that count?

– Dennis – 2015-07-02T22:59:17.547

Give the player a starting location (optionally random) Does this mean that we can choose to make the player start in the same place every time, as long as the amulet's position is random? – M. I. Wright – 2015-07-02T23:46:53.693

1@Dennis I'm trying to specify the amulet should start somewhere other than the player and you shouldn't be able to, say, "move right 4, up 3" every single time to "beat" the game. Don't know a better way to word that. – None – 2015-07-03T00:34:54.117

@M.I.Wright Yes. I believe the python solution by Mauris does just that. – None – 2015-07-03T00:38:17.570

@tolos: Can I assume Caps Lock is on? I'd like to use uppercase letters for input. – Dennis – 2015-07-03T04:42:31.520

1It should be nice.to have a challenge asking for a little more complex roguelike game. – edc65 – 2015-07-03T14:00:02.777

@Dennis That's fine. I was trying to leave input requirements a bit open and glad to see answers like the TI-BASIC submission by Thomas Kwa. – None – 2015-07-03T15:40:08.023

1

Related: http://codegolf.stackexchange.com/questions/375/build-an-engine-for-a-maze-game

– dmckee --- ex-moderator kitten – 2015-07-04T02:39:38.760

Answers

37

TI-BASIC, 42 41 38 36 35 bytes

For your TI-83 or 84+ series graphing calculator.

int(5irand→A                          //Randomize amulet position
6log(ie^(6→C                          //15.635 + 4.093i
Repeat Ans=A                          //Ans holds the player pos. (starts bottom right)
iPart(C-iPart(C-Ans-e^(igetKey-i      //Boundary check, after adjusting player position
prgmDISPLAY
End

----------
PROGRAM:DISPLAY
For(X,0,15
For(Y,0,4
Output(Y+1,X+1,".
If A=X+Yi
Output(Y+1,X+1,"¨
If Ans=X+Yi
Output(Y+1,X+1,"@
End
End

Which direction the player will go is a function of the key code of the key pressed, but four keys that definitely work are these on the top row:

Key        [Y=]  [WINDOW]  [ZOOM]  [TRACE]  [GRAPH]
           -------------------------------------------
Key code    11      12       13               15
Direction  Left     Up     Right             Down

The amulet starts on one of the five squares in the first column, and the player starts at the bottom right square. For example, a possible arrangement is:

................
¨...............
................
................
...............@

Explanation

The player's position is stored as a complex number from 0+0i to 15+4i, where the real part goes to the right and the imaginary part goes down. This facilitates easy boundary checking on the top and left: we simply offset the number slightly and round towards zero. For example, if the offset is 0.5 and our position is -1+3i (off the screen to the left), then the position will be corrected to iPart(-0.5+3.5i)=0+3i, where it should be. Checking for the bottom and right boundaries is slightly more complicated; we need to subtract the number from a constant C, which is about 15.635 + 4.093i (it's the shortest one I could find between 15+4i and 16+5i), round, subtract from C again to flip the number back, and round again.

When a key is pressed, the unadjusted player position will move by 1 unit in some direction, but the integer part only changes when certain keys are pressed. Luckily, the keys that work are all on the top row. Below is a graph of the offsets in cases where keys 11, 12, 13, and 15 are pressed, and when no key is pressed (No press is the point inside the center square, causing the integer parts to be unchanged; the four keypresses' offsets have different integer parts). C is the red cross at the center of the circle.

enter image description here

Old code (42 bytes):

int(9irand→A                     // 0≤rand≤1, so int(9irand) = i*x where 0≤x≤8
1                                //set "Ans"wer variable to 1+0i
Repeat Ans=A                     
Ans-iPart(i^int(48ln(getKey-1    //add -i,-1,i,1 for WASD respectively (see rev. history)
Ans-int(Ans/16+real(Ans/7        //ensure player is inside dungeon
prgmDISPLAY                      //display on top 5 rows of the homescreen   
                                 //(for this version switch X+Yi with Y=Xi in prgmDISPLAY)
End

Limitations

There is no way to escape a " character, so strings with a " cannot be generated inside a program. Therefore, this uses the umlaut mark ¨ instead of a quote (if there were an already-existing string with a quote mark, I could display that). To get ¨ and @ in a program, an outside tool is required; however, it is valid TI-BASIC.

lirtosiast

Posted 2015-07-02T17:04:39.110

Reputation: 20 331

2

Just for fun, I made this snippet that puts the diaeresis and @ symbol in Str1 (as opposed to 'an outside tool'). Pick the line at the top that matches your calculator, enter it into a new program, and run that program with Asm(.

– M. I. Wright – 2015-07-04T01:22:29.850

Oh, I forgot about coding in machine code on the calculator. Don't' want to crash mine with a mistype, but I trust that it works. – lirtosiast – 2015-07-04T22:43:44.933

44

CHIP-8, 48 bytes

This may not be considered legal, but why the hell not. I wrote my program in CHIP-8, a bytecode-based programming language for a virtual game console. You can try the complete program (99 bytes) in your browser using an emulator/debugger I wrote called Octo:

Screenshot

http://johnearnest.github.io/Octo/index.html?gist=1318903acdc1dd266469

A hex dump of that complete program is as follows:

0x60 0x14 0x61 0x04 0xC4 0x3C 0xC5 0x08
0x22 0x36 0xF6 0x0A 0x22 0x52 0x40 0x00
0x12 0x16 0x46 0x07 0x70 0xFC 0x40 0x3C
0x12 0x1E 0x46 0x09 0x70 0x04 0x41 0x00
0x12 0x26 0x46 0x05 0x71 0xFC 0x41 0x10
0x12 0x2E 0x46 0x08 0x71 0x04 0x22 0x52
0x3F 0x01 0x12 0x0A 0x00 0xFD 0xA2 0x58
0xD4 0x54 0x22 0x52 0x62 0xFF 0xA2 0x5B
0xD2 0x34 0x72 0x04 0x32 0x3F 0x12 0x40
0x62 0xFF 0x73 0x04 0x33 0x14 0x12 0x40
0x00 0xEE 0xA2 0x5F 0xD0 0x14 0x00 0xEE
0xA0 0xA0 0x40 0x00 0x00 0x20 0x00 0xF0
0x90 0x90 0xD0

You can move the player with the ASWD keys, or the 7589 keys on the original CHIP-8 keypad. If I remove all the code and data for drawing the background and the player, I instead get this 48 byte dump:

0x60 0x14 0x61 0x04 0xC4 0x3C 0xC5 0x08
0xF6 0x0A 0x40 0x00 0x12 0x12 0x46 0x07
0x70 0xFC 0x40 0x3C 0x12 0x1A 0x46 0x09
0x70 0x04 0x41 0x00 0x12 0x22 0x46 0x05
0x71 0xFC 0x41 0x10 0x12 0x2A 0x46 0x08
0x71 0x04 0x3F 0x01 0x12 0x08 0x00 0xFD

The ungolfed, complete form of the program was written in a high level assembly language as follows:

:alias px v0
:alias py v1
:alias tx v2
:alias ty v3
:alias ax v4
:alias ay v5
:alias in v6

: main
    px := 20
    py := 4
    ax := random 0b111100
    ay := random 0b001000
    draw-board
    loop
        in := key
        draw-player
        if px != 0 begin
            if in == 7 then px += -4
        end
        if px != 0x3C begin
            if in == 9 then px +=  4
        end
        if py != 0 begin
            if in == 5 then py += -4
        end
        if py != 16 begin
            if in == 8 then py +=  4
        end
        draw-player
        if vf != 1 then
    again
    exit

: draw-board
    i := amulet
    sprite ax ay 4
    draw-player
    tx := -1
    i := ground
    : draw
    loop
        sprite tx ty 4
        tx += 4
        if tx != 63 then jump draw
        tx := -1
        ty += 4
        if ty != 20 then
    again
;

: draw-player
    i := player
    sprite px py 4  
;

: amulet  0xA0 0xA0 0x40
: ground  0x00 0x00 0x20 0x00
: player  0xF0 0x90 0x90 0xD0

Note that the compiled bytes themselves are the CHIP-8 programming language; the assembler is simply a more convenient means of composing such programs.

JohnE

Posted 2015-07-02T17:04:39.110

Reputation: 4 632

19Right tool for the job. – Dennis – 2015-07-02T19:24:14.253

6+1 for making me waste a bunch of time playing your game over and over. – Alex A. – 2015-07-02T19:48:16.193

4

@AlexA. if you want to waste even more time you should try Cave Explorer. ASWD move in the overworld and QE are used to reset/move blocks in the platformer levels.

– JohnE – 2015-07-02T20:05:26.003

4Great, well there goes my weekend. – Alex A. – 2015-07-02T20:11:36.593

1I was skeptical at first, but Cave Explorer was fun and CHIP-8 has been around a lot longer than I would have guessed. So I guess I'll have to learn this. – None – 2015-07-02T21:34:23.803

1

@tolos: the Octo repository on github has lots of documentation to get you started. Drop me a line if you write anything cool!

– JohnE – 2015-07-02T21:44:43.850

10

Python 3, 86 bytes

def d():
    import sys
    for y in range(5):
        line = []
        for x in range(16):
            line.append('@' if y*16+x == p else \
                        '"' if y*16+x == a else \
                        '.')
        print(''.join(line))
    print()
    sys.stdout.flush()

p=79;a=id(9)%p
while p-a:d();p+=[p%16<15,16*(p<64),-(p%16>0),-16*(p>15)][ord(input())%7%5]

Only counting the bottom two lines, and dropping d();.

Lynn

Posted 2015-07-02T17:04:39.110

Reputation: 55 648

I saved another byte by starting the player in the bottom-right corner (the "last" square), then randomly sampling from the first 79 squares. – Lynn – 2015-07-02T18:35:32.877

Oops, sorry, fixed! I guess I'm not amazing at manually counting bytes. :< – Lynn – 2015-07-02T18:39:28.017

1I think you can save another character by replacing a=id(9)%79 with a=id(9)%p. – kirbyfan64sos – 2015-07-02T20:00:16.563

@kirbyfan64sos Brilliant! Thanks. – Lynn – 2015-07-02T20:13:31.870

1Also, if you make this for Python 3, you can change the raw_input call to just input. – kirbyfan64sos – 2015-07-02T20:23:30.443

Oh, I'm mixing Python versions as-is anyway (print())! I'll just make it Python 3. – Lynn – 2015-07-02T20:43:04.570

You may be able to save some bytes if you store y*16+x in a variable – Levi – 2015-07-03T22:29:48.563

@Levi: the entire function d() isn't part of the code golf. (y*16+x is precisely what p represents, though.) – Lynn – 2015-07-03T22:31:38.930

10

C, 122 121 115 104 102 101 bytes

#define o ({for(int t=0;t<80;++t)t%16||putchar(10),putchar(t^p?t^a?46:34:64);})
p;main(a){for(a=1+time(0)%79;p^a;o,p+=(int[]){-16*(p>15),16*(p<64),-!!(p%16),p%16<15}[3&getchar()/2]);}

First time posting here ! I hope you like it :)

o is the printing, erm, function. Our brave hero can be moved around with 2, 4, 6 and 8, but beware of not sending any other input (no newlines !).

Update 1 : brought a and i into main's parameters.

Update 2 : OP having confirmed that a single string of input is OK, I got rid of scanf (which I used to skip the newline).

Update 3 : Used a compound array literal and modified the input layout. Program now goes haywire if you enter an invalid direction ;)

Update 4 : Noticed that the call to the printing function does not count. Took note to read rules more carefully.

Update 5 : one byte saved, thanks to Mikkel Alan Stokkebye Christia.

Quentin

Posted 2015-07-02T17:04:39.110

Reputation: 1 187

Could !!(p%16) be p%16>0? I don't remember my order of operations. – lirtosiast – 2015-07-02T22:33:49.087

@ThomasKwa it is, but that unary - can't help but stick to p, so parentheses are needed either way. The double-bang is just obfuscation :) – Quentin – 2015-07-02T22:35:37.767

@Quentin 3&getchar()/2 < getchar()/2-25 – Mikkel Alan Stokkebye Christia – 2015-07-03T10:45:45.027

@MikkelAlanStokkebyeChristia thank you :) – Quentin – 2015-07-03T17:04:20.403

9

CJam, 46 45 44 40 39 37 bytes

{'.80*W$Gb'"t1$Gb'@tG/W%N*oNo}:P;
5,G,m*:Dmr~{P_l~4b2fm.+_aD&!$_W$=!}gP];

The first line (defines a function that prints the current game state) and the P's on the second line (call that function) do not contribute to the byte count.

Both the starting position and the amulet's position are selected pseudo-randomly. The distribution is as uniform and the underlying PRNG allows.

Input is E, 6, 9 and B for Up, Down, Left and Right, with Caps Lock activated, followed by Enter.

Alternate version

{'.80*W$Gb'"t1$Gb'@tG/W%N*oNo}:P;
5,G,m*:Dmr~{P_ZYm*l~(=:(.+_aD&!$_W$=!}gP];

At the cost of four more bytes, the input format is improved significantly:

  • This version accepts 8, 2, 4 and 6 for Up, Down, Left and Right, which matches the corresponding arrow keys on the numpad.
  • It also accepts 7, 9, 1 and 3 for the corresponding diagonal moves.

Testing

Since the I/O is interactive, you should try this code with the Java interpreter.

Download the latest version and run the program like this:

java -jar cjam-0.6.5.jar nethack.cjam

To avoid pressing Enter after each key, and for in-place updates of the output, you can use this wrapper:

#!/bin/bash

lines=5
wait=0.05

coproc "$@"
pid=$!

head -$lines <&${COPROC[0]}

while true; do
    kill -0 $pid 2>&- || break
    read -n 1 -s
    echo $REPLY >&${COPROC[1]}
    printf "\e[${lines}A"
    head -$lines <&${COPROC[0]}
    sleep $wait
done

printf "\e[${lines}B"

Invoke like this:

./wrapper java -jar cjam-0.6.5.jar nethack.cjam

Main version

5,G,   e# Push [0 1 2 3 4] and [0 ... 15].
m*:D   e# Take the Cartesian product and save in D.
mr~    e# Shuffle and dump the array on the stack.
       e# This pushes 80 elements. We'll consider the bottommost the amulet's
       e# position and the topmost the player's.
{      e# Do:
  P    e#   Call P.
  _    e#   Copy the player's position.
  l~   e#   Read and evaluate one line of input.
       e#      "6" -> 6, "9" -> 9, "B" -> 11, "E" -> 14 
  4b   e#   Convert to base 4.
       e#     6 -> [1 2], 9 -> [2 1], 11 -> [2 3], 14 -> [3 2]
  2f-  e#   Subtract 2 from each coordinate.
       e#     [1 2] -> [-1 0], [2 1] -> [0 -1], [2 3] -> [0 1], [3 2] -> [1 0]
  .+   e#   Add the result to the copy of player's position.
  _aD& e#   Push a copy, wrap it in an array and intersect with D.
  !    e#   Logical NOT. This pushes 1 iff the intersection was empty.
  $    e#   Copy the corresponding item from the stack.
       e#     If the intersection was empty, the new position is invalid
       e#     and 1$ copies the old position.
       e#     If the intersection was non-empty, the new position is valid
       e#     and 0$ copies the new position.
  _W$  e#   Push copies of the new position and the amulet's position.
  =!   e#   Push 1 iff the positions are different.
}g     e# While =! pushes 1.
P      e# Call P.
];     e# Clear the stack.

Alternate version

ZYm*   e# Push the Cartesian product of 3 and 2, i.e.,
       e#   [[0 0] [0 1] [0 2] [1 0] [1 1] [1 2] [2 0] [2 1] [2 2]].
l~     e#   Read and evaluate one line of input.
(=     e# Subtract 1 and fetch the corresponding element of the array.
:(     e# Subtract 1 from each coordinate.

Function P

'.80*  e# Push a string of 80 dots.
W$Gb   e# Copy the amulet's position and convert from base 16 to integer.
'"t    e# Set the corresponding character of the string to '"'.
1$Gb   e# Copy the player's position and convert from base 16 to integer.
'@t    e# Set the corresponding character of the string to '@'.
G/     e# Split into chunks of length 16.
W%     e# Reverse the chunks (only needed for the alternate program).
N*     e# Join the chunks, separating by linefeeds.
oNo    e# Print the resulting string and an additional linefeed.

Dennis

Posted 2015-07-02T17:04:39.110

Reputation: 196 637

2I'm right on your tail with TI-BASIC! I'll post once I verify my solution. – lirtosiast – 2015-07-02T21:00:12.847

8

Java, 231 bytes (196 if function)

Here's the full program code at 342:

class H{public static void main(String[]a){int p=0,y=79,c;y*=Math.random();y++;p(p,y);do p((p=(c=new java.util.Scanner(System.in).next().charAt(0))<66&p%16>0?p-1:c==68&p%16<15?p+1:c>86&p>15?p-16:c==83&p<64?p+16:p),y);while(p!=y);}static void p(int p,int y){for(int i=0;i<80;i++){System.out.print((i==p?'@':i==y?'"':'.')+(i%16>14?"\n":""));}}}

Without the print function, 231:

class H{public static void main(String[]a){int p=0,y=79,c;y*=Math.random();y++;p(p,y);do p((p=(c=new java.util.Scanner(System.in).next().charAt(0))<66&p%16>0?p-1:c==68&p%16<15?p+1:c>86&p>15?p-16:c==83&p<64?p+16:p),y);while(p!=y);}}

If just a function is okay (I'm unclear from the spec), then I can cut this down a bit further to 196:

void m(){int p=0,y=79,c;y*=Math.random();y++;p(p,y);do p((p=(c=new java.util.Scanner(System.in).next().charAt(0))<66&p%16>0?p-1:c==68&p%16<15?p+1:c>86&p>15?p-16:c==83&p<64?p+16:p),y);while(p!=y);}

And with some line breaks for a bit of clarity...

class H{
    public static void main(String[]a){
        int p=0,y=79,c;
        y*=Math.random();
        y++;p(p,y);
        do 
            p(
                (p=(c=new java.util.Scanner(System.in).next().charAt(0))<66&p%16>0?
                    p-1:
                    c==68&p%16<15?
                        p+1:
                        c>86&p>15?
                            p-16:
                            c==83&p<64?
                                p+16:
                                p)
                ,y);
        while(p!=y);
    }

    static void p(int p,int y){
        for(int i=0;i<80;i++){
            System.out.print((i==p?'@':i==y?'"':'.')+(i%16>14?"\n":""));
        }
    }
}

Note that I'm not counting the print function p(p,y) itself, but I am counting the call to it, since I have stuff changing inside the call statement.

It works with capital letters ASDW. Due to the way it checks for those, some other letters might also work, but the spec doesn't really say anything about what should happen if I press different keys.

Geobits

Posted 2015-07-02T17:04:39.110

Reputation: 19 061

I prefer formatting nested ternary operators at the same level of indentation, as you would a chain of if/else if. It's more readable. – lirtosiast – 2015-07-02T17:51:48.763

@ThomasKwa Yea, I've done it both ways at times. This is more like nested if statements than chained. It feels better to me because both halves of each ternary are at the same level, but distinct from the others. – Geobits – 2015-07-02T17:54:04.063

If an anonymous function is acceptable you can trim your answer to 192 bytes: void m() becomes ()-> – ankh-morpork – 2015-07-04T11:49:33.957

@dohaqatar7 Yea, but I don't use Java's anon functions for code golf. It feels shady to me on principle, whether or not others feel the same. – Geobits – 2015-07-04T13:51:58.060

Can you do p+=? – lirtosiast – 2015-07-04T22:34:30.237

5

Java, 574 bytes

import java.util.*;public class N{static Random r=new Random();static int v,b,n,m;public static void main(String[] a){v=r.nextInt(16);b=r.nextInt(5);n=r.nextInt(16);m=r.nextInt(5);p();do{Scanner e=new Scanner(System.in);char m=e.next().charAt(0);if(m=='w')b=b>0?b-1:b;if(m=='s')b=b<5?b+1:b;if(m=='a')v=v>0?v-1:v;if(m=='d')v=v<16?v+1:v;p();}while(v!=n || b!=m);}static void p(){System.out.println();for(int y=0;y<5;y++){for(int x=0;x<16;x++){if(x==z && y==x)System.out.print('@');else if(x==n && y==m)System.out.print('"');else System.out.print('.');}System.out.println();}}}

Basically the same as the C# version, except obfuscated & minimized.

phase

Posted 2015-07-02T17:04:39.110

Reputation: 2 540

so many unnecessary spaces... ;) – Will – 2015-07-02T17:30:45.923

@Will I was just fixing that :P – phase – 2015-07-02T17:32:17.300

1Also, use one-letter names and ternaries. – lirtosiast – 2015-07-02T17:32:24.100

@ThomasKwa fixed :D – phase – 2015-07-02T17:35:53.750

In the C# solution, you could save a few character by declaring variables local to main (without static keyword). And I'd change the conditionals to something like px += (px < 16) ? 1 : 0; – None – 2015-07-02T17:36:32.993

6There are still lots of unnecessary spaces and lack of ternaries ;) – Will – 2015-07-02T17:40:44.703

5

Julia, 161 bytes

Uses w, a, s, and d to move up, left, down, and right, respectively.

Full code, including printing (330 bytes):

B=fill('.',5,16)
a=[rand(1:5),rand(1:16)]
B[a[1],a[2]]='"'
c=[1,a==[1,1]?2:1]
B[c[1],c[2]]='@'
for i=1:5 println(join(B[i,:]))end
while c!=a
B[c[1],c[2]]='.'
m=readline()[1]
c[2]+=m=='a'&&c[2]>1?-1:m=='d'&&c[2]<16?1:0
c[1]+=m=='w'&&c[1]>1?-1:m=='s'&&c[1]<5?1:0
m∈"wasd"&&(B[c[1],c[2]]='@')
for i=1:5 println(join(B[i,:]))end
end

Scored code, excludes printing (161 bytes):

a=[rand(1:5),rand(1:16)]
c=[1,a==[1,1]?2:1]
while c!=a
m=readline()[1]
c[2]+=m=='a'&&c[2]>1?-1:m=='d'&&c[2]<16?1:0
c[1]+=m=='w'&&c[1]>1?-1:m=='s'&&c[1]<5?1:0
end

The difference here is that we don't save the game state as a matrix; all relevant information is contained in the arrays c and a. And, of course, nothing is printed. The user will no longer be prompted for input once the player reaches the amulet.


Ungolfed + explanation (full code):

# Initialize a 5x16 matrix of dots
B = fill('.', 5, 16)

# Get a random location for the amulet
a = [rand(1:5), rand(1:16)]

# Put the amulet in B
B[a[1], a[2]] = '"'

# Start the player in the upper left unless the amulet is there
c = [1, a == [1,1] ? 2 : 1]

# Put the player in B
B[c[1], c[2]] = '@'

# Print the initial game state
for i = 1:5 println(join(B[i,:])) end

# Loop until the player gets the amulet
while c != a

    # Put a dot in the player's previous location
    B[c[1], c[2]] = '.'

    # Read a line from STDIN, take the first character
    m = readline()[1]

    # Move the player horizontally within the bounds
    if m == 'a' && c[2] > 1
        c[2] -= 1
    elseif m == 'd' && c[2] < 16
        c[2] += 1
    end

    # Move the player vertically within the bounds
    if m == 'w' && c[1] > 1
        c[1] -= 1
    elseif m == 's' && c[1] < 5
        c[1] += 1
    end

    # Set the player's new location in B
    if m ∈ "wasd"
        B[c[1], c[2]] = '@'
    end

    # Print the game state
    for i = 1:5 println(join(B[i,:])) end

end

Alex A.

Posted 2015-07-02T17:04:39.110

Reputation: 23 761

I think that's fine – None – 2015-07-03T00:41:43.560

3+1 for the compressed/obfuscated version looking very similar to what I remember of the actual nethack source code. – Ben Jackson – 2015-07-03T16:34:49.287

You can save a few bytes by using worse randomization: a=[rand(1:5),1] c=a+1 – lirtosiast – 2015-07-03T23:55:10.587

@ThomasKwa: What fun is that if it's always on the first line? :) – Alex A. – 2015-07-04T00:01:51.033

3

Batch, 329 Bytes

@echo off
set e=goto e
set f= set/a
%f%a=0
%f%b=0
%f%c=%random%*3/32768+1
%f%d=%random%*16/32768+1
:et
call:p %a% %b% %c% %d%
if %a%%b% EQU %c%%d% exit/b
choice/C "wasd"
goto %errorlevel%
:1
%f%a-=1
%e%
:2
%f%b-=1
%e%
:3
%f%a+=1
%e%
:4
%f%b+=1
:e
if %a% GTR 4%f%a=4
if %a% LSS 0%f%a=0
if %b% GTR 15%f%b=15
if %b% LSS 0%f%b=0
%e%t

:p
setlocal enabledelayedexpansion
::creating a new line variable for multi line strings
set NL=^


:: Two empty lines are required here
cls
set "display="
for /l %%r in (0,1,4) do (
    set "line="
    for /l %%c in (0,1,15) do (
        set "char=."
        if %3 EQU %%r (
            if %4 EQU %%c (
                set char="
            )
        )
        if %1 EQU %%r (
            if %2 EQU %%c (
                set "char=@"
            )
        )
        set "line=!line!!char!"
    )
    set "display=!display!!line!!NL!"
)
echo !display!
exit /b

ankh-morpork

Posted 2015-07-02T17:04:39.110

Reputation: 1 350

This is highly impressive. I'm surprised it's possible to do this. – eis – 2015-07-06T07:39:16.193

This doesn't display the dungeon for me, only a series of lines prompting [W,A,S,D]?. It does appear to "work" - walking the undisplayed dungeon eventually exits. win7 cmd.exe – Dan Pritts – 2015-07-06T15:01:14.907

It works for me witn Win7 cmd.exe - when I type ver I get Microsoft Windows [Version 6.1.7601] – Jerry Jeremiah – 2015-07-07T12:21:45.413

@DanPritts That's odd. It works perfectly for me on Windows 8 (Microsoft Windows [Version 6.2.9200]) – ankh-morpork – 2015-07-07T13:54:27.330

doh, i hadn't copied & pasted the whole thing, i didn't scroll down in the window. Well done. – Dan Pritts – 2015-07-07T15:50:23.307

3

Perl, 228 222 characters (not counting the newlines that are not integral to the working of the code) — 207 if not counting the print and print if statement parts which are used for printing, but don't add to the game logic; 144 if also considering the field representation generation code as part of printing, as suggested by Yakk in the comments)

This code uses lowercase wasd for control; input must be confirmed with Enter. Tested with Perl 5.14.2.

($a=$==rand(79))+=($a>=($==rand(80)));
print $_=("."x80)=~s/(.{$=})./\1@/r=~s/(.{$a})./\1"/r=~s/(.{16})/\1\n/gr;
%r=qw(w s/.(.{16})@/@\1./s a s/.@/@./ s s/@(.{16})./.\1@/s d s/@./.@/);
while(/"/){print if eval $r{getc STDIN}}

Note that for this code, it is impossible to separate calculation and printing, since the operations are done directly on the printed representation using regular expressions.

Explanation:

($a=$==rand(79))+=($a>=($==rand(80)));

This line determines the position of the player and the amulet. The player position is determined by $==rand(80) and is actually easy to understand: On a 5×16 board, there are 80 distinct positions where the player can be. The position is stored in the $= variable which forces the stored value into integer; this saves a few bytes for not needing to explicitly cast the result to integer (rand delivers a floating point value).

Since one of the positions is already occupied by the player, there are only 79 positions left for the amulet, therefore for the amulet's position, $a=$==rand(79) is used. Again, the assignment to $= forces a conversion to integer, however I further assign it to $a in order to reuse $= for the player's position.

Now to avoid the amulet to occupy the same position as the player, it is advanced by one position if its position is at least as large as the player's, giving an uniform distribution on the places not occupied by the player. This is achieved by $a = ($a >= $=) where $= here holds the player's position. Now the first line is generated by inserting the two initial assignments instead of the first $a$ and the only$=` in this expression.

print $_=("."x80)=~s/(.{$=})./\1@/r=~s/(.{$a})./\1"/r=~s/(.{16})/\1\n/gr;

This generates the initial field, and afterwards prints is. ("."x80) just generates a string of 80 dots. =~s/(.{$=})./\1@/r then replaces the $=th character with @, and =~s/(.{$=})./\1@/r the $ath character with ". Due to the r modifier they don't try to modify in place, but return the modified string, that's why they can be applied to the previous expressions. Finally, =~s/(.{16})/\1\n/gr inserts a newline every 16 characters. Note that the field is stored in the special variable $_ which can be used implicitly in later statements.

%r=qw(w s/.(.{16})@/@\1./s a s/.@/@./ s s/@(.{16})./.\1@/s d s/@./.@/);

This creates a hash containing the replacement rules for the different moves. A more readable version of this is

%r = ( 'w' => 's/.(.{16})@/@\1./s',
       'a' => 's/.@/@./',
       's' => 's/@(.{16})./.\1@/s',
       'd' => 's/@./.@/' );

The keys are the characters for the moves, and the values are strings containing the corresponding replacement rule.

while(/"/){print if eval"\$_=~$r{getc STDIN}"}

This is the main loop. while(/"/) checks if there is still a " character in $_ (that is, in the field). If we move onto the amulet, its character gets replaced with the player character so it disappears from the field.

eval $r{getc STDIN} reads a character from standard input, looks up the corresponding replacement rule from the has %r and applies it to $_, that is, the field. This evaluates to true if a replacement was actually made (that is, the key was found in the hash and the move was possible; an impossible move won't match in the replacement rule). In that case print is executed. Since it is called without argument, it prints $_, that is, the modified field.

celtschk

Posted 2015-07-02T17:04:39.110

Reputation: 4 650

1Just because you include linefeeds for readability doesn't mean you have to count them. I see 228 bytes. Also, per the specific rules of this question, the printing part of your code does not contribute to the byte count. – Dennis – 2015-07-04T22:37:42.567

@Dennis: For the printing part, see the explanation I've now added: You cannot meaningfully separate printing and evaluation in my code. I now changed the counting as you suggested. – celtschk – 2015-07-04T23:04:53.517

Are there any state changes in your printing code? No? Well, in my opinion, reusing your printing code's output for your logic shouldn't penalize you. The movement code (which is distinct!) should count, but the code that generates the "display string" shouldn't count. – Yakk – 2015-07-06T17:38:11.830

@Yakk: Which part of my code do you consider the printing code? (Actually my opinion is that exclusion of the printing code from counting was a bad idea, exactly because it is not always well defined what "printing code" is.) – celtschk – 2015-07-06T21:16:46.870

@celtschk ("."x80)=~s/(.{$=})./\1@/r=~s/(.{$a})./\1"/r=~s/(.{16})/\1\n/gr is pretty close at first glance, but my perl-fu is a few years rusty. I could have missed a state change there. – Yakk – 2015-07-06T21:18:38.037

@Yakk: No state change here (the regexes are all with r modifier, and the variables are only read, not written in that part). Anyway, I just noticed that in the example code, IIUC also the calls to the print function are not counted, therefore I think that the two print keywords, the if following the second print (that only controls the print itself but doesn't have an effect on the actual game logic) and the associated whitespace (that only separates those statements from the rest of the code) should not be counted. That would make 15 characters less. – celtschk – 2015-07-06T21:46:10.760

2

C#, 256 248 234 227 226 225 bytes

Uses the NumPad arrows with NumLock turned on to move.

Indented and commented for clarity:

using System;
class P{
    static void Main(){
        int a=0,b=0,c,d,e;
        var r=new Random();
        while(0<((c=r.Next(16))&(d=r.Next(5))));
        Draw(a,b,c,d); // Excluded from the score.
        while(a!=c|b!=d){
            e=Console.ReadKey().KeyChar-48;
            a+=e==4&a>0?-1:e==6&a<15?1:0;
            b+=e==8&b>0?-1:e==2&b<4?1:0;
            Draw(a,b,c,d); // Excluded from the score.
        }
    }
    // The following method is excluded from the score.
    static void Draw(int a, int b, int c, int d){
        Console.Clear();
        for (int y = 0; y < 5; y++)
        {
            for (int x = 0; x < 16; x++)
            {
                Console.Write(
                    x == a && y == b ? '@' :
                    x == c && y == d ? '"' :
                                       '.'
                );
            }
            Console.WriteLine();
        }
    }
}

Hand-E-Food

Posted 2015-07-02T17:04:39.110

Reputation: 7 912

1I think C# ints implicitly init to zero. Also (can't check at the moment) if casting isn't an issue you can convert character literals to ints, or at least the 'a' to 97 (I think) though the others are three digits. – None – 2015-07-03T02:54:18.773

Only class fields are initialised by default, and that requires them being declared static in this case. Method variables must be initialised before first use. That takes less characters: 4 vs 7. – Hand-E-Food – 2015-07-03T03:23:08.260

Thanks @tolos for the tip on implicitly casting char to int! Better yet, if I use the ConsoleKey enum cast as int, I can use 2-digit values. – Hand-E-Food – 2015-07-03T03:32:57.307

Technically, the Main method doesn't have to be called Main, so you could shave off another three characters. – Luaan – 2015-07-03T11:45:57.280

@Luaan, I think you're mistaken. C# documentation: https://msdn.microsoft.com/en-us/library/acy3edy3.aspx

– Hand-E-Food – 2015-07-03T13:16:56.577

@Hand-E-Food Yeah, it's noted there - you can use the /main compiler option to change the entry point. That's why I said "technically" - as far as the code is concerned, it doesn't have to be Main. You'll have to tell the compiler the proper main method, though. Main is just the default. – Luaan – 2015-07-03T13:28:47.563

2

Html+JavaScript(ES6), score maybe 217

Too looong, but playable online in the snippets below.

Line 6 (T.value ...) is for output and not counted (but for simplicity I counted the textarea open and close tags, even if it's output too)

As for randomness: the amulet is always in the right half of the grid and the player always start in the left half.

Click on the textArea (after enlarging it) to start and restart the game.

<textarea id=T onclick='s()' onkeyup='m(event.keyCode)'></textarea>
<script>
R=n=>Math.random()*n|0,
s=e=>m(y=R(5),x=R(8),a=R(5)*17+R(8)+8),
m=k=>(x+=(x<15&k==39)-(x>0&k==37),y+=(y<4&k==40)-(y>0&k==38),p=y*17+x,
T.value=p-a?(t=[...('.'.repeat(16)+'\n').repeat(5)],t[a]='X',t[p]='@',t.join('')):t='Well done!'
)
</script>

EcmaScript 6 Snippet (Firefox only)

R=n=>Math.random()*n|0
s=e=>m(y=R(5),x=R(8),a=R(5)*17+R(8)+8)
m=k=>(
  x+=(x<15&k==39)-(x>0&k==37),
  y+=(y<4&k==40)-(y>0&k==38),
  p=y*17+x,
  T.value=p-a?(t=[...('.'.repeat(16)+'\n').repeat(5)],t[a]='"',t[p]='@',t.join('')):t='Well done!'
)
<textarea id=T onclick='s()' onkeyup='m(event.keyCode)'></textarea>

EcmaScript 5 snippet (tested in Chrome)

function R(n) { return Math.random()*n|0 }

function s() { m(y=R(5),x=R(8),a=R(5)*17+R(8)+8) }

function m(k) {
  x+=(x<15&k==39)-(x>0&k==37)
  y+=(y<4&k==40)-(y>0&k==38)
  p=y*17+x
  T.value=p-a?(t=('.'.repeat(16)+'\n').repeat(5).split(''),t[a]='"',t[p]='@',t.join('')):t='Well done!'
}
<textarea id=T onclick='s()' onkeyup='m(event.keyCode)'></textarea>

edc65

Posted 2015-07-02T17:04:39.110

Reputation: 31 086

2

Actionscript 3: 267 bytes

A working example is online

var a:int,p:int,t;function g(){var r=Math.random;while(p==a){a=r()*80;p=r()*80}addEventListener("keyDown",function(e){if(a==p)return;if(e.keyCode==87&&p>15)p-=16if(e.keyCode==83&&p<64)p+=16if(e.keyCode==65&&p%16>0)p--if(e.keyCode==68&&(p+1)%16>0)p++print()});print()}

Here's a complete (whitespaces included for readability) program using the game function:

package
{
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFormat;

    public class MiniRogue extends Sprite
    {
        var a:int, p:int, t;

        public function MiniRogue()
        {
            g();
        }

        function g(){
            var r=Math.random;
            while(p==a){
                a=r()*80;
                p=r()*80
            }
            addEventListener("keyDown",function(e){
                if(a==p)
                    return;
                if(e.keyCode==87&&p>15)
                    p-=16
                if(e.keyCode==83&&p<64)
                    p+=16
                if(e.keyCode==65&&p%16>0)
                    p--
                if(e.keyCode==68&&(p+1)%16>0)
                p++
                print()
            });
            print()
        }

        var old:int = -1;
        private function print():void {
            if (!t) {
                t = new TextField()
                t.defaultTextFormat = new TextFormat("_typewriter", 8)
                t.width=500;
                t.height=375;
                addChild(t)
            }
            var board:String = "";
            for (var i:int=0; i<80;i++) {
                if (i == p) {
                    board += "@";
                } else if (i == a) {
                    board += '"';
                } else {
                    board += ".";
                }
                if ((i + 1) % 16 == 0) {
                    board += "\n";
                }
            }
            if (a==p) {
                board += "Win!";
            }
            if (p == old) {
                board += "Bump!";
            }
            old = p;
            t.text = board;
        }
    }
}

Brian

Posted 2015-07-02T17:04:39.110

Reputation: 231

2

Javascript: 307 216

You can play in the snippet below! The numbers on the left are just so the console (chrome one at least) doesn't merge the rows.

To run the code:

  1. hit "run code snippet"
  2. hit ctrl-shift-j to open the console
  3. click in the result section
  4. use arrow keys and play

var x=y=2,m=Math,b=m.floor(m.random()*5),a=14,i,j,t,c=console,onload=d;function d(){c.clear();for(i=0;i<5;i++){t=i;for(j=0;j<16;j++){t+=(i==y&&j==x)?"@":(i==b&&j==a)?'"':".";if(a==x&&b==y)t=":)";}c.log(t);}}onkeydown=function(){switch(window.event.keyCode){case 37:if(x>0)x--;break;case 38:if(y>0)y--;break;case 39:if(x<15)x++;break;case 40:if(y<4)y++;break;}d();};

Un-golfed:

var px=py=2,m=Math,ay=m.floor(m.random()*5),ax=14,i,j,t,c=console,onload=draw;
function draw() {
  c.clear();
  for(i=0;i<5;i++) {
    t=i;
    for(j=0;j<16;j++) {
      t+=(i==py&&j==px)?"@":
         (i==ay&&j==ax)?'"':".";
      if(ax==px&&ay==py)t=":)";
    }
    c.log(t);
  }
}
onkeydown=function() {
  switch (window.event.keyCode) {
    case 37:
      if(px>0)px--;
      break;
    case 38:
      if(py>0)py--;
      break;
    case 39:
      if(px<15)px++;
      break;
    case 40:
      if(py<4)py++;
      break;
  }
  draw();
};

Edit 1: Read rules more carefully and rewrote my code accordingly

  • amulet y value is now randomized
  • player can no longer escape room
  • I no longer count the characters in the draw function or calls to it

Eric Vincent

Posted 2015-07-02T17:04:39.110

Reputation: 171

1

SpecBAS - 428 402 (excluding printing, 466 425 when counted)

Uses Q/A/O/P to move up/down/left/right respectively.

The line to print the dungeon at line 1 is the only line that can be ignored, but have golfed that down a bit as well.

1 PRINT ("."*16+#13)*5
2 LET px=8: LET py=3
3 LET ax=INT(RND*16): LET ay=INT(RND*5): IF ax=px AND ay=py THEN GO TO 3
4 PRINT AT ay,ax;#34;AT py,px;"@": LET ox=px: LET oy=py: PAUSE 0: LET k$=INKEY$
5 LET px=px+(k$="p")-(k$="o")
6 IF px<0 THEN LET px=0
7 IF px>15 THEN LET px=15
8 LET py=py+(k$="a")-(k$="q")
9 IF py<0 THEN LET py=0
10 IF py>4 THEN LET py=4
11 PRINT AT oy,ox;"."
12 IF SCREEN$(px,py)<>#34 THEN GO TO 4

The reference to #34 is just a short-hand way of putting CHR$(34) in the code.

Thanks @Thomas Kwa, I hadn't noticed player start position being random was optional. Also used separate IF statements to shave off a few characters.

Brian

Posted 2015-07-02T17:04:39.110

Reputation: 1 209

You may be able to save some characters by randomizing less well :2 LET px=1: LET py=1: LET ax=2: LET ay=INT(RND*5) and also using IF instead of ELSE IF. – lirtosiast – 2015-07-05T01:17:08.417

1

Another C#, 221 171 170

Here is another way in C# with both positions random. Wanted to show this even if this part is 7 bytes longer than Hand-E-Food's solution.
Hand-E-Food's answer will be shorter of course as soon as he will use Console.Read().
The downside of Consol.Read is, that pressing the needed Enter causes the field to be printed 2 more times.
But I don't think there is a requirement to print just on (real) input.

Navigation is done by 8426 as in Hand-E-Foods solution.

using System;
class P
{
static void Main()
{
Func<int> n=new Random().Next;
int x=n()%16,y=n()%5,a=n()%16,b,m;
while(y==(b=n()%5));

while(x!=a|y!=b)
{
Printer.Print(a, b, x, y);  // Excluded from the score.
m=Console.Read()-48;
y+=m==8&y>0?-1:m==2&y<4?1:0;
x+=m==4&x>0?-1:m==6&x<15?1:0;
}
}
}


Edit: (added new solution and moved PrinterClass to the end)
Edit2: (changed a 14 to a 15 and saved the byte by starting on right-bottom)

Adapting the technique of Mauris it is possible to melt it down to 171 bytes in C# (of course now without both positions random):

using System;
class P
{
static void Main()
{
int p=79,a=new Random().Next()%p,m;
while(p!=a){
Printer.Print(p,a);  // Excluded from the score.
m=Console.Read()-48;
p+=m==4&p/5>0?-5:m==6&p/5<15?5:m==8&p%5>0?-1:m==2&p%5<4?1:0;
}
}
}

The Printer Class is nearly the same, just a new overload of print...

class Printer
{
    public static void Print(int ax, int ay, int px, int py)
    {
        Console.Write('\n');
        for (int y = 0; y < 5; y++)
        {
            for (int x = 0; x < 16; x++)
            {
                if (x == px && y == py)
                    Console.Write('@');
                else if (x == ax && y == ay)
                    Console.Write('"');
                else
                    Console.Write('.');
            }
            Console.Write('\n');
        }
    }

    public static void Print(int p, int a)
    {
        Print(p/5,p%5,a/5,a%5);
    }
}

Phil

Posted 2015-07-02T17:04:39.110

Reputation: 21

1

Ruby, 185

Here is a Ruby example too.
I'm very new to Ruby, maybe someone knows how to do that better :)

I have counted lineFeeds as 1 since the Program will crash otherwise...

Navigation is done by 8462. You need to send input each time with enter.

def display(ax,ay,px,py)
    puts
    for y in 0..4
        for x in 0..15
            if (x == px && y == py)
                print "@"
            elsif (x == ax && y == ay)
                print '"'
            else
                print '.'
            end
        end
        puts
    end
end


x=y=0
a=Random.rand(16) while y==(b=Random.rand(5))
while x!=a or y!=b
display(a,b,x,y)  # Excluded from the score.
m=gets.chomp.to_i
y-=m==8?1:0 if y>0
y+=m==2?1:0 if y<4
x-=m==4?1:0 if x>0
x+=m==6?1:0 if x<15
end

Phil

Posted 2015-07-02T17:04:39.110

Reputation: 21

0

QBasic, 103 bytes

Per the rules of the challenge, the Show subprogram is not included in the byte-count, nor is the Show p, q, a, b call (with the following newline).

b=1+TIMER MOD 9
1Show p, q, a, b
INPUT m
p=p-(m=2)*(p>0)+(m=4)*(p<4)
q=q-(m=1)*(q>0)+(m=3)*(q<15)
IF(p<>a)+(q<>b)GOTO 1


SUB Show (playerRow, playerCol, amuletRow, amuletCol)
CLS
FOR row = 0 TO 4
  FOR col = 0 TO 15
    IF row = playerRow AND col = playerCol THEN
      PRINT "@";
    ELSEIF row = amuletRow AND col = amuletCol THEN
      PRINT CHR$(34);    ' Double quote mark
    ELSE
      PRINT ".";
    END IF
  NEXT
  PRINT
NEXT
END SUB

To move, input a number and press Enter: 1 to go left, 2 to go up, 3 to go right, and 4 to go down.

This code does not output the game state at the end, when the player has found the amulet. To make it do so, add another Show p, q, a, b after the IF statement.

Explanation

Let a, b represent the coordinates of the amulet and p, q the coordinates of the player. The player starts at (0, 0), and the amulet starts on row 0, with a column between 1 and 9, inclusive, based on the 1's digit of the current time.

The rest is just a bunch of math with conditionals. The important thing to remember is that conditionals in QBasic return 0 for false, -1 for true. Let's look at the player row update statement:

p=p-(m=2)*(p>0)+(m=4)*(p<4)

If m=2, we want to move up by subtracting 1 from p, as long as p>0. Similarly, if m=4, we want to move down by adding 1 to p, as long as p<4. We can obtain the desired behavior by multiplying. If both factors are -1, their product will be 1, which we can subtract from or add to p. If either conditional is 0, the product will be 0, with no effect.

Similarly, the condition to determine whether the player has found the amulet is:

IF(p<>a)+(q<>b)GOTO 1

If either of the conditionals is true, their sum will be nonzero (either -1 or -2) and thus truthy, and the program returns to line 1. Once p equals a and q equals b, both conditionals will be 0, so their sum will be 0 and control flow can reach the end of the program.

DLosc

Posted 2015-07-02T17:04:39.110

Reputation: 21 213