Guest
Public paste!

soooookoooo

By: a guest | Mar 22nd, 2010 | Syntax: Java | Size: 5.70 KB | Hits: 70 | Expires: Never
Copy text to clipboard
  1. package main;
  2.  
  3. public class Level {
  4.     private int maxwidth = 0;
  5.    
  6.     public char[][] grid;
  7.     public boolean[][] targets;
  8.     private int playerX;
  9.     private int playerY;
  10.     private int width;
  11.     private int height;
  12.     private History history;
  13.  
  14.    
  15.     static final char WALL = '#';
  16.     static final char EMPTY = ' ', TARGET = '.';
  17.     static final char PLAYER = '@', PLAYER_ON_TARGET = '+';
  18.     static final char BOX = '$', BOX_ON_TARGET = '*';
  19.    
  20.    
  21.    
  22.     public Level(String line) {    
  23.    
  24.         String[] lines = line.split("\\|");    
  25.  
  26.         this.height = lines.length;
  27.        
  28.         for (int i = 0; i < lines.length; i++) {
  29.             if (lines[i].length() > maxwidth) {
  30.                 maxwidth = lines[i].length();
  31.             }
  32.         }
  33.  
  34.         this.width = maxwidth;
  35.         grid = new char[lines.length][maxwidth];
  36.         targets = new boolean[lines.length][maxwidth];
  37.  
  38.        
  39.         for (int i = 0;i<lines.length;i++){
  40.             for (int j=0;j<maxwidth;j++){
  41.                 grid[i][j] = (j < lines[i].length() ? lines[i].charAt(j) : EMPTY);
  42.             }
  43.         }        
  44.  
  45.        
  46.        
  47.         for (int i = 0; i < grid.length; i++) {
  48.             for (int j = 0; j < grid[i].length; j++) {
  49.                 if (grid[i][j] == PLAYER) {
  50.                     playerY = i;
  51.                     playerX = j;
  52.                 }
  53.                 if (grid[i][j] == TARGET) {
  54.                     targets[i][j] = true;
  55.                 }
  56.                 else if (grid[i][j] == PLAYER_ON_TARGET) {
  57.                     targets[i][j] = true;
  58.                 }
  59.                 else if (grid[i][j] == BOX_ON_TARGET) {
  60.                     targets[i][j] = true;
  61.                 }
  62.                 else {
  63.                     targets[i][j] = false;
  64.  
  65.                 }
  66.             }
  67.         }
  68.        
  69.         history = new History();
  70.  
  71.     }
  72.    
  73.  
  74.     public int getX() {
  75.         return this.playerX;
  76.     }
  77.    
  78.     public int getY() {
  79.         return this.playerY;
  80.     }
  81.    
  82.     public int getWidth() {
  83.         return this.width;
  84.     }
  85.    
  86.     public int getHeight() {
  87.         return this.height;
  88.     }
  89.    
  90.     public int getMoves() {
  91.         return history.getMoves();
  92.     }
  93.    
  94.     public int getPushes() {
  95.         return history.getPushes();
  96.     }
  97.    
  98.     public void setCell(int y, int x, char c) {
  99.         if (c == BOX && targets[y][x] == true) {
  100.             grid[y][x] = BOX_ON_TARGET;
  101.         }
  102.         else if (c == PLAYER && targets[y][x] == true) {
  103.             grid[y][x] = PLAYER_ON_TARGET;
  104.         }
  105.         else {
  106.             grid[y][x] = c;
  107.         }
  108.     }
  109.    
  110.    
  111.    
  112.     public void doMove(int dx, int dy) {
  113.         boolean push = true;
  114.         if (grid[playerY + dy][playerX + dx] == BOX) {
  115.             setCell(playerY + dy, playerX + dx, PLAYER);
  116.             setCell(playerY + (dy*2), playerX + (dx*2), BOX);
  117.             if (targets[playerY][playerX]) setCell (playerY, playerX, TARGET); else setCell (playerY, playerX, EMPTY);
  118.         }
  119.         else if (grid[playerY + dy][playerX + dx] == BOX_ON_TARGET) {
  120.             setCell(playerY + dy, playerX + dx, PLAYER);
  121.             setCell(playerY + (dy*2), playerX + (dx*2), BOX);
  122.             if (targets[playerY][playerX]) setCell (playerY, playerX, TARGET); else setCell (playerY, playerX, EMPTY);
  123.         }
  124.         else {
  125.             setCell (playerY + dy, playerX + dx, PLAYER);
  126.             push = false;
  127.             if (targets[playerY][playerX]) setCell (playerY, playerX, TARGET); else setCell (playerY, playerX, EMPTY);
  128.         }
  129.  
  130.         history.add(new Log(playerY, playerX, dy, dx, push));
  131.        
  132.         playerX = playerX + dx;
  133.         playerY = playerY + dy;
  134.     }
  135.    
  136.  
  137.     public boolean checkMove (int x, int y) {
  138.         boolean move = true;
  139.        
  140.         if (grid[playerY + y][playerX + x] == WALL) {
  141.             move = false;
  142.         }
  143.         else if (grid[playerY+y][playerX+x] == BOX && grid[playerY+(y*2)][playerX+(x*2)] == WALL) {
  144.             move = false;
  145.         }
  146.         else if (grid[playerY+y][playerX+x] == BOX_ON_TARGET && grid[playerY+(y*2)][playerX+(x*2)] == WALL) {
  147.             move = false;
  148.         }
  149.         else if (grid[playerY+y][playerX+x] == BOX && grid[playerY+(y*2)][playerX+(x*2)] == BOX) {
  150.             move = false;
  151.         }
  152.         else if (grid[playerY+y][playerX+x] == BOX && grid[playerY+(y*2)][playerX+(x*2)] == BOX_ON_TARGET) {
  153.             move = false;
  154.         }
  155.         else if (grid[playerY+y][playerX+x] == BOX_ON_TARGET && grid[playerY+(y*2)][playerX+(x*2)] == BOX) {
  156.             move = false;
  157.         }
  158.         else {
  159.             move = true;
  160.         }
  161.        
  162.         return move;
  163.     }    
  164.    
  165.    
  166.         public Log undoMove() {
  167.                 Log log = history.remove();
  168.                 if(log != null){
  169.                         setCell(playerY, playerX, EMPTY);
  170.                         setCell(playerY - log.getDy(), playerX - log.getDx(), PLAYER);
  171.                         if (log.getPush()) {
  172.                                 setCell(playerY, playerX, BOX);
  173.                                 setCell(playerY + log.getDy(), playerX + log.getDx(), EMPTY);
  174.                         }
  175.                         playerX -= log.getDx();
  176.                         playerY -= log.getDy();
  177.                 }
  178.                 return log;
  179.         }
  180.  
  181.    
  182.     public boolean hasWon() {
  183.         boolean hasWon = true;
  184.         for (int i = 0; i < grid.length; i++) {
  185.                 for (int j = 0; j < grid[i].length; j++) {
  186.                                 if (grid[i][j] == BOX) {hasWon = false; break;}
  187.                         }
  188.                 }
  189.         return hasWon;
  190.     }
  191.    
  192.     public History getHistory() {
  193.         return this.history;
  194.     }
  195.  
  196.        
  197.  
  198.     public String toString() {
  199.             String output="";
  200.             for (int i = 0; i < grid.length; i++) {
  201.                 for (int j = 0; j < grid[i].length; j++) {
  202.                     output += grid[i][j];
  203.                 }
  204.             }
  205.             return(output);
  206.     }
  207.  
  208.  
  209.  
  210. }