// Vehicle final int carUP = 1; final int carRIGHT = 2; final int carDOWN = 3; final int carLEFT = 4; final color DARKBLUE = color( 11 , 80 , 184); // Dark Blue final color LIGHTBLUE = color( 102 , 184 , 234); // Light Blue final color BLACK = color( 75 , 66 , 71 ); // Black final color LIGHTBROWN = color( 240 , 180 , 126); // Light Brown final color LIGHTGREEN = color( 110 , 217 , 163); // Light Green final color DARKGREEN = color( 1 , 128 , 121); // Dark Green final color UGLYGREEN = color( 78 , 86 , 9 ); // Ugly Green final color LIGHTYELLOW = color( 255 , 223 , 100); // Light Yellow final color DARKYELLOW = color( 255 , 255 , 3 ); // Dark Yellow final color ORANGE = color( 255 , 160 , 1 ); // Orange final color RED = color( 253 , 54 , 0 ); // Red final color PINK = color( 255 , 101 , 131); // Pink final color DARKBROWN = color( 176 , 104 , 54 ); // Dark Brown final color DARKPURPLE = color( 141 , 115 , 189); // Dark Purple final color LIGHTPURPLE = color( 200 , 170 , 182); // Light Purple final color TURQUISE = color( 0 , 255 , 255); // Turquise public class Vehicle { int Length; color myColor; int xOriginPoint, yOriginPoint; int direction; // --------------------------------------------------------- Constructor public Vehicle(int Length, color myColor, int xOriginPoint, int yOriginPoint, int direction) { this.Length = Length; this.myColor = myColor; this.xOriginPoint = xOriginPoint; this.yOriginPoint = yOriginPoint; this.direction = direction; if (DEBUG) println("NEW VEHICLE CREATED:\n" + toString()); } // --------------------------------------------------------- legalPosition public boolean legalPosition(Grid testGrid) { int tempX = xOriginPoint; int tempY = yOriginPoint; switch(direction) { case carUP: while (tempY > (yOriginPoint - Length - 0)) { // check boundry if ((tempY > testGrid.rows - 1) || ( tempY < 0) ) { return false; // check other cars } else if ( testGrid.filled[xOriginPoint][tempY] == true) { return false; } tempY--; } return true; case carRIGHT: while (tempX < (xOriginPoint + Length - 0)) { // check boundry if ((tempX > testGrid.cols - 1) || ( tempX < 0) ) { return false; // check other cars } else if ( testGrid.filled[tempX][yOriginPoint] == true) { return false; } tempX++; } return true; case carDOWN: while (tempY < (yOriginPoint + Length - 0)) { // check boundry if ((tempY > testGrid.rows - 1) || ( tempY < 0) ) { return false; // check other cars } else if ( testGrid.filled[xOriginPoint][tempY] == true) { return false; } tempY++; } return true; case carLEFT: while (tempX > (xOriginPoint - Length - 0)) { // check boundry if ((tempX > testGrid.cols - 1) || ( tempX < 0) ) { return false; // check other cars } else if ( testGrid.filled[tempX][yOriginPoint] == true) { return false; } tempX--; } return true; } return true; } // --------------------------------------------------------- move public void move(int carDirection, int spaces, Grid tempGrid) { // Check if intended direction to move is the same as orientation of car // i.e. only a car aimed up can move up/down int oldX = xOriginPoint; int oldY = yOriginPoint; switch(carDirection) { case(carUP): if ((direction == carDirection) || (direction == carDirection+2)) // if car is aimed up/down and wants to move up { if (DEBUG) println("car is aimed up/down and wants to move up"); tempGrid.removeVehicle(this); // check if path to intended final destination is clear for (int i = oldY; i > oldY-spaces; i--) { if (DEBUG) println("in move loop " + i); yOriginPoint--; if (!(legalPosition(tempGrid))) { if (DEBUG) println("conflict found"); yOriginPoint++; tempGrid.addVehicle(this); break; } } if (DEBUG) println("no conflict found"); tempGrid.addVehicle(this); } break; case(carRIGHT): if ((direction == carDirection) || (direction == carDirection+2)) // if car is aimed right/left and wants to move right { if (DEBUG) println("car is aimed right/left and wants to move right"); tempGrid.removeVehicle(this); // check if path to intended final destination is clear for (int i = oldX; i < oldX+spaces; i++) { if (DEBUG) println("in move loop " + i); xOriginPoint++; if (!(legalPosition(tempGrid))) { if (DEBUG) println("conflict found"); xOriginPoint--; tempGrid.addVehicle(this); break; } } if (DEBUG) println("no conflict found"); tempGrid.addVehicle(this); } break; case(carDOWN): if ((direction == carDirection) || (direction == carDirection-2)) // if car is aimed up/down and wants to move down { if (DEBUG) println("car is aimed up/down and wants to move down"); tempGrid.removeVehicle(this); // check if path to intended final destination is clear for (int i = oldY; i < oldY+spaces; i++) { if (DEBUG) println("in move loop " + i); yOriginPoint++; if (!(legalPosition(tempGrid))) { if (DEBUG) println("conflict found"); yOriginPoint--; tempGrid.addVehicle(this); break; } } if (DEBUG) println("no conflict found"); tempGrid.addVehicle(this); } break; case(carLEFT): if ((direction == carDirection) || (direction == carDirection-2)) // if car is aimed right/left and wants to move left { if (DEBUG) println("car is aimed right/left and wants to move left"); tempGrid.removeVehicle(this); // check if path to intended final destination is clear for (int i = oldX; i > oldX-spaces; i--) { if (DEBUG) println("in move loop " + i); xOriginPoint--; if (!(legalPosition(tempGrid))) { if (DEBUG) println("conflict found"); xOriginPoint++; tempGrid.addVehicle(this); break; } } if (DEBUG) println("no conflict found"); tempGrid.addVehicle(this); } break; } tempGrid.updateCarsOnGrid(); } /* // --------------------------------------------------------- moveTo public void moveTo(int newX, int newY, Grid tempGrid) { switch(direction) { case(carUP): break; case(carRIGHT): break; case(carDOWN): break; case(carLEFT): break; } tempGrid.updateCarsOnGrid(); } */ // --------------------------------------------------------- toString public String toString() { String Discription = ""; if (myColor == DARKBLUE) Discription += "Dark Blue "; if (myColor == LIGHTBLUE) Discription += "Light Blue "; if (myColor == BLACK) Discription += "Black "; if (myColor == LIGHTBROWN) Discription += "Light Brown "; if (myColor == LIGHTGREEN) Discription += "Light Green "; if (myColor == DARKGREEN) Discription += "Dark Green "; if (myColor == UGLYGREEN) Discription += "Ugly Green "; if (myColor == DARKYELLOW) Discription += "Dark Yellow "; if (myColor == LIGHTYELLOW) Discription += "Light Yellow "; if (myColor == ORANGE) Discription += "Orange "; if (myColor == RED) Discription += "Red "; if (myColor == PINK) Discription += "Pink "; if (myColor == DARKBROWN) Discription += "Dark Brown "; if (myColor == DARKPURPLE) Discription += "Dark Purple "; if (myColor == LIGHTPURPLE) Discription += "Light Purple "; if (myColor == TURQUISE) Discription += "Turquise "; if (Length == 2) Discription += "Car "; if (Length == 3) Discription += "Truck "; if (Length > 3) Discription += Length; Discription += "starting at ( " + xOriginPoint + ", " + yOriginPoint + " ) aiming:"; switch(direction) { case carUP: Discription += " UP"; break; case carRIGHT: Discription += " RIGHT"; break; case carDOWN: Discription += " DOWN"; break; case carLEFT: Discription += " LEFT"; break; } return Discription; } }