Advertisement
Mary_99

GameArea correct

May 6th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.86 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.     public void selectNew() {
  147.         this.tabSquere[this.x1][this.y1] = 3;
  148.         this.tabSquere[this.x2][this.y2] = 3;
  149.         this.tabSquere[this.x3][this.y3] = 3;
  150.     }
  151.  
  152.     public void mouseReleased(MouseEvent e) {
  153.         if (this.selected) {
  154.             if (!this.freeSpaces()) {
  155.                 this.clearSelected();
  156.             }
  157.  
  158.             this.printSquere();
  159.         }
  160.  
  161.     }
  162.  
  163.     public void mouseEntered(MouseEvent e) {
  164.     }
  165.  
  166.     public void mouseExited(MouseEvent e) {
  167.     }
  168.  
  169.     public void clearSelected() {
  170.         this.selected = false;
  171.  
  172.         for(int x = 0; x < 9; ++x) {
  173.             for(int y = 0; y < 9; ++y) {
  174.                 this.tabSquere[x][y] = 0;
  175.             }
  176.         }
  177.  
  178.     }
  179.  
  180.     public boolean select(int x, int y) {
  181.         return this.tabBall[x][y] == 0 && this.tabSquere[x][y] == 0;
  182.     }
  183.  
  184.     public boolean freeSpaces() {
  185.         boolean pola = false;
  186.  
  187.         int index;
  188.         do {
  189.             index = 0;
  190.  
  191.             for(int x = 0; x < 9; ++x) {
  192.                 for(int y = 0; y < 9; ++y) {
  193.                     if (this.tabSquere[x][y] > 0) {
  194.                         if (x - 1 >= 0 && this.select(x - 1, y)) {
  195.                             ++index;
  196.                             this.tabSquere[x - 1][y] = 1;
  197.                             pola = true;
  198.                         }
  199.  
  200.                         if (x + 1 < 9 && this.select(x + 1, y)) {
  201.                             ++index;
  202.                             this.tabSquere[x + 1][y] = 1;
  203.                             pola = true;
  204.                         }
  205.  
  206.                         if (y - 1 >= 0 && this.select(x, y - 1)) {
  207.                             ++index;
  208.                             this.tabSquere[x][y - 1] = 1;
  209.                             pola = true;
  210.                         }
  211.  
  212.                         if (y + 1 < 9 && this.select(x, y + 1)) {
  213.                             ++index;
  214.                             this.tabSquere[x][y + 1] = 1;
  215.                             pola = true;
  216.                         }
  217.                     }
  218.                 }
  219.             }
  220.         } while(index > 0);
  221.  
  222.         return pola;
  223.     }
  224.  
  225.     public void newBall() {
  226.         int x;
  227.         int y;
  228.         do {
  229.             x = this.los.nextInt(9);
  230.             y = this.los.nextInt(9);
  231.         } while(this.tabBall[x][y] > 0);
  232.  
  233.         this.tabBall[x][y] = this.los.nextInt(5) + 1;
  234.         this.tempX = x;
  235.         this.tempY = y;
  236.     }
  237.  
  238.     public boolean check() {
  239.         boolean con = false;
  240.  
  241.         for(int x = 0; x < 9; ++x) {
  242.             for(int y = 0; y < 9; ++y) {
  243.                 if (this.tabBall[x][y] > 0 && this.line1(x, y)) {
  244.                     con = true;
  245.                 }
  246.  
  247.                 if (this.tabBall[x][y] > 0 && this.line2(x, y)) {
  248.                     con = true;
  249.                 }
  250.             }
  251.         }
  252.  
  253.         return con;
  254.     }
  255.  
  256.     public boolean line1(int x, int y) {
  257.         if (this.tabBall[x][y] == 0) {
  258.             return false;
  259.         } else {
  260.             int index = 0;
  261.             int color = this.tabBall[x][y];
  262.  
  263.             for(int i = x; i < 9; ++i) {
  264.                 int j;
  265.                 if (this.tabBall[i][y] != color) {
  266.                     if (index < 5) {
  267.                         return false;
  268.                     }
  269.  
  270.                     for(j = i - 1; j > x - 1; --j) {
  271.                         this.tabBall[j][y] = 0;
  272.                     }
  273.  
  274.                     this.addPoints(index);
  275.                     return true;
  276.                 }
  277.  
  278.                 ++index;
  279.                 if (i == 8) {
  280.                     if (index < 5) {
  281.                         return false;
  282.                     }
  283.  
  284.                     for(j = i; j > x - 1; --j) {
  285.                         this.tabBall[j][y] = 0;
  286.                     }
  287.  
  288.                     this.addPoints(index);
  289.                     return true;
  290.                 }
  291.             }
  292.  
  293.             return false;
  294.         }
  295.     }
  296.  
  297.     public boolean line2(int x, int y) {
  298.         if (this.tabBall[x][y] == 0) {
  299.             return false;
  300.         } else {
  301.             int index = 0;
  302.             int color = this.tabBall[x][y];
  303.  
  304.             for(int i = y; i < 9; ++i) {
  305.                 int j;
  306.                 if (this.tabBall[x][i] != color) {
  307.                     if (index < 5) {
  308.                         return false;
  309.                     }
  310.  
  311.                     for(j = i - 1; j > y - 1; --j) {
  312.                         this.tabBall[x][j] = 0;
  313.                     }
  314.  
  315.                     this.addPoints(index);
  316.                     return true;
  317.                 }
  318.  
  319.                 ++index;
  320.                 if (i == 8) {
  321.                     if (index < 5) {
  322.                         return false;
  323.                     }
  324.  
  325.                     for(j = i; j > y - 1; --j) {
  326.                         this.tabBall[x][j] = 0;
  327.                     }
  328.  
  329.                     this.addPoints(index);
  330.                     return true;
  331.                 }
  332.             }
  333.  
  334.             return false;
  335.         }
  336.     }
  337.  
  338.     public void addPoints(int p) {
  339.         switch(p) {
  340.             case 5:
  341.                 Ball.points += 5;
  342.                 break;
  343.             case 6:
  344.                 Ball.points += 8;
  345.                 break;
  346.             case 7:
  347.                 Ball.points+= 15;
  348.                 break;
  349.             case 8:
  350.                 Ball.points += 30;
  351.                 break;
  352.             case 9:
  353.                 Ball.points += 50;
  354.         }
  355.  
  356.     }
  357. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement