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

2014/11/29

One step forward, two steps back algorithm

1
2
inkpenKingpin inkpenKingpin

2014/11/29

#
I'm struggling with a school project, and I need some help. How exactly do you make something move 1 forward, 2 back, 3 forward, 4 back, etc. until you "find" the object placed on the track? Currently I have a direction variable set to 1 if facing EAST (0 degrees), and -1 if facing WEST (180 degrees). I just can't figure out how to code the actor to move the way I want it to.
{
    int direction = getRotation();
    int n = 0;
    /**
     * 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() 
    {

    }
    
    public void directionFace()
    {
        if (direction == 0)
        { 
            n = 1;
        }
        else if (direction == 180)
        {
            n = -1;
        }
    }
    
    public void distance()
    {
        
    }
    
danpost danpost

2014/11/29

#
First off, nothing will ever happen unless you put something in the act method. Secondly, you 'int direction' will always start with a value of zero; as the rotation of the actor has not had a chance to change before the field is initialized. Finally, if the 'directionFace' method was ever called, the value of 'n' will never be set to '-1' as the value of the 'direction' field never changes. In short, your Robot object will just sit there, stationary. There are two things you need to track as far as what was given above (well, three; if you include the track itself). The first is a direction field, which you say you set to '1' and '-1'. Then your code shows a 'direction' field which does not change and an 'n' field with values of '1' and '-1' (maybe the 'n' field should be the direction field). The other field you need is a distance field which should be incremented each act cycle. The product of the distance and direction fields gives the movement vector (a value that gives both direction and distance). So, if you alternate the direction and increment the distance each act cycle, and use the 'move' method for moving you got it.
inkpenKingpin inkpenKingpin

2014/11/29

#
So every time the act method is called, the distance needs to be increased by one. I'm very new to this, so excuse my ignorance, but I'm not exactly sure HOW to code that. I realize my object will stay stationary, I don't have a move action in the act method. That's my problem, If I add move(1); to the move method, it will move infinitely in the direction it's facing. I just don't know how to tell my actor "Hey, after 1 move, switch directions and move 2 backwards" I know what I want it to do in theory, I just don't really know how to do it in practice.
danpost danpost

2014/11/29

#
Using 'move(distance)' will have it move at varying distances each act and using 'move(direction*distance)' will have it move at varying distance in varying directions (well, left and right). This is, of course, if the value of the variables are changed. That is what a variable is used for -- to allow a specific value to have variance.
inkpenKingpin inkpenKingpin

2014/11/29

#
Okay, that makes a little more sense, I'm gonna try and code something and see if it works. If I have trouble, I'll let you know.
inkpenKingpin inkpenKingpin

2014/11/29

#
    public void direction()
    {
        if (getRotation() == 0)
        { 
            n = 1;
        }
        else if (getRotation() == 180)
        {
            n = -1;
        }
    }
    
        public void distance()
    {
        distance = 1;
        if (move())
        {
            distance++;;
        }
    }
    
        public void act() 
    {
        if (n == 1)
        {
            setRotation(0);
            move(distance);
            setRotation(180);
        }
        else if (n == -1)
        {
            setRotation(180);
            move(distance);
            setRotation(0);
        }
    }
}
this is what I have. I get an error in the distance method, how do I get the distance counter to increase?
danpost danpost

2014/11/29

#
There is no 'move()' method in the Actor class -- only a 'move(int)' method. Please copy/paste any error message you are having problems in dealing with (the entire message) -- and post the entire class code, instead of bits and pieces of it. It makes it so much easier to help that way.
Mickey09 Mickey09

2014/11/30

#
Just a thought, I know that the move() method calls an integer, but it also calls a variable, if you place the name of the variable inside the (). So, he could do move(distance)....Oh, is the code asking if the actor simply moves and then the code will react?
danpost danpost

2014/11/30

#
@Mickey09, when I said 'move(int)', that is the method signature -- a method with the name of 'move' that takes one 'int' value as a parameter. The 'int' value can be hard-coded, or it could be a variable, or it could be a method call that returns an 'int' value -- basically anything whose end result is an 'int' value. Also, code cannot respond to actions, it can only produce actions; and those actions can be limited to happen only when certain conditions are met (when returned values or fields are set to specific values or ranges).
Mickey09 Mickey09

2014/11/30

#
Right, my apologies.
inkpenKingpin inkpenKingpin

2014/11/30

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.ArrayList;

/**
 * Write a description of class Robot here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Robot extends Actor
{
    int direction = getRotation();
    int distance = 1;
    int n = 0;
    /**
     * 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 direction()
    {
        if (getRotation() == 0)
        { 
            n = 1;
        }
        else if (getRotation() == 180)
        {
            n = -1;
        }
    }
    
        public void move()
    {
        if (move())
        {
            distance++;;
        }
    }
    
        public void act() 
    {
        if (n == 1)
        {
            setRotation(0);
            move(distance);
            setRotation(180);
        }
        else if (n == -1)
        {
            setRotation(180);
            move(distance);
            setRotation(0);
        }
    }
}
That's the code. I get an incompatible type error at the if statement in the move method.
Super_Hippo Super_Hippo

2014/11/30

#
You can't call a method called 'move' from the method 'move'. That doesn't make sense. Also I don't know what you want to do with it, because you have it as the condition of the if-statement which wouldn't work either, because its return type is void. Oh, and you never call this method from this class, so I don't know if you need it at all.
inkpenKingpin inkpenKingpin

2014/11/30

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.ArrayList;

/**
 * Write a description of class Robot here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Robot extends Actor
{
    int direction = getRotation();
    int distance = 1;
    int n = 0;
    /**
     * 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 direction()
    {
        if (getRotation() == 0)
        { 
            n = 1;
        }
        else if (getRotation() == 180)
        {
            n = -1;
        }
    }
    
        public void moveRobot()
    {
        move(distance);
        distance++;
    }
    
        public void act() 
    {
        if (n == 1)
        {
            moveRobot();
            setRotation(180);
        }
        else if (n == -1)
        {
            moveRobot();
            setRotation(0);
        }
    }
}
This is what I have now. It compiles, but the actor doesnt move, it just stays stationary.
Super_Hippo Super_Hippo

2014/11/30

#
In line 15, you set 'n' to 0. The act method only does something if 'n' is equal to 1 or -1, so if the 'n' isn't changed from somewhere, it won't do anything itself.
danpost danpost

2014/11/30

#
Again, line 13 will never return anything other than zero. Line 13 is only ever executed once for each robot created; it is executed at the time of creation for each robot and the rotation of the robots at that time will always be zero. The values given to fields in their declaration lines are their initial values. The do not represent a constant re-evaluation of what the value of the field should be at any time. Line 35 uses a method call to 'move' as a condition for the 'if' clause; yet, 'move' has no return type (its return type is declared void, or absent of a return value). The 'if' condition must be able to evaluate to either true or false. Another problem with having 'move' in line 35 is that it is calling the method that the call is in. This will create an infinite loop of the method continually calling itself (if you were able to get the code to compile). There is another discussion thread on this very issue you should probably look at. It has a step-by-step in it.
There are more replies on the next page.
1
2