float gridWidth; float gridHeight; // Grid public class Grid { int rows, cols; boolean[][] filled; ArrayList myCars = new ArrayList(); // --------------------------------------------------------- constructor public Grid(int rows, int cols) { this.rows = rows; this.cols = cols; filled = new boolean[rows][cols]; clearGrid(); } // --------------------------------------------------------- emptyGrid public void emptyGrid() { clearGrid(); myCars.clear(); } // --------------------------------------------------------- clearGrid public void clearGrid() { for (int i = 0; i < filled.length;i++) { for (int j = 0; j < filled[i].length;j++) { filled[i][j] = false; } } } // --------------------------------------------------------- updateCarsOnGrid public void updateCarsOnGrid() { clearGrid(); // Check if there are any cars in arraylist if (!(myCars.isEmpty())) { for (int i = 0; i < myCars.size(); i++) { if (DEBUG) println("CURRENT CAR " + myCars.get(i)); Vehicle tempCar = (Vehicle) myCars.get(i); int ver = 0; int hor = 0; switch(tempCar.direction) { case(carUP): ver = -1; break; case(carRIGHT): hor = 1; break; case(carDOWN): ver = 1; break; case(carLEFT): hor = -1; break; } for (int j = 0; j < tempCar.Length; j++) { filled[tempCar.xOriginPoint + hor*j][tempCar.yOriginPoint + ver*j] = true; } } } } // --------------------------------------------------------- drawGrid public void drawGrid() { //float gridWidth = width/(float)filled.length; //float gridHeight = height/(float)filled[0].length; // Draw the grid for (int i = 0; i < filled.length;i++) { for (int j = 0; j < filled[i].length;j++) { //if (DEBUG) println("i " + i + " j " + j + "\n"); if (DEBUG) { if( filled[i][j]) { fill(0); } else { noFill(); } } stroke(0); noFill(); rect(i * gridWidth, j * gridHeight, gridWidth, gridHeight); } } // Draw Cars if (!(myCars.isEmpty())) { for (int i = 0; i < myCars.size(); i++) { //print(myCars.get(i)); Vehicle tempCar = (Vehicle) myCars.get(i); stroke(tempCar.myColor); fill(tempCar.myColor); switch(tempCar.direction) { case(carUP): rect((tempCar.xOriginPoint * gridWidth)+1, ((tempCar.yOriginPoint-tempCar.Length+1) * gridHeight)+1, gridWidth-2, (gridHeight * tempCar.Length)-2); break; case(carRIGHT): rect((tempCar.xOriginPoint * gridWidth)+1, (tempCar.yOriginPoint * gridHeight)+1, (gridWidth * tempCar.Length)-2, gridHeight-2); break; case(carDOWN): rect((tempCar.xOriginPoint * gridWidth)+1, (tempCar.yOriginPoint * gridHeight)+1, gridWidth-2, (gridHeight * tempCar.Length)-2); break; case(carLEFT): rect(((tempCar.xOriginPoint-tempCar.Length+1) * gridWidth)+1, (tempCar.yOriginPoint * gridHeight)+1, (gridWidth * tempCar.Length)-2, gridHeight-2); break; } stroke(0); if (selectedCar == tempCar) { fill(0); } else { noFill(); } ellipseMode(CENTER); ellipse((tempCar.xOriginPoint * gridWidth)+gridWidth/2, (tempCar.yOriginPoint * gridHeight)+gridHeight/2, 20, 20); } } } // --------------------------------------------------------- addVehicle public boolean addVehicle(Vehicle newVehicle) { if (newVehicle.legalPosition(this)) { myCars.add(newVehicle); if (DEBUG) println("Car Placed on Grid"); updateCarsOnGrid(); return true; } if (DEBUG) println("Car NOT Placed on Grid"); return false; } // --------------------------------------------------------- removeVehicle public boolean removeVehicle(Vehicle newVehicle) { if (myCars.contains(newVehicle)) { myCars.remove((myCars.indexOf(newVehicle))); updateCarsOnGrid(); return true; } return false; } // --------------------------------------------------------- toString public String toString() { String discription = ""; for (int i = 0; i < filled.length;i++) { for (int j = 0; j < filled[i].length;j++) { if (filled[j][i]) { discription += "X "; } else { discription += "O "; } } discription += "\n"; } return discription; } }