[Question]
  [
[NetHack](https://en.wikipedia.org/wiki/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:
```
................
...."...........
..........@.....
................
................
................
...."...........
.........@......
................
................
................
...."...........
........@.......
................
................
```
  
[Answer]
# [CHIP-8](https://en.wikipedia.org/wiki/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](https://i.stack.imgur.com/uMI59.png)
<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.
[Answer]
# 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](http://tibasicdev.wikidot.com/key-codes) 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](https://i.stack.imgur.com/BBrom.png)
## 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.
[Answer]
# 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();`.
[Answer]
# 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](https://codegolf.stackexchange.com/questions/52547/minimal-nethack/52565#comment125400_52547) 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.
[Answer]
# 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](https://sourceforge.net/projects/cjam/files/).
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.
```
[Answer]
# 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.
[Answer]
# 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.
[Answer]
# 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
```
[Answer]
# 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
```
[Answer]
# 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 `$a`th 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.
[Answer]
## 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();
        }
    }
}
```
[Answer]
# 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>
```
[Answer]
# Actionscript 3: 267 bytes
A working example is [online](http://www.fastswf.com/iZ2UH4U)
`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;
        }
    }
}
```
[Answer]
# 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
[Answer]
# 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.
[Answer]
## 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);
    }
}
```
[Answer]
## 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
```
[Answer]
# 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.
]