Summary


Complete RobotRat.java Source Code Listing

Example 3.14: RobotRat.java (Complete Listing)

image from book
 1     /* ******************************************************************* 2        Class:   IST 149 3        Project: Robot Rat 4        Student: Rick Miller 5     ******************************************************************* */ 6 7     import java.io.*; 8 9     /** 10    * @author <b> Rick Miller </b> 11    * @version 1.0 12    * 13    */ 14    public class RobotRat { 15       // private instance attributes 16 17          private BufferedReader console = null; 18          private int pen_position = 0; 19          private int direction = 0; 20          private boolean floor[][] = null; 21          private int current_row = 0; 22          private int current_col = 0; 23 24        // class constants 25 26          private static final int NORTH = 0; 27          private static final int SOUTH = 1; 28          private static final int EAST = 2; 29          private static final int WEST = 3; 30          private static final int UP = 0; 31          private static final int DOWN = 1; 32 33        /** 34        * RobotRat constructor 35        * @param rows The number of rows to create in the floor array 36        * @param cols The number of columns to create in the floor array 37        */ 38          public RobotRat(int rows, int cols){ 39            //Initialize RobotRat attributes 40            console = new BufferedReader(new InputStreamReader(System.in)); 41            direction = EAST; 42            pen_position = UP; 43            current_row = 0; 44            current_col = 0; 45            floor = new boolean[rows][cols]; 46            initializeFloor(); 47          } 48 49          /** 50          * Initializes each floor array element to false 51          */ 52          private void initializeFloor(){ 53            for(int i = 0; i<floor.length; i++){ 54               for(int j = 0; j<floor[i].length; j++){ 55             floor[i][j] = false; 56             } 57            } 58          } 59 60          /** 61          * Repeatedly displays the RobotRat control menu and 62          * processes user commands. 63          */ 64          public void run(){ 65            while(true){ 66              printMenu(); 67              processMenuChoice(); 68            } 69          } 70 71          /** 72          * Prints the RobotRat control menu to the console 73          */ 74          private void printMenu(){ 75             System.out.println("\n\n"); 76             System.out.println(" Robot Rat Control Menu"); 77             System.out.println("\n"); 78             System.out.println(" 1. Pen Up"); 79             System.out.println(" 2. Pen Down"); 80             System.out.println(" 3. Turn Right"); 81             System.out.println(" 4. Turn Left"); 82             System.out.println(" 5. Move Forward"); 83             System.out.println(" 6. Print Floor"); 84             System.out.println(" 7. Exit"); 85             System.out.println("\n\n\n"); 86          } 87 88          /** 89          * Gets number of spaces to move the RobotRat 90          * @return An integer value represeting spaces to move 91          */ 92          private int getSpaces(){ 93            int temp = 0; 94            try{ 95             temp = Integer.parseInt(console.readLine()); 96            }catch(Exception e){ 97            System.out.println("Spaces has been set to zero!"); 98            } 99          return temp; 100        } 101 102        /** 103        * Reads text string user input and returns the first character 104        * @return First character of user text string input 105        */ 106         private char readChar(){ 107            String s = null; 108            System.out.print("Please select from the menu: "); 109            try{ 110              s = console.readLine(); 111            }catch(Exception ignored){} 112            return s.charAt(0); 113           } 114 115        /** 116        * Menu processing method. 117        * @see #readChar() 118        */ 119          private void processMenuChoice(){ 120 121            switch(readChar()){ 122                case '1': setPenUp(); 123                        break; 124                case '2': setPenDown(); 125                        break; 126                case '3': turnRight(); 127                        break; 128                case '4': turnLeft(); 129                    break; 130                case '5': move(); 131                    break; 132                case '6': printFloor(); 133                    break; 134                case '7': exit(); 135                default: printErrorMessage(); 136            } 137          } 138 139         /** 140         * Sets the RobotRat's pen to the UP position 141         */ 142         private void setPenUp(){ 143           pen_position = UP; 144           System.out.println("The pen_position is UP"); 145         } 146 147         /** 148         * Sets the RobotRat's pen to the DOWN position 149         */ 150         private void setPenDown(){ 151           pen_position = DOWN; 152           System.out.println("pen_position is DOWN"); 153         } 154 155 156         /** 157         * Turns the RobotRat to the right 158         */ 159         private void turnRight(){ 160           switch(direction){ 161               case NORTH:  direction = EAST; 162                             System.out.println("RobotRat facing EAST"); 163                             break; 164               case EAST:   direction = SOUTH; 165                             System.out.println("RobotRat facing SOUTH"); 166                             break; 167               case SOUTH:  direction = WEST; 168                             System.out.println("RobotRat facing WEST"); 169                             break; 170                case WEST:  direction = NORTH; 171                             System.out.println("RobotRat facing NORTH"); 172                             break; 173                default:    direction = EAST; 174                             System.out.println("RobotRat facing EAST"); 175          } 176         System.out.println("turnRight() method"); 177       } 178 179        /** 180        * Turns the RobotRat to the left 181        */ 182        private void turnLeft(){ 183          switch(direction){ 184              case NORTH:  direction = WEST; 185                            System.out.println("RobotRat facing WEST"); 186                            break; 187              case EAST:   direction = NORTH; 188                            System.out.println("RobotRat facing NORTH"); 189                            break; 190              case SOUTH:  direction = EAST; 191                            System.out.println("RobotRat facing EAST"); 192                            break; 193              case WEST:   direction = SOUTH; 194                            System.out.println("RobotRat facing SOUTH"); 195                            break; 196                 default:        direction = EAST; 197           System.out.println("RobotRat facing EAST"); 198          } 199       } 200 201         /** 202         * This method moves the RobotRat about the floor. 203         * @see #getSpaces() 204         */ 205         private void move(){ 206           System.out.print("Please enter spaces to move: "); 207           int spaces = getSpaces(); 208           switch(pen_position){ 209               case UP: switch(direction){ 210                           case NORTH: if((current_row - spaces) <= 0) 211                           current_row = 0; 212                            else current_row = current_row - spaces; 213                            break; 214                           case SOUTH:  if((current_row + spaces) >= (floor[0].length -1)) 215                           current_row =  (floor[0].length -1); 216                            else current_row = current_row + spaces; 217                            break; 218                           case EAST:   if((current_col + spaces) >= (floor.length -1)) 219                           current_col =  (floor.length -1); 220                            else current_col = current_col + spaces; 221                            break; 222                           case WEST: if((current_col - spaces) <= 0) 223                           current_col = 0; 224                            else current_col = current_col - spaces; 225                            break; 226               } 227               break; 228               case DOWN: switch(direction){ 229                           case NORTH: if((current_row - spaces) <= 0){ 230                           while(current_row >= 1) 231                           floor[current_row--][current_col] = true; 232                           } 233                           else while(spaces-- > 0) 234                           floor[current_row--][current_col] = true; 235                           break; 236                           case SOUTH: if((current_row + spaces)  >= (floor[0].length -1)){ 237                           while(current_row < (floor[0].length-1)) 238                           floor[current_row++][current_col] = true; 239                           } 240                           else while(spaces-- > 0) 241                           floor[current_row++][current_col] = true; 242                           break; 243                           case EAST: if((current_col + spaces)   >= (floor.length -1)){ 244                           while(current_col < (floor.length-1)) 245                           floor[current_row][current_col++] = true; 246                           } 247                           else while(spaces-- > 0) 248                           floor[current_row][current_col++] = true; 249                           break; 250                           case WEST: if((current_col - spaces)   <= 0){ 251                           while(current_col >= 1) 252                           floor[current_row][current_col--] = true; 253                           } 254                           else while(spaces-- > 0) 255                           floor[current_row][current_col--] = true; 256                           break; 257               } 258               break; 259           } 260       } 261 262          /** 263         * Prints the floor array pattern to the console 264                                                                        * @see #floor 265         */ 266         private void printFloor(){ 267           for(int i = 0; i<floor.length; i++){ 268             for(int j=0; j<floor[i].length; j++){ 269              if(floor[i][j] == true) 270                   System.out.print('*'); 271              else System.out.print('0'); 272             } 273            System.out.println(); 274           } 275          } 276 277          /** 278         * Exits the RobotRat program 279         * @see System#exit 280         */ 281         private void exit(){ 282           System.exit(0); 283         } 284 285 286         /** 287         * Prints error text message when invalid RobotRat menu item entered 288         */ 289         private void printErrorMessage(){ 290           System.out.println("Please enter a valid menu choice!"); 291         } 292 293 294       /** 295         * The main method. This makes RobotRat an application. 296         */ 297         public static void main(String[] args){ 298           RobotRat rr = new RobotRat(20, 20); 299           rr.run(); 300        } 301    }//end RobotRat class definition
image from book




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net