Guest User

Untitled

a guest
Jul 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.12 KB | None | 0 0
  1. import java.util. * ;
  2. import java.awt. * ;
  3. import java.awt.event. * ;
  4. import javax.swing. * ;
  5.  
  6. public class BreakthroughGUI {
  7.     //Button array
  8.     JButton[][] BUTTONS;
  9.  
  10.     //'Selected' GameButton Placeholder
  11.     GameButton selectedButton;
  12.  
  13.     //Constructor
  14.     public BreakthroughGUI() {
  15.         //set up GUI
  16.         JFrame frame = new JFrame("Breakthrough");
  17.  
  18.         frame.setLayout(new GridLayout(8, 8));
  19.  
  20.         selectedButton = null;
  21.  
  22.         //Create the buttons
  23.         //We want our board to be 8x8,
  24.         //so we'll make an 8x8 2D array
  25.         int cols = 8;
  26.         int rows = 8;
  27.  
  28.         BUTTONS = new GameButton[cols][rows];
  29.  
  30.  
  31.         //now we loop  through the board and instantiate a button for each
  32.         for (int i = 0; i < rows; i++) {
  33.             for (int j = 0; j < cols; j++) {
  34.                 GameButton theButt = new GameButton();
  35.                 BUTTONS[i][j] = theButt;
  36.                 theButt.setXPos(j);
  37.                 theButt.setYPos(i);
  38.                 //BUTTONS[i][j].setID(500);
  39.                 frame.add(BUTTONS[i][j]);
  40.             }
  41.         }
  42.  
  43.         for (int x = 0; x < 2; x++) {
  44.             for (int y = 0; y < 8; y++) {
  45.                 BUTTONS[y][x].setText("X");
  46.             }
  47.         }
  48.  
  49.         for (int z = 7; z > 5; z--) {
  50.             for (int y = 0; y < 8; y++) {
  51.                 BUTTONS[y][z].setText("O");
  52.             }
  53.         }
  54.  
  55.  
  56.  
  57.         for (int i = 0; i < 8; i++) {
  58.             for (int j = 0; j < 8; j++) {
  59.                 BUTTONS[i][j].addActionListener(new ActionListener() {
  60.                     public void actionPerformed(ActionEvent ae) {
  61.                         GameButton button = (GameButton) ae.getSource();
  62.  
  63.                         if (selectedButton == null) {
  64.                             select(button);
  65.                         } else {
  66.                             if (selectedButton.getText() == "X") {
  67.                                 //Check if it's going backwards
  68.                                 if (button.getXPos() < selectedButton.getXPos()) {
  69.                                     //Drop out if it is backwards
  70.                                     return;
  71.                                 }
  72.  
  73.                                 //Check to see if it's in front of the selected box
  74.                                 //If we subtract the clicked squares's point from the selected square's point,
  75.                                 // x should be 1 and y should be -1 through 1
  76.                                 int xDif = button.getXPos() - selectedButton.getXPos();
  77.                                 if (xDif != 1) {
  78.                                     return;
  79.                                 }
  80.  
  81.                                 int yDif = button.getYPos() - selectedButton.getYPos();
  82.                                 if (-1 > yDif || yDif > 1) {
  83.                                     return;
  84.                                 }
  85.  
  86.                                 //Should be all clear for now (the return;s would have dropped us out of the method
  87.                                 // if the x and y difs didnt match what we need
  88.  
  89.  
  90.  
  91.                                 //Move the 'X' over
  92.                                 selectedButton.setText("");
  93.                                 button.setText("X");
  94.  
  95.                                 deselect();
  96.  
  97.  
  98.  
  99.                             } else if (selectedButton.getText() == "O") {
  100.                                 //Check if it's going backwards
  101.                                 if (button.getXPos() > selectedButton.getXPos()) {
  102.                                     //Drop out if it is backwards
  103.                                     return;
  104.                                 }
  105.  
  106.                                 //Check to see if it's in front of the selected box
  107.                                 //If we subtract the clicked squares's point from the selected square's point,
  108.                                 // x should be -1 and y should be -1 through 1
  109.                                 int xDif = button.getXPos() - selectedButton.getXPos();
  110.                                 if (xDif != -1) {
  111.                                     return;
  112.                                 }
  113.  
  114.                                 int yDif = button.getYPos() - selectedButton.getYPos();
  115.                                 if (-1 > yDif || yDif > 1) {
  116.                                     return;
  117.                                 }
  118.  
  119.                                 //Should be all clear for now (the return;s would have dropped us out of the method
  120.                                 // if the x and y difs didnt match what we need
  121.  
  122.  
  123.  
  124.                                 //Move the 'O' over
  125.                                 selectedButton.setText("");
  126.                                 button.setText("O");
  127.  
  128.                                 deselect();
  129.                             }
  130.                         }
  131.  
  132.  
  133.  
  134.                     }
  135.  
  136.  
  137.                 });
  138.             }
  139.         }
  140.  
  141.  
  142.         frame.setVisible(true);
  143.         frame.pack();
  144.         frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
  145.     }
  146.  
  147.     //Main constructor
  148.     public static void main(String[] args) {
  149.         new BreakthroughGUI();
  150.     }
  151.  
  152.     //Functions for changing whats selected and what isn't
  153.     // This lets us change what happens during the (de)select process
  154.     //  (e.g. graphical attributes, text changes, etc)
  155.     public void deselect() {
  156.         selectedButton.setEnabled(true);
  157.         selectedButton = null;
  158.  
  159.     }
  160.  
  161.     public void select(GameButton btn) {
  162.         selectedButton = btn;
  163.         selectedButton.setEnabled(false);
  164.     }
  165. }
  166.  
  167.  
  168. //Here's our custom button class!
  169. class GameButton extends JButton {
  170.     //Positions in the grid
  171.     private int xPos = -1;
  172.     private int yPos = -1;
  173.  
  174.  
  175.     //Constructor
  176.     public GameButton() {
  177.         super();
  178.     }
  179.  
  180.  
  181.     //Getters and Setters for the grid position
  182.     public void setXPos(int val) {
  183.         xPos = val;
  184.     }
  185.  
  186.     public void setYPos(int val) {
  187.         yPos = val;
  188.     }
  189.  
  190.     public int getXPos() {
  191.         return xPos;
  192.     }
  193.  
  194.     public int getYPos() {
  195.         return yPos;
  196.     }
  197. }
Add Comment
Please, Sign In to add comment