// This program defines and exercises a subclass of the science of computing // robots that do various things involving red tiles -- thus I call the // robots RedRobots. The real purpose of this class is to give people // practice writing recursive algorithms 'though. // History: // September 2003 -- Created by Doug Baldwin. import geneseo.cs.sc.*; import java.awt.Color; class RedRobot extends Robot { // Initialize a RedRobot with default position, orientation, and room. public RedRobot() { super(); } // Initialize a RedRobot with a client-specified position, orientation, // and room. public RedRobot( int col, int row, int heading, RobotRoom room ) { super( col, row, heading, room ); } // Move a RedRobot forward until it is standing on a red tile. This // is based on the recursive insight that if the robot is already // standing on a red tile, then there is nothing to do. Otherwise, // move the robot one tile forward and then move it further until it // is standing on a red tile. // Precondition: There is a red tile between the robot and whatever // wall it is facing ("between" may mean the tile the robot is standing // on). public void toRed() { if ( this.colorOfTile() != Color.red ) { this.move(); this.toRed(); } } // Draw a diagonal red line n steps long. This algorithm is based on the // recursive insight that to draw a 0-step diagonal one need do nothing, // whereas to draw a longer diagonal one paints the tile the robot is on, // moves forward and right, and then paints a one step shorter diagonal. // Preconditions: The robot is standing where the lower left end of the // diagonal will be, and n >= 0. public void diagonal( int n ) { if ( n > 0 ) { this.paint( Color.red ); this.move(); this.turnRight(); this.move(); this.turnLeft(); this.diagonal( n - 1 ); } } // Count the red tiles between the robot and the wall. This algorithm is // based on the recursive insight that if the robot is at a wall, then // the number of red tiles is either 1 or 0, according to whether the // robot is standing on a red tile or some other color. Otherwise, the // number of red tiles is 1 plus the number of red tiles between the // next tile and the wall, if the robot is standing on a red tile, or // the number of red tiles between the next tile and the wall if the // robot is initially standing on some other color. public int countRed() { if ( this.okToMove() ) { if ( this.colorOfTile() == Color.red ) { this.move(); return 1 + this.countRed(); } else { this.move(); return this.countRed(); } } else { if ( this.colorOfTile() == Color.red ) { return 1; } else { return 0; } } } // Square an integer. This method has nothing to do with robots and // red tiles, but it is part of the assignment that this class solves. // This algorithm is based on the recursive insight that 0^2 = 0, and // for larger n, n^2 = (n-1)^2 + 2n - 1. public static int square( int n ) { if ( n > 0 ) { return square( n - 1 ) + 2 * n - 1; } else { return 0; } } // The main method creates and exercises a RedRobot. Note that I also // create a custom room for the robot to move in, so that I can place // red tiles in strategic places. public static void main( String args[] ) { RobotRoom redRoom = new RobotRoom( "10 10 1 2 R 1 6 R 8 1 R 8 4 R" ); // Test "toRed", both for a robot initially on a red tile, and for a robot not // initially on a red tile: RedRobot redFinder1 = new RedRobot( 1, 6, Robot.NORTH, redRoom ); redFinder1.toRed(); RedRobot redFinder2 = new RedRobot( 1, 5, Robot.NORTH, redRoom ); redFinder2.toRed(); // Test the red diagonal method: RedRobot diag = new RedRobot( 1, 8, Robot.NORTH, redRoom ); diag.diagonal( 5 ); // Count red tiles, both when there are some between the robot and the // wall, and when there aren't: RedRobot counter1 = new RedRobot( 7, 8, Robot.NORTH, redRoom ); System.out.println( "There are " + counter1.countRed() + " red tiles in column 7." ); RedRobot counter2 = new RedRobot( 8, 8, Robot.NORTH, redRoom ); System.out.println( "There are " + counter2.countRed() + " red tiles in column 8." ); // Test the square method, for two special cases (0 and 1, both square // to themselves), and one "typical" case: System.out.println( "0^2 = " + square(0) ); System.out.println( "1^2 = " + square(1) ); System.out.println( "3^2 = " + square(3) ); } }