Advertisement
Guest User

Magic

a guest
Sep 27th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.56 KB | None | 0 0
  1. // Nukopinau kas manau bus tau naudingiausia.
  2. // GameManager klase, pagrindine zaidimo logika ir tt. Cia tik map kurimas
  3.  
  4.     private static final int mapSize = 30;
  5.     private static final int cellSize = 20;
  6.     private static final int numberOfBombs = 100;
  7.  
  8.     public static boolean gameBegan;
  9.    
  10.     public static CellData[][] gameMatrix;
  11.  
  12. public GameManager(){ // Konstruktorius
  13.         gameBegan = false;
  14.         visC = new VisualClass(cellSize, mapSize); // Cia pagrindine vizualizacijos klase. Tesiog sukuriu langa.
  15.         visC.setColors(Color.lightGray, Color.white);
  16.         try{
  17.            if(((mapSize * mapSize) - 5) < numberOfBombs){
  18.                throw new Exception("Too many bombs! Can't proceed as there would be no numbers...");
  19.            }
  20.            if(numberOfBombs <= 0){
  21.                throw new Exception("Number of bombs is invalid...");
  22.            }
  23.            
  24.         }
  25.         catch (Exception ex) {
  26.             Logger.getLogger(GameManager.class.getName()).log(Level.SEVERE, null, ex);
  27.         }
  28.     }
  29.    
  30. // Taip kaip mapas kuriamas tik po pirmo paspaudimo,
  31. // todel sitas metodas kvieciamas VisualClass klasejem, po pirmojo paspaudimo.
  32.     public static void generateMap(int clickPosX, int clickPosY){
  33.         MapManager mapMan = new MapManager(mapSize, numberOfBombs); // Mapas generojamas isvis kitoi vietoi, nes ten daug skaiciavimu //atliekama. grubiai tariant, du for'ai ir daug ifu.
  34.         gameMatrix = mapMan.createGameMatrix(clickPosX, clickPosY);
  35.         gameBegan = true;
  36.     }
  37.  
  38.  
  39.  
  40. // VisualClass. Vizualizacijos cia.
  41.  
  42. public class VisualClass extends ScreenKTU{
  43.     int gridSize;
  44.    
  45.    int[] lastPaintedPos = new int[2]; // cia skirta animacijai, nekreipk demesio (tas visas animacijas istrinsiu, nes neveikia, dar :D)
  46.     boolean isCurrentlyMarking = false; // sis irgi animacijai
  47.        
  48.     public VisualClass(int cellSize, int screenSize){
  49.         super(cellSize, cellSize, screenSize, screenSize, ScreenKTU.Grid.ON);
  50.         gridSize = screenSize;
  51.         isCurrentlyMarking = false;
  52.         lastPaintedPos[0] = 0;
  53.         lastPaintedPos[1] = 0;
  54.     }
  55.    
  56.     @Override
  57.     public void mousePressed(MouseEvent e){
  58.         int[] pos = GameManager.getCellFromScreenCords(e.getX(), e.getY());
  59.         isCurrentlyMarking = true;
  60.         lastPaintedPos = pos;
  61.         if(!GameManager.gameBegan){ // jei pirmas paspaudimas tai sukuria mapa su GameManager.generateMap();
  62.             System.out.print("MOUSE PRESSED! " + e.getX() + " " + e.getY() + "\n");
  63.             paintGridWhite();
  64.             GameManager.generateMap(pos[0], pos[1]);
  65.         }
  66.         if(GameManager.gameMatrix[pos[1]][pos[0]].isOpen == false){
  67.             paintCellDGray(pos); // Nuspalvinu su fillRect ir iskarto sekancioje eiluteje atnaujinu.
  68.             refresh(1);
  69.         }
  70.        
  71.     }
  72.     @Override
  73.     public void mouseReleased(MouseEvent e){ // cia irgi animacija, lalalalala
  74.         System.out.print("Mouse: " + e.getButton());
  75.         int[] pos = GameManager.getCellFromScreenCords(e.getX(), e.getY());
  76.         if(pos[0] == gridSize){ // Just never underestimate users...  (just in case to not get out of bounds exception
  77.             pos[0] = gridSize - 1;
  78.         }
  79.         if(pos[1] == gridSize){  
  80.             pos[1] = gridSize - 1;
  81.         }
  82.         if(e.getButton() == 1){ // LMB
  83.             revealCell(pos);
  84.         }
  85.         if(e.getButton() == 3){ // RMB, NOTICE BUTTON ID = 3
  86.             markCell(pos);
  87.         }
  88.         lastPaintedPos = pos; // Animacijai
  89.         isCurrentlyMarking = false;
  90.     }
  91.    
  92.     @Override
  93.     public void mouseDragged(MouseEvent e){ // Animacijai
  94.         //System.out.print("MouseMoved method is being called\n");
  95.         int[] pos = GameManager.getCellFromScreenCords(e.getX(), e.getY());
  96.         if(lastPaintedPos != pos){
  97.             // TODO: Magic
  98.         }
  99.     }
  100.    
  101. // Toliau spalvinimai
  102.      
  103.     private void paintGridWhite(){ // pradziai viska nuspalvinu baltai
  104.         fillRect(0, 0, gridSize, gridSize, Color.lightGray);
  105.         refresh(1);
  106.     }
  107.    
  108.     private void paintCellDGray(int posX, int posY){
  109.         fillRect(posY, posX, 1, 1, Color.darkGray);
  110.         refresh(1);
  111.     }
  112.    
  113.     private void paintCellDGray(int[]pos){
  114.         paintCellDGray(pos[0], pos[1]);
  115.     }
  116.    
  117.     private void revealCell(int[] pos){
  118.         revealCell(pos[0], pos[1]);
  119.     }
  120.    
  121.     private void revealCell(int i, int j){
  122.         if(GameManager.gameMatrix[j][i].isOpen == false){
  123.             switch(GameManager.gameMatrix[j][i].value){
  124.                 case -1:
  125.                     setColors(Color.pink, Color.black);
  126.                     print(j, i, 'B');
  127.                     break;
  128.                 case 0:
  129.                     /*setColors(Color.white, Color.black);
  130.                     print(j, i, '0');*/
  131.                     revealCellWithZero(i, j);
  132.                     break;
  133.                 case 1:
  134.                     setColors(Color.white, Color.green);
  135.                     print(j, i,'1');
  136.                     break;
  137.                 case 2:
  138.                     setColors(Color.white, Color.cyan);
  139.                     print(j, i, '2');
  140.                     break;
  141.                 case 3:
  142.                     setColors(Color.white, Color.blue);
  143.                     print(j, i, '3');
  144.                     break;
  145.                 case 4:
  146.                     setColors(Color.white, Color.yellow);
  147.                     print(j, i, '4');
  148.                     break;
  149.                 case 5:
  150.                     setColors(Color.white, Color.orange);
  151.                     print(j, i, '5');
  152.                     break;
  153.                 case 6:
  154.                     setColors(Color.white, Color.magenta);
  155.                     print(j, i, '6');
  156.                     break;
  157.                 case 7:
  158.                     setColors(Color.white, Color.red);
  159.                     print(j, i, '7');
  160.                     break;
  161.                 case 8:
  162.                     setColors(Color.white, Color.black);
  163.                     print(j, i, '8');
  164.                     break;
  165.             }
  166.             GameManager.gameMatrix[j][i].isOpen = true;
  167.             refresh(1);
  168.         }
  169.     }
  170.    
  171.     private void revealCellWithZero(int posX, int posY){
  172.         if(posX >= gridSize || posY >= gridSize ||
  173.                 posX < 0 || posY < 0){ return; } // Checking if not out of bounds
  174.        
  175.         if(GameManager.gameMatrix[posY][posX].isOpen) { return; }
  176.        
  177.         // If cell is still closed, continues...
  178.         if(GameManager.gameMatrix[posY][posX].value != 0){
  179.             revealCell(posX, posY);
  180.             return;
  181.         }
  182.         // If this cell is "zero"
  183.         setColors(Color.white, Color.black);
  184.         print(posY, posX, '0');
  185.         GameManager.gameMatrix[posY][posX].isOpen = true;
  186.         revealCellWithZero(posX+1, posY); // right
  187.         revealCellWithZero(posX-1, posY); // left
  188.         revealCellWithZero(posX, posY+1); // up
  189.         revealCellWithZero(posX, posY-1); // down
  190.        
  191.         // not sure about this but my game will have it. (checking corners)
  192.         revealCellWithZero(posX+1, posY+1); // bottom right
  193.         revealCellWithZero(posX+1, posY-1); // top right
  194.         revealCellWithZero(posX-1, posY+1); // bottom left
  195.         revealCellWithZero(posX-1, posY-1); // top left
  196.                
  197.     }
  198.    
  199.     private void markCell(int[] pos) {
  200.         markCell(pos[0], pos[1]);
  201.     }
  202.    
  203.     private void markCell(int posX, int posY){
  204.         setColors(Color.yellow, Color.black);
  205.         print(posY, posX, 'X');
  206.         refresh(1);
  207.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement