var tick = 0
var roomcode = new Array(0, 0, 0, 0)
var indolence = 0
var atstart = 1 
var messages = new Array(32)

messages[0] = "Welcome to the fabled, mythic Dungeon of Doom, " +
              "where you must slay or be slain!\n" 
messages[1] = "\n\nHit any key to play again." 

j = "From a dark corner of the room, thirteen bloodthirsty lamiae burst " +
    "out and try to devour you. Your blade is swift and your arm true; "
messages[2] = j + "but in spite of this it is not long till you have been " +
               "slain by the lamiae!" 
messages[3] = j + "one by one they fall till none are left. You have " +
               "slain the vicious lamiae!"

j = "Out of the blue, over a thousand million chambered nautili fall into " +
    "the room and try to overwhelm you with sheer numbers. "
messages[4] = j + "They succeed in this attempt; long, long before you " +
 "can kill them all, you die of old age. You have been slain by the nautili!" 
messages[5] = j + "However, though it takes you a very long time, you do " +
 "smash every last cephalopod mollusk. You have slain the vicious nautili!"

j = "With no warning, an algebraic topologist assails you with a poleaxe. " +
    "You struggle with the mathematician, trading powerful blows, "
messages[6] = j + "but it is no use. Before long you have been slain by " +
             "the algebraic topologist!" 
messages[7] = j + "until finally you defeat him. You have slain the " +
             "vicious algebraic topologist!"

j = "Far faster than you would think a ton of rock could move, a massive " +
    "massif rushes at you and tries to crush you. You flail at the stone "
messages[8] = j + "for hours, but to no effect at all; the vast rock's " +
               "attack just goes on, until you have been slain by the massif!"
messages[9] = j + "for hours; after what seems to be an eternity, it once " +
               "again becomes immobile. You have slain the vicious massif!"   

j = "When you're looking elsewhere, a gang of greedy gonifs rushes at you " +
    "with daggers to kill you for your treasure. With your mighty vorpal " +
    "sword, you fight valiantly against them, "
messages[10] = j + "but ultimately you lose to the gang. You have been " +
              "slain by the gonifs!"
messages[11] = j + "and ultimately you trounce the gang. You have slain " +
              "the vicious gonifs!"

j = "From out of nowhere, a rodent of unknown species jumps on you, and " +
    "tries to chomp on your extremities. With one swift blow "
messages[12] = j + "you accidentally knock yourself out, and before even " +
 "one hour has passed you have been slain by the rodent of unknown species!"
messages[13] = j + "you decapitate the wee critter. You have slain the " +
              "vicious rodent of unknown species!"

j = "In the blink of an eye, a brace of giant fire-eating axolotls is " +
    "breathing flames at you. You try frantically to fight one of them " +
    "off with each hand; "
messages[14] = j + "but although you are ambidextrous, it won't work. " +
                   "You have been slain by the fire-eating axolotls!"
messages[15] = j + "fortunately, being ambidextrous, you can do this. You " +
                   "have slain the vicious fire-eating axolotls!"

j = "Suddenly, a vast raging army of sentient alfalfa sprouts leaps " +
    "upon you. Fighting for your life, you strike at the sprouts madly, " 
messages[16] = j + "but it is in vain. Within minutes you have been slain " +
              "by the sentient alfalfa!"
messages[17] = j + "until at last you are victorious. You have slain the " +
              "vicious sentient alfalfa!" 

j = "\nGo:  (0) North, (1) Northwest, (2) West, (3) Southwest, (4) " +
    "South, (5) Southeast, (6) East, (7) Northeast, (8) None of the above."
messages[18] = "You're in a maze of square little rooms, all different. " +
   "It is nighttime; you thus are likely to be (say) eaten by a grue. " + j
messages[19] = "You're in a maze of square little rooms, all different. It " + 
   "is daytime; hence, you are not likely to be eaten by a grue.   " + j

messages[20] = "\n\nBut fortunately, you have a resurrection scroll in " +
 "your pocket, and you come back to life. And then you're just fine till," 
messages[21] = "\n\nAnd then as you're catching your breath,"
messages[22] = "You now exit the dungeon, having neither slain nor been " +
               "slain. How dull."

function say_message(w, x)
{ 
var i = 0;
var j = messages[x] 
var k = w;
while (i < j.length) {
  if (j.substr(i, 1) == " ") { i++ } 
  else if (j.substr(i, 1) == "\n") { k += "\n" ; i++ }
  else { k += j.substr(i, 40) + "\n" ; i += 40 }  
  }
document.getElementById("aaa").value = k
}

function add_message(x, d)
{ 
var i = 0;
var j = messages[x]   
var k = ""; 
if (d) { j = j.substr(0, 1).toLowerCase() + j.substr(1,j.length - 1) }
while (i < j.length) {
  if (j.substr(i, 1) == " ") { i++ } 
  else if (j.substr(i, 1) == "\n") { k += "\n" ; i++ }
  else { k += j.substr(i, 40) + "\n" ; i += 40 }  
  }
document.getElementById("aaa").value += k
}

function code_shift()
{
  k = roomcode[0] % 16
  roomcode = new Array(roomcode[1], roomcode[2], roomcode[3], 
                       k * 16 + ((roomcode[0] - k) / 16)) 
}
   
function changeRooms(x)
{
var y = document.getElementById(x).value
var day = !(tick % 2)
var threat = 0;

if (atstart) { 
  say_message("", 0) 
  add_message(19, 0) 
  tick = 0
  roomcode = new Array(0, 0, 0, 0)
  indolence = 0
  atstart = 0
  }

else {
 if ("012345678".indexOf(y) == -1 || y.length != 1) 
   { say_message("You have hit an incorrect key.\n\n", day + 18) }
 else if (y == 8) {
   if (indolence) { say_message("You can't go that way.\n\n", day + 18) }
   else { indolence = (day) ? 68 : 17; threat++ } }
 else if ((y < 4) ? (roomcode[y] % 16) : (roomcode[y - 4] >= 16)) 
   { say_message("You can't go that way.\n\n", day + 18) }
 else { roomcode[y % 4] += ((y < 4) ? 1 : 16) * ((day) ? 4 : 1); threat++ }
   
 if (threat)
 {
   fulfill = 0
   for (i = 0; i < 4; i++) {
     if (indolence && indolence == roomcode[i]) {
       fulfill++  
       if (fulfill == 1) { say_message("You go there.\n\n", 2 * i + 2 + day) }
       else { add_message(20 + day, 0) ; add_message(2 * i + 2 + day, 1) } 
     }
     code_shift()
     if (roomcode[1] % 16 && 
         !((roomcode[0] + roomcode[2] - 2 * roomcode[1]) % 16)) { 
       fulfill++  
       if (fulfill == 1) { say_message("You go there.\n\n", 2 * i + 10 + day) }
       else { add_message(20 + day, 0) ; add_message(2 * i + 10 + day, 1) } 
     }
     code_shift()
   }
   if (fulfill) {
     add_message(1, 0)
     atstart = 1
     } 
   else if (tick == 8) {
     say_message("", 22) 
     add_message(1, 0)
     atstart = 1
     }
   else {
     say_message("Twelve hours elapse.\n\n", (!day) + 18) 
     tick++
     }
   }
 }
document.getElementById(x).value=""
}
