Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. // Game Map
  2.  
  3.     public class GameMap {
  4.  
  5.  
  6.  
  7.         /* Item's ID */
  8.  
  9.         private final int snakeID = 0,
  10.  
  11.         mapID = 1,
  12.  
  13.         borderID = 2,
  14.  
  15.         appleID = 3;
  16.  
  17.  
  18.  
  19.         /* Game Map */
  20.  
  21.         private int[][] GameMap;
  22.  
  23.    
  24.  
  25.         /* Snake Object */
  26.  
  27.         public Snake object;
  28.  
  29.  
  30.  
  31.         /* Apple Pos */
  32.  
  33.         private int[] applePosition = new int[2];
  34.  
  35.  
  36.  
  37.         public GameMap() {
  38.  
  39.         }
  40.  
  41.    
  42.  
  43.         /* Map Dimension: 7x7 */
  44.  
  45.         public void createMap(){
  46.  
  47.             int[][] map = {
  48.  
  49.                     {2, 2, 2, 2, 2, 2, 2},
  50.  
  51.                         {2, 1, 1, 1, 1, 1, 2},
  52.  
  53.                         {2, 1, 1, 1, 1, 1, 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, 2, 2, 2, 2, 2, 2}
  62.  
  63.                                 };
  64.  
  65.  
  66.  
  67.             this.GameMap = map;
  68.  
  69.         }
  70.  
  71.    
  72.  
  73.         public boolean setMap(int[] pos, int[] newPos){
  74.  
  75.             // Remove a old snake pos.
  76.  
  77.             this.GameMap[pos[0]][pos[1]] = this.mapID;
  78.  
  79.        
  80.  
  81.             // Set a new snake position.
  82.  
  83.             if (this.GameMap[newPos[0]][newPos[1]] == this.borderID){ // if occur colision then..
  84.  
  85.                 return true;
  86.  
  87.             } else if (this.GameMap[newPos[0]][newPos[1]] == this.appleID) { // if eat apple then..
  88.  
  89.                 object.setSize();
  90.  
  91.                 createApple();         
  92.  
  93.                 this.GameMap[newPos[0]][newPos[1]] = this.snakeID; // new pos.
  94.  
  95.             } else {
  96.  
  97.                 this.GameMap[newPos[0]][newPos[1]] = this.snakeID; // new pos.
  98.  
  99.             }
  100.  
  101.             return false;
  102.  
  103.         }      
  104.  
  105.  
  106.  
  107.         /* Create Snake on the map */
  108.  
  109.         public void createSnake(){
  110.  
  111.    
  112.  
  113.             int row = (int) Math.random() * this.GameMap.length,
  114.  
  115.             column = (int) Math.random() * this.GameMap.length;
  116.  
  117.  
  118.  
  119.             if(this.GameMap[row][column] == 1){
  120.  
  121.                 this.GameMap[row][column] = this.snakeID;
  122.  
  123.            
  124.  
  125.                 object = new Snake(row, column);
  126.  
  127.             } else {
  128.  
  129.                 createSnake();
  130.  
  131.             }
  132.  
  133.         }
  134.  
  135.  
  136.  
  137.         /* Create Apple on the map */
  138.  
  139.         public void createApple(){
  140.  
  141.             int row = (int) Math.random() * this.GameMap.length;
  142.  
  143.             int column = (int) Math.random() * this.GameMap.length;
  144.  
  145.  
  146.  
  147.             if(this.GameMap[row][column] != 2 && this.GameMap[row][column] != 0){
  148.  
  149.                 this.GameMap[row][column] = this.appleID;
  150.  
  151.            
  152.  
  153.                 this.applePosition[0] = row;
  154.  
  155.                 this.applePosition[1] = column;
  156.  
  157.             } else {
  158.  
  159.                 createApple();
  160.  
  161.             }
  162.  
  163.         }
  164.  
  165.    
  166.  
  167.         public int[] getApplePosition(){
  168.  
  169.             return this.applePosition;
  170.  
  171.         }
  172.  
  173.    
  174.  
  175.         public int getMapID(int row, int column){
  176.  
  177.             return this.GameMap[row][column];
  178.  
  179.         }
  180.  
  181.    
  182.  
  183.     }
  184.  
  185. // Snake
  186.  
  187. public class Snake {
  188.  
  189.     private boolean Live;
  190.  
  191.     private int size; // implements after.
  192.  
  193.     private int[] position = new int[2];
  194.  
  195.     public GameMap pos;
  196.  
  197.    
  198.  
  199.     public Snake(int row, int column){
  200.  
  201.         this.Live = true;
  202.  
  203.         this.size = 1;
  204.  
  205.        
  206.  
  207.         this.position[0] = row;
  208.  
  209.         this.position[1] = column;
  210.  
  211.     }
  212.  
  213.  
  214.  
  215.     public int[] getPosition() {
  216.  
  217.         return this.position;
  218.  
  219.     }
  220.  
  221.    
  222.  
  223.     public void setPosition(int[] newPos){ 
  224.  
  225.         pos = new GameMap();
  226.  
  227.         boolean check = pos.setMap(this.position, newPos);
  228.  
  229.        
  230.  
  231.         if(check == true){
  232.  
  233.             this.Live = false;
  234.  
  235.         } else {
  236.  
  237.             this.position = newPos;
  238.  
  239.  
  240.  
  241.         }
  242.  
  243.     }
  244.  
  245.  
  246.  
  247.     public void setSize(){
  248.  
  249.         this.size += 1;
  250.  
  251.     }  
  252.  
  253.  
  254.  
  255.     public boolean isLive() {
  256.  
  257.         return this.Live;
  258.  
  259.     }  
  260.  
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement