package cse1030.games.boggle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * The controller for the Boggle app. * * @author CSE1030_F13_14 * */ public class BoggleController implements ActionListener { private Boggle model; private BoggleView view; /** * Creates a controller with no model and no view. */ public BoggleController() { this.model = null; this.view = null; } /** * Sets the controller to use the given model. * * @pre. model must not be null * * @param model the model that the controller should use */ public void setModel(Boggle model) { this.model = model; } /** * Sets the controller to use the given view. * * @pre. view must not be null * * @param view the view that the controller should use */ public void setView(BoggleView view) { this.view = view; } /** * Responds to events from the view. This method responds to an * event where the action command is either * BoggleView.ROLL_COMMAND or * BoggleView.SUBMIT_COMMAND. * * @param event an event emitted by the view * * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ @Override public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); if (command.equals(BoggleView.ROLL_COMMAND)) { this.model.shuffleAndRoll(); this.view.setDice(this.model.getDice()); } else if (command.equals(BoggleView.SUBMIT_COMMAND)) { String word = this.view.getWord(); boolean isOk = this.model.lookUp(word); this.view.setWordIsValid(isOk); } } }