8 Replies - 856 Views - Last Post: 08 April 2012 - 02:23 PM Rate Topic: -----

#1 alive22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-April 12

I am struggling with this project and have for weeks. I need help.

Posted 08 April 2012 - 01:28 PM

/**
 * A Turtle for a Turtle Graphics system.
 */
import java.awt.*;

public class Turtle {

  /**
   * Instance variables
   */      
  private  TurtleWorld TW;
  private double direction;
  private double x;
  private double y;
  private boolean penDown;
  private Color penColor;
  // Constructors:
 
  /**
   * ensure: create a new Turtle with: 
   *      TurtleWorld drawing context TW,
   *      heading due north (0 degrees), 
   *      position at the origin (0,0), and 
   *      and pen in "up" position
   */  
  public Turtle(TurtleWorld TW) {
    this.TW = TW;
    direction = 0;
    x = 0;
    y = 0;
    penDown = false;
    penColor = Color.GREEN;
    
  }

  /**
   * ensure: create a new Turtle with: 
   *      TurtleWorld drawing context TW,
   *      heading inith degrees to the right of due north, 
   *      position at (x,y), and 
   *      and pen in "up" position
   */ 
  public Turtle (TurtleWorld TW, double inith, double X, double Y, Color initColor) {
    this.TW = TW;
    direction = inith;
    x = X;
    y = Y;
    penColor = initColor;
  }

  /**
   * ensure: create a new Turtle with: 
   *      TurtleWorld drawing context TW,
   *      heading inith degrees to the right of due north, 
   *      position at the origin (0,0), and 
   *      and pen in "up" position
   */ 
  public Turtle (TurtleWorld TW, double inith) {
    this.TW = TW;
    direction = inith;
    x = 0;
    y = 0;
    penColor = Color.GREEN;
  }

  /**
   * ensure: create a new Turtle with: 
   *      TurtleWorld drawing context TW,
   *      with heading due north (0 degrees),  
   *      position at (x,y), and 
   *      and pen in "up" position
   */ 
  public Turtle (TurtleWorld TW, double X, double Y) {
    this.TW = TW;
    direction = 0;
    x = X;
    y = Y;
    penColor = Color.GREEN;
  }

  // Commands:

   /**
   * ensure: set the pen down. 
   */ 
  public void pd () {
    penDown = true;
  } 
  
   /**
   * ensure: set the pen up. 
   */ 
  public void pu () {
    penDown = false;
  }
  
  /**
   * ensure: The Turtle is at the x-coordinate position. 
   */ 
  public void setx (double X) {
    this.x = X;
  }

  /**
   * ensure: The Turtle is at the y-coordinate position.  
   */ 
  public void sety (double Y) {
    this.y = Y;
  }  
  
    /**
   * ensure: the Turtle has heading tHeading. 
   */ 
  public void setHeading (double tHeading) {
    this.direction = tHeading;
  }

  
  /**
   * require: offset can be any positive or negative value (No restriction).
   * ensure: the Turtle heading is set to offset degrees to the right
   *         of its previous heading, mod 360.
   *         (The Turtle position is unchanged).   
   */ 
  public void rt (double offset) {
	  direction = (direction - offset) % 360;
	  if(direction < 0){
		  direction = direction + 360;
	  }

  }

  /**
   * require: offset can be any positive or negative value (No restriction).
   * ensure: the Turtle heading is set to offset degrees to the left
   *         of its previous heading, mod 360.  
   *         (The Turtle position is unchanged).   
   */
  public void lt (double offset) {
	  direction = (direction + offset) % 360;
	  if(direction > 0){
		  direction = direction + 360;
		  
	  }
  }
  
 



/**
   * ensure: Move dist steps in the direction Turtle.heading().
   *         (The Turtle heading is unchanged).  
   */  
  public void fd (double dist) {
    double inith = direction;
    double prevY = y;
    double prevX = x;

    /*
     * implementation partially hidden    
     */
    
    if (penDown)
      TW.doDrawLine(prevX,prevY,x,y);    
  }
 
  /**
   * ensure: Move dist steps in the direction opposite of Turtle.heading().
   *         (The Turtle heading is unchanged).  
   */   
  public void bk (double dist) {
	  double prevX = x;
	  double prevY = y;
	  x = x - dist*Math.sin(Math.toRadians(direction));
	  y = y - dist*Math.cos(Math.toRadians(direction));
	  TW.doDrawLine(prevX, prevY, x,y);

  }
  


  // Queries:

  /**
   * ensure: Return status of pen.
   */ 
  public boolean isPenDown () {
    return penDown;
  }
  
  /**
   * ensure: Current heading of this Turtle in degrees, 0 to < 360, with 0 due north.
   */ 
  public double heading () {
    return direction;
  }

  /**
   * ensure: Current x position of the Turtle.
   */ 
  public double xcor () {
    return x;
  }

  /**
   * ensure: Current y position of the Turtle.
   */ 
  public double ycor () {
    return y;
  }
  
  public Color getColor(){
	  return penColor;
  }
  
  
  
 
}


import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;

/** 
 * TurtleWorld provides a high level drawing context for a Turtle
 */
public class TurtleWorld {
int gridSize = 400;
int gridopatch = 40;
int patchSize = 20;
int[][]gridArray= new int[gridopatch][gridopatch];
int winSize;


  /**
   * Instance Variables
   */
  JFrame frame;
  TurtlePanel tPanel;
  
  public TurtleWorld(int wSize, int pSize){
	  patchSize=pSize;
	  winSize=wSize;
	  
	  // Initializing grid array to 0s.
	  
	  for (int i=0;i<=gridopatch-1;i++){
		  
		  for (int j=0;j<=gridopatch-1;j++){
			 gridArray[i][j] = 0; 
		  }
	  }
	  
	  frame = new JFrame();
	  tPanel = new TurtlePanel(patchSize, wSize);
	  frame.getContentPane().add (tPanel);
	  frame.pack();
	  frame.setVisible(true);
	  
	  
	  
	  
  }

  /**
   * Constructor for a TurtleWorld "context".
   */
  
 
  public TurtleWorld(){
    /**
     * A swing JFrame and TurtlePanel (JPanel) 
     * are created and initially painted
     */
    frame = new JFrame();
    tPanel = new TurtlePanel(patchSize, gridSize);
    frame.getContentPane().add (tPanel);
    frame.pack();
    frame.setVisible(true);
    
    
  }
  
  /**
   * Draw a line on the TurtleWorld "context".
   */
 public void  doDrawLine(double x1, double y1, double x2, double y2){
    
	  // Four different line cases.
	  // First, a point.
	  if ((int)(x1-x2)==0){
		  if ((int)(y1-y2)==0){
			  gridArray[((wrapAround((int)x1))/10)][(wrapAround((int)y1)/10)] = 1;
		  }
		  // Then a vertical line.
		  else{
			  if(y1>y2){
				  double y = y2;
				  y2 = y1;
				  y1 = y;
			  }
			  for(int j=(int)y1;j<=(int)y2;j++){
				  gridArray[((wrapAround((int)x1))/10)][((wrapAround(j))/10)] = 1;
			  }
			  
			  
		  }
	  }
	  // Then a line at an angle.
	  else{
		  if ((int)(y1-y2)!=0){
			  int m = (int)((y1-y2)/(x1-x2));
			  int b = (int)(y1-m*x1);
			  for(int i=(int)x1;i<=(int)x2;i++){
				  int y = m*i+b;
				  gridArray[((wrapAround(i))/10)][((wrapAround((int)y))/10)] = 1;
			  }
		  }
		  // Then a horizontal line.
		  else{
			  if(x1>x2){
				  double x = x2;
				  x2 = x1;
				  x1 = x;
			  }
			  for(int x=(int)x1;x<=(int)x2;x++){
				  
				  gridArray[((wrapAround(x))/10)][((wrapAround((int)y1))/10)] = 1;
			  }
		  }
		  
	  }
	  
	  
	  
	  
	  
	tPanel.doDrawLine(x1,y1,x2,y2);
 }

  
    
    
  
  
	public int wrapAround(int coordinate){
	  	if (coordinate > gridSize){
	  		if ((coordinate % gridSize)==gridSize){
	  			coordinate = gridSize;
	  			return coordinate;
	  		}
	  		if ((coordinate % gridSize) < gridSize){
	  			coordinate = coordinate % gridSize;
	  			return coordinate;
	  		}
	  	}
	  	
	  	if (coordinate < 0){
	  		if ((coordinate % gridSize)==-gridSize){
	  			coordinate = -gridSize;
	  			return coordinate;
	  	  	}
	  	  	if ((coordinate % gridSize) < -gridSize){
	  	  		coordinate = (coordinate % gridSize)+gridSize;
	  	  		return coordinate;
	  	  	}
	  	}
	  	return coordinate;
	}

	public void display() {
		for (int i=0;i<=gridopatch-1;i++){
			  
			  for (int j=0;j<=gridopatch-1;j++){
				  System.out.print(gridArray[i][j]);
				  
			  }
			  System.out.println("");
		  }
		
		
	}


	
	
}





import java.awt.*;

import javax.swing.*;



/**
 * @author 
 *
 */
public class TurtlePanel extends JPanel {
	int pSize;//size of patch
	int winSize;// size of the window
	private LineList LL;
	protected Color color;

	
	/**
	 * @param windowSize passing in window size.
	 * @param patchSize passing in size of each patch.
	 */
	public TurtlePanel(int patchSize,int wSize) {
		LL= new LineList();
		pSize = patchSize;
		winSize = wSize;
		color = Color.blue;
		setPreferredSize( new Dimension (winSize, winSize));
		setVisible(true);
		
		
		

		
	}

	public void doDrawLine (double x, double y,double x1, double y1){
		Line L = new Line(x,y,x1,y1);
		LL.add(L);
		
	}
	
	
	

	/**
	 * The buttons are made and the turtles are passed into
	 * paintComponent to be drawn to the screen.
	 */
	public synchronized void paintComponent(Graphics g) {
		super.paintComponent(g);
		

		// Works only for a square window.
		for (int x = 1; x< winSize/pSize; x++){ 
		g.drawLine(x*pSize, 0, x*pSize, winSize);
		g.drawLine(0, x*pSize, winSize, x*pSize);
		g.setColor(color);
		g.fillRect(pSize, 0, winSize,0);
		
		
}
		
		
	}
	
	

		
	/**
	 * getPreferredSize is mandatory and used by the system to set the size of
	 * the visual TurtlePanel, in this case 400x400 pixels.
	 */
/**
	public Dimension getPreferredSize() {
		return new Dimension(winSize, winSize);
	}

	/**
	 * getMinimumSize is mandatory and used by the system to set the minimum
	 * size of the visual TurtlePanel.
	 */
	/**public Dimension getMinimumSize() {
		return this.getPreferredSize();
	}
	
}
*/
}



import java.awt.*;

import javax.swing.*;


/**
 * Controller object for the TurtleGraphics system
 */
public class TurtleController{
  
  /**
   * instance variables
   */
  private BumpingTurtle BT;
  private Turtle t;
  private TurtleWorld TW;
  private int turtles = 300;
  Turtle [] TurtleArray= new Turtle [turtles];
  
  /**
   * Construct a TurtleController object
   */
  
  public TurtleController(){
   
	/**
     * A TurtleController object is created with a Turtle
     * and an associated TurtleWorld context.
     */
	  
	    
	 t = new Turtle(TW,90,0,0, null);
	 int pSize = 15;
	 int wSize = 600;
	 TW = new TurtleWorld(wSize,pSize);
	 
	 
	
	 
	 
	 
	 for (int i=300;i<= 0;i++){
		  TurtleArray[i]= new BumpingTurtle (TW, (int)Math.random()*360,(int)(Math.random()*400),(int)(Math.random()*400));
		  }
	  

    
   }
  
 

/**
   * The drawStuff method completely draws a series of turtle 
   * shapes.
   */ 
  public void drawStuff(){
  
	  double randomwalk;
	   for (int i=1; i<=0; i++){
		    randomwalk = TurtleArray[i].heading()+ (Math.random()*60-30);
		    TurtleArray[i].setHeading(randomwalk);
		    
		    
	   }
	   
	

  }
  
  

}

public class BumpingTurtle extends Turtle {
	
	/**
	 * 
	 * @param TW
	 */
	

	public BumpingTurtle(TurtleWorld TW) {
		super(TW);    
		
		
	} 
	
	/**
	 * 
	 * @param TW
	 * @param inith
	 * @param X
	 * @param Y
	 */

	public BumpingTurtle(TurtleWorld TW, double inith, double X, double Y) {
		super(TW, inith, X, Y, null);
		
	}
	
	/**
	 * 
	 * @param TW
	 * @param inith
	 */

	public BumpingTurtle(TurtleWorld TW, double inith) {
		super(TW, inith);
		
	}
	
	/**
	 * 
	 * @param TW
	 * @param X
	 * @param Y
	 */

	public BumpingTurtle(TurtleWorld TW, double X, double Y) {
		super(TW, X, Y);
		
		
	}
	
	/**
	 * Takes the coordinate of 2 points and returns true if they are on the same patch, and false if they are not.
	 * @param x1
	 * @param y1
	 * @param x2
	 * @param y2
	 * @return - Boolean value, true if the two points collide or not.
	 */
	
	public boolean togetherPatch(double x1, double y1, double x2, double y2){
		int inPatchx;
		int inPatchy;
		int inPatchx1;
		int inPatchy1;
		
		
		inPatchx = cor(x1);
		inPatchx1 = cor(x2);
		inPatchy = cor(y1);
		inPatchy1 = cor(y2);
		
		if(inPatchx==inPatchx1){
			if(inPatchy==inPatchy1){
				return true;
			}else{
				return false;
			}
		}
		
		return false;

	}

	/**
	 * Takes the x and y coordinate of a point and returns the patch number that point is in.
	 * @param x - X coordinate of point to be checked..
	 * @param y - Y coordinate of point to be checked.
	 * @return - Patch coordinate of x.
	 */
	public int cor(double x){
		
		int patchNum = (int)(x/15);
		return patchNum;
	}
	
	
	 //Methods
	  
	  /**
	   * postcondition: If there is more than one other Turtle on the same grid "patch" as the
	   * requesting turtle, then an arbitrary one of these turtles is returned
	   * If there are no other turtle on the same grid "patch" as the requesting turtle then
	   * a "null" turtle reference is returned
	   */
	  
	  public Turtle oneOfTurtlesHere() {
		  int arbTurtle =0;
		  int samePatch= 1;
		  //int reqTurtle =0;
		  
		  //the patch is set to 1 turtle, if the same patch has more than one turtle the print the arbitrary turtle
		  for(int i = 1; i<= samePatch; i++){
			  System.out.print(arbTurtle);
		  }
		  
		  
		  
		return null;
		  
	}
	  
	  /**
	   * The following two methods collectively access a collection of turtles on the
	   * current grid "patch of this turtle, including this turtle!
	   */
	  
	  /**
	   * postcondition: returns the number of turtles on the current grid "patch" including 
	   * this turtle
	   * Number of turtles in the grid
	 * @param <numTurtles>
	   */
	 
	  public int numGridTurtles(int patchX,int patchY){
		  patchX= cor(patchX);
		  patchY= cor(patchY);
		  int numTurtles = 0;
		  return numTurtles;
		  
		  
	  }
	  
	  /**
	   * post condition: returns the ith turtle in the grid patch collection sequence, including this
	   * turtle'
	   * 0<=i<= numGridTurtles()-1 (note that the sequences is indexed starting at 0!)
	   */
	  public Turtle turtleAt(int i) {
		  for(i=0; i <= numGridTurtles(0, i)-1;){
			  return turtleAt(0);
		  }
		 

			 return null;
		
	}
	
	

}




I have read a ton of articles and chapters and I am just not getting it. Maybe the problem is just simple and I am not in the right mind set. Basically I have a grid with about 300 turtles on it and I have to keep track of the times a turtle bumping into each other using an array that is initialized to 0 and will turn to 1 when they are bumped. So far I just have a grid my array disappeared. I just really want some help, it is very difficult for me and some people are jut rude on campus with no intention of trying to help me at least. Any and all help will be GREATLY appreciated.

MOD EDIT: Added code tags. When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 08 April 2012 - 01:44 PM


Is This A Good Question/Topic? 0
  • +

Replies To: I am struggling with this project and have for weeks. I need help.

#2 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: I am struggling with this project and have for weeks. I need help.

Posted 08 April 2012 - 01:38 PM

First of all, welcome on the board of DIC ^^

Second, while I understand you have a problem, you can't expect us (me) to dig trough ~ 719 lines of code (which are unformatted, use code blocks when you're posting code ;) ) to "find and solve your error". Not to be rude, but I cannot understand what you mean with this: "So far I just have a grid my array disappeared."

What exactly happens? Do you get an Exception, if so, post the stack trace. Where exactly does the error occur, and so on.

You have to be more elaborate if you want adequate help ;)

This post has been edited by karabasf: 08 April 2012 - 01:40 PM

Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: I am struggling with this project and have for weeks. I need help.

Posted 08 April 2012 - 01:42 PM

Before you post in a forum to ask for help, you should take a few seconds to read the rules. If you had, you might have noticed the instructions for posting your code in code tags.

You've posted a ton of code - you might win the most code posted in a single post for the week (no prize) - and have given us little idea of the problem:

Quote

So far I just have a grid my array disappeared.

Can you be more specific? What are you expecting your code to do, what is it doing or not doing, where do you think the problem might be? Can you give example inputs/outputs, both desired and actual?

You'll find helpful people here who really want to help and will go the distance, but you've got to give us something to work with besides tons of code, improperly posted, that we've got to sort through to figure out what the problem might be on our own.

Please come back with more info and good luck.

This post has been edited by GregBrannon: 08 April 2012 - 01:43 PM

Was This Post Helpful? 0
  • +
  • -

#4 alive22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-April 12

Re: I am struggling with this project and have for weeks. I need help.

Posted 08 April 2012 - 01:49 PM

oh I'm super sorry. I am not really getting any error but I am just trying to get actual turtles on the grid something in particular I am having trouble with is this :
/**
	   * postcondition: If there is more than one other Turtle on the same grid "patch" as the
	   * requesting turtle, then an arbitrary one of these turtles is returned
	   * If there are no other turtle on the same grid "patch" as the requesting turtle then
	   * a "null" turtle reference is returned
	   */
	  
	  public Turtle oneOfTurtlesHere() {
		  int arbTurtle =0;
		  int samePatch= 1;
		  //int reqTurtle =0;
		  
		  //the patch is set to 1 turtle, if the same patch has more than one turtle the print the arbitrary turtle
		  for(int i = 1; i<= samePatch; i++){
			  System.out.print(arbTurtle);
		  }
		  
		  
		  
		return null;
		  
	}


I am trying to make sure that the turtle is checking. I just feel like it isn't doing anything. I have tried a bunch of things

*Edited: code tags added. :code:

This post has been edited by pbl: 08 April 2012 - 01:59 PM

Was This Post Helpful? 0
  • +
  • -

#5 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: I am struggling with this project and have for weeks. I need help.

Posted 08 April 2012 - 02:00 PM

No problem, anyways:

	  /**
	   * postcondition: If there is more than one other Turtle on the same grid "patch" as the
	   * requesting turtle, then an arbitrary one of these turtles is returned
	   * If there are no other turtle on the same grid "patch" as the requesting turtle then
	   * a "null" turtle reference is returned
	   */
	  
	  public Turtle oneOfTurtlesHere() {
		  int arbTurtle =0;
		  int samePatch= 1;
		  //int reqTurtle =0;
		  
		  //the patch is set to 1 turtle, if the same patch has more than one turtle the print the arbitrary turtle
		  for(int i = 1; i<= samePatch; i++){
			  System.out.print(arbTurtle);
		  }
		return null;  
	}



What this does, is: Printing out "0" and return null. The loop would execute once (i = 1 and samePatch = 1)

So, I guess you want to check if two turtles are there on the same patch or? Also, how is this method called or how is it supposed to be called? I assume it has to get a position on the grid from somewhere.

This post has been edited by karabasf: 08 April 2012 - 02:02 PM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: I am struggling with this project and have for weeks. I need help.

Posted 08 April 2012 - 02:03 PM

In this code:
- you set arbTutle to 0
- you set samePach to 1
- your for(int i = 1; i <= 1; i)) // so only one iteration
- you print arbTutle which is 0

so what your code does is:

System.out.println(0);
return null;



based on the comments at the beginning of the method it is uspposed to test something but there is no if in your code
Was This Post Helpful? 0
  • +
  • -

#7 alive22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-April 12

Re: I am struggling with this project and have for weeks. I need help.

Posted 08 April 2012 - 02:08 PM

Yes I am checking to see if two turtles are in the same patch. It is called from the Turtle class.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: I am struggling with this project and have for weeks. I need help.

Posted 08 April 2012 - 02:15 PM

So shouldn't you check grisArray ?
and even if the number is > 1 how will you know which turtle are there ?
Was This Post Helpful? 0
  • +
  • -

#9 alive22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-April 12

Re: I am struggling with this project and have for weeks. I need help.

Posted 08 April 2012 - 02:23 PM

I will do that, thanx.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1