Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1.     public void takeAction() throws InputFileException {
  2.         if (isInitial) {
  3.             computeNext();
  4.         } else {
  5.             try {
  6.                 readMatrix();
  7.             } catch (Exception e) {
  8.                 throw new InputFileException();
  9.             }
  10.         }
  11.     }
  12.    
  13.     public void computeNext() {
  14.         int[][] nextMap = new int[20][30];
  15.         for (int i = 0; i < 20; i++){
  16.             for (int j = 0; j < 30; j++){
  17.                 nextMap[i][j] = 0;
  18.                 // The terrain always stays the same
  19.                 nextMap[i][j] = setFigureGeneral(nextMap[i][j], 0, getFigure(i,j,0));
  20.                 // The road map always stays the same
  21.                 nextMap[i][j] = setFigureGeneral(nextMap[i][j], 1, getFigure(i,j,1));
  22.                 // The crashes are always the same
  23.                 nextMap[i][j] = setFigureGeneral(nextMap[i][j], 3, getFigure(i,j,3));
  24.             }
  25.         }
  26.         // Iterate over all cells in the game map and move the cars
  27.         for (int i = 0; i < 20; i++){
  28.             for (int j = 0; j < 30; j++){
  29.                 // If there IS a car here, then it should move
  30.                 if (getFigure(i,j,2) != 0){
  31.                     int newi, newj, newdir;
  32.                     if (getFigure(i,j,1) != 0){
  33.                         // If there is an arrow here, follow it
  34.                         newi = i + vi[getFigure(i,j,1)];
  35.                         newj = j + vj[getFigure(i,j,1)];
  36.                         newdir = getFigure(i,j,1);
  37.                     } else {
  38.                         // Otherwise, just go to where you were going!
  39.                         newi = i + vi[getFigure(i,j,2)];
  40.                         newj = j + vj[getFigure(i,j,2)];
  41.                         newdir = getFigure(i,j,2);
  42.                     }
  43.                     if (getFigureGeneral(nextMap[newi][newj], 0) == 0 && getFigureGeneral(nextMap[newi][newj], 2) == 0 && getFigureGeneral(nextMap[newi][newj],3) == 0){
  44.                         // If the terrain up ahead is a road, and there is no car there yet, and no accident, move there
  45.                         nextMap[newi][newj] = setFigureGeneral(nextMap[newi][newj],2,newdir);
  46.                     } else {
  47.                         // Move there, but crash like a crying bitch
  48.                         nextMap[newi][newj] = setFigureGeneral(nextMap[newi][newj],3,1);
  49.                         nextMap[newi][newj] = setFigureGeneral(nextMap[newi][newj],2,0);
  50.                     }
  51.                 }
  52.             }
  53.         }
  54.         gameMap = nextMap;
  55.         allStates.add(gameMap);
  56.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement