/* * Created on 2004-7-27 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package boggle.controller; import boggle.model.*; import boggle.view.*; import mvc143.*; import java.io.*; import javax.swing.JFrame; import javax.swing.JFileChooser; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JOptionPane; import javax.swing.Timer; import java.net.URL; import java.util.List; /** * @author xm * */ public class BoggleController implements IBoggleController { public static final String author = "X. Miao"; public static final String description = "the controller"; private static final int timer_interval = 500; /* (non-Javadoc) * @see mvc143.IBoggleController#terminateApplication() */ private Timer timer; private IBoggleModel theModel; private IBoggleView theView; private ScoreChecker theScoreChecker; private StringBuffer proposal; private int col,row; private String status="SimpleBoggleGame ver 1.0 \n Authors:\n Martin Dickey\n Xu Miao\n"; private long gameTime = 60000; private long timeRemaining; private boolean isRunning = false; public BoggleController(){ JFrame.setDefaultLookAndFeelDecorated(true); String boardFile=""; String dictFile=""; ClassLoader cl=BoggleController.class.getClassLoader(); JFileChooser chooser = new JFileChooser(); chooser.setDialogTitle("load a board"); int returnVal = chooser.showOpenDialog(null); if(returnVal == JFileChooser.APPROVE_OPTION) { boardFile = chooser.getSelectedFile().getAbsolutePath(); } chooser.setDialogTitle("load a dictionary"); returnVal = chooser.showOpenDialog(null); if(returnVal == JFileChooser.APPROVE_OPTION) { dictFile = chooser.getSelectedFile().getAbsolutePath(); } theModel = new BoggleModel(boardFile,dictFile,4,4); URL url = cl.getResource("boggle/controller/score.txt"); theScoreChecker = new ScoreChecker(url); theView = new BoggleView(this); } public synchronized void setGameTime(long gametime){ gameTime = gametime; } public boolean removeLetterFromProposal(){ return false; } public long getGameTime(){ return gameTime; } public boolean isRunning(){ return isRunning; } public boolean isNewProposal(){ if (proposal!=null && proposal.length()==0) return true; return false; } public synchronized void startTimer() { if (timer != null) { timer.stop(); } else { timer = new Timer(timer_interval, timerListener); } this.timeRemaining = gameTime; timer.start(); } public long getMillisRemaining() { return this.timeRemaining; } public synchronized boolean addToProposal(int row, int col) { int theChar = theModel.getLetter(row,col); if (theChar!=-1){ if (theModel.getWordsModel().isValidPosition(row,col)){ proposal.append((char)theChar); status = proposal.toString(); theView.redraw(); return true; } } theView.redraw(); status = proposal.toString()+"\n"+" Not an adjacent letter!\n"; return false; } public int submitProposal(){ if (proposal!=null && proposal.length()>0){ String word = this.proposal.toString(); IBoggleWordsModel wModel = theModel.getWordsModel(); int i = 0; switch(i){ case 0: status = word+"\n a correct word!\n"; theModel.addScore(theScoreChecker.scoring(word)); break; case 1: status = word+"\n not in the dictionary!\n"; break; case 2: status = word+"\n has been submitted before!\n"; break; default: } } return 0; } public synchronized void newProposal() { proposal = new StringBuffer(); } /* * @see boggle.IBoggleController#getScore() */ public int getScore() { return theModel.getScore(); } public String getStatus(){ return status; } /* * @see boggle.IBoggleController#endGame() */ public void endGame() { } public synchronized void addWordToDictionary(String proposal){ String[] strs = new String[1]; strs[0] = proposal; theModel.getWordsModel().addToDictionary(strs); } /* * @see boggle.IBoggleController#startGame() */ public synchronized void startGame() { isRunning = true; newProposal(); theModel.createBoard(null); theModel.resetScore(); startTimer(); theModel.getWordsModel().emptyHistory(); status = "New game begins!\n Please select the word!\n"; } public synchronized void startGame(String fileID){ isRunning = true; newProposal(); if (fileID!=null) theModel.createBoard(fileID); theModel.resetScore(); startTimer(); theModel.getWordsModel().emptyHistory(); status = "New game begins!\n Please select the word!\n"; } ActionListener timerListener = new ActionListener() { public synchronized void actionPerformed(ActionEvent e) { //timer has fired timeRemaining -= timer_interval; if (timeRemaining <= 0){ isRunning = false; timer.stop(); submitProposal(); newProposal(); status = "Time is out!\nThe game is over."; } theView.redraw(); } }; /** * get a history of all accepted words * @return a List containing all the accepted words */ public List getWordsHistory() { return theModel.getWordsModel().getHistory(); } public void addToDictionary(String[] strs){ theModel.getWordsModel().addToDictionary(strs); } public boolean loadDictionary(String fileID){ return theModel.getWordsModel().loadDictionary(fileID); } public int getLetterInBoard(int row, int col){ return theModel.getLetter(row,col); } public int getBoardCols(){ return theModel.getCols(); } public int getBoardRows(){ return theModel.getRows(); } public void terminateApplication() { JOptionPane.showMessageDialog(null, "Boggle sessions ends"); theModel.terminate(); theView.terminateView(); System.exit(0); } public static void main(String[] args) { new BoggleController(); } } class ScoreChecker { /* (non-Javadoc) * @see mvc143.IScoreChecker#loadScoringTable(java.net.URL) */ private int[] scoretable; private int maximumLength; public ScoreChecker(URL url) { scoretable = loadScoringTable(url); maximumLength = scoretable.length; } public int[] loadScoringTable(URL url) { int[] scoretable=null; String str; int length; if (url != null) { try { //Open the resource URL InputStream istream = url.openStream(); BufferedReader input; input = new BufferedReader(new InputStreamReader(istream)); str = input.readLine(); String[] strs = str.split(" "); scoretable = new int[strs.length]; for (int i=0;i