// This program makes a simulated robot move from anywhere in a room to the // room's northwest corner, possibly dodging around one other robot in the room // that might be blocking the way to the corner. This program's main use is to // provide an opportunity to reason about whether a non-trivial algorithm is // correct or not. // History: // January 2004 -- Created by Doug Baldwin and CSci 141. import geneseo.cs.sc.Robot; import geneseo.cs.sc.RobotRoom; class CornerFinder { public static void main(String args[]) { // Set up the room: RobotRoom room = new RobotRoom(); Robot hunter = new Robot( 1, 6, Robot.EAST, room ); Robot obstacle = new Robot( 1, 1, Robot.NORTH, room ); // Preconditions: 2 robots, no interior walls, room is at least // 2 tiles high and wide // Overall plan: go north to wall (dodging obstacle if necessary) // then go west to corner (dodging obsatcle if neceesary) while ( hunter.heading() != Robot.NORTH ) { // face north hunter.turnLeft(); } // Claim: Hunter is facing north while ( hunter.okToMove() ) { hunter.move(); } // Claim: Hunter is at north wall or obstacle robot hunter.turnLeft(); // Claim: hunter is facing west if ( hunter.okToMove() ) { hunter.move(); hunter.turnRight(); while ( hunter.okToMove() ) { hunter.move(); } // Claim: Hunter is at north wall } else { hunter.turnRight(); hunter.turnRight(); // Claim: hunter is facing east if ( hunter.okToMove() ) { hunter.move(); hunter.turnLeft(); while ( hunter.okToMove() ) { hunter.move(); } } // Claim: hunter is at north wall } // Claim: hunter is at north wall // move hunter to west wall while ( hunter.heading() != Robot.WEST ) { // face west hunter.turnLeft(); } while ( hunter.okToMove() ) { hunter.move(); } // Claim: hunter is in the northwest corner, or is at an obstacle // along the north wall. Hunter is facing west. hunter.turnLeft(); // Claim: hunter is facing south if ( hunter.okToMove() ) { hunter.move(); hunter.turnRight(); while ( hunter.okToMove() ) { hunter.move(); } // Claim: Hunter is at the west wall, in the 2nd-north-most row, // facing west. hunter.turnRight(); if ( hunter.okToMove() ) { hunter.move(); // Claim: Hunter is in the northwest corner, i.e., the // western-most free tile in the north row } else { // Claim: The obstacle is in the northwest corner hunter.turnRight(); hunter.move(); hunter.turnLeft(); hunter.move(); // Claim: Hunter is on the western-most free tile in the north row } } else { // Claim: Hunter is in the northwest corner (because hunter was // on the north wall, either in the corner or at an obstacle, // before the conditional; the room is at least 2 tiles high, // so the only way it can not be OK for hunter to move south is // if the obstacle is to hunter's south, meaning that the obstacle // to the west must be the wall) } // Claim: Hunter is on the western-most free tile of the north row. } }