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

2014/11/28

Treasure Hunt

1
2
3
4
danpost danpost

2014/11/30

#
What is up the the '*2's?
Mickey09 Mickey09

2014/11/30

#
But, when I used this code, the second actor, just kept going and did not turn around. I want to double the limit each act cycle. (x2)
public class Player2 extends Actor
{
    private int limit = 2;
    private int steps = 1;
   
    /**
     *
     */
   public void act() 
   {
        move(); 
   }
   public void move()
   {
      move(1); 
      steps = steps *2;
         if (steps == limit)
         {
          turn (180); 
          steps = 0;
          limit = limit *2;
         }
      TouchingTreasure();
   }
   public void TouchingTreasure() 
   {   
        Actor treasure = getOneObjectAtOffset(0, 0, Treasure.class); 
        if (treasure !=null)
        {
            System.out.println("Good Job! You found the treasure!");
            Greenfoot.stop();
    
        }
   }
    public int getLimit()
   {
       return limit;
   }
}
danpost danpost

2014/11/30

#
Mickey09 wrote...
No, what I am referring to is the other algorithm, where the steps double after each cycle.
You would only need to double the 'limit' field when it changes. You should not need do anything else.
Mickey09 Mickey09

2014/11/30

#
oh, lol.
public class Player2 extends Actor
{
    private int limit = 2;
    private int steps = 1;
   
    /**
     *
     */
   public void act() 
   {
        move(); 
   }
   public void move()
   {
      move(1); 
      steps = steps + 1;
         if (steps == limit)
         {
          turn (180); 
          steps = 0;
          limit = limit*2;
         }
      TouchingTreasure();
   }
   public void TouchingTreasure() 
   {   
        Actor treasure = getOneObjectAtOffset(0, 0, Treasure.class); 
        if (treasure !=null)
        {
            System.out.println("Good Job! You found the treasure!");
            Greenfoot.stop();
    
        }
   }
    public int getLimit()
   {
       return limit;
   }
} 
danpost danpost

2014/11/30

#
You should have the same initial values for the fields declared on lines 3 and 4 (one for the initial limit and zero for the steps).
Mickey09 Mickey09

2014/11/30

#
Alright! Thank you! That was driving me nuts for days!. Next question would be, if I wanted a screen or image overlay to appear once the treasure has been found, I would first create the image and add to the greenfoot folder, then I would use the getImage command? under the TouchingTreasure() method?
danpost danpost

2014/11/30

#
'getImage' will only get an image that is assigned to an Actor object. It will not get an image from your scenario 'images' folder. Maybe you meant to say 'setImage', which could be used on 'treasure' (the variable that is declared on line 27 and found not to be null on line 28. This might work provided that you do not an act method in the Treasure class.
Mickey09 Mickey09

2014/11/30

#
Cool! Thanks!
Mickey09 Mickey09

2014/11/30

#
What is the code to figure out the time it takes for the actor to intersect with the treasure?
danpost danpost

2014/11/30

#
Add another 'int' field called 'totalSteps' and increment it every act cycle. Then you will have the number of steps taken to find the treasure.
Mickey09 Mickey09

2014/11/30

#
Right, I have that. I would like a time that it takes. If you know that code. It would be displayed in the console.
danpost danpost

2014/11/30

#
The system clock cannot take into effect any inconsistencies due to lag or variance in frames per second that the scenario runs. Counting the act cycles is a more precise measurement of how long it takes. If you want, you could divide it by 5 or 6 and show the value with one decimal place. It will appear to be close to the number of seconds taken if your scenario is running at nominal speeds (speed slider near the middle).
Mickey09 Mickey09

2014/11/30

#
Alright, I have another question for you. I am using these algorithms to determine how much faster one is then the other one. As of right now, with the results, the +1 is faster then the *2, which is not true. After running the cycles, the limit for each player is essentially how many step they have taken, the +1 has less then the *2 which is not what I want. Oh, wait a minute, I think I got it. Brb.
danpost danpost

2014/11/30

#
Please show the code you are using.
Mickey09 Mickey09

2014/11/30

#
It works, the way I want it to, I was just using the wrong int to display how many cycles it took to find the treasure. My next task is to break down this huge number to something more easier on the eyes....
public class Player2 extends Actor
{
    private int limit = 1;
    private int steps = 0;
    private int time = 0;
   
   
    /**
     *
     */
   public void act() 
   {
        move(); 
   }
   public void move()
   {
      move(1); 
      steps = steps + 1;
         if (steps == limit)
         {
          turn (180); 
          steps = 0;
          limit = limit*2;
         }
      TouchingTreasure();
      time = time + 1;
   }
   public void TouchingTreasure() 
   {   
        Actor treasure = getOneObjectAtOffset(0, 0, Treasure.class); 
        if (treasure !=null)
        {
            System.out.println("Good Job Player 2! You have found the Treasure after: " + time/60);
            getWorld().removeObject (this);
            
        }
   }
    public int getLimit()
   {
       return time;
   }
}  
There are more replies on the next page.
1
2
3
4