Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.57 KB | None | 0 0
  1. package zadachki.neshto;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7. public class connectFour {
  8.     JFrame frame;
  9.     JPanel panel;
  10.     final int rowTiles = 6;
  11.     final int colTiles = 7;
  12.     static int[][] grid = new int[6][7];
  13.     int row, col, rowSelected, colSelected = 0;
  14.     int pTurn = 0;
  15.     boolean win = false;
  16.     JButton[][] button = new JButton[rowTiles][colTiles];
  17.     JButton clear;
  18.     JLabel whoWon;
  19.     GridLayout myGrid = new GridLayout(7,7);
  20.     //recommended image size is 100 by 100 pixels
  21.     //final ImageIcon c0 = new ImageIcon("p0.png");
  22.     //final ImageIcon c1 = new ImageIcon("p1.png");
  23.     //final ImageIcon c2 = new ImageIcon("p2.png");
  24.  
  25.     public connectFour() {
  26.         frame = new JFrame("Connect Four");
  27.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28.         frame.setBounds(100, 100, 700, 600);
  29.         panel = new JPanel();
  30.         frame.getContentPane().setLayout(null);
  31.         panel.setLayout(myGrid);
  32.         whoWon = new JLabel("");
  33.         clear = new JButton("Clear");
  34.         clear.addActionListener(new clearListener());
  35.         clear.setPreferredSize(new Dimension(100,100));
  36.  
  37.         //-1 are empty slots not allowed to go in (nothing is under them)
  38.         //0 are empty slots allowed to fill, either bottom most or already a piece under them
  39.         //1 for player1, 2 for player2
  40.         for (int x = rowTiles - 2; x >= 0; x--) {
  41.             for (int y = colTiles - 1; y >= 0; y--) {
  42.                 grid[x][y] = -1;
  43.             }
  44.         }
  45.  
  46.         for (row = 0; row <= rowTiles - 1; row++) {
  47.             for (col = 0; col <= colTiles - 1; col++) {
  48.                 button[row][col] = new CircleButton("");
  49.                 button[row][col].addActionListener(new buttonListener());
  50.                 panel.add(button[row][col]);
  51.             }
  52.         }
  53.  
  54.         panel.add(whoWon);
  55.         panel.add(clear);
  56.         frame.setContentPane(panel);
  57.         frame.pack();
  58.         frame.setVisible(true);
  59.     }
  60.  
  61.     class buttonListener implements ActionListener {
  62.         public void actionPerformed(ActionEvent event) {
  63.             for (row = rowTiles-1; row >= 0; row--) {
  64.                 for (col = colTiles-1; col >= 0; col--) {
  65.                     if (button[row][col] == event.getSource()) {
  66.                         if (pTurn % 2 == 0 && grid[row][col] == 0) {
  67.                             button[row][col].setBackground(Color.black);
  68.                             grid[row][col] = 1;
  69.                             try {
  70.                                 grid[row-1][col] = 0;
  71.                             }
  72.                             catch (ArrayIndexOutOfBoundsException e) {
  73.                                 System.out.println("Reached top of column");
  74.                             }
  75.                             if (checkWin()) {
  76.                                 System.out.println("player 1 win");
  77.                                 whoWon.setText("Player 1 wins");
  78.                                 for (int x = rowTiles - 1; x >=0; x--) {
  79.                                     for (int y = colTiles - 1; y >= 0; y--) {
  80.                                         grid[x][y] = -1;
  81.                                     }
  82.                                 }
  83.                             }
  84.                             pTurn = pTurn + 1;
  85.                             break;
  86.                         }
  87.                         if (pTurn % 2 == 1 && grid[row][col] == 0) {
  88.                             button[row][col].setBackground(Color.PINK);
  89.                             grid[row][col] = 2;
  90.                             try {
  91.                                 grid[row-1][col] = 0;
  92.                             }
  93.                             catch (ArrayIndexOut    OfBoundsException e) {
  94.                                 System.out.println("Reached top of column");
  95.                             }
  96.                             if (checkWin()) {
  97.                                 System.out.println("player 2 win");
  98.                                 whoWon.setText("Player 2 wins");
  99.                                 for (int x = rowTiles - 1; x >=0; x--) {
  100.                                     for (int y = colTiles - 1; y >= 0; y--) {
  101.                                         grid[x][y] = -1;
  102.                                     }
  103.                                 }
  104.                             }
  105.                             pTurn = pTurn + 1;
  106.                             break;
  107.                         }
  108.                         else {
  109.                             System.out.println(":)/>");//poker face
  110.                         }
  111.                     }
  112.                 }
  113.             }
  114.         }
  115.     }
  116.  
  117.     class clearListener implements ActionListener {
  118.         public void actionPerformed(ActionEvent event) {
  119.             for (int x = rowTiles - 1; x >= 0; x--) {
  120.                 for (int y = colTiles - 1; y >= 0; y--) {
  121.                     grid[x][y] = -1;
  122.                     button[x][y].setBackground(Color.WHITE);
  123.                 }
  124.             }
  125.             for (int y = colTiles - 1; y >= 0; y--) {
  126.                 grid[5][y] = 0;
  127.             }
  128.             whoWon.setText("");
  129.         }
  130.     }
  131.  
  132.     public boolean checkWin() {
  133.         // check for a horizontal win
  134.         for (int x=0; x<6; x++) {
  135.             for (int y=0; y<4; y++) {
  136.                 if (grid[x][y] != 0 && grid[x][y] != -1 &&
  137.                 grid[x][y] == grid[x][y+1] &&
  138.                 grid[x][y] == grid[x][y+2] &&
  139.                 grid[x][y] == grid[x][y+3]) {
  140.                     win = true;
  141.                 }
  142.             }
  143.         }
  144.         // check for a vertical win
  145.         for (int x=0; x<3; x++) {
  146.             for (int y=0; y<7; y++) {
  147.                 if (grid[x][y] != 0 && grid[x][y] != -1 &&
  148.                 grid[x][y] == grid[x+1][y] &&
  149.                 grid[x][y] == grid[x+2][y] &&
  150.                 grid[x][y] == grid[x+3][y]) {
  151.                     win = true;
  152.                 }
  153.             }
  154.         }
  155.         // check for a diagonal win (positive slope)
  156.         for (int x=0; x<3; x++) {
  157.             for (int y=0; y<4; y++) {
  158.                 if (grid[x][y] != 0 && grid[x][y] != -1 &&
  159.                 grid[x][y] == grid[x+1][y+1] &&
  160.                 grid[x][y] == grid[x+2][y+2] &&
  161.                 grid[x][y] == grid[x+3][y+3]) {
  162.                     win = true;
  163.                 }
  164.             }
  165.         }
  166.         // check for a diagonal win (negative slope)
  167.         for (int x=3; x<6; x++) {
  168.             for (int y=0; y<4; y++) {
  169.             if (grid[x][y] != 0 && grid[x][y] != -1 &&
  170.                 grid[x][y] == grid[x-1][y+1] &&
  171.                 grid[x][y] == grid[x-2][y+2] &&
  172.                 grid[x][y] == grid[x-3][y+3]) {
  173.                     win = true;
  174.                 }
  175.             }
  176.         }
  177.         return win;
  178.     }
  179.  
  180.     public static void main(String[] args) {
  181.         EventQueue.invokeLater(new Runnable() {
  182.             public void run() {
  183.                 try {
  184.                     //JFrame.setDefaultLookAndFeelDecorated(true);
  185.                 connectFour window = new connectFour();
  186.                 window.frame.setVisible(true);
  187.                 }catch (Exception e) {
  188.                     e.printStackTrace();
  189.                 }
  190.             }
  191.         });
  192.     }
  193. }
  194.  
  195. /* Hey Mr. Qayum, you know that I'm not good at software stuff, but if that stuff last week didn't happen, I don't think I would have had the motivation to pull this off.
  196.  *It doesn't look pretty, but works nicely. Thanks for everything this year, see you in september =)
  197.  *
  198.  *David Tan
  199.  *
  200.  *Brian Wong worked unbelievably hard one-man army'ing Part A, props to him.
  201.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement