program CodeCup;

{
  Example of a Lamistra player program for the CodeCup.

  This only demonstrates how to do the input and output. You will
  have to write the algorithms for the game all by yourself.
  The use of this example is not compulsory.
}

{ statustype: IO is determined by the actual status }
type statustype=(init,tomove,attacking,waiting,attacked,defending,ready);

{ global variables }
var stat: statustype;  { what is the actual situation }
    line: string;      { line from standard input or to standard output }
    red: boolean;      { is the player playing with red? }
    nMoves: integer;   { number of moves played }
    fromcol,fromrow,tocol,torow: integer; {move parameters}

{ This procedure initilaizes all variables }
procedure InitGame;
begin
  stat:=init;
  nMoves:=0;
  { TODO: initialize the game }
end;

{ This procedures inputs a line from standard input }
procedure readInputLine;
begin
  readln(line)
end;

{ This procedure outputs a line to standard output }
procedure writeOutputLine;
begin
  writeln(line);
  flush(output)
end;

{ This function checks if the To-field contains my piece }
function MyField(col,row: integer): boolean;
begin
  { TODO: are you being attacked or not? }
end; 

{ This procedure is used to process the move of the opponent }
procedure ProcessMove(s: string);
begin
  inc(nMoves);
  fromcol := ord(s[1]) - ord('a') + 1;
  fromrow := ord(s[2]) - ord('1') + 1;
  tocol := ord(s[4]) - ord('a') + 1;
  torow := ord(s[5]) - ord('1') + 1;

  if MyField(tocol,torow) then stat := attacked else 
  begin
    { TODO: process your opponents move internally }
    stat := tomove
  end
end;


{ this procedure is used to select a move }
procedure SelectMove(var s: string; var a: boolean; var l: char);
begin
  { TODO: select what you think is the best move }
  { s is the string presenting the move to perform }
  { a is whether this move will perform an attack }
  { l gives the value of the attacking piece }
end;

{ This procedure is called to select then value of an attacked piece }
procedure SelectPiece(var s: string);
begin
  { TODO: select the piece you think gives the best defence }
end;

{ This procedure is called when the game is started }
procedure PerformInit;
begin
  readInputLine;
  if line = 'Start' then
  begin
    red := true;
    stat := tomove
  end else
  begin
    red := false;
    ProcessMove(line)
  end
end;

{ This procedure is called when it is time to make a move }
procedure PerformTomove;
var attack: boolean;
    letter: char;
begin
  SelectMove(line,attack,letter);
  writeOutputLine;
  if attack then
  begin
    line := letter;
    writeOutputLine;
    stat := attacking
  end else
  begin
    { TODO: process your move internally }
    stat := waiting
  end;
  inc(nMoves)
end;


{ This procedure is called when an attack is performed }
procedure PerformAttacking;
begin
  readInputLine;
  if line = 'X' then stat := ready else
  begin
    { TODO: process your move internally with the information 
      gathered in performTomove() and in this procedure }
    stat := waiting
  end
end;

{ This procedure is called when the opponent has to move }
procedure PerformWaiting;
begin
  readInputLine;
  if line = 'X' then stat := ready else ProcessMove(line)
end;

{ This procedure is called when the opponent attacked }
procedure PerformAttacked;
begin
  SelectPiece(line);
  writeOutputLine;
  stat := defending
end;

{ This procedure is called when the opponent has to reveil his piece }
Procedure PerformDefending;
begin
  readInputLine;
  if line = 'X' then stat := ready else
  begin
    { TODO: process your opponents move internally with the information
      gathered in ProcessMove(), PerformAttacked() and in this procedure }
    stat:=tomove
  end
end;

{ Main program }
begin
  InitGame;
  while stat<>ready do
  begin
    case stat of
      init:      PerformInit;
      tomove:    PerformTomove;
      attacking: PerformAttacking;
      waiting:   PerformWaiting;
      attacked:  PerformAttacked;
      defending: PerformDefending;
    end
  end
end.


syntax highlighted by Code2HTML, v. 0.9.1