(*
 * skeleton.pas
 * last updated on Internet: April 28, 1999
 * Original version by by Chris Alfeld and Chad Barb
 * Pascal version by Chris Mayfield <cmay@cs.utah.edu>
 *
 * Nanobots - 1999 High School Programming Contest Take Home Problem
 *
 * Here's a basic skeleton that you can use to write your bot.  Routines
 * for all input and output are provided as well as a basic main program
 * BEGIN/END block (at the end of this file) for you to expand.
 *
 * You may modify any part of this file as you see fit.
 *
 * To write a nanobot:
 *   1.  Read takehome.html
 *   2.  Read readme.txt
 *   3.  Become familiar with this file
 *       (look at the Global Variables and functions provided)
 *   4.  Fill out the skeleton with your own code (see "MODIFY ME!" comments)
 *
 * PASCAL NOTES:  We will recompile this code for the contest.  Our compiler
 * is rather picky.  It is both case sensitive and completely unaware of
 * anything beyond standard pascal.  When in doubt use lower case.  All 'uses'
 * statements are absolutely forbidden!
 *)

program acm99;

(*****************************************************************************)
(* Global Constants *)
const

  (* Maximum map size *)
  MAXMAP = 252;

(*****************************************************************************)
(* Global Variables
 *
 * mapWidth  - the width of the input map
 * mapHeight - the hieght of the input map
 * fuel      - the amount of fuel the nanobot has
 * map       - a two dimensional array holding the map.  The first index is row,
 *             the second column (map[y,x]), the upper left corner is (0,0).
 * baseX     - the X coordinate of the base
 * baseY     - the Y coordinate of the base
 *)
var

  mapWidth, mapHeight, fuel : integer;
  map : packed array[0..MAXMAP-1,0..MAXMAP-1] of char;
  baseX, baseY : integer;

(*****************************************************************************)
(* MODIFY ME! - Put your data types here! ************************************)


(*****************************************************************************)
(* MODIFY ME! - Put your globals here! ***************************************)


(*****************************************************************************)
(* MODIFY ME! - Put your added procedures/functions here! ********************)


(*****************************************************************************)
(* Provided Code - edit the following code at your own risk ******************)
(*****************************************************************************)

(*****************************************************************************)
(* Display an error message and exit *)

procedure bomb (message : string);
begin
  writeln('fatal error: ', message);
  writeln;
  halt();
end; (* bomb *)

(*****************************************************************************)
(* Reads the map from the standard input and sets up the global variables.   *)

procedure acquireMap;
var
  l : string;
  i, j : integer;
begin

  (* reads in the map specs from standard input *)
  read (mapWidth, mapHeight, fuel);
  readln(l); (* dummy statement to flush the input stream *)

  if ((mapWidth  < 3) or (mapWidth  > MAXMAP) or
      (mapHeight < 3) or (mapHeight > MAXMAP) or (fuel <= 0)) then
    bomb('Invalid map (bad width, height and/or fuel specs.)' );

  (* clear the map file memory *)
  for i := 0 to mapHeight-1 do
    for j := 0 to mapWidth-1 do
      map[i,j] := 'X';

  (* input map into array *)
  baseX := -1;
  baseY := -1;
  for i := 0 to mapHeight-1 do
  begin

    readln(l);

    for j := 0 to mapWidth-1 do
    begin

      map[i,j] := l[j+1]; (* j+1 because pascal strings based at 1 *)

      if (map[i,j] = 'B') then
      begin
        baseX := j;
        baseY := i;
      end;

    end;

  end; (* for loop *)

  (* check for no base *)
  if (baseX = -1) then
    bomb('no base found in map.');

end; (* acquireMap *)

(*****************************************************************************)
(* Call these procedures to send the appropriate output for moving *)

procedure MoveNorth;
begin
  writeln('NORTH');
end; (* MoveNorth *)

procedure MoveSouth;
begin
  writeln('SOUTH');
end; (* MoveSouth *)

procedure MoveEast;
begin
  writeln('EAST');
end; (* MoveEast *)

procedure MoveWest;
begin
  writeln('WEST');
end; (* MoveWest *)

(*****************************************************************************)
(* Action procedures:  These are the other two actions your rover can do *)

procedure TakeMolecule;
begin
  writeln('TAKE');
end; (* TakeMolecule *)

procedure EndRun;
begin
  writeln('END');
end; (* EndRun *)

(*****************************************************************************)
(* End Provided Code - edit the above code at your own risk ******************)
(*****************************************************************************)

(*****************************************************************************)
(* MAIN PROGRAM BLOCK ********************************************************)
(*****************************************************************************)

begin

  (* read in the map *)
  acquireMap;

  (* MODIFY ME! - Your Code Goes Here! *)

end. (* MAIN PROGRAM *)

(*****************************************************************************)
