// updated Thu Oct 10 21:53:23 EDT 2002 (sklar)
// sample program to demonstrate lots of things we've covered
// in class up till now...

public class bbot {

    // global constants
    static final int YMAX = 10;
    static final int XMAX = 10;
    static final int FWD = 3;
    static final int LAT = 5;
    
    static final char EMPTY = '.';
    static final char ROBOT = 'R';
    static final char GOLD  = '$';
    static final char EVIL  = '@';
    static final char FOOD  = '(';
    static final char BLIND = 'X';
    static final char EOW   = '*';
    
    static final int MAXGOLD = (XMAX*YMAX)/10;
    static final int MAXEVIL = (XMAX*YMAX)/10;
    static final int MAXFOOD = (XMAX*YMAX)/10;
    
    // global variables
    static char world[][] = new char[YMAX][XMAX];
    static char robot[][] = new char[FWD][LAT];
    static int yloc;
    static int xloc;


    public static void main( String[] args ) {

	initWorld();
	yloc = (int)( Math.random() * YMAX );
	xloc = (int)( Math.random() * XMAX );
	world[yloc][xloc] = 'R';
	setRobot();

	System.out.println( "starting position:" );
	show( robot,FWD,LAT );
	System.out.println();
	show( world,YMAX,XMAX );

	ReadInput input = new ReadInput( System.in );
	String s;
	boolean more = true;
	while ( more ) {
	    moveRobot();
	    show( robot,FWD,LAT );
	    System.out.println();
	    show( world,YMAX,XMAX );
	    System.out.print( "press enter to continue, q to quit " );
	    s = input.readLine();
	    more = ! (( s.length() > 0 ) && ( s.charAt(0) == 'q' ));
	} // end while

    } // end of main()


    public static void initWorld() {
	for ( int y=0; y<YMAX; y++ ) {
	    for ( int x=0; x<XMAX; x++ ) {
		world[y][x] = EMPTY;
	    }
	}
	placeItem( MAXGOLD,GOLD );
	placeItem( MAXEVIL,EVIL );
	placeItem( MAXFOOD,FOOD );
    } // end of initWorld()


    public static void placeItem( int nitem, char item ) {
	for ( int i=0; i<nitem; i++ ) {
	    int y = (int)( Math.random() * YMAX );
	    int x = (int)( Math.random() * XMAX );
	    while ( world[y][x] != EMPTY ) {
		y = (int)( Math.random() * YMAX );
		x = (int)( Math.random() * XMAX );
	    }
	    world[y][x] = item;
	}
    } // end of placeItem() 


    public static void setRobot() {
	for ( int y=0; y<FWD; y++ ) {
	    for ( int x=0; x<LAT; x++ ) {
		int ypos = yloc - FWD + 1 + y;
		int xpos = xloc - LAT/2 + x;
		if (( ypos >= 0 ) && ( ypos < YMAX ) &&
		    ( xpos >= 0 ) && ( xpos < XMAX )) {
		    robot[y][x] = world[ypos][xpos];
		}
		else { // y is okay, x is out of range
		    robot[y][x] = EOW;
		}
	    }
	}
	robot[2][2] = 'R';
    } // end of setRobot() 


    public static void show( char[][] area, int ymax, int xmax ) {
	for ( int y=0; y<ymax; y++ ) {
	    for ( int x=0; x<xmax; x++ ) {
		System.out.print( area[y][x] );
	    }
	    System.out.println();
	}
    } // end of show()


    public static void moveRobot() {
	int dir = (int)( Math.random() * 8 );
	world[yloc][xloc] = EMPTY;
	switch( dir ) {
	case 0:
	    if ( yloc > 0 ) {
		System.out.println( "moving robot NORTH" );
		yloc--;
	    }
	    else {
		System.out.println( "robot is trying to go north, but it is stuck at the edge of the world :-(" );
	    }
	    break;
	case 1:
	    if (( xloc < XMAX-1 ) && ( yloc > 0 )) {
		System.out.println( "moving robot NORTHEAST" );
		yloc--;
		xloc++;
	    }
	    else {
		System.out.println( "robot is trying to go northeast, but it is stuck at the edge of the world :-(" );
	    }
	    break;
	case 2:
	    if ( xloc < XMAX-1 ) {
		System.out.println( "moving robot EAST" );
		xloc++;
	    }
	    else {
		System.out.println( "robot is trying to go east, but it is stuck at the edge of the world :-(" );
	    }
	    break;
	case 3:
	    if (( xloc < XMAX-1 ) && ( yloc < YMAX-1 )) {
		System.out.println( "moving robot SOUTHEAST" );
		xloc++;
		yloc++;
	    }
	    else {
		System.out.println( "robot is trying to go southeast, but it is stuck at the edge of the world :-(" );
	    }
	    break;
	case 4:
	    if ( yloc < YMAX-1 ) {
		System.out.println( "moving robot SOUTH" );
		yloc++;
	    }
	    else {
		System.out.println( "robot is trying to go south, but it is stuck at the edge of the world :-(" );
	    }
	    break;
	case 5:
	    if (( xloc > 0 ) && ( yloc < YMAX-1 )) {
		System.out.println( "moving robot SOUTHWEST" );
		xloc--;
		yloc++;
	    }
	    else {
		System.out.println( "robot is trying to go southwest, but it is stuck at the edge of the world :-(" );
	    }
	    break;
	case 6:
	    if ( xloc > 0 ) {
		System.out.println( "moving robot WEST" );
		xloc--;
	    }
	    else {
		System.out.println( "robot is trying to go west, but it is stuck at the edge of the world :-(" );
	    }
	    break;
	case 7:
	    if (( xloc > 0 ) && ( yloc > 0 )) {
		System.out.println( "moving robot NORTHWEST" );
		xloc--;
		yloc--;
	    }
	    else {
		System.out.println( "robot is trying to go northwest, but it is stuck at the edge of the world" );
	    }
	    break;
	} // end of switch
	world[yloc][xloc] = ROBOT;
	setRobot();
    } // end of moveRobot()


} // end of bbot
