/* * 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.model; import mvc143.*; import java.util.Random; import java.io.*; import java.util.HashSet; import java.util.List; import java.util.ArrayList; import java.util.Collection; /** * @author xm * */ public class BoggleModel implements IBoggleModel{ public static final String author = "X. Miao"; public static final String description = "the model"; /* (non-Javadoc) * @see mvc143.IBoggleModel#createBoard(java.net.URL) */ private static final char[] alphabet = {'A','B','C','D','E','F','G','H','I','J', 'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; private char[][] board; private int cols; private int rows; private int score; private IBoggleWordsModel theDictModel; public BoggleModel(String boardFile,String dictFile,int rows,int cols){ this.cols = cols; this.rows = rows; score = 0; theDictModel = new BoggleWordsModel(dictFile); createBoard(boardFile); } public boolean createBoard(String fileID) { board = new char[rows][cols]; if (fileID==null){ createBoard(); } else{ try{ BufferedReader input = new BufferedReader(new FileReader(fileID)); String str; int row = 0; while((str=input.readLine())!=null && row=0 && row=0) return board[row][col]; else return -1; } /* (non-Javadoc) * @see mvc143.IBoggleModel#getcols() */ public int getCols() { return cols; } /* (non-Javadoc) * @see mvc143.IBoggleModel#getrows() */ public int getRows() { return rows; } /* (non-Javadoc) * @see mvc143.IBoggleModel#addScore(int) */ public int addScore(int score) { return (this.score += score); } /* (non-Javadoc) * @see mvc143.IBoggleModel#getScore() */ public int getScore(){ return score; } public void resetScore(){ score = 0; } public IBoggleWordsModel getWordsModel(){ return theDictModel; } public void terminate(){ } } class BoggleWordsModel implements IBoggleWordsModel { private List proposalHistory = new ArrayList();; private HashSet dict; private int lastrow, lastcol; private boolean newproposal = true; private List history; public BoggleWordsModel(String fileID){ loadDictionary(fileID); history = new ArrayList(); } /* (non-Javadoc) * @see mvc143.IBoggleDictionaryModel#loadDictionary(java.net.URL) */ public boolean loadDictionary(String fileID) { String str; dict = new HashSet(); if (fileID==null){ return false; } try { BufferedReader input = new BufferedReader(new FileReader(fileID)); while ((str = input.readLine())!= null) { dict.add(str.toUpperCase()); } // Close input stream input.close(); return true; } catch (FileNotFoundException fe) { System.out.println("Error reading file; file not found"); return false; } catch (IOException e) { System.out.println("An IOException occured"); e.printStackTrace(); return false; } } /* (non-Javadoc) * @see mvc143.IBoggleDictionaryModel#addToDictionary(java.lang.String) */ public void addToDictionary(String[] words){ for (int i=0;i= this.row) return 1; else if (p.col >= this.col) return 1; else return -1; } } }