aspose file tools*
The moose likes Java in General and the fly likes Hoe can i make the rat to move? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

Hoe can i make the rat to move?

Gregori Smith
Greenhorn

Joined: Feb 11, 2006
Posts: 6
hi everyone

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.

I got the GUI part, but i can't move the rat!!!

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.


import javax.swing.*;
import java.awt.event.*;
import java.awt.*;


public class RobotRat extends JFrame implements ActionListener {

private JPanel _panel1 = null;
private JPanel _panel2 = null;
private JLabel _label1 = null;
private JTextField _textfield1 = null;
private JButton _button1 = null;
private JButton _button2 = null;
private JButton _button3 = null;
private JButton _button4 = null;
private JButton _button5 = null;
private JButton _button6 = null;
private JButton _button7 = null;
private JButton _button8 = null;
private JButton _button9 = null;

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");

_rows = rows;
_cols = cols;
_pen_position = UP;
_rats_direction = EAST;
_current_row = 0;
_current_col = 0;

floor = new boolean[_rows][_cols];

_panel1 = new JPanel();
_panel2 = new JPanel();

_label1 = new JLabel("Spaces:");
_textfield1 = new JTextField(10);

_button1 = new JButton("NW");
_button1.addActionListener(this);

_button2 = new JButton("N");
_button2.addActionListener(this);

_button3 = new JButton("NE");
_button3.addActionListener(this);

_button4 = new JButton("W");
_button4.addActionListener(this);

_button5 = new JButton("Toggle Pen");
_button5.addActionListener(this);

_button6 = new JButton("E");
_button6.addActionListener(this);

_button7 = new JButton("SW");
_button7.addActionListener(this);

_button8 = new JButton("S");
_button8.addActionListener(this);

_button9 = new JButton("SE");
_button9.addActionListener(this);


_panel1.add(_label1);
_panel1.add(_textfield1);

_panel2.setLayout(new GridLayout(3,3,0,0));
_panel2.add(_button1);
_panel2.add(_button2);
_panel2.add(_button3);
_panel2.add(_button4);
_panel2.add(_button5);
_panel2.add(_button6);
_panel2.add(_button7);
_panel2.add(_button8);
_panel2.add(_button9);

this.getContentPane().setLayout(new GridLayout(2,1,0,0));
this.getContentPane().add(_panel1);
this.getContentPane().add(_panel2);
this.setSize(300, 300);
this.setLocation(200, 200);
this.show();



} // end constructor





public void actionPerformed(ActionEvent ae){

if(ae.getActionCommand().equals("NW")){
moveNorthWest();
}else if(ae.getActionCommand().equals("N")){
moveNorth();
}else if(ae.getActionCommand().equals("NE")){
moveNorthEast();
}else if(ae.getActionCommand().equals("Toggle Pen")){
togglePen();
}


} // end actionPerformed()


private void printFloor(){
for(int i = 0; i < floor.length; i++){
for(int j = 0; j < floor[i].length; j++){
if( floor[i][j] ){
System.out.print("-");
}else {
System.out.print("0");
}
}
System.out.println();
}

}


private void initializeFloor(){
for(int i = 0; i < floor.length; i++){
for(int j = 0; j < floor[i].length; j++){
floor[i][j] = false;
}
}
}


private void moveNorthWest(){
System.out.println("North West!");
_rats_direction = NORTH_WEST;
move();
printFloor();
}

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:

}
} // end outer switch


} // end move() method



public static void main(String[] args){

System.out.println("RobotRat lives!");
RobotRat rr = new RobotRat(20, 20);
}




}

private void moveNorthWest(){
System.out.println("North West!");
_rats_direction = NORTH_WEST;
move();
printFloor();
}

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();
}
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

This is a cross-post. Please continue this discussion in the intermediate forum. (And please don't cross-post.)
[ February 11, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Hoe can i make the rat to move?