Guest User

Untitled

a guest
Dec 14th, 2017
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.45 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3.  
  4. //Controls all the game logic .. most important class in this project.
  5. public class ThreadsController extends Thread {
  6.      ArrayList<ArrayList<DataOfSquare>> Squares= new ArrayList<ArrayList<DataOfSquare>>();
  7.      Tuple headSnakePos;
  8.      int sizeSnake=3;
  9.      long speed = 50;
  10.      public static int directionSnake ;
  11.  
  12.      ArrayList<Tuple> positions = new ArrayList<Tuple>();
  13.      Tuple foodPosition;
  14.      
  15.      //Constructor of ControlleurThread
  16.      ThreadsController(Tuple positionDepart){
  17.         //Get all the threads
  18.         Squares=Window.Grid;
  19.        
  20.         headSnakePos=new Tuple(positionDepart.x,positionDepart.y);
  21.         directionSnake = 1;
  22.  
  23.         //!!! Pointer !!!!
  24.         Tuple headPos = new Tuple(headSnakePos.getX(),headSnakePos.getY());
  25.         positions.add(headPos);
  26.        
  27.         foodPosition= new Tuple(Window.height-1,Window.width-1);
  28.         spawnFood(foodPosition);
  29.  
  30.      }
  31.      
  32.      //Important part :
  33.      public void run() {
  34.          while(true){
  35.              moveInterne(directionSnake);
  36.              checkCollision();
  37.              moveExterne();
  38.              deleteTail();
  39.              pauser();
  40.          }
  41.      }
  42.      
  43.      //delay between each move of the snake
  44.      private void pauser(){
  45.          try {
  46.                 sleep(speed);
  47.          } catch (InterruptedException e) {
  48.                 e.printStackTrace();
  49.          }
  50.      }
  51.      
  52.      //Checking if the snake bites itself or is eating
  53.      private void checkCollision() {
  54.          Tuple posCritique = positions.get(positions.size()-1);
  55.          for(int i = 0;i<=positions.size()-2;i++){
  56.              boolean biteItself = posCritique.getX()==positions.get(i).getX() && posCritique.getY()==positions.get(i).getY();
  57.              if(biteItself){
  58.                 stopTheGame();
  59.              }
  60.          }
  61.          
  62.          boolean eatingFood = posCritique.getX()==foodPosition.y && posCritique.getY()==foodPosition.x;
  63.          if(eatingFood){
  64.              System.out.println("Yummy!");
  65.              sizeSnake=sizeSnake+1;
  66.                 foodPosition = getValAleaNotInSnake();
  67.  
  68.              spawnFood(foodPosition);  
  69.          }
  70.      }
  71.      
  72.      //Stops The Game
  73.      private void stopTheGame(){
  74.          System.out.println("COLISION! \n");
  75.          while(true){
  76.              pauser();
  77.          }
  78.      }
  79.      
  80.      //Put food in a position and displays it
  81.      private void spawnFood(Tuple foodPositionIn){
  82.             Squares.get(foodPositionIn.x).get(foodPositionIn.y).lightMeUp(1);
  83.      }
  84.      
  85.      //return a position not occupied by the snake
  86.      private Tuple getValAleaNotInSnake(){
  87.          Tuple p ;
  88.          int ranX= 0 + (int)(Math.random()*19);
  89.          int ranY= 0 + (int)(Math.random()*19);
  90.          p=new Tuple(ranX,ranY);
  91.          for(int i = 0;i<=positions.size()-1;i++){
  92.              if(p.getY()==positions.get(i).getX() && p.getX()==positions.get(i).getY()){
  93.                  ranX= 0 + (int)(Math.random()*19);
  94.                  ranY= 0 + (int)(Math.random()*19);
  95.                  p=new Tuple(ranX,ranY);
  96.                  i=0;
  97.              }
  98.          }
  99.          return p;
  100.      }
  101.      
  102.      //Moves the head of the snake and refreshes the positions in the arraylist
  103.      //1:right 2:left 3:top 4:bottom 0:nothing
  104.      private void moveInterne(int dir){
  105.          switch(dir){
  106.             case 4:
  107.                  headSnakePos.ChangeData(headSnakePos.x,(headSnakePos.y+1)%20);
  108.                  positions.add(new Tuple(headSnakePos.x,headSnakePos.y));
  109.                 break;
  110.             case 3:
  111.                 if(headSnakePos.y-1<0){
  112.                      headSnakePos.ChangeData(headSnakePos.x,19);
  113.                  }
  114.                 else{
  115.                  headSnakePos.ChangeData(headSnakePos.x,Math.abs(headSnakePos.y-1)%20);
  116.                 }
  117.                  positions.add(new Tuple(headSnakePos.x,headSnakePos.y));
  118.                 break;
  119.             case 2:
  120.                  if(headSnakePos.x-1<0){
  121.                      headSnakePos.ChangeData(19,headSnakePos.y);
  122.                  }
  123.                  else{
  124.                      headSnakePos.ChangeData(Math.abs(headSnakePos.x-1)%20,headSnakePos.y);
  125.                  }
  126.                 positions.add(new Tuple(headSnakePos.x,headSnakePos.y));
  127.  
  128.                 break;
  129.             case 1:
  130.                  headSnakePos.ChangeData(Math.abs(headSnakePos.x+1)%20,headSnakePos.y);
  131.                  positions.add(new Tuple(headSnakePos.x,headSnakePos.y));
  132.                  break;
  133.          }
  134.      }
  135.      
  136.      //Refresh the squares that needs to be
  137.      private void moveExterne(){
  138.          for(Tuple t : positions){
  139.              int y = t.getX();
  140.              int x = t.getY();
  141.              Squares.get(x).get(y).lightMeUp(0);
  142.              
  143.          }
  144.      }
  145.      
  146.      //Refreshes the tail of the snake, by removing the superfluous data in positions arraylist
  147.      //and refreshing the display of the things that is removed
  148.      private void deleteTail(){
  149.          int cmpt = sizeSnake;
  150.          for(int i = positions.size()-1;i>=0;i--){
  151.              if(cmpt==0){
  152.                  Tuple t = positions.get(i);
  153.                  Squares.get(t.y).get(t.x).lightMeUp(2);
  154.              }
  155.              else{
  156.                  cmpt--;
  157.              }
  158.          }
  159.          cmpt = sizeSnake;
  160.          for(int i = positions.size()-1;i>=0;i--){
  161.              if(cmpt==0){
  162.                  positions.remove(i);
  163.              }
  164.              else{
  165.                  cmpt--;
  166.              }
  167.          }
  168.      }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment