/* * Filename: GameInterface.java * This was a modification to program 6 * Updated on March 23, 2006 * ULID: sawhite2 Course: * * ITK 168 Section: 4 Instructor: White */ import javax.swing.*; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * Graphical User Interface for the Super Master Mind game. * * This class has a private inner-class called GuessPanel used * to hold the buttons for the guessed pattern. * * All buttons are fully functional. * * @author Shirley White */ public class GameView implements ActionListener { /** width of the frame initialized to 600 */ public static final int WIDTH = 600; /** height of the frame initialized to 600 */ public static final int HEIGHT = 600; // panels // main panel private JPanel panel = new JPanel(); // subpanel to hold color choices private JPanel colorPanel = new JPanel(); // declare a subpanel array to hold 12 guess panels ********************************** private JFrame frame = new JFrame(); // declare a button array for 8 color choices *************************************** private JButton checkButton = new JButton("Check Guess"); private JLabel winnerLabel = new JLabel(); private JButton newGameButton = new JButton("Start New Game"); // dclare a button and label for showing required statistics *********************** private Game game; /** * Default constructor sets up the frame. */ public GameView(Game game) { this.game = game; // set main panel layout to add each component in a new row panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); // fill main panel // setup and add color choice buttons panel this.setUpColorChoices(); panel.add(colorPanel); // setup and add 12 guess panels from the array *************************************** // setup and add button to check the guess checkButton.setEnabled(false); checkButton.addActionListener(this); panel.add(checkButton); panel.add(this.winnerLabel); // setup and add new game button this.newGameButton.addActionListener(this); panel.add(this.newGameButton); // setup and add button and label to show game stats ****************************** // display frame frame.setBounds(10, 10, WIDTH, HEIGHT); frame.setContentPane(panel); frame.setTitle("Super Master Mind"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } /* * Private method to build the 8 color choice buttons. * Called in the constructor. */ private void setUpColorChoices() { // set colors panel layout to fill all components in one row colorPanel.setLayout(new BoxLayout(colorPanel, BoxLayout.X_AXIS)); //build buttons for color choices ************************************************* // set the background colors for each element in the colorButtons array************* // add action listener to each button *********************************************** // add color bottons to colors panel ************************************************ } /** * Method to enable the color choice buttons. */ public void enableColors() { // code this for all 8 colors ******************************************************** } /** * Method to disable the color choice buttons. */ public void disableColors() { // code this for all 8 colors ******************************************************* } /** * Method to update the view every time the Game class * object changes. * * i.e. When a new guess is made, perhaps winning the game. */ public void update() { // complete this method carefully**************************************************** } /** * This method acts as the controller in our MVC architecture. * * This method processes all clicks of the color buttons, the * button to check the guess, or the button to start a new * game - updating the Game object as necessary. */ public void actionPerformed(ActionEvent e) { // index variable to use in guessPanels array int index = this.game.getNumGuesses(); JButton clicked = (JButton) e.getSource(); // complete this method carefully*************************************************** } /** * This method is used to clear the feedback messages and to * clear any guesses when a new game is started. */ public void clearView() { // complete this method carefully*************************************************** } /* * GuessPanel is a private inner class used only in the GameView * class. * * This class is used to create a subpanel to hold the guess number, * 5 colors for the guess, and feedback labels for correct color and * correct places in a single row. * * The buttons are fully functional. */ private class GuessPanel extends JPanel implements ActionListener { // notice these instance varialbes have been changed in this version *************** // you will not need to add any others ********************************************* public Object setCorrectColorLabel; // buttons for current guess private JButton[] guessButtons = new JButton[5]; private JLabel correctColorLabel = new JLabel(" "); private JLabel correctPlaceLabel = new JLabel(" "); // holds the color choice that was last clicked by the user private Color choice; /** * Default constructor sets up the guess panel. * * @param num The guess number - ranging from 1 to 10 inclusive */ public GuessPanel(int num) { // build 5 buttons for the guessButtons array ************************************** this.setInitialBGColor(); // setup guess panel - with buttons initially disabled this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); // adjust spacing for 1 or 2 digit guess number if(num<10) { this.add(new JLabel("Guess " + num + " ")); } else { this.add(new JLabel("Guess " + num + " ")); } for(int i=0; i<5;i++) { this.add(this.guessButtons[i]); } this.add(new JLabel(" Correct Color: ")); this.add(this.correctColorLabel); this.add(new JLabel(" Correct Places: ")); this.add(this.correctPlaceLabel); // disable buttons this.disableButtons(); // add action listener for(int i=0; i<5; i++) { this.guessButtons[i].addActionListener(this); } } // the following methods are complete - you may add any additional methods you need**** /** * @param string */ public void setCorrectPlaceLabel(String string) { this.correctPlaceLabel.setText(string); } /** * @param string */ public void setCorrectColorLabel(String string) { this.correctColorLabel.setText(string); } /** * Method to set the color of all buttons to a light gray. * This is the start color - indicating the user has not fully setup * the guess yet. */ public void setInitialBGColor() { for(int i=0; i<5;i++) { this.guessButtons[i].setBackground(Color.LIGHT_GRAY); } } /** * This method is used to guild a Guess ojbect from the colors the user * has set the 3 buttons to. * @return Returns a Guess object with the 3 column characters set to * match the color choices entered by the user. */ public Guess buildGuess() { Color[] colors = new Color[5]; for(int i=0; i<5; i++) { colors[i] = this.guessButtons[i].getBackground(); } Guess guess = new Guess(colors); return guess; } /** * Method to enable all buttons. Recall the buttons are initially * disabled so only one choice can be filled at a time. */ public void enableButtons() { for(int i=0; i<5; i++) { this.guessButtons[i].setEnabled(true); } } /** * Method to disable all buttons. Recall the buttons are initially * disabled so only one choice can be filled at a time. */ public void disableButtons() { for(int i=0; i<5; i++) { this.guessButtons[i].setEnabled(false); } } /** * This method responds to all button clicks in this class. * * When a color is set, the other buttons are disabled to make * sure no 2 buttons ahve the same color in the pattern. * * When all 3 buttons have colors set, the checkButton is enabled. * However the user can still change colors before submitting the * guess. */ public void actionPerformed(ActionEvent e) { JButton clicked = (JButton) e.getSource(); clicked.setBackground(choice); if(this.allGuessed()) { checkButton.setEnabled(true); } } /** * @return */ private boolean allGuessed() { for(int i=0; i<5; i++) { if(this.guessButtons[i].getBackground().equals(Color.LIGHT_GRAY)) return false; } return true; } /** * * @param choice The last color chosen by the user. */ public void setChoice(Color choice) { this.choice = choice; } /** * * @return Returns the last color chosen by the user. */ public Color getChoice() { return this.choice; } }// end inner class GuessPanel }// end outer class GameView