/** * Is called from the model when data is changed, updates members and view of this class. Checks * if the game is over. * * @param e ChangeEvent object */ public void stateChanged(ChangeEvent e) { // update the view: accessors and repaint data = model.getData(); repaint(); if (model.isFinish() && !gameOver) { gameOver = true; JFrame frame = new JFrame("Declare Winner"); if (model.declareWinner() == 1) JOptionPane.showMessageDialog(frame, "A is the winner!!!"); else if (model.declareWinner() == 2) JOptionPane.showMessageDialog(frame, "B is the winner!!!"); else JOptionPane.showMessageDialog(frame, "It is a tie!!!"); } }
/** * Constructor that takes model as a parameter * * @param model */ public BoardFrame(Model model) { this.model = model; data = model.getData(); gameOver = false; initScreen(); setLayout(new BorderLayout()); setLocation(300, 200); // undoBtn is a controller that resets the turn and last state of board undoBtn = new JButton("Undo : " + this.model.getUndoCounter()); undoBtn.setPreferredSize(new Dimension(80, 50)); undoBtn.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { if (BoardFrame.this.model.getUndoCounter() != 0) { BoardFrame.this.model.undo(); undoBtn.setText("Undo : " + BoardFrame.this.model.getUndoCounter()); } else ; } }); JPanel undoPanel = new JPanel(); undoPanel.add(undoBtn); // end of undoBtn code add(boardPanel, BorderLayout.CENTER); add(undoPanel, BorderLayout.SOUTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setResizable(false); setVisible(true); }
/** Initial Option Panes that lets user select style and number of stones */ public void initScreen() { Object[] options = {"Style A", "Style B"}; int input = JOptionPane.showOptionDialog( null, "Choose a Board Style:", "Board Styles", JOptionPane.DEFAULT_OPTION, JOptionPane.DEFAULT_OPTION, null, options, options[0]); if (input == 0) { boardPanel = boardContextDoWork(new StrategyGreen()); } else if (input == 1) { boardPanel = boardContextDoWork(new StrategyBrown()); // change later } else { System.exit(0); } Object[] optionStones = {"3", "4"}; int inputStones = JOptionPane.showOptionDialog( null, "Choose Number of Stones:", "Initial Stones", JOptionPane.DEFAULT_OPTION, JOptionPane.DEFAULT_OPTION, null, optionStones, optionStones[0]); if (inputStones == 0) { model.refreshBoard(3); } else if (inputStones == 1) { model.refreshBoard(4); } else { System.exit(0); } }