I need help for this project: Write an application that controls the movements of a robot rat via a control panel. The robot rat will move about a "floor" represented by a two-dimensional boolean array. The robot rat has a pen attached to its tail. As the robot rat moves about the floor it will leave a mark if the pen is in the down position and not if the pen is in the up position.
Control the robot rat with several buttons displayed by your application. You can make the robot rat turn right, turn left and move forward a number of spaces that are contained in a JTextField. Another button will let you toggle the pen's position.
Each time you move the rat forward print the floor to show the rat's progress. You may print the floor pattern to the console or to a GUI component.
private int _rows = 0; private int _cols = 0; private boolean[][] floor = null;
private static final int UP = 0; private static final int DOWN = 1; private int _pen_position;
private static final int NORTH = 0; private static final int NORTH_EAST = 1; private static final int EAST = 2; private static final int SOUTH_EAST = 3; private static final int SOUTH = 4; private static final int SOUTH_WEST = 5; private static final int WEST = 6; private static final int NORTH_WEST = 7; private int _rats_direction;
private int _current_row; private int _current_col;
public RobotRat(int rows, int cols){ super("RobotRat Control Panel");
private void moveNorth(){ System.out.println("North!"); // you need to add program logic here to move rat _rats_direction = NORTH; move(); printFloor(); }
private void moveNorthEast(){ System.out.println("North East"); // you need to add program logic here to move rat _rats_direction = NORTH_EAST; move(); printFloor(); }
private void togglePen(){ switch(_pen_position){ case UP: _pen_position = DOWN; System.out.println("Pen is now down"); break; case DOWN: _pen_position = UP; System.out.println("Pen is now up"); break; default : _pen_position = UP; } }
private void move(){ int spaces_to_move = 0;
try{
spaces_to_move = Integer.parseInt(_textfield1.getText()); }catch(NumberFormatException nfe){ System.out.println("Invalid integer value!"); System.out.println("spaces to move is being set to 1."); spaces_to_move = 1; _textfield1.setText("1"); }
switch(_pen_position){
case UP: switch(_rats_direction){
case NORTH : System.out.println("Pen Up - Move North"); break; case NORTH_EAST: System.out.println("Pen Up - Move North East"); break; case EAST: System.out.println("Pen Up - Move East"); break; case SOUTH_EAST: System.out.println("Pen Up - Move South East"); break; case SOUTH: System.out.println("Pen Up - Move South"); break; case SOUTH_WEST: System.out.println("Pen Up - Move South West"); break; case WEST: System.out.println("Pen Up - Move West"); break; case NORTH_WEST: System.out.println("Pen Up - Move North West");
} break; case DOWN:switch(_rats_direction){
case NORTH : break; case NORTH_EAST: break; case EAST: break; case SOUTH_EAST: break; case SOUTH: break; case SOUTH_WEST: break; case WEST: break; case NORTH_WEST:
System.out.println("RobotRat lives!"); RobotRat rr = new RobotRat(20, 20); }
} Here is what is happening. This code brings up a control panel with seven direction buttons ( North, South, East, South East, North West...)it also has a toggle pen button. Above the button panel there is a panel with a text field to enter number of spaces to move. The dos window also appears with the message "robot rat lives" .The boolean array is 20x20. I need to set this up so that when an integer is entered in the text field and the directional button is pushed the "rat" will move position in the array without extending past the limits of the array. I also need to have this set so if the pen is toggled down the track of the rat is out put. Is that of any help???
private void moveNorth(){ System.out.println("North!"); // MY PROFESSOR SAID THAT I need to add program logic here to move rat _rats_direction = NORTH; move(); printFloor(); }
private void moveNorthEast(){ System.out.println("North East"); // MY PROFESSOR SAID THAT I need to add program logic here to move rat _rats_direction = NORTH_EAST; move(); printFloor(); }