Computer science 1 (Java project)
package p3; /** * This program generate a Random walk from a point to another. * * Using the @author tag at the end of the comment will let others know that you * wrote the program. * * @author Fahad Alqufaili */ import java.awt.Point; import java.util.ArrayList; import java.util.Random; public class RandomWalk { private int grideSize=0; private Random rand; private boolean done; private ArrayList<Point> path; Point start; Point currPoint; Point endPoint; public RandomWalk(int gridSize,long seed) { this.grideSize= gridSize; rand= new Random(seed); path = new ArrayList<Point>(); start = new Point(0,grideSize-1); currPoint = start; endPoint = new Point(grideSize-1,0); path.add(start); } public RandomWalk(int gridSize) { this.grideSize= gridSize; rand= new Random(); path = new ArrayList<Point>(); start = new Point(0,grideSize -1 ); currPoint = start; endPoint = new Point(grideSize-1,0); path.add(start); } /** * * @return we returned our end point to the current point ones it reach the end point. */ public boolean isDone() { return currPoint.x>=endPoint.x && currPoint.y<=endPoint.y; } /** * * We are getting the last point from our path list */ public void step() { int p1= rand.nextInt(2); if(p1%2==0 && currPoint.x < endPoint.x) { // we determine if we are at the end point currPoint = new Point(currPoint.x +1, currPoint.y); }else if(currPoint.y > endPoint.y) { currPoint = new Point(currPoint.x, currPoint.y -1); } path.add(currPoint); //Point currPoint = path.get(path.size() - 1); //Point endPoint = new Point(size-1,0); // int endPointX = endPoint.x; // int endPointY = endPoint.y; // //Point x = new Point(size+1,0); // //Point y = new Point(0,size-1); // if(done ==false) { // //Point endPoint = new Point(0,size-1); // path.add(endPoint); // // } // // // if(done == true){ // return; // } // // // Random rand = new Random(); // int n = rand.nextInt(2); // if(done== false) { // // } if(n== 0 && size < endPointX) { // // Point newX = new Point(currPoint.x + 1,0); // path.add(newX); // // } else if (n== 1 && size > endPointY){ // Point newY = new Point(0,currPoint.y-1); // path.add(newY); // // } //Point p1 = new Point(size + 1,0); //path.add(p1); } public void createWalk() { while(!isDone()) { step(); } } public ArrayList<Point> getPath() { return path; } public String toString() { String pathString = ""; for(int n=0; n< path.size(); n++) { pathString+="["+path.get(n).x +","+path.get(n).y+"]"; } return pathString; } }