Advertisement
Guest User

CE203 Assignment 2

a guest
Dec 8th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.42 KB | None | 0 0
  1. package Asssignment2;
  2.  
  3. import javax.swing.*;
  4. import javax.swing.undo.UndoManager;
  5. import java.awt.*;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10.  
  11. public class Game extends JFrame {
  12.  
  13.     JButton buttons[] = new JButton[9];
  14.     //int[] magicSquare = new int[]{4, 9, 2, 3, 5, 7, 8, 1, 6};
  15.     int[] magicSquare = new int[]{2, 7, 6, 9, 5, 1, 4, 3, 8};
  16.     int[][] winConditionLines = {
  17.             {0, 1, 2},
  18.             {3, 4, 5},
  19.             {6, 7, 8}
  20.     };
  21.     HashMap<Object, Object> hashMap = new HashMap<>();
  22.  
  23.  
  24.     int turn = 0;
  25.  
  26.  
  27.     public void initialiseButtons()
  28.     {
  29.         for(int i = 0; i <= 8; i++)
  30.         {
  31.             buttons[i] = new JButton();
  32.             buttons[i].setText("");
  33.             buttons[i].setBackground(Color.BLACK);
  34.             add(buttons[i]); //adds this button to JPanel (note: no need for JPanel.add(...)
  35.             //because this whole class is a JPanel already
  36.         }
  37.     }
  38.  
  39.     public void action() {
  40.         for(int i = 0; i <= 8; i++) {
  41.             buttons[i].addMouseListener(new MouseClickListener(this));
  42.         }
  43.     }
  44.  
  45.  
  46.     public void resetButtons()
  47.     {
  48.         for(int i = 0; i <= 8; i++)
  49.         {
  50.             buttons[i].setEnabled(true);
  51.             buttons[i].setText("");
  52.             buttons[i].addKeyListener(new KeyboardListener(this));
  53.         }
  54.     }
  55.  
  56.     /*boolean hasWon(String player) {
  57.         for (int i = 0; i < 9; i++)
  58.             for (int j = 0; j < 9; j++)
  59.                 for (int k = 0; k < 9; k++)
  60.                     if (i != j && i != k && j != k)
  61.                         if (buttons[i].getText() == player && buttons[j].getText() == player && buttons[k].getText() == player)
  62.                             if (magicSquare[i] + magicSquare[j] + magicSquare[k] == 15)
  63.                                 return true;
  64.         return false;
  65.     }*/
  66.  
  67.     /*public boolean winCondition(String player, String player2) {
  68.         hashMap.put(buttons, magicSquare);
  69.         for (int i = 0; i < 9; i++) {
  70.             for (int j = 0; j < 9; j++) {
  71.                 for (int k = 0; k < 9; k++) {
  72.                     if (buttons[i].getText() == player && buttons[j].getText() == player && buttons[k].getText() == player) {
  73.                         if (magicSquare[i] + magicSquare[j] + magicSquare[k] == 15) {
  74.                             setTitle(player + " has won!");
  75.                             return true;
  76.                         }
  77.                     } else {
  78.                         setTitle(player2 + " has won!");
  79.                         return true;
  80.                     }
  81.                 }
  82.             }
  83.         }
  84.         return false;
  85.     }*/
  86.  
  87.     /*public void winCondition() {
  88.         hashMap.put(buttons, magicSquare);
  89.         for (int i =  0; i < magicSquare.length; i++)
  90.         {
  91.             for (int j  = i+1; j < magicSquare.length; j++)
  92.             {
  93.                 for (int k = j+1; k < magicSquare.length; k++) {
  94.                     if (buttons[i].getText().equals(buttons[j].getText()) && buttons[j].getText().equals(buttons[k].getText()) && !buttons[i].getText().equals("")) {
  95.                         if (magicSquare[i] + magicSquare[j] + magicSquare[k] == 15 ) {
  96.                             JOptionPane.showMessageDialog(null, "Player has won!");
  97.                         }
  98.                     }
  99.                 }
  100.             }
  101.         }
  102.     }*/
  103.  
  104.     /*public void winCondition() {
  105.         for (int[] line : winConditionLines) {
  106.             String first = buttons[line[0]].getText();
  107.             String second = buttons[line[1]].getText();
  108.             String third = buttons[line[2]].getText();
  109.             if (first.equals(second) && second.equals(third)) {
  110.                 JOptionPane.showMessageDialog(null, "Player " + buttons[line[0]].getText() + " has won!");
  111.             }
  112.         }
  113.     }*/
  114.  
  115.     public void winCondition() {
  116.         int[][] winningLines = {{0,1,2}, {3,4,5}, {6,7,8}, {0,3,6}, {1,4,7}, {2,5,8}, {0,4,8}, {2,4,6}};
  117.         for (int[] line : winningLines) {
  118.             if (
  119.                     buttons[line[0]].getText().equals(buttons[line[1]].getText()) &&
  120.                             buttons[line[1]].getText().equals(buttons[line[2]].getText()) &&
  121.                             !buttons[line[0]].getText().equals("")
  122.             ) {
  123.                 JOptionPane.showMessageDialog(null, "Player has won!");
  124.             }
  125.         }
  126.     }
  127.  
  128.  
  129.     public void windCondition() {
  130.  
  131.         boolean win = false;
  132.  
  133.         // Who Won
  134.  
  135.         // Horizontal
  136.         if (buttons[0].getText().equals(buttons[1].getText()) && buttons[1].getText().equals(buttons[2].getText()) && !buttons[0].getText().equals("")) {
  137.             win = true;
  138.         } else if (buttons[3].equals(buttons[4]) && buttons[4].equals(buttons[5]) && !buttons[3].equals("")) {
  139.             win = true;
  140.         } else if (buttons[6].equals(buttons[7]) && buttons[7].equals(buttons[8]) && !buttons[6].equals("")) {
  141.             win = true;
  142.         }
  143.  
  144.         // Vertical
  145.         if (buttons[0].equals(buttons[3]) && buttons[3].equals(buttons[6]) && !buttons[0].equals("")) {
  146.             win = true;
  147.         } else if (buttons[1].equals(buttons[4]) && buttons[4].equals(buttons[7]) && !buttons[1].equals("")) {
  148.             win = true;
  149.         } else if (buttons[2].equals(buttons[5]) && buttons[5].equals(buttons[8]) && !buttons[2].equals("")) {
  150.             win = true;
  151.         }
  152.  
  153.         // Diagonal
  154.         if (buttons[0].equals(buttons[4]) && buttons[4].equals(buttons[8]) && !buttons[0].equals("")) {
  155.             win = true;
  156.         } else if (buttons[2].equals(buttons[4]) && buttons[4].equals(buttons[6]) && !buttons[2].equals("")) {
  157.             win = true;
  158.         }
  159.  
  160.         if (win) {
  161.             JOptionPane.showMessageDialog(null, "Player " + buttons + " wins!");
  162.             for (JButton i : buttons) {
  163.                 i.setEnabled(false);
  164.             }
  165.         } /*else if (!win) {
  166.             JOptionPane.showMessageDialog(null, "The game ended in a tie.");
  167.         }*/
  168.     }
  169.  
  170.     public void endGame() {
  171.         for (int i = 0; i <=8; i++) {
  172.             if (!buttons[i].isEnabled()) {
  173.                 JFrame gameOver = new JFrame("Post-game");
  174.                 JButton playAgain = new JButton("Play again");
  175.                 gameOver.setSize(300, 300);
  176.                 gameOver.add(playAgain);
  177.                 gameOver.setVisible(true);
  178.                 playAgain.addActionListener(new ActionListener() {
  179.                     @Override
  180.                     public void actionPerformed(ActionEvent actionEvent) {
  181.                         gameOver.dispose();
  182.                         new Game();
  183.                     }
  184.                 });
  185.             }
  186.         }
  187.     }
  188.  
  189.     public Game() {
  190.  
  191.  
  192.         // set up game environment
  193.         initialiseButtons();
  194.         resetButtons();
  195.         action();
  196.         //winCondition("X", "O");
  197.         winCondition();
  198.         endGame();
  199.  
  200.  
  201.         // key listener to respond to key events
  202.         //addKeyListener(new KeyboardListener(this));
  203.  
  204.         //int num = 10;
  205.         //setTitle(String.valueOf(num));
  206.  
  207.         // standard configuration
  208.         setSize(new Dimension(400, 400));
  209.         setLayout(new GridLayout(3,3));
  210.         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  211.         setVisible(true);
  212.  
  213.     }
  214.  
  215.  
  216.     public static void main(String[] args) {
  217.         new Game();
  218.     }
  219.  
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement