Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Asssignment2;
- import javax.swing.*;
- import javax.swing.undo.UndoManager;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.HashMap;
- import java.util.Map;
- public class Game extends JFrame {
- JButton buttons[] = new JButton[9];
- //int[] magicSquare = new int[]{4, 9, 2, 3, 5, 7, 8, 1, 6};
- int[] magicSquare = new int[]{2, 7, 6, 9, 5, 1, 4, 3, 8};
- int[][] winConditionLines = {
- {0, 1, 2},
- {3, 4, 5},
- {6, 7, 8}
- };
- HashMap<Object, Object> hashMap = new HashMap<>();
- int turn = 0;
- public void initialiseButtons()
- {
- for(int i = 0; i <= 8; i++)
- {
- buttons[i] = new JButton();
- buttons[i].setText("");
- buttons[i].setBackground(Color.BLACK);
- add(buttons[i]); //adds this button to JPanel (note: no need for JPanel.add(...)
- //because this whole class is a JPanel already
- }
- }
- public void action() {
- for(int i = 0; i <= 8; i++) {
- buttons[i].addMouseListener(new MouseClickListener(this));
- }
- }
- public void resetButtons()
- {
- for(int i = 0; i <= 8; i++)
- {
- buttons[i].setEnabled(true);
- buttons[i].setText("");
- buttons[i].addKeyListener(new KeyboardListener(this));
- }
- }
- /*boolean hasWon(String player) {
- for (int i = 0; i < 9; i++)
- for (int j = 0; j < 9; j++)
- for (int k = 0; k < 9; k++)
- if (i != j && i != k && j != k)
- if (buttons[i].getText() == player && buttons[j].getText() == player && buttons[k].getText() == player)
- if (magicSquare[i] + magicSquare[j] + magicSquare[k] == 15)
- return true;
- return false;
- }*/
- /*public boolean winCondition(String player, String player2) {
- hashMap.put(buttons, magicSquare);
- for (int i = 0; i < 9; i++) {
- for (int j = 0; j < 9; j++) {
- for (int k = 0; k < 9; k++) {
- if (buttons[i].getText() == player && buttons[j].getText() == player && buttons[k].getText() == player) {
- if (magicSquare[i] + magicSquare[j] + magicSquare[k] == 15) {
- setTitle(player + " has won!");
- return true;
- }
- } else {
- setTitle(player2 + " has won!");
- return true;
- }
- }
- }
- }
- return false;
- }*/
- /*public void winCondition() {
- hashMap.put(buttons, magicSquare);
- for (int i = 0; i < magicSquare.length; i++)
- {
- for (int j = i+1; j < magicSquare.length; j++)
- {
- for (int k = j+1; k < magicSquare.length; k++) {
- if (buttons[i].getText().equals(buttons[j].getText()) && buttons[j].getText().equals(buttons[k].getText()) && !buttons[i].getText().equals("")) {
- if (magicSquare[i] + magicSquare[j] + magicSquare[k] == 15 ) {
- JOptionPane.showMessageDialog(null, "Player has won!");
- }
- }
- }
- }
- }
- }*/
- /*public void winCondition() {
- for (int[] line : winConditionLines) {
- String first = buttons[line[0]].getText();
- String second = buttons[line[1]].getText();
- String third = buttons[line[2]].getText();
- if (first.equals(second) && second.equals(third)) {
- JOptionPane.showMessageDialog(null, "Player " + buttons[line[0]].getText() + " has won!");
- }
- }
- }*/
- public void winCondition() {
- 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}};
- for (int[] line : winningLines) {
- if (
- buttons[line[0]].getText().equals(buttons[line[1]].getText()) &&
- buttons[line[1]].getText().equals(buttons[line[2]].getText()) &&
- !buttons[line[0]].getText().equals("")
- ) {
- JOptionPane.showMessageDialog(null, "Player has won!");
- }
- }
- }
- public void windCondition() {
- boolean win = false;
- // Who Won
- // Horizontal
- if (buttons[0].getText().equals(buttons[1].getText()) && buttons[1].getText().equals(buttons[2].getText()) && !buttons[0].getText().equals("")) {
- win = true;
- } else if (buttons[3].equals(buttons[4]) && buttons[4].equals(buttons[5]) && !buttons[3].equals("")) {
- win = true;
- } else if (buttons[6].equals(buttons[7]) && buttons[7].equals(buttons[8]) && !buttons[6].equals("")) {
- win = true;
- }
- // Vertical
- if (buttons[0].equals(buttons[3]) && buttons[3].equals(buttons[6]) && !buttons[0].equals("")) {
- win = true;
- } else if (buttons[1].equals(buttons[4]) && buttons[4].equals(buttons[7]) && !buttons[1].equals("")) {
- win = true;
- } else if (buttons[2].equals(buttons[5]) && buttons[5].equals(buttons[8]) && !buttons[2].equals("")) {
- win = true;
- }
- // Diagonal
- if (buttons[0].equals(buttons[4]) && buttons[4].equals(buttons[8]) && !buttons[0].equals("")) {
- win = true;
- } else if (buttons[2].equals(buttons[4]) && buttons[4].equals(buttons[6]) && !buttons[2].equals("")) {
- win = true;
- }
- if (win) {
- JOptionPane.showMessageDialog(null, "Player " + buttons + " wins!");
- for (JButton i : buttons) {
- i.setEnabled(false);
- }
- } /*else if (!win) {
- JOptionPane.showMessageDialog(null, "The game ended in a tie.");
- }*/
- }
- public void endGame() {
- for (int i = 0; i <=8; i++) {
- if (!buttons[i].isEnabled()) {
- JFrame gameOver = new JFrame("Post-game");
- JButton playAgain = new JButton("Play again");
- gameOver.setSize(300, 300);
- gameOver.add(playAgain);
- gameOver.setVisible(true);
- playAgain.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent actionEvent) {
- gameOver.dispose();
- new Game();
- }
- });
- }
- }
- }
- public Game() {
- // set up game environment
- initialiseButtons();
- resetButtons();
- action();
- //winCondition("X", "O");
- winCondition();
- endGame();
- // key listener to respond to key events
- //addKeyListener(new KeyboardListener(this));
- //int num = 10;
- //setTitle(String.valueOf(num));
- // standard configuration
- setSize(new Dimension(400, 400));
- setLayout(new GridLayout(3,3));
- setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- setVisible(true);
- }
- public static void main(String[] args) {
- new Game();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement