/** Check for a win or loss in the game. */ private void checkWin() { if (isEndPosition(px, py)) { gameOver = true; userWins = true; } else if (manualDriver.getRobot().hasStopped()) { gameOver = true; userWins = false; } }
/** Sets up the robot driver for this maze depending on the type selected */ private void setUpRobotDriver() { switch (driverType) { case "Wizard": driver = new Wizard(); driver.setDistance(mazedists); driver.setRobot(new BasicRobot(this)); break; case "Wall Follower": driver = new WallFollower(); driver.setRobot(new BasicRobot(this)); break; case "Curious Mouse": driver = new CuriousMouse(); driver.setDimensions(mazew, mazeh); driver.setRobot(new BasicRobot(this)); break; // The default case is a manual driver. default: manualDriver = new ManualDriver(); driver = manualDriver; manualDriver.setRobot(new BasicRobot(this)); break; } }
/** * Method incorporates all reactions to keyboard input in original code, after refactoring, Java * Applet and Java Application Panel call this method to communicate input. */ public boolean keyDown(int key) { // possible inputs for key: unicode char value, 0-9, A-Z, Escape, 'k','j','h','l' // react to input for directions // react to input for displaying a map of the current path or of the overall maze (on/off toggle // switch) // react to input to display solution (on/off toggle switch) switch (key) { case 'k': case '8': // UP if (driverType.equals("Manual")) { // Move the robot by way of the driver. try { // Move the robot. manualDriver.moveForward(); checkWin(); } catch (Exception e) { checkWin(); } } break; case 'h': case '4': // LEFT if (driverType.equals("Manual")) { // turn the robot by way of the driver. try { // Move the robot. manualDriver.turnLeft(); checkWin(); } catch (Exception e) { checkWin(); } } break; case 'l': case '6': // RIGHT if (driverType.equals("Manual")) { // Turn the robot by way of the driver. try { // Move the robot. manualDriver.turnRight(); checkWin(); } catch (Exception e) { checkWin(); } } break; case 'j': case '2': // DOWN // Move the robot by way of the driver. if (driverType.equals("Manual")) { try { // Move the robot. manualDriver.moveBackward(); checkWin(); } catch (Exception e) { checkWin(); } } break; case 'm': // SHOW/HIDE MAP mapMode = !mapMode; if (driverType.equals("Manual")) { notifyViewerRedraw(); } break; case 'z': // SHOW/HIDE WALLS showMaze = !showMaze; if (driverType.equals("Manual")) { notifyViewerRedraw(); } break; case 's': // SHOW/HIDE SOLUTION showSolution = !showSolution; if (driverType.equals("Manual")) { notifyViewerRedraw(); } break; } return true; }