/* Import the Java I/O package */
import java.io.*;
import AppletConsoleApp;

/**
 * Solution to the 1998 British Informatics Olympiad exam
 * question 2: Tamworth Two
 *
 * Sample run :
 * 5 6
 * 4 8
 * 
 * ..........
 * ..........
 * ...F......
 * ..........
 * ....P.....
 * ..........
 * ..........
 * ..........
 * ..........
 * ..........
 * 
 * O 2
 * 4 10
 * 1 1
 * 
 * ...*......
 * ..........
 * ...F......
 * ..........
 * ....P.....
 * ..........
 * ..........
 * ..........
 * ..........
 * *.........
 * 
 * M 4
 * 
 * Farmer and pigs meet on move 3 at (5,9)
 * 
 * ...*P.....
 * .....F....
 * ..........
 * ..........
 * ..........
 * ..........
 * ..........
 * ..........
 * ..........
 * *.........
 * 
 * X
 * End of sample run.
 *
 * This application that can be run either from the console
 * as a stand-alone application or from an in-applet
 * console created with the AppletConsole applet.
 *
 * Solution copyright (c) 1998 The British Informatics Olympiad (BIO).
 *
 * This program may be freely copied by persons or organisations
 * involved in the British Informatics Olympiad or the International
 * Olympiad in Informatics, on condition that no changes are made and
 * this notice is not altered. Distribution for profit is forbidden
 * unless permission is first obtained in writing from the BIO.
 *
 * This program is for educational purposes only and comes with no
 * warranty, implied or otherwise, as to its fitness for any purpose.
 *
 * Author:   Antony Rix
 * Internet: http://www.christs.cam.ac.uk/bio/
 * E-mail:   a.rix@lineone.net
 * S-mail:   The British Informatics Olympiad
 *           Christ's College
 *           Cambridge CB2 3BU
 *           United Kingdom
 *
 * @author  Antony Rix
 * @version 0.1
 * @see     AppletConsoleApp
 * @see     AppletConsole
 */
class BIO98R1Q2App extends AppletConsoleApp {
  /**
   * Start the application from the command line.
   */
  public static void main(String[] args) {
	  BIO98R1Q2App thisApp = new BIO98R1Q2App();
	  thisApp.redirectStreams(System.in, System.out);
	  thisApp.run();
	}

  /**
   * Representation of the board
   */
  int[][] board = new int[12][12];

  /**
   * Definitions for the objects
   */
  static int blank = 0;
  static int obstacle = 1;
  static int farmer = 2;
  static int pigs = 3;
  static int capture = 4;

  /**
   * Characters for each object
   */
  static char[] objects = { '.', '*', 'F', 'P', '+' };

  /**
   * Directions the pigs and farmer are moving in (default up)
   */
  int directionPigs = 0;
  int directionFarmer = 0;

  /**
   * Locations of pigs and farmer
   */
  int xPigs, yPigs, xFarmer, yFarmer;

  /**
   * Current move number
   */
  int move = 0;
  
  /**
   * The implementation of this application.
   */
	public void run() {
    int n, i, x, y;
    boolean Playing = true;
    
    /* Initialise the board */
    for( x = 0; x <= 11; x++ ) for( y = 0; y <= 11; y++ )
      board[x][y] = obstacle;
    for( x = 1; x <= 10; x++ ) for( y = 1; y <= 10; y++ )
      board[x][y] = blank;
    try {
      out.println(
        "BIO'98 Question 2" );
  	  /* Create a StreamTokenizer to allow us to read in the two numbers */
  	  StreamTokenizer sin = new StreamTokenizer( in );

  	  /* Read in initial co-ordinates */
  	  do {
        out.print( "Pigs at: " );
	      sin.nextToken();
	      xPigs = (int)sin.nval;
  	    sin.nextToken();
	      yPigs = (int)sin.nval;
	      if( (xPigs < 0) || (xPigs > 10) || (yPigs < 0) || (yPigs > 10) ) {
	        out.println( "Expected location for the pigs from" );
	        out.println( "(1,1)-(10,10) or (0,0) to demo 2(c)" );
	      }
	    } while( (xPigs < 0) || (xPigs > 10) || (yPigs < 0) || (yPigs > 10) );
	    if( (xPigs > 0) && (yPigs > 0) ) {
	      /* Play the standard game, placing the pigs and farmer */
  	    board[xPigs][yPigs] = pigs;
    	  do {
          out.print( "Farmer at: " );
	        sin.nextToken();
  	      xFarmer = (int)sin.nval;
  	      sin.nextToken();
    	    yFarmer = (int)sin.nval;
	        if( (xFarmer < 1) || (xFarmer > 10) || (yFarmer < 1) || (yFarmer > 10) ) {
  	        out.println( "Expected location for the Farmer from" );
	          out.println( "(1,1) to (10,10)" );
	        }
  	    } while( (xFarmer < 1) || (xFarmer > 10) || (yFarmer < 1) || (yFarmer > 10) );
  	    board[xFarmer][yFarmer] = farmer;
  	    showBoard();
  
  	    /* Move the pigs and farmer */
  	    while( Playing ) {
  	      sin.nextToken();
  	      if( sin.sval.compareTo( "T" ) == 0 ) {
  	        sin.nextToken();
  	        n = (int)sin.nval;
  	        out.println( "Enter " + n + " trees:" );
  	        for( i = 1; i <= n; i++ ) {
  	          sin.nextToken();
  	          x = (int)sin.nval;
  	          sin.nextToken();
  	          y = (int)sin.nval;
  	          board[x][y] = obstacle;
  	        }
  	        showBoard();
  	      } else if( sin.sval.compareTo( "M" ) == 0 ) {
  	        sin.nextToken();
  	        n = (int)sin.nval;
  	        for( i = 1; i <= n; i++ )
  	          makeMove();
  	        showBoard();
  	      } else if( sin.sval.compareTo( "X" ) == 0 ) {
  	        Playing = false;
  	      } else out.print( "\nUnrecognised command '" + sin.sval + "'.\n" );
  	    }
  	  } else {
  	    xPigs = 1; yPigs = 1;
  	    board[xPigs][yPigs] = pigs;
        directionPigs = 0;
  	    xFarmer = 2; yFarmer = 1;
  	    board[xFarmer][yFarmer] = farmer;
  	    directionFarmer = 0;
  	    out.println( "Start:" );
  	    showBoard();
  	    for( i = 1; i <= 19; i++ ) {
    	    makeMove();
    	    out.print( "After move " + move );
    	    showBoard();
  	    }
  	  }
  	} catch (IOException e) { out.println("I/O failure"); };
	  out.println( "Program finished." );
	}

	/**
	 * Display the board
	 */
	void showBoard() {
	  int x, y;
	  out.println();
	  for( y = 10; y >= 1; y-- ) {
	    for( x = 1; x <= 10; x++ ) out.print( objects[board[x][y]] );
	    out.println();
	  }
	  out.println();
	}

	/**
	 * Make a move
	 */
  void makeMove() {
    int xPigsNew = xPigs;
    int yPigsNew = yPigs;
    int xFarmerNew = xFarmer;
    int yFarmerNew = yFarmer;

    /* Move pigs */
    if( directionPigs == 0 ) yPigsNew += 1;
    if( directionPigs == 1 ) xPigsNew += 1;
    if( directionPigs == 2 ) yPigsNew -= 1;
    if( directionPigs == 3 ) xPigsNew -= 1;
    if( board[xPigsNew][yPigsNew] == obstacle ) {
      /* We cannot move to this point, so we rotate */
      xPigsNew = xPigs; yPigsNew = yPigs;
      directionPigs = (directionPigs + 1) % 4;
    }
    /* and farmer */
    if( directionFarmer == 0 ) yFarmerNew += 1;
    if( directionFarmer == 1 ) xFarmerNew += 1;
    if( directionFarmer == 2 ) yFarmerNew -= 1;
    if( directionFarmer == 3 ) xFarmerNew -= 1;
    if( board[xFarmerNew][yFarmerNew] == obstacle ) {
      xFarmerNew = xFarmer; yFarmerNew = yFarmer;
      directionFarmer = (directionFarmer + 1) % 4;
    }
    /* Move the pieces */
    board[xPigs][yPigs] = blank;
    board[xFarmer][yFarmer] = blank;
    xPigs = xPigsNew; yPigs = yPigsNew;
    xFarmer = xFarmerNew; yFarmer = yFarmerNew;
    move++;
    
    if( (xPigs == xFarmer) && (yPigs == yFarmer) ) {
      out.println( "Farmer and pigs meet on move " + move +
        " at (" + xPigs + "," + yPigs + ")" );
      board[xPigs][yPigs] = capture;
    } else {
      board[xPigs][yPigs] = pigs;
      board[xFarmer][yFarmer] = farmer;
    }
  }
}
