Advertisement
utroz

Untitled

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