Advertisement
utroz

Untitled

May 19th, 2011
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.08 KB | None | 0 0
  1. // @uthor Utroz.
  2.  
  3. // Game Map
  4. public class GameMap {
  5.  
  6.     /* Item's ID */
  7.     private final int snakeID = 0,
  8.     mapID = 1,
  9.     borderID = 2,
  10.     appleID = 3;
  11.  
  12.     /* Game Map */
  13.     private int[][] GameMap;   
  14.  
  15.     /* Snake Object */
  16.     public Snake object;
  17.  
  18.     /* Apple Pos */
  19.     private int[] applePosition = new int[2];
  20.  
  21.     private boolean GameStatus;
  22.  
  23.     public GameMap() {
  24.  
  25.         GameStatus = true;
  26.  
  27.     }      
  28.  
  29.     /* Map Dimension: 7x7 */
  30.     public void createMap(){
  31.  
  32.         int[][] map = {
  33.                 {2, 2, 2, 2, 2, 2, 2},
  34.                     {2, 1, 1, 1, 1, 1, 2},
  35.                     {2, 1, 1, 1, 1, 1, 2},
  36.                     {2, 1, 1, 1, 1, 1, 2},
  37.                     {2, 1, 1, 1, 1, 1, 2},
  38.                     {2, 1, 1, 1, 1, 1, 2},
  39.                     {2, 2, 2, 2, 2, 2, 2}
  40.                             };
  41.  
  42.         this.GameMap = map;
  43.     }  
  44.  
  45.     public void setMap(int[] newPos){
  46.  
  47.         // Catch a snake position.
  48.         int[] pos = object.getPosition();
  49.  
  50.         // Remove a old snake pos.
  51.         this.GameMap[pos[0]][pos[1]] = this.mapID;     
  52.  
  53.         // Set a new snake position.
  54.         if (this.GameMap[newPos[0]][newPos[1]] == this.borderID){
  55.  
  56.             object.setLive(false); // set dead.
  57.             this.GameStatus = false; // finish game.
  58.  
  59.         } else if (this.GameMap[newPos[0]][newPos[1]] == this.appleID) {
  60.  
  61.             object.setSize();
  62.             createApple();         
  63.  
  64.             this.GameMap[newPos[0]][newPos[1]] = this.snakeID; // new pos.
  65.  
  66.             object.setPosition(newPos);
  67.  
  68.         } else {
  69.  
  70.             this.GameMap[newPos[0]][newPos[1]] = this.snakeID; // new pos.
  71.             object.setPosition(newPos);
  72.         }
  73.     }  
  74.  
  75.     /* Create Snake on the map */
  76.  
  77.     public void createSnake(){ 
  78.  
  79.         int row = (int) Math.random() * this.GameMap.length,
  80.         column = (int) Math.random() * this.GameMap.length;
  81.  
  82.         if(this.GameMap[row][column] == 1){
  83.  
  84.             this.GameMap[row][column] = this.snakeID;          
  85.  
  86.             object = new Snake(row, column);
  87.  
  88.         } else {
  89.             createSnake();
  90.         }
  91.  
  92.     }
  93.  
  94.     /* Create Apple on the map */
  95.  
  96.     public void createApple(){
  97.  
  98.         int row = (int) Math.random() * this.GameMap.length;
  99.         int column = (int) Math.random() * this.GameMap.length;
  100.  
  101.         if(this.GameMap[row][column] != 2 && this.GameMap[row][column] != 0){
  102.  
  103.             this.GameMap[row][column] = this.appleID;          
  104.  
  105.             this.applePosition[0] = row;
  106.             this.applePosition[1] = column;
  107.  
  108.         } else {
  109.  
  110.             createApple();
  111.  
  112.         }
  113.  
  114.     }  
  115.  
  116.     public int[] getApplePosition(){
  117.  
  118.         return this.applePosition;
  119.     }  
  120.  
  121.     public int getMapID(int row, int column){
  122.  
  123.         return this.GameMap[row][column];
  124.  
  125.     }
  126.  
  127. }
  128.  
  129. // public class Snake {
  130.  
  131.     private boolean Live;
  132.     private int size;
  133.     private int[] position = new int[2];    
  134.  
  135.     public Snake(int row, int column){
  136.  
  137.         this.Live = true;
  138.         this.size = 1;        
  139.  
  140.         this.position[0] = row;
  141.         this.position[1] = column;
  142.  
  143.     }
  144.  
  145.     public int[] getPosition() {
  146.         return this.position;
  147.     }    
  148.  
  149.     public void setPosition(int[] newPos){    
  150.  
  151.         this.position[0] = newPos[0];
  152.         this.position[1] = newPos[1];
  153.  
  154.     }    
  155.  
  156.     public void setLive(boolean arg){
  157.         this.Live = false;
  158.     }
  159.  
  160.     public void setSize(){
  161.         this.size += 1;
  162.  
  163.     }    
  164.  
  165.     public boolean isLive() {
  166.         return this.Live;
  167.  
  168.     }    
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement