Advertisement
Guest User

Untitled

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