// 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= 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 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