/** * 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!!!

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