Advertisement
utroz

Snake

May 21st, 2011
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 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.GameMap[row][column];
  196.  
  197.     }
  198.  
  199.    
  200.  
  201. }
  202.  
  203. public class Snake {
  204.  
  205.     private boolean Live;
  206.  
  207.     private int size;
  208.  
  209.     private int[] position = new int[2];
  210.  
  211.    
  212.  
  213.     public Snake(int row, int column){
  214.  
  215.         this.Live = true;
  216.  
  217.         this.size = 1;
  218.  
  219.        
  220.  
  221.         this.position[0] = row;
  222.  
  223.         this.position[1] = column;
  224.  
  225.     }
  226.  
  227.  
  228.  
  229.     public int[] getPosition() {
  230.  
  231.         return this.position;
  232.  
  233.     }
  234.  
  235.    
  236.  
  237.     public void setPosition(int[] newPos){ 
  238.  
  239.         this.position[0] = newPos[0];
  240.  
  241.         this.position[1] = newPos[1];
  242.  
  243.     }
  244.  
  245.    
  246.  
  247.     public void setLive(boolean arg){
  248.  
  249.         this.Live = false;
  250.  
  251.     }
  252.  
  253.  
  254.  
  255.     public void setSize(){
  256.  
  257.         this.size += 1;
  258.  
  259.     }  
  260.  
  261.  
  262.  
  263.     public boolean isLive() {
  264.  
  265.         return this.Live;
  266.  
  267.     }  
  268.  
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement