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

2014/11/28

Treasure Hunt

1
2
3
4
Mickey09 Mickey09

2014/11/30

#
Woops lol. However, I want a code that theoretically, would just continue on forever, and not write ten moves to move ten times.
private int a = 1;
private int b = a + 1;

move(a); turn(180); move(b); turn (180);
danpost danpost

2014/11/30

#
Well, you will not be moving ten times within the same act cycle. That is why you need a step counter field to track the number of times you move(1) in the same direction. So, declare a second int field to go with the 'a' field for counting the steps. It will now be the step counter that you increment each act cycle and only increment the distance field when the step counter reaches the value of the distance field. The step counter will be reset back to zero at the same time.
Mickey09 Mickey09

2014/11/30

#
Lol, sorry, but I do not know how to do that. xD I mean, I understand what you are saying, but I do not know how to write the code for it.
danpost danpost

2014/11/30

#
Alright, rename the 'a' field to 'limit' and add another field 'int steps'. Then repost the class code.
Mickey09 Mickey09

2014/11/30

#
public class Player extends Actor
{
    private int limit = 1;
    private int step = 1;
   
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   public void act() 
   {
        move();
        
   }
   public void move()
   {
      move(limit); 
      turn (180); 
     
   }
}
danpost danpost

2014/11/30

#
We have not taken any steps yet, so set the 'step' field to zero. Also, we will not be moving by the value of 'limit' each act cycle; we will be only move 1 pixel per act. Also, add a line in the 'move' method to increment the 'step' field; then repost the class code.
Mickey09 Mickey09

2014/11/30

#
public class Player extends Actor
{
    private int limit = 1;
    private int steps = 0;
   
    /**
     *
     */
   public void act() 
   {
        move();
        
   }
   public void move()
   {
      move(1); 
      steps = steps + 1;
      turn (180); 
     
   }
}
danpost danpost

2014/11/30

#
Ok. Now, we do not want to turn around every act cycle; we only want to turn around after we have taken the limit number of steps. So, we need an 'if' statement that asks if the two fields are equal and the code within the 'if' block will turn the actor around, reset the value of 'steps' back to zero and increment the 'limit' field. Repost the class code when done.
Mickey09 Mickey09

2014/11/30

#
public class Player extends Actor
{
    private int limit = 1;
    private int steps = 0;
   
    /**
     *
     */
   public void act() 
   {
        move(); 
   }
   public void move()
   {
      move(1); 
      steps = steps + 1;
          if(steps==1)
          {
          turn (180); 
          steps = 0;
          limit = limit + 1;
         }
   }
}
danpost danpost

2014/11/30

#
Line 17 needs to compare the two fields, not the one field to equal one:
if (steps == limit)
Mickey09 Mickey09

2014/11/30

#
public class Player extends Actor
{
    private int limit = 1;
    private int steps = 0;
   
    /**
     *
     */
   public void act() 
   {
        move(); 
   }
   public void move()
   {
      move(1); 
      steps = steps + 1;
         if (steps == limit)
         {
          turn (180); 
          steps = 0;
          limit = limit + 1;
         }
   }
}
danpost danpost

2014/11/30

#
Now ... is this the behavior you want?
Mickey09 Mickey09

2014/11/30

#
Yes, most definitely. It seems a little slow, but it does the trick. I appreciate the help. Now, if i wanted to incorporate the other algorithm, I would simply,
public class Player extends Actor
{
    private int limit = 1;
    private int steps = 0;
   
    /**
     *
     */
   public void act() 
   {
        move(); 
   }
   public void move()
   {
      move(1); 
      steps = steps *2;
         if (steps == limit)
         {
          turn (180); 
          steps = 0;
          limit = limit *2;
         }
   }
}
?
danpost danpost

2014/11/30

#
If you are talking about the 'TouchingTreasure' method, you can add the method back into the class and call it from the act method after the 'move' call.
Mickey09 Mickey09

2014/11/30

#
No, what I am referring to is the other algorithm, where the steps double after each cycle.
There are more replies on the next page.
1
2
3
4