[Home]Hawk/Robots

ec2-54-160-133-33.compute-1.amazonaws.com | ToothyWiki | Hawk | RecentChanges | Login | Webcomic

This is the classic escape from the robots game.  It just about works, though the robots sometimes move toward where you were rather than where you are.

It doesn't detect when you've won yet either, though it does notice when you lose.

[Click to play]

NEW:

TODO:
Just made this even harder by taking the robot movement out of the player movement loop, may be impossible without reverting that
No in fact think I can do this by abusing if in a similar way to that which I already do in the display section.

BUGS?
Now fixed. (Variables were being initialised as strings, causing comparisons to return values incorrect for numerical data. Once some math had been performed on the variables, Javascript silently converted them to integers.) - MoonShadow

ToothyGDL

logic
{
  int boardWidth {20}
  int boardHeight {17}

  int TILE_EMPTY {0}
  int TILE_ROBOT {2}
  int TILE_MIN_EXPLOSION  {3}

  int playerX {8}
  int playerY {12}

  array board {
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0,
    0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  }

  int numRobots {8}
  array robotX { 2, 4, 19, 15, 13, 8, 9, 17 }
  array robotY { 8, 4, 4, 5, 7, 2, 16, 15 }

  array xMoves { -1, 0, 1, -1, 1, -1, 0, 1 }
  array yMoves { 1, 1, 1, 0, 0, -1, -1, -1 }

  // Generate a random board
  when
  {
    (1 == 1)
  }
  allow
  {
    for y (0 .. (boardHeight - 1))
    {
      for x (0 .. (boardWidth - 1))
      {
        board[x + (y * boardWidth)] = TILE_EMPTY;
      }
    }
    for i (0..(numRobots-1))
    {
      robotX[i] = random[boardWidth];
      robotY[i] = random[boardHeight];
      board[robotX[i] + (robotY[i] * boardWidth)] = board[robotX[i] + (robotY[i] * boardWidth)] + TILE_ROBOT;
    }
    playerX = random[boardWidth];
    playerY = random[boardHeight];
  }
  label
  {
    "Reset"
  }

  for p (0..7)
  {
    // Move player.
    when
    {
          ((playerX + xMoves[p]) >= 0)
      and ((playerX + xMoves[p]) < boardWidth)
      and ((playerY + yMoves[p]) >= 0)
      and ((playerY + yMoves[p]) < boardHeight)
    }
    allow
    {
      playerX = (playerX + xMoves[p]);
      playerY = (playerY + yMoves[p]);
    }
    label
    {
      "Player dx=" xMoves[p] " dy=" yMoves[p] "."
    }
  }

    for r (0 .. (numRobots - 1))
    {
      // Move each robot toward the player, killing him if he's hit.
      when
      {
            (playerX > robotX[r])
        and (playerY > robotY[r])
        and (board[robotX[r] + (robotY[r] * boardWidth)] == TILE_ROBOT)
      }
      allow
      {
        board[robotX[r] + (robotY[r] * boardWidth)] = TILE_EMPTY;
        board[robotX[r] + 1 + ((robotY[r] + 1) * boardWidth)] = board[robotX[r] + 1 + ((robotY[r] + 1) * boardWidth)] + TILE_ROBOT;
        robotX[r] = robotX[r] + 1;
        robotY[r] = robotY[r] + 1;
      }
      label
      {
        "Robot move"
      }
      when
      {
            (playerX > robotX[r])
        and (playerY == robotY[r])
        and (board[robotX[r] + (robotY[r] * boardWidth)] == TILE_ROBOT)
      }
      allow
      {
        board[robotX[r] + (robotY[r] * boardWidth)] = TILE_EMPTY;
        board[robotX[r] + 1 + (robotY[r] * boardWidth)] = board[robotX[r] + 1 + (robotY[r] * boardWidth)] + TILE_ROBOT;
        robotX[r] = robotX[r] + 1;
      }
      label
      {
        "Robot move"
      }
      when
      {
            (playerX > robotX[r])
        and (playerY < robotY[r])
        and (board[robotX[r] + (robotY[r] * boardWidth)] == TILE_ROBOT)
      }
      allow
      {
        board[robotX[r] + (robotY[r] * boardWidth)] = TILE_EMPTY;
        board[robotX[r] + 1 + ((robotY[r] - 1) * boardWidth)] = board[robotX[r] + 1 + ((robotY[r] - 1) * boardWidth)] + TILE_ROBOT;
        robotX[r] = robotX[r] + 1;
        robotY[r] = robotY[r] - 1;
      }
      label
      {
        "Robot move"
      }
      when

      {
            (playerX == robotX[r])
        and (playerY > robotY[r])
        and (board[robotX[r] + (robotY[r] * boardWidth)] == TILE_ROBOT)
      }
      allow
      {
        board[robotX[r] + (robotY[r] * boardWidth)] = TILE_EMPTY;
        board[robotX[r] + ((robotY[r] + 1) * boardWidth)] = board[robotX[r] + ((robotY[r] + 1) * boardWidth)] + TILE_ROBOT;
        robotY[r] = robotY[r] + 1;
      }
      label
      {
        "Robot move"
      }
      when
      {
            (playerX == robotX[r])
        and (playerY < robotY[r])
        and (board[robotX[r] + (robotY[r] * boardWidth)] == TILE_ROBOT)

      }
      allow
      {
        board[robotX[r] + (robotY[r] * boardWidth)] = TILE_EMPTY;
        board[robotX[r] + ((robotY[r] - 1) * boardWidth)] = board[robotX[r] + ((robotY[r] - 1) * boardWidth)] + TILE_ROBOT;
        robotY[r] = robotY[r] - 1;
      }
      label
      {
        "Robot move"
      }
      when
      {
            (playerX < robotX[r])
        and (playerY > robotY[r])
        and (board[robotX[r] + (robotY[r] * boardWidth)] == TILE_ROBOT)
      }
      allow
      {
        board[robotX[r] + (robotY[r] * boardWidth)] = TILE_EMPTY;
        board[robotX[r] - 1 + ((robotY[r] + 1) * boardWidth)] = board[robotX[r] - 1 + ((robotY[r] + 1) * boardWidth)] + TILE_ROBOT;
        robotX[r] = robotX[r] - 1;
        robotY[r] = robotY[r] + 1;
      }
      label
      {
        "Robot move"
      }
      when
      {
            (playerX < robotX[r])
        and (playerY == robotY[r])
        and (board[robotX[r] + (robotY[r] * boardWidth)] == TILE_ROBOT)
      }
      allow
      {
        board[robotX[r] + (robotY[r] * boardWidth)] = TILE_EMPTY;
        board[robotX[r] - 1 + (robotY[r] * boardWidth)] = board[robotX[r] - 1 + (robotY[r] * boardWidth)] + TILE_ROBOT;
        robotX[r] = robotX[r] - 1;
      }
      label
      {
        "Robot move"
      }
      when
      {
            (playerX < robotX[r])
        and (playerY < robotY[r])
        and (board[robotX[r] + (robotY[r] * boardWidth)] == TILE_ROBOT)
      }
      allow
      {
        board[robotX[r] + (robotY[r] * boardWidth)] = TILE_EMPTY;
        board[robotX[r] - 1 + ((robotY[r] - 1) * boardWidth)] = board[robotX[r] - 1 + ((robotY[r] - 1) * boardWidth)] + TILE_ROBOT;
        robotX[r] = robotX[r] - 1;
        robotY[r] = robotY[r] - 1;
      }
      label
      {
        "Robot move"
      }
    }
}

display
{
  stringtable items
  {
    " ", "^", "$", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*" 
  }

  box
  {
    content { "Robots!" }
  }
  newline
  for y (0 .. (boardHeight - 1))
  {
    for x (0 .. (boardWidth - 1))
    {
      box
      {
        width { 2em }
        height { 2em }
        border { 1px solid black }

        content { items[board[x + (y * boardWidth)]] }

        if ((x == playerX) and (y == playerY))
        {
          content { "&" }
          if (board[x + (y * boardWidth)] != TILE_EMPTY)
          {
            background { #900 }
            content { "X" }
          }
        }
      }
    }
    newline
  }
  newline

  box
  {
    width { 2em }
    height { 2em }
    border { 1px solid black }
    content { "7" }
    if (board[playerX + (playerY * boardWidth)] == TILE_EMPTY)
    {
      bind_action { "Player dx=-1 dy=-1." }
      bind_action { "Robot move" }
    }
  }
  box
  {
    width { 2em }
    height { 2em }
    border { 1px solid black }
    content { "8" }
    if (board[playerX + (playerY * boardWidth)] == TILE_EMPTY)
    {
      bind_action { "Player dx=0 dy=-1." }
      bind_action { "Robot move" }
    }
  }
  box
  {
    width { 2em }
    height { 2em }
    border { 1px solid black }
    content { "9" }

    if (board[playerX + (playerY * boardWidth)] == TILE_EMPTY)


    {
      bind_action { "Player dx=1 dy=-1." }
      bind_action { "Robot move" }
    }
  }
  newline
  box
  {
    width { 2em }
    height { 2em }
    border { 1px solid black }
    content { "4" }
    if (board[playerX + (playerY * boardWidth)] == TILE_EMPTY)
    {
      bind_action { "Player dx=-1 dy=0." }
      bind_action { "Robot move" }
    }
  }
  box
  {
    width { 2em }
    height { 2em }
    border { 1px solid black }
    content { "5" }
    if (board[playerX + (playerY * boardWidth)] == TILE_EMPTY)
    {
      bind_action { "Robot move" }
    }
  }
  box
  {
    width { 2em }
    height { 2em }
    border { 1px solid black }
    content { "6" }
    if (board[playerX + (playerY * boardWidth)] == TILE_EMPTY)
    {
      bind_action { "Player dx=1 dy=0." }
      bind_action { "Robot move" }
    }
  }
  newline
  box
  {
    width { 2em }
    height { 2em }
    border { 1px solid black }
    content { "1" }
    if (board[playerX + (playerY * boardWidth)] == TILE_EMPTY)
    {
      bind_action { "Player dx=-1 dy=1." }
      bind_action { "Robot move" }
    }
  }
  box
  {
    width { 2em }
    height { 2em }
    border { 1px solid black }
    content { "2" }
    if (board[playerX + (playerY * boardWidth)] == TILE_EMPTY)
    {
      bind_action { "Player dx=0 dy=1." }
      bind_action { "Robot move" }
    }
  }
  box
  {
    width { 2em }
    height { 2em }
    border { 1px solid black }
    content { "3" }
    if (board[playerX + (playerY * boardWidth)] == TILE_EMPTY)
    {
      bind_action { "Player dx=1 dy=1." }
      bind_action { "Robot move" }
    }
  }
  newline

  box
  {
    if (board[playerX + (playerY * boardWidth)] == TILE_EMPTY)
    {
      content { "Make your escape!" }
    }

    if (board[playerX + (playerY * boardWidth)] != TILE_EMPTY)
    {
      content { "Game over!" }
    }
  }
  newline
  box
  {
    border { 1px solid black }
    content { "Regenerate" }
    bind_action { "Reset" }
  }
}


[Recompile this page]
[Compiler error log]

ec2-54-160-133-33.compute-1.amazonaws.com | ToothyWiki | Hawk | RecentChanges | Login | Webcomic
Edit this page | View other revisions | Recently used referrers
Last edited October 14, 2005 1:30 pm (viewing revision 32, which is the newest) (diff)
Search: