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)


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


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); 


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

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

        static void print()
            for (int y=0; y<5; y++)
                for (int x = 0; x < 16; x++)
                    if (x == px && y == py)
                    else if (x == ax && y == ay)

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:



  • 10
    \$\begingroup\$ I have a feeling this will be of interest to @Doorknob. \$\endgroup\$ – Alex A. Jul 2 '15 at 17:15
  • 9
    \$\begingroup\$ Rogue 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? \$\endgroup\$ – Gilles Jul 2 '15 at 19:10
  • 5
    \$\begingroup\$ @Gilles Why not call this a minimal snake? \$\endgroup\$ – Casey Kuball Jul 2 '15 at 22:03
  • 25
    \$\begingroup\$ Psshh, no diagonal movement? No yubnhjkl? Not even an up staircase to climb after getting the amulet? :P (furiously upvotes anyway) \$\endgroup\$ – Doorknob Jul 2 '15 at 22:43
  • 2
    \$\begingroup\$ @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? \$\endgroup\$ – Dennis Jul 2 '15 at 22:59

18 Answers 18


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

If A=X+Yi
If Ans=X+Yi

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:



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)


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.

  • 2
    \$\begingroup\$ 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(. \$\endgroup\$ – M. I. Wright Jul 4 '15 at 1:22
  • \$\begingroup\$ 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. \$\endgroup\$ – lirtosiast Jul 4 '15 at 22:43

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:



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
        in := key
        if px != 0 begin
            if in == 7 then px += -4
        if px != 0x3C begin
            if in == 9 then px +=  4
        if py != 0 begin
            if in == 5 then py += -4
        if py != 16 begin
            if in == 8 then py +=  4
        if vf != 1 then

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

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

  • 19
    \$\begingroup\$ Right tool for the job. \$\endgroup\$ – Dennis Jul 2 '15 at 19:24
  • 6
    \$\begingroup\$ +1 for making me waste a bunch of time playing your game over and over. \$\endgroup\$ – Alex A. Jul 2 '15 at 19:48
  • 4
    \$\begingroup\$ @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. \$\endgroup\$ – JohnE Jul 2 '15 at 20:05
  • 4
    \$\begingroup\$ Great, well there goes my weekend. \$\endgroup\$ – Alex A. Jul 2 '15 at 20:11
  • 1
    \$\begingroup\$ I 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. \$\endgroup\$ – user21677 Jul 2 '15 at 21:34

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 \

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();.

  • \$\begingroup\$ I saved another byte by starting the player in the bottom-right corner (the "last" square), then randomly sampling from the first 79 squares. \$\endgroup\$ – Lynn Jul 2 '15 at 18:35
  • \$\begingroup\$ Oops, sorry, fixed! I guess I'm not amazing at manually counting bytes. :< \$\endgroup\$ – Lynn Jul 2 '15 at 18:39
  • 1
    \$\begingroup\$ I think you can save another character by replacing a=id(9)%79 with a=id(9)%p. \$\endgroup\$ – kirbyfan64sos Jul 2 '15 at 20:00
  • \$\begingroup\$ @kirbyfan64sos Brilliant! Thanks. \$\endgroup\$ – Lynn Jul 2 '15 at 20:13
  • 1
    \$\begingroup\$ Also, if you make this for Python 3, you can change the raw_input call to just input. \$\endgroup\$ – kirbyfan64sos Jul 2 '15 at 20:23

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);})

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.

  • \$\begingroup\$ Could !!(p%16) be p%16>0? I don't remember my order of operations. \$\endgroup\$ – lirtosiast Jul 2 '15 at 22:33
  • \$\begingroup\$ @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 :) \$\endgroup\$ – Quentin Jul 2 '15 at 22:35
  • \$\begingroup\$ @Quentin 3&getchar()/2 < getchar()/2-25 \$\endgroup\$ – Mikkel Alan Stokkebye Christia Jul 3 '15 at 10:45
  • \$\begingroup\$ @MikkelAlanStokkebyeChristia thank you :) \$\endgroup\$ – Quentin Jul 3 '15 at 17:04

CJam, 46 45 44 40 39 37 bytes


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


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.


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:



coproc "$@"

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

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.
  • 2
    \$\begingroup\$ I'm right on your tail with TI-BASIC! I'll post once I verify my solution. \$\endgroup\$ – lirtosiast Jul 2 '15 at 21:00

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;
                (p=(c=new java.util.Scanner(System.in).next().charAt(0))<66&p%16>0?

    static void p(int p,int y){
        for(int i=0;i<80;i++){

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.

  • \$\begingroup\$ 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. \$\endgroup\$ – lirtosiast Jul 2 '15 at 17:51
  • \$\begingroup\$ @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. \$\endgroup\$ – Geobits Jul 2 '15 at 17:54
  • \$\begingroup\$ If an anonymous function is acceptable you can trim your answer to 192 bytes: void m() becomes ()-> \$\endgroup\$ – ankh-morpork Jul 4 '15 at 11:49
  • \$\begingroup\$ @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. \$\endgroup\$ – Geobits Jul 4 '15 at 13:51
  • \$\begingroup\$ Can you do p+=? \$\endgroup\$ – lirtosiast Jul 4 '15 at 22:34

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.

  • \$\begingroup\$ so many unnecessary spaces... ;) \$\endgroup\$ – Will Jul 2 '15 at 17:30
  • \$\begingroup\$ @Will I was just fixing that :P \$\endgroup\$ – phase Jul 2 '15 at 17:32
  • 1
    \$\begingroup\$ Also, use one-letter names and ternaries. \$\endgroup\$ – lirtosiast Jul 2 '15 at 17:32
  • \$\begingroup\$ @ThomasKwa fixed :D \$\endgroup\$ – phase Jul 2 '15 at 17:35
  • 6
    \$\begingroup\$ There are still lots of unnecessary spaces and lack of ternaries ;) \$\endgroup\$ – Will Jul 2 '15 at 17:40

Julia, 161 bytes

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

Full code, including printing (330 bytes):

for i=1:5 println(join(B[i,:]))end
while c!=a
for i=1:5 println(join(B[i,:]))end

Scored code, excludes printing (161 bytes):

while c!=a

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

    # 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

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

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

  • \$\begingroup\$ I think that's fine \$\endgroup\$ – user21677 Jul 3 '15 at 0:41
  • 3
    \$\begingroup\$ +1 for the compressed/obfuscated version looking very similar to what I remember of the actual nethack source code. \$\endgroup\$ – Ben Jackson Jul 3 '15 at 16:34
  • \$\begingroup\$ You can save a few bytes by using worse randomization: a=[rand(1:5),1] c=a+1 \$\endgroup\$ – lirtosiast Jul 3 '15 at 23:55
  • \$\begingroup\$ @ThomasKwa: What fun is that if it's always on the first line? :) \$\endgroup\$ – Alex A. Jul 4 '15 at 0:01

Batch, 329 Bytes

@echo off
set e=goto e
set f= set/a
call:p %a% %b% %c% %d%
if %a%%b% EQU %c%%d% exit/b
choice/C "wasd"
goto %errorlevel%
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

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

:: Two empty lines are required here
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
  • \$\begingroup\$ This is highly impressive. I'm surprised it's possible to do this. \$\endgroup\$ – eis Jul 6 '15 at 7:39
  • \$\begingroup\$ 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 \$\endgroup\$ – Dan Pritts Jul 6 '15 at 15:01
  • \$\begingroup\$ It works for me witn Win7 cmd.exe - when I type ver I get Microsoft Windows [Version 6.1.7601] \$\endgroup\$ – Jerry Jeremiah Jul 7 '15 at 12:21
  • \$\begingroup\$ @DanPritts That's odd. It works perfectly for me on Windows 8 (Microsoft Windows [Version 6.2.9200]) \$\endgroup\$ – ankh-morpork Jul 7 '15 at 13:54
  • \$\begingroup\$ doh, i hadn't copied & pasted the whole thing, i didn't scroll down in the window. Well done. \$\endgroup\$ – Dan Pritts Jul 7 '15 at 15:50

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.

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.



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.

  • 1
    \$\begingroup\$ Just 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. \$\endgroup\$ – Dennis Jul 4 '15 at 22:37
  • \$\begingroup\$ @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. \$\endgroup\$ – celtschk Jul 4 '15 at 23:04
  • \$\begingroup\$ 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. \$\endgroup\$ – Yakk Jul 6 '15 at 17:38
  • \$\begingroup\$ @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.) \$\endgroup\$ – celtschk Jul 6 '15 at 21:16
  • \$\begingroup\$ @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. \$\endgroup\$ – Yakk Jul 6 '15 at 21:18

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();
        Draw(a,b,c,d); // Excluded from the score.
            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){
        for (int y = 0; y < 5; y++)
            for (int x = 0; x < 16; x++)
                    x == a && y == b ? '@' :
                    x == c && y == d ? '"' :
  • 1
    \$\begingroup\$ I 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. \$\endgroup\$ – user21677 Jul 3 '15 at 2:54
  • \$\begingroup\$ 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. \$\endgroup\$ – Hand-E-Food Jul 3 '15 at 3:23
  • \$\begingroup\$ 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. \$\endgroup\$ – Hand-E-Food Jul 3 '15 at 3:32
  • \$\begingroup\$ Technically, the Main method doesn't have to be called Main, so you could shave off another three characters. \$\endgroup\$ – Luaan Jul 3 '15 at 11:45
  • \$\begingroup\$ @Luaan, I think you're mistaken. C# documentation: msdn.microsoft.com/en-us/library/acy3edy3.aspx \$\endgroup\$ – Hand-E-Food Jul 3 '15 at 13:16

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>
T.value=p-a?(t=[...('.'.repeat(16)+'\n').repeat(5)],t[a]='X',t[p]='@',t.join('')):t='Well done!'

EcmaScript 6 Snippet (Firefox only)

  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) {
  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>


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:

    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()

        function g(){
            var r=Math.random;

        var old:int = -1;
        private function print():void {
            if (!t) {
                t = new TextField()
                t.defaultTextFormat = new TextFormat("_typewriter", 8)
            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;

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();};


var px=py=2,m=Math,ay=m.floor(m.random()*5),ax=14,i,j,t,c=console,onload=draw;
function draw() {
  for(i=0;i<5;i++) {
    for(j=0;j<16;j++) {
onkeydown=function() {
  switch (window.event.keyCode) {
    case 37:
    case 38:
    case 39:
    case 40:

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

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.

  • \$\begingroup\$ 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. \$\endgroup\$ – lirtosiast Jul 5 '15 at 1:17

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;

Printer.Print(a, b, x, y);  // Excluded from the score.

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;
Printer.Print(p,a);  // Excluded from the score.

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)
        for (int y = 0; y < 5; y++)
            for (int x = 0; x < 16; x++)
                if (x == px && y == py)
                else if (x == ax && y == ay)

    public static void Print(int p, int a)

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)
    for y in 0..4
        for x in 0..15
            if (x == px && y == py)
                print "@"
            elsif (x == ax && y == ay)
                print '"'
                print '.'

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

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

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

SUB Show (playerRow, playerCol, amuletRow, amuletCol)
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
      PRINT ".";
    END IF

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.


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:


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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy