Advertisement
Guest User

Untitled

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