This site requires JavaScript, please enable it in your browser!
Greenfoot back
mattjames
mattjames wrote ...

2013/9/12

Help please!

1
2
mattjames mattjames

2013/9/12

#
Hi guys. Coming to the end of a game I am writing but I have hit a problem. The robot will move and fuel will be used, and then the program is to test whether their is a person in the same square to be picked up, if there is space in the robot's passenger bay. If not then it cannot pick anybody up. It then adds one to the score if you pick someone up, and then has to test if the robot is at the basecamp, and if it is, it has to drop off the passengers it is carrying so it can go and pick up more. Here is the code for the relevant sections:
public void passengerpickuptester(){
        Actor person;    //tests to see if there is a person on the same square
        person=getOneObjectAtOffset(0,0,person.class);
        if(person != null) { //there is a person on that square

            testifcanpickuppassenger(); //go and see if there is space to pick up a passenger
        }
        else{ //there is not a person in that square
            atbasecamp(); //go see if you are at the basecamp

        }
        
    }

    public void testifcanpickuppassenger() {
        Actor person;    
        person=getOneObjectAtOffset(0,0,person.class);
        if(passengerscarrying<maximumcarrying){ 
        
            World world;
            world=getWorld();
            world.removeObject(person);
            passengerscarrying=passengerscarrying+1;

            scoringpassengers(); //score for the passenger you just picked up
        }
        else{
            atbasecamp();

        }
    }
    

    public void scoringpassengers(){

        world worldWorld = (world) getWorld(); //counter2 shows how many passengers you are currently carrying at the bottom of the screen
        Counter2 counter2 = worldWorld.getCounter2();
        counter2.bumpCount2(1);
        atbasecamp();

    } 

    public void atbasecamp(){

        Actor basecamp;    //this is the problem - when the robot lands on the basecamp it doesn't drop off passengers
        basecamp=getOneObjectAtOffset(0,0,basecamp.class);
        if(basecamp != null) { //there is a person on that square
            if(passengerscarrying>0){
            world worldWorld = (world) getWorld();
            Counter2 counter2 = worldWorld.getCounter2();
            counter2.bumpCount2(-1); //need to sort this
            passengersleft=passengersleft-1;
            passengerscarrying=passengerscarrying-1;
            //basecampunload();

        }
        else{
            wintester();

        }
        }
        else{ //there is not a person in that square
            wintester();

        }
    }
Any ideas????
jamesnidd jamesnidd

2013/9/12

#
yeah if you post a counter class inside the for loop it'll be sorted
danpost danpost

2013/9/12

#
You are calling 'atbasecamp' at the end of (at least) two different methods. Better would be to call it once from the 'act' method directly after calling 'passengerpickuptester'. Does 'wintester' check that passengersleft is zero? is not the value for passengersleft being displayed in a counter also? Changing the 'if' in line 48 to 'while' should drop off all the passengerscarrying. I believe that the following is a little more straight-forward:
// replace all starting at line 57 with:
        wintester();
    }
}
This should only check for win at the time you drop off passengers.
mattjames mattjames

2013/9/18

#
The wintester section checks to see if there are no passengers left in the world and that the fule level is not less than zero.
danpost danpost

2013/9/18

#
The fuel level should be checked as you decrease the amount of fuel and has nothing to do with winning, but losing. Just as the passenger count should be checked as the number of passengers in the world decreases which goes toward winning. There is not need to check the values of the fields unless they are changed; and the direction of the change can play a role in what check is needed, if any.
mattjames mattjames

2013/9/19

#
Ok but why wont the code above drop off the passengers at the base camp then?
mattjames mattjames

2013/9/19

#
Just did a little test to see if was a problem with the basecamp using this code:
 public void atbasecamp(){

        Actor basecamp;    
        basecamp=getOneObjectAtOffset(0,0,basecamp.class);
        if(basecamp != null) {
            //there is a basecamp on that square
             tester=true;
            if(passengerscarrying>0){
           
                world worldWorld = (world) getWorld();
            Counter2 counter2 = worldWorld.getCounter2();
            counter2.bumpCount2(-1); //need to sort this
            passengersleft=passengersleft-1;
            passengerscarrying=passengerscarrying-1;
            //basecampunload();

        }
        else{
            wintester();

        }
        }
        else{ //there is not a person in that square
            wintester();

        }
    }
The boolean tester should turn true if it is on the same square, however even when the robot clearly if, the boolean remains false. Any ideas?
danpost danpost

2013/9/19

#
You may have to provide the entire class code to find the problem.
davmac davmac

2013/9/19

#
The boolean tester should turn true if it is on the same square, however even when the robot clearly if, the boolean remains false. Any ideas?
Perhaps the code isn't even running because the method isn't being called. Try putting a System.out.println(...) at the start of the method.
public void atbasecamp(){
    System.out.println("atbasecamp() called.");
    // etc
mattjames mattjames

2013/9/22

#
Davmac - sorry for long reply been a busy few days - I put this bit in and checked using a normal boolean and it came up as it had been going through the method, so that isn't the problem.
danpost danpost

2013/9/22

#
Try naming your 'basecamp' class 'Basecamp', with a capital 'B'. The compiler may be checking the class name with null instead of the field. Anyways, by convention, all class names should begin with capital letters and all field names should begin with a lowercase letter (each subsequent word in both name types is usually capitalized). For testing purposes, change the field name in lines 3, 4 and 5 to 'bc' and try it.
mattjames mattjames

2013/9/24

#
Changed the name to Basecamp and still it cannot tell that the robot is in the same square. Game compiles and runs without error but will not drop off passengers. Will provide full code now
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class robottrackslarge here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class robot extends Actor //need to score passengers, add second level
{
    private int choice;
    public void addedToWorld(World w)  
    {  
        world worldWorld = (world) w;  
        Counter3 counter3 = worldWorld.getCounter3(); 

        choice = counter3.getCount();  
    }  
    private Counter2 theCounter2;
    private Counter theCounter;
    public int maximumcarrying=0; //maximum the robot can carry
    private int score=0;
    private int scorelevelone=150;//this sets the score
    private int scoreleveltwo=150;
    private boolean win=false;
    private int targetscore; //may need to remove this
    private int level=1;
    private int passengersleft=4; //number of passengers the robot needs to drop off
    private int passengerscarrying=0; //number of passengers the robot is carrying
public int locationX;
public int locationY;
public boolean tester;


    /**
     * Act - do whatever the robot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {

        maximumcarryingsetter();// Add your action code here.
    }  
    //maybe try using a boolean to see whether the game has been won

     public void maximumcarryingsetter(){ //this will decide whether to keep the counter at 150 or lower it to 130
        if (choice==1){
            maximumcarrying=1;
             wintester();

        }
        if (choice==2){
                        maximumcarrying=2;
             wintester();

        }
        if (choice==3){
                        maximumcarrying=3;
            wintester();

        }
        if (choice==4){
                        maximumcarrying=1;
            wintester();

        }
        if (choice==5){
                       maximumcarrying=2;
            wintester();

        }
        if (choice==6){
                        maximumcarrying=3;
            wintester();

        }
        if (choice==7){
                        maximumcarrying=1;
            wintester();

        }
        if (choice==8){
                        maximumcarrying=2;
            wintester();

        }
        if (choice==9){
                        maximumcarrying=3;
            wintester();

        }

    }
    public void setcounter(){ //this will decide whether to keep the counter at 150 or lower it to 130
        if (level==2){

            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-20);
            scoretester();
        }
        else{
            scoretester();

        }

    }

    public void wintester(){ //this will test if the game is won
        if(win==false){
            scoretester();

        }
        else{
            win=false;
            setcounter(); //set the counter to 130 then begin level two

        }

    }

    public void scoretester(){ //this part checks to see that the game hasnt been won yet and links to wintester()
        world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();

        if (passengersleft==0&&counter.getCount()>0){
            win=true;
        }
        else if(counter.getCount()<1&& 
        passengersleft>0){
            //display failure code and send to main menu.

        }
        else if(counter.getCount()>0&&
        passengersleft>0){
            canmove();

        }

    }

    public void canmove(){
        int locationy =getY();
        int testedlocationy;
        testedlocationy=locationy+1;

        if(testedlocationy>9){

            movementnotdown();

        }
        else{
            movement();
        }
    }

    public void movement()
    {
        if(Greenfoot.isKeyDown("right"))
        { 

            move(1);
            Greenfoot.delay(10);
            floortesterIce();

        }
        else if(Greenfoot.isKeyDown("left"))
        {
            move(-1);
            Greenfoot.delay(10);
            floortesterIce();

        }
        else if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(),getY()-1);
            Greenfoot.delay(10);
            floortesterIce();

        }
        else if(Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(),getY()+1);
            Greenfoot.delay(10);
            floortesterIce();
        }
    }

    public void movementnotdown()
    {
        if(Greenfoot.isKeyDown("right"))
        {
            move(1);
            Greenfoot.delay(10);
            floortesterIce();

        }
        else if(Greenfoot.isKeyDown("left"))
        {
            move(-1);
            Greenfoot.delay(10);
            floortesterIce();
        }
        else if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(),getY()-1);
            Greenfoot.delay(10);
            floortesterIce();
        }

    }

    public void floortesterIce()
    {
        Actor Ice;    
        Ice=getOneObjectAtOffset(0,0,Ice.class);
        if(Ice != null) {
            //this needs to go

            scoringmovementice();

        }
        else{
            floortestergrass();
        }
    }

    public void floortestergrass()
    {
        Actor grass;    
        grass=getOneObjectAtOffset(0,0,grass.class);

        if(grass != null) {

            scoringmovementgrass();

        }
        else{
            floortesterrock();
        }
    }

    public void floortesterrock()
    {
        Actor rock;    
        rock=getOneObjectAtOffset(0,0,rock.class);

        if(rock != null) {

            scoringmovementrock();

        }
        else{
            canmove();
        }
    }

    public void scoringmovementgrass(){

        if (choice==1){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-3);
            passengerpickuptester();

        }
        if (choice==2){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-4);
            passengerpickuptester();

        }
        if (choice==3){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-5);
            passengerpickuptester();

        }
        if (choice==4){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-3);
            passengerpickuptester();

        }
        if (choice==5){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-4);
            passengerpickuptester();

        }
        if (choice==6){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-5);
            passengerpickuptester();

        }
        if (choice==7){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-1);
            passengerpickuptester();

        }
        if (choice==8){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-2);
            passengerpickuptester();

        }
        if (choice==9){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-3);
            passengerpickuptester();

        }

    }

    public void scoringmovementrock(){

        if (choice==1){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-3);
            passengerpickuptester();

        }
        if (choice==2){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-4);
            passengerpickuptester();

        }
        if (choice==3){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-5);
            passengerpickuptester();

        }
        if (choice==4){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-3);
            passengerpickuptester();

        }
        if (choice==5){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-4);
            passengerpickuptester();

        }
        if (choice==6){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-5);
            passengerpickuptester();

        }
        if (choice==7){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-2);
            passengerpickuptester();

        }
        if (choice==8){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-3);
            passengerpickuptester();

        }
        if (choice==9){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-4);
            passengerpickuptester();

        }
    }
    public void scoringmovementice(){

        if (choice==1){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-1);
            passengerpickuptester();

        }
        if (choice==2){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-2);
            passengerpickuptester();

        }
        if (choice==3){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-3);
            passengerpickuptester();

        }
        if (choice==4){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-3);
            passengerpickuptester();

        }
        if (choice==5){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-4);
            passengerpickuptester();

        }
        if (choice==6){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-5);
            passengerpickuptester();

        }
        if (choice==7){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-3);
            passengerpickuptester();

        }
        if (choice==8){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-4);
            passengerpickuptester();

        }
        if (choice==9){
            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-5);
            passengerpickuptester();

        }
    }
    public void passengerpickuptester(){
        Actor person;    //tests to see if there is a person on the same square
        person=getOneObjectAtOffset(0,0,person.class);
        if(person != null) { //there is a person on that square

            testifcanpickuppassenger();
        }
        else{ //there is not a person in that square
            atbasecamp();

        }
        
    }

    public void testifcanpickuppassenger() {
        Actor person;    
        person=getOneObjectAtOffset(0,0,person.class);
        if(passengerscarrying<maximumcarrying){
        
            World world;
            world=getWorld();
            world.removeObject(person);
            passengerscarrying=passengerscarrying+1;

            scoringpassengers(); //score for the passenger you just picked up
        }
        else{
            atbasecamp();

        }
    }
    

    public void scoringpassengers(){

        world worldWorld = (world) getWorld();
        Counter2 counter2 = worldWorld.getCounter2();
        counter2.bumpCount2(1);
        atbasecamp();

    } 

    public void atbasecamp(){

        Actor Basecamp;    
        Basecamp=getOneObjectAtOffset(0,0,Basecamp.class);
        if(Basecamp != null) {
            //there is a basecamp on that square
             tester=true;
             System.out.println("hello");
            if(passengerscarrying>0){
           
                world worldWorld = (world) getWorld();
            Counter2 counter2 = worldWorld.getCounter2();
            counter2.bumpCount2(-1); //need to sort this
            passengersleft=passengersleft-1;
            passengerscarrying=passengerscarrying-1;
            //basecampunload();

        }
        else{
            wintester();

        }
        }
        else{ //there is not a person in that square
            wintester();

        }
    }

    //public void basecampunload(){ this is all old i just dont want to lose it yet :-(
        //if(passengerscarrying>0){
         //   world worldWorld = (world) getWorld();
           // Counter2 counter2 = worldWorld.getCounter2();
           // counter2.bumpCount2(-1); //need to sort this
           // passengersleft=passengersleft-1;
           // passengerscarrying=passengerscarrying-1;
           // basecampunload();

       // }
       // else{
        //    wintester();

       // }

    //}

}
davmac davmac

2013/9/24

#
So you are seeing the "hello" message? In that case 'tester' is definitely getting set to true. I guess the next question is, what makes you think that it is false, i.e. how are you checking the value? And, are you definitely checking the same instance of robot or is it possible that you've made another robot (via "new robot()") and are checking the new one instead?
danpost danpost

2013/9/24

#
By looking over the given code for the robot, I was not able to determine why you are getting those results. However, there are a few things that could help in making you a better programmer. First, does 'maximumcarryingsetter' have to be called every act method or is just one time sufficient? if once is good, then set it in the 'addedToWorld' method after 'choice' is set. You can use 'maximumcarrying = (choice+2)%3+1;' to set its value. Also, you can set 'theCounter' and 'theCounter2' in the 'addedToWorld' method and use those fields for referencing those counters throughout your class. It seems like you wrote the code backwards (not saying that is bad, but takes a lot more coding) and I am not used to code written that way. The main drawback is that you are always checking for situations that do not have to be checked every act method. Such as winning, which I would think only needs to be checked when a passenger is dropped off. Also, you are calling 'wintester', 'atbasecamp' and 'floortesterIce' multiple times where all paths lead to executing those methods. By placing a single call to them at the appropriate places, you can still accomplish the goal. For example, move 'Greenfoot.delay(10); floortesterIce();' below and outside the last 'else if' clause in the 'movement' and 'movementnotdown' methods. A similar thing can be done with 'atbasecamp' and 'wintester' (in the 'atbasecamp' method). Checking for not being able to move down can be done directly in the 'movement' method; so you would only have to call it and can remove the 'movementnotdown' method. For a revised untested class code, post a request.
mattjames mattjames

2013/9/26

#
If you make it better, then I will be very grateful. As for the problem, I didnt put in the System.out.println("atbasecamp() called."); but created another boolean which would be set to false originally and changed to true if you go into that method. When I move the robot one square, it changes to true when I move it, so this shows it does go in this method. I am simply pausing the game to check and then inspecting the variables. It seems like it is a problem with the if(Basecamp != null) { part.
There are more replies on the next page.
1
2