Advertisement
Mary_99

Pole gry

May 7th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.69 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.MouseEvent;
  4. import java.awt.event.MouseListener;
  5. import java.awt.image.BufferedImage;
  6. import java.awt.image.ImageObserver;
  7. import java.util.Random;
  8.  
  9. public class GameArea extends Canvas implements MouseListener {
  10.     public BufferedImage image = new BufferedImage(405, 405, 1);
  11.     public Graphics2D graphic;
  12.     public int[][] tabSquere;
  13.     public int[][] tabBall;
  14.     public byte selectedX;
  15.     public byte selectedY;
  16.     public int tempX;
  17.     public int tempY;
  18.     public int x1;
  19.     public int y1;
  20.     public int x2;
  21.     public int y2;
  22.     public int x3;
  23.     public int y3;
  24.     public boolean selected;
  25.     public Random los;
  26.  
  27.     public GameArea() {
  28.         this.graphic = (Graphics2D)this.image.getGraphics();
  29.         this.los = new Random();
  30.         this.addMouseListener(this);
  31.         this.tabSquere = new int[9][9];
  32.         this.tabBall = new int[9][9];
  33.         this.newGame();
  34.     }
  35.  
  36.     public void newGame() {
  37.         for(int x = 0; x < 9; ++x) {
  38.             for(int y = 0; y < 9; ++y) {
  39.                 this.tabBall[x][y] = 0;
  40.                 this.tabSquere[x][y] = 0;
  41.             }
  42.         }
  43.  
  44.         this.newBall();
  45.         this.newBall();
  46.         this.newBall();
  47.         this.newBall();
  48.         Ball.points = 0;
  49.         this.selected = false;
  50.     }
  51.  
  52.     public void paint(Graphics g) {
  53.         super.paint(g);
  54.         this.printSquere();
  55.     }
  56.  
  57.     public void printSquere() {
  58.         this.squere();
  59.         Graphics g2 = this.getGraphics();
  60.         g2.drawImage(this.image, 0, 0, (ImageObserver)null);
  61.         g2.dispose();
  62.     }
  63.  
  64.     public void squere() {
  65.         int index = 0;
  66.  
  67.         for(int x = 0; x < 9; ++x) {
  68.             for(int y = 0; y < 9; ++y) {
  69.                 if (this.tabBall[x][y] == 0) {
  70.                     ++index;
  71.                 }
  72.  
  73.                 this.graphic.setColor(Ball.SQUERE_COLOUR[this.tabSquere[x][y]]);
  74.                 this.graphic.fillRect(x * 45, y * 45, 45, 45);
  75.                 this.graphic.setColor(Ball.BACKGROUNG_COLOUR);
  76.                 this.graphic.drawRect(x * 45, y * 45, 45, 45);
  77.                 if (this.tabBall[x][y] > 0) {
  78.                     this.oneBall(x, y, this.tabBall[x][y]);
  79.                 }
  80.             }
  81.         }
  82.  
  83.         if (index < 3) {
  84.             this.gameOver();
  85.         }
  86.  
  87.     }
  88.  
  89.     public void gameOver() {
  90.         JOptionPane.showMessageDialog((Component)null, "Your Score: " + String.valueOf(Ball.points), "GAME OVER", 1);
  91.         this.newGame();
  92.         Ball.lPoints.setText("0");
  93.     }
  94.  
  95.     public void oneBall(int x, int y, int k) {
  96.         this.graphic.setColor(Color.BLACK);
  97.         this.graphic.fillOval(x * 45 + 2, y * 45 + 2, 41, 41);
  98.         this.graphic.setColor(Ball.BALL_COLOUR[k - 1]);
  99.         this.graphic.fillOval(x * 45 + 4, y * 45 + 3, 38, 39);
  100.         this.graphic.setColor(Color.WHITE);
  101.         this.graphic.fillOval(x * 45 + 8, y * 45 + 8, 7, 7);
  102.     }
  103.  
  104.     public void mouseClicked(MouseEvent e) {
  105.     }
  106.  
  107.     public void mousePressed(MouseEvent e) {
  108.         int x = e.getX() / 45;
  109.         int y = e.getY() / 45;
  110.         if (e.getButton() == 1) {
  111.             if (this.tabBall[x][y] > 0 && !this.selected) {
  112.                 this.selected = true;
  113.                 this.selectedX = (byte)x;
  114.                 this.selectedY = (byte)y;
  115.                 this.tabSquere[x][y] = 2;
  116.                 this.printSquere();
  117.             } else if (this.tabSquere[x][y] == 1) {
  118.                 this.tabBall[x][y] = this.tabBall[this.selectedX][this.selectedY];
  119.                 this.tabBall[this.selectedX][this.selectedY] = 0;
  120.                 this.clearSelected();
  121.                 if (!this.check()) {
  122.                     this.newBall();
  123.                     this.x1 = this.tempX;
  124.                     this.y1 = this.tempY;
  125.                     this.newBall();
  126.                     this.x2 = this.tempX;
  127.                     this.y2 = this.tempY;
  128.                     this.newBall();
  129.                     this.x3 = this.tempX;
  130.                     this.y3 = this.tempY;
  131.                     this.newBall();
  132.                 } else {
  133.                     Ball.lPoints.setText(String.valueOf(Ball.points));
  134.                 }
  135.  
  136.                 this.printSquere();
  137.                 this.clearSelected();
  138.             } else {
  139.                 this.clearSelected();
  140.                 this.printSquere();
  141.             }
  142.         }
  143.  
  144.     }
  145.  
  146.  
  147.  
  148.     public void mouseReleased(MouseEvent e) {
  149.         if (this.selected) {
  150.             if (!this.freeSpaces()) {
  151.                 this.clearSelected();
  152.             }
  153.  
  154.             this.printSquere();
  155.         }
  156.  
  157.     }
  158.  
  159.     public void mouseEntered(MouseEvent e) {
  160.     }
  161.  
  162.     public void mouseExited(MouseEvent e) {
  163.     }
  164.  
  165.     public void clearSelected() {
  166.         this.selected = false;
  167.  
  168.         for(int x = 0; x < 9; ++x) {
  169.             for(int y = 0; y < 9; ++y) {
  170.                 this.tabSquere[x][y] = 0;
  171.             }
  172.         }
  173.  
  174.     }
  175.  
  176.     public boolean select(int x, int y) {
  177.         return this.tabBall[x][y] == 0 && this.tabSquere[x][y] == 0;
  178.     }
  179.  
  180.     public boolean freeSpaces() {
  181.         boolean pola = false;
  182.  
  183.         int index;
  184.         do {
  185.             index = 0;
  186.  
  187.             for(int x = 0; x < 9; ++x) {
  188.                 for(int y = 0; y < 9; ++y) {
  189.                     if (this.tabSquere[x][y] > 0) {
  190.                         if (x - 1 >= 0 && this.select(x - 1, y)) {
  191.                             ++index;
  192.                             this.tabSquere[x - 1][y] = 1;
  193.                             pola = true;
  194.                         }
  195.  
  196.                         if (x + 1 < 9 && this.select(x + 1, y)) {
  197.                             ++index;
  198.                             this.tabSquere[x + 1][y] = 1;
  199.                             pola = true;
  200.                         }
  201.  
  202.                         if (y - 1 >= 0 && this.select(x, y - 1)) {
  203.                             ++index;
  204.                             this.tabSquere[x][y - 1] = 1;
  205.                             pola = true;
  206.                         }
  207.  
  208.                         if (y + 1 < 9 && this.select(x, y + 1)) {
  209.                             ++index;
  210.                             this.tabSquere[x][y + 1] = 1;
  211.                             pola = true;
  212.                         }
  213.                     }
  214.                 }
  215.             }
  216.         } while(index > 0);
  217.  
  218.         return pola;
  219.     }
  220.  
  221.     public void newBall() {
  222.         int x;
  223.         int y;
  224.         do {
  225.             x = this.los.nextInt(9);
  226.             y = this.los.nextInt(9);
  227.         } while(this.tabBall[x][y] > 0);
  228.  
  229.         this.tabBall[x][y] = this.los.nextInt(5) + 1;
  230.         this.tempX = x;
  231.         this.tempY = y;
  232.     }
  233.  
  234.     public boolean check() {
  235.         boolean con = false;
  236.  
  237.         for(int x = 0; x < 9; ++x) {
  238.             for(int y = 0; y < 9; ++y) {
  239.                 if (this.tabBall[x][y] > 0 && this.line1(x, y)) {
  240.                     con = true;
  241.                 }
  242.  
  243.                 if (this.tabBall[x][y] > 0 && this.line2(x, y)) {
  244.                     con = true;
  245.                 }
  246.             }
  247.         }
  248.  
  249.         return con;
  250.     }
  251.  
  252.     public boolean line1(int x, int y) {
  253.         if (this.tabBall[x][y] == 0) {
  254.             return false;
  255.         } else {
  256.             int index = 0;
  257.             int color = this.tabBall[x][y];
  258.  
  259.             for(int i = x; i < 9; ++i) {
  260.                 int j;
  261.                 if (this.tabBall[i][y] != color) {
  262.                     if (index < 5) {
  263.                         return false;
  264.                     }
  265.  
  266.                     for(j = i - 1; j > x - 1; --j) {
  267.                         this.tabBall[j][y] = 0;
  268.                     }
  269.  
  270.                     this.addPoints(index);
  271.                     return true;
  272.                 }
  273.  
  274.                 ++index;
  275.                 if (i == 8) {
  276.                     if (index < 5) {
  277.                         return false;
  278.                     }
  279.  
  280.                     for(j = i; j > x - 1; --j) {
  281.                         this.tabBall[j][y] = 0;
  282.                     }
  283.  
  284.                     this.addPoints(index);
  285.                     return true;
  286.                 }
  287.             }
  288.  
  289.             return false;
  290.         }
  291.     }
  292.  
  293.     public boolean line2(int x, int y) {
  294.         if (this.tabBall[x][y] == 0) {
  295.             return false;
  296.         } else {
  297.             int index = 0;
  298.             int color = this.tabBall[x][y];
  299.  
  300.             for(int i = y; i < 9; ++i) {
  301.                 int j;
  302.                 if (this.tabBall[x][i] != color) {
  303.                     if (index < 5) {
  304.                         return false;
  305.                     }
  306.  
  307.                     for(j = i - 1; j > y - 1; --j) {
  308.                         this.tabBall[x][j] = 0;
  309.                     }
  310.  
  311.                     this.addPoints(index);
  312.                     return true;
  313.                 }
  314.  
  315.                 ++index;
  316.                 if (i == 8) {
  317.                     if (index < 5) {
  318.                         return false;
  319.                     }
  320.  
  321.                     for(j = i; j > y - 1; --j) {
  322.                         this.tabBall[x][j] = 0;
  323.                     }
  324.  
  325.                     this.addPoints(index);
  326.                     return true;
  327.                 }
  328.             }
  329.  
  330.             return false;
  331.         }
  332.     }
  333.  
  334.     public void addPoints(int p) {
  335.         switch(p) {
  336.             case 5:
  337.                 Ball.points += 5;
  338.                 break;
  339.             case 6:
  340.                 Ball.points += 8;
  341.                 break;
  342.             case 7:
  343.                 Ball.points+= 15;
  344.                 break;
  345.             case 8:
  346.                 Ball.points += 30;
  347.                 break;
  348.             case 9:
  349.                 Ball.points += 50;
  350.         }
  351.  
  352.     }
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement