Help with Gridworld

A place to discuss the science of computers and programs, from algorithms to computability.

Formal proofs preferred.

Moderators: phlip, Moderators General, Prelates

Master007
Posts: 6
Joined: Tue Feb 03, 2009 11:59 am UTC

Help with Gridworld

Postby Master007 » Tue Feb 03, 2009 12:09 pm UTC

I'm messing around with GridWorld and I thought it would be cool to use the existing bug class and extend it in order to create a bug that goes around in a circle...like a circle bug....but shoots bugs every time it takes a step(also when it shoots it shoots the same direction its facing)...i have gotten a circle bug but i don't know what to do from that point. Help much appreciated. Thanks!! :D


Here is the Code for the CircleBug, but its called GunBug:

import info.gridworld.actor.Bug;

public class GunBug extends Bug
{
private int steps;
private int sideLength;

public GunBug(int n)
{
sideLength = n;
}
public void act()
{
if (steps < sideLength && canMove())
{
move();
steps++;
}
else
{
turn();
steps = 0;
}
}
}

Can someone complete the code for me...I have no clue what to do...Appreciate all those who help!! :D
Last edited by Master007 on Tue Feb 03, 2009 3:42 pm UTC, edited 2 times in total.

User avatar
quintopia
Posts: 2906
Joined: Fri Nov 17, 2006 2:53 am UTC
Location: atlanta, ga

Re: Help with Gridworld

Postby quintopia » Tue Feb 03, 2009 2:02 pm UTC

i take it there are no guns already implemented in GridWorld?

Then I have two suggestions:
1) Upgrade to something like Robot Battle.
2) Otherwise, write a BulletBug that is shaped like a bullet, and just goes straight until it hits something, and then just create a new one and place it in the cell in front of the bug at each step.

Master007
Posts: 6
Joined: Tue Feb 03, 2009 11:59 am UTC

Re: Help with Gridworld

Postby Master007 » Tue Feb 03, 2009 2:05 pm UTC

will you help me write that code...I'm confused on how to create a bug in front of the moving bug without disrupting the originals bugs path and one that will disapear when it cant go any further...?

Master007
Posts: 6
Joined: Tue Feb 03, 2009 11:59 am UTC

Re: Help with Gridworld

Postby Master007 » Tue Feb 03, 2009 2:07 pm UTC

i'm just learning the basics...so i wanted bugs to come out everytime the circlebug steps, im not allowed to create my own bugs yet just use the given bugs in a creative manner..Thanks

User avatar
quintopia
Posts: 2906
Joined: Fri Nov 17, 2006 2:53 am UTC
Location: atlanta, ga

Re: Help with Gridworld

Postby quintopia » Tue Feb 03, 2009 3:29 pm UTC

You can't have a bug come out at every step if sideLength>1. At best, you can have a bug come out every time it turns. I don't know enough about GridWorld to even begin to say what methods you would use to create a new bug and place it on the grid, but I can tell you that code would most likely go right after the turn() call.

Master007
Posts: 6
Joined: Tue Feb 03, 2009 11:59 am UTC

Re: Help with Gridworld

Postby Master007 » Tue Feb 03, 2009 3:34 pm UTC

huh? can you explain in code...how is the call used?

User avatar
quintopia
Posts: 2906
Joined: Fri Nov 17, 2006 2:53 am UTC
Location: atlanta, ga

Re: Help with Gridworld

Postby quintopia » Tue Feb 03, 2009 5:31 pm UTC

assume that you can create a new bug that goes straight initially facing direction dir with new ForwardBug(dir) and that you can get a bug's location in the grid with Bug.getX() and Bug.getY(), and you can put a bug in the grid by Grid.put(x,y,bug), and that you can get with direction a bug is facing by Bug.getDirection(), where it returns 0 for north (negative y direction), 1 for east (positive x direction), 2 for south (positive y direction) and 3 for west (negative x direction). I don't know if any of this is true for GridWorld, because finding out would require actual work. Then you can modify your code to get a bug that moves in squares and shoots a bug in straight out in front every time it turns in the following way:

Code: Select all

} else {
turn();
steps=0;
int dir = getDirection();
int x;
int y;
if (dir == 0) {//north
  int x = getX();
  int y = getY()-1;
} else if (dir == 1) {//east
  int x = getX()+1;
  int y = getY();
} else if (dir == 2) {//south
  int x = getX();
  int y = getY()+1;
} else if (dir == 3) {
  int x = getX()-1;
  int y = getY();
}

grid.put(x,y,new ForwardBug(dir));
}


Of course, the above code may be infinitely harder or trivially easier depending on how GridWorld is actually implemented.

Master007
Posts: 6
Joined: Tue Feb 03, 2009 11:59 am UTC

Re: Help with Gridworld

Postby Master007 » Tue Feb 03, 2009 10:39 pm UTC

hey thanks so much quintopia :D :D :D

User avatar
quintopia
Posts: 2906
Joined: Fri Nov 17, 2006 2:53 am UTC
Location: atlanta, ga

Re: Help with Gridworld

Postby quintopia » Wed Feb 04, 2009 1:28 am UTC

I just read that bugs turn in 45 degree increments. So the above code must be changed to reflect there are 8 directions where a bug can be placed (. And placing a bug really is as simple as getGrid().put(new Location(row,column),new Bug().setDirection(Location.NORTH));

Master007
Posts: 6
Joined: Tue Feb 03, 2009 11:59 am UTC

Re: Help with Gridworld

Postby Master007 » Wed Feb 04, 2009 2:04 pm UTC

You know in the code you gave me how do you change the numbers so that it allows me to turn northeast, northwest, southeast and south west...? and to add a new bug i have to put the
getGrid().put(new Location(row,column),new Bug().setDirection(Location.NORTH));
at the end of each if, else if statements right?

if (dir == 0) {//north
int x = getX();
int y = getY()-1; //like this one
} else if (dir == 1) {//east
int x = getX()+1; //like this one
int y = getY();
} else if (dir == 2) {//south
int x = getX();
int y = getY()+1; //like this one
} else if (dir == 3) {
int x = getX()-1; //like this one
int y = getY();
}

User avatar
quintopia
Posts: 2906
Joined: Fri Nov 17, 2006 2:53 am UTC
Location: atlanta, ga

Re: Help with Gridworld

Postby quintopia » Thu Feb 05, 2009 6:47 am UTC

here's the simpler way of thinking about how to figure out where to place the bug: if you're facing north (or NE or NW), subtract one from y, if you're facing south (or SE or SW) add one. For x, add 1 for east directions and subtract one for west directions.

And the actual creation and placement of the bug needs to only be written once, after the if block.

another way to construct the if block, if you'd like to do something slightly more advanced, would be as follows:

Code: Select all

switch(getDirection()) {

    case Location.NORTH:
        x=getLocation().getCol();
        y=getLocation().getRow()-1;
        break;
    case Location.NORTHEAST:
        x=getLocation().getCol()+1;
        y=getLocation().getRow()-1;
        break;

    //etc.

}

Locke1689
Posts: 13
Joined: Tue Apr 29, 2008 1:59 am UTC

Re: Help with Gridworld

Postby Locke1689 » Tue Feb 10, 2009 4:12 pm UTC

A little context question: Is this the equivalent of the old Marine Biology Case Study in the AP computer science curriculum?

User avatar
Why Two Kay
Posts: 266
Joined: Sun Mar 23, 2008 6:25 pm UTC
Location: Plano, TX
Contact:

Re: Help with Gridworld

Postby Why Two Kay » Thu Feb 12, 2009 5:05 am UTC

Locke1689 wrote:A little context question: Is this the equivalent of the old Marine Biology Case Study in the AP computer science curriculum?


Yes. This was the one introduced (I believe) in 2008. It is still in use this year.
tl;dr - I said nothing important.

User avatar
eaglef2
Posts: 93
Joined: Mon Feb 09, 2009 4:52 am UTC
Location: I am over there

Re: Help with Gridworld

Postby eaglef2 » Sat Mar 07, 2009 11:14 pm UTC

It depends if you want the bug shot out to disappear after a while or whenever it runs into another, non-flower, object, or if you want the bug to run out infinitely until it runs into an impassible object.

I wrote the code for the two bugs, the first is the GunBug, which creates DisappearBugs which disappear after moving the length of the gun bug's step length.
The only thing you would have to change for the DisappearBug to walk an infinite distance is to get rid of the fields distance and step in the code, as well as all references to them.
Spoiler:
GunBug code:

Code: Select all

import info.gridworld.actor.Bug;
import info.gridworld.grid.Location;

public class GunBug extends Bug
{
   private int step, distance;
   
   public GunBug(int length)
   {
      distance = length;
      step = 0;
   }
   
   public void act()
   {
      if(step < distance && canMove())
      {
         move();
         step++;
      }
      else
      {
         DisappearingBug temp = new DisappearingBug(distance);
         temp.setDirection(getDirection());
         try
         {
            temp.putSelfInGrid(this.getGrid(), this.getLocation().getAdjacentLocation(getDirection()));
         }catch(IllegalArgumentException e)
         {
         }
         turn();
         step = 0;
      }
   }
}

Spoiler:
DisappearingBug code:

Code: Select all

import info.gridworld.actor.*;

public class DisappearingBug extends Bug
{
   private int step, distance;
   
   public DisappearingBug(int length)
   {
      distance = length;
      step = 0;
   }
   
   public void act()
   {
      if(canMove() && step < distance)
      {
         move();
         step++;
      }
      else
      {
         removeSelfFromGrid();
      }
   }
}


edit* the gun bug creates the disappearing bug in the direction it is in right before it turns and directly in front of itself. You don't have to use lengthy switch-case statements as all Location objects have a method called getAdjacentLocation(int direction) which is explained in the Location class.

edit** I realized in the gun bug code that an ActorWorld argument is bad code, so I changed it to not use any ActorWorld objects


Return to “Computer Science”

Who is online

Users browsing this forum: No registered users and 7 guests