/*

  codecup_C_example.c

  Example of a Lamistra player program for the CodeCup.

  This only demonstrates how to deal with 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.

*/

#include <stdio.h>
#include <string.h>

/* statustype: IO is determined by the actual status */

typedef enum 
{
  INIT,
  TOMOVE,
  ATTACKING,
  WAITING,
  ATTACKED,
  DEFENDING,
  READY
} statustype;

/* global variables */

statustype stat;     // what is the actual situation
char       line[6];  // line from standard input or to standard output
int        red;      // is the player playing with red?
int        nMoves;   // number of moves played
int        fromcol,  // move parameters
           fromrow,
           tocol,
           torow;

/* This function initializes all variables */

void InitGame(void)
{
  stat=INIT;
  nMoves=0;

  /* TODO: initialize the game  */ 

}

/* This function inputs a line from standard input */

void readInputLine(void)
{
  scanf("%s", line);
}

/* This function outputs a line to standard output */

void writeOutputLine(void)
{
  printf("%s\n", line);
  fflush(stdout);
}

/* This function checks if the To-field contains my piece */

int MyField(int col, int row)
{
  int answer;
  
  /* TODO: are you being attacked or not? */

  return answer;
}

/* This function is used to process the move of the opponent */

void ProcessMove(char *s)
{
  ++nMoves;
  fromcol=s[1]-'a'+1;
  fromrow=s[2]-'1'+1;
  tocol=s[4]-'a'+1;
  torow=s[5]-'1'+1;
  if (MyField(tocol,torow))
  {
    stat=ATTACKED;
  }  
  else
  {
 
  /* TODO: process your opponents move internally */ 

    stat=TOMOVE;
  }  
}

/* This function is used to select a move */

void SelectMove(char *s, int *a, char *l)
{
  
  /* 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

}

/* This function is called to select the value of an attacked piece */

void SelectPiece(char *s)
{
  
  /* TODO: select the piece you think gives the best defence */

}

/* This function is called when the game is started */

void PerformInit(void)
{
  readInputLine();
  if (strcmp("Start", line)==0)
  {
    red=1;
    stat=TOMOVE;
  }
  else
  {
    red=0;
    ProcessMove(line);
  }
}

/* This function is called when it is time to make a move */

void PerformTomove(void)
{
  int attack;
  char letter;
  SelectMove(line, &attack, &letter);
  writeOutputLine();
  if (attack)
  {
    line[0]=letter;
    line[1]='\0';
    writeOutputLine();
    stat=ATTACKING;
  }
  else
  {
    
    /* TODO: process your move internally */
    
    stat=WAITING;
  }
  ++nMoves;
}


/* This function is called when an attack is performed */

void PerformAttacking(void)
{
  readInputLine();
  if (line[0]=='X')
  {
    stat=READY;
  }
  else
  {
    
    /* TODO: process your move internally with the information 
             gathered in performTomove() and in this function 
    */ 

    stat=WAITING;
  }
}

/* This function is called when the opponent has to move */

void PerformWaiting(void)
{
  readInputLine();
  if (line[0]=='X') stat=READY; else ProcessMove(line);
}

/* This function is called when the opponent attacks */

void PerformAttacked(void)
{
  SelectPiece(line);
  writeOutputLine();
  stat=DEFENDING;
}

/* This procedure is called when the opponent has to reveil his piece */

void PerformDefending(void)
{
  readInputLine();
  if (line[0]=='X')  
  {
    stat=READY;
  }
  else
  {

    /* TODO: process your opponents move internally with the information
             gathered in ProcessMove(), PerformAttacked() and in this function
    */ 

    stat=TOMOVE;
  }
}

/* Main program */

int main(void)
{
  InitGame();
  while (stat!=READY)
  {
    switch(stat)
    {
      case INIT:      PerformInit(); break;
      case TOMOVE:    PerformTomove(); break;
      case ATTACKING: PerformAttacking(); break;
      case WAITING:   PerformWaiting(); break;
      case ATTACKED:  PerformAttacked(); break;
      case DEFENDING: PerformDefending(); break;
      case READY: break;
    }
  }
  return 0;
}



syntax highlighted by Code2HTML, v. 0.9.1