Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void takeAction() throws InputFileException {
- if (isInitial) {
- computeNext();
- } else {
- try {
- readMatrix();
- } catch (Exception e) {
- throw new InputFileException();
- }
- }
- }
- public void computeNext() {
- int[][] nextMap = new int[20][30];
- for (int i = 0; i < 20; i++){
- for (int j = 0; j < 30; j++){
- nextMap[i][j] = 0;
- // The terrain always stays the same
- nextMap[i][j] = setFigureGeneral(nextMap[i][j], 0, getFigure(i,j,0));
- // The road map always stays the same
- nextMap[i][j] = setFigureGeneral(nextMap[i][j], 1, getFigure(i,j,1));
- // The crashes are always the same
- nextMap[i][j] = setFigureGeneral(nextMap[i][j], 3, getFigure(i,j,3));
- }
- }
- // Iterate over all cells in the game map and move the cars
- for (int i = 0; i < 20; i++){
- for (int j = 0; j < 30; j++){
- // If there IS a car here, then it should move
- if (getFigure(i,j,2) != 0){
- int newi, newj, newdir;
- if (getFigure(i,j,1) != 0){
- // If there is an arrow here, follow it
- newi = i + vi[getFigure(i,j,1)];
- newj = j + vj[getFigure(i,j,1)];
- newdir = getFigure(i,j,1);
- } else {
- // Otherwise, just go to where you were going!
- newi = i + vi[getFigure(i,j,2)];
- newj = j + vj[getFigure(i,j,2)];
- newdir = getFigure(i,j,2);
- }
- if (getFigureGeneral(nextMap[newi][newj], 0) == 0 && getFigureGeneral(nextMap[newi][newj], 2) == 0 && getFigureGeneral(nextMap[newi][newj],3) == 0){
- // If the terrain up ahead is a road, and there is no car there yet, and no accident, move there
- nextMap[newi][newj] = setFigureGeneral(nextMap[newi][newj],2,newdir);
- } else {
- // Move there, but crash like a crying bitch
- nextMap[newi][newj] = setFigureGeneral(nextMap[newi][newj],3,1);
- nextMap[newi][newj] = setFigureGeneral(nextMap[newi][newj],2,0);
- }
- }
- }
- }
- gameMap = nextMap;
- allStates.add(gameMap);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement