com.eriksilkensen.sudoku
Class SudokuModel

java.lang.Object
  extended by com.eriksilkensen.sudoku.SudokuModel
All Implemented Interfaces:
Observable

public class SudokuModel
extends java.lang.Object
implements Observable

SudokuModel is the Model component in the M-V-C framework and so behaves like one would expect of a Model.

It has no knowledge of the View of Control, and so only has to provide for its implementation of the Sudoku 'Model.' This includes the logic of the gameplay - which is mostly just reacting to changes to the Board that a user might make.

SudokuModel is Observable, so Observers can register themselves with it and be notified via notifyObservers() -> update() when necessary.

Since:
1.0
Version:
04/26/07
Author:
Erik Silkensen

Field Summary
static int IDLE
           
static int LOST
           
static int PLAYING
           
static int WON
           
 
Constructor Summary
SudokuModel()
          The only constructor needed for the Model, which doesn't require any parameters.
 
Method Summary
 void addHints()
          Since a Hint is simply a random Cell from the Board that hasn't already been solved for, addHints() just adds all "Hints" to the list of Hints
 boolean boardHasHints()
          Not terribly useful, but still will check for any remaining Hints
 void checkBoard()
          Checks the current board for any mistakes, populates an int[] with the coordinates of all the incorrectly placed Cells, and then notifies all Observers
 void checkForBestTimes()
          checkForBestTimes() checks for the possibility that the user is eligible for a Best Time.
 void clear()
          Clears all of the user's placed Cells on the board, resets the Hints, and then notifies all Observers
 void clearBestTimeAllowed()
          Sets the bestTimeAllowed boolean to False
 void clearChanged()
          Sets the changed boolean to False
 void clearStatsAllowed()
          Sets the statsAllowed boolean to False
 void clearTimes()
          Simply clears all Best Times
 void gameWon()
          gameWon() is called when, as one might predict, a game has been won.
 java.util.ArrayList<BestTime> getBestTimes()
          Returns the current list of Best Times
 Board getBoard()
          Returns the current board
 int getBoardID()
          Returns the ID number of the current Board.
 boolean getCellType(int row, int col)
          Returns the type of the Cell at the position [row][col]
 int getCellValue(int row, int col)
          Returns the value of the Cell at the position [row][col]
 BoardDifficulties getDifficulty()
          Returns the current difficulty level
 int getIDLimit()
          Returns the highest possible legal ID
 java.lang.String getRange()
          Returns a String representation of the current range of legal Board ID numbers
 Board getSolution()
          Returns the current board's solution
 int getStatus()
          Returns the current status
 Hint giveHint()
          Hands over a Hint to whoever asks for one.
 void giveUp()
          Self-explanatory.
 void initialize()
           
 boolean isLegal(int row, int col)
          Returns whether or not it is legal for a user to try to place a Cell at the given location [row][col] on the Board
 boolean isLegalID(int ID)
          Helper method to determine the legality of a candidate ID number
 void newDownloadedGame()
          Starts a new game by downloading a random Board from the internet.
 void newGame(Board board)
          Starts a new game using the supplied Board.
 void newGame(BoardDifficulties difficulty)
          The first of three newGame() implementations.
 void newGame(int ID)
          Starts a new game using the specified ID to decide which Board to load.
 void notifyObservers()
          Notifies all Observers.
 void printBoard()
          Prints the current Board
 void printSolution()
          Prints the current Solution
 void registerObserver(Observer observer)
          Registers the observer with this Observable's list of Observers.
 void removeObserver(Observer observer)
          Removes the observer from this Observable's list of Observers
 java.lang.String requestName()
          The weird part to this method is that it will only return the name of the first Observer in the list.
 void setBestTimeAllowed()
          Sets the bestTimeAllowed boolean to True
 void setChanged()
          Sets the changed boolean to True
 void setDifficulty(BoardDifficulties difficulty)
          Sets the current difficulty level
 void setStatsAllowed()
          Sets the statsAllowed boolean to True
 void setStatusIdle()
          Sets the status of this Model to IDLE.
 void setStatusPlaying()
          Sets the status of this Model to PLAYING.
 void setTime(int time)
          Sets the time
 void submitBestTimes()
          Submits the BestTimes at the current difficulty level to the internet and updates the list of BestTimes at the JSudoku website.
 void updateBoard(int row, int col, Cell cell)
          Updates the Cell at the location [row][col] and sets it to the passed Cell argument
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PLAYING

public static final int PLAYING
See Also:
Constant Field Values

WON

public static final int WON
See Also:
Constant Field Values

LOST

public static final int LOST
See Also:
Constant Field Values

IDLE

public static final int IDLE
See Also:
Constant Field Values
Constructor Detail

SudokuModel

public SudokuModel()
The only constructor needed for the Model, which doesn't require any parameters. It simply initialises the necessary variables and is then ready for the Control to actually 'initialize' it, and for the user to start up a new game.

Method Detail

initialize

public void initialize()

newGame

public void newGame(BoardDifficulties difficulty)
The first of three newGame() implementations. This one should be used when the user simply wants to start a new game at a given difficulty but doesn't care about what ID it is. Onces each game has been setup, a call to notifyObservers() should be made in order to update() all of the observers.


newGame

public void newGame(int ID)
Starts a new game using the specified ID to decide which Board to load. This ID shouldn't be zero-based, the internal Inventory Helper will take care of necessary subtraction. This makes it smoother to get the ID directly from the user (1 is the first Board, not 0) and pass it along to the Helper.


newGame

public void newGame(Board board)
Starts a new game using the supplied Board. This would probably be used when the user has loaded a previously saved game and the Board is deserialized directly from the file, which doesn't require a specific Factory or Helper.


newDownloadedGame

public void newDownloadedGame()
                       throws java.lang.Exception
Starts a new game by downloading a random Board from the internet. The game won't be eligible for Best Times, but it should be able to be saved and loaded.

Throws:
java.lang.Exception

clearTimes

public void clearTimes()
Simply clears all Best Times


setTime

public void setTime(int time)
Sets the time


registerObserver

public void registerObserver(Observer observer)
Registers the observer with this Observable's list of Observers. SudokuModel can only register SudokuObservers, despite the method signature. If an Observer of any type other than SudokuObserver is passed in, an IllegalArgumentException will be thrown.

Specified by:
registerObserver in interface Observable

removeObserver

public void removeObserver(Observer observer)
Removes the observer from this Observable's list of Observers

Specified by:
removeObserver in interface Observable

notifyObservers

public void notifyObservers()
Notifies all Observers. The unintuitive method of deciding which argument to pass along to the Observers is decided based on the state of the changed boolean.

If changed is true, pass the Observer the board. Otherwise pass it the locations of all wrong Cells. A better way to solve this problem would probably be to not push everything to the Observers and instead allow them access to the changed flag and assume they can pull whatever they want from this Observable.

Specified by:
notifyObservers in interface Observable

gameWon

public void gameWon()
gameWon() is called when, as one might predict, a game has been won. A rather nice feature to it, though, is that it kicks off a new Thread to chug through all the possibly slow I/O stuff while the user gets to enjoy a smiley face animation. Before, the game would stall while it decided if the user was eligible for a Best Time.


checkForBestTimes

public void checkForBestTimes()
checkForBestTimes() checks for the possibility that the user is eligible for a Best Time. If so, it gets the user's name and adds it hands it over to the BestTimeHelper


submitBestTimes

public void submitBestTimes()
                     throws java.net.MalformedURLException,
                            com.jcraft.jsch.JSchException,
                            java.io.IOException
Submits the BestTimes at the current difficulty level to the internet and updates the list of BestTimes at the JSudoku website.

Throws:
java.net.MalformedURLException
com.jcraft.jsch.JSchException
java.io.IOException

giveUp

public void giveUp()
Self-explanatory.


giveHint

public Hint giveHint()
Hands over a Hint to whoever asks for one. Using hints disqualifies the user from achieving a Best Time.


addHints

public void addHints()
Since a Hint is simply a random Cell from the Board that hasn't already been solved for, addHints() just adds all "Hints" to the list of Hints


boardHasHints

public boolean boardHasHints()
Not terribly useful, but still will check for any remaining Hints


getBestTimes

public java.util.ArrayList<BestTime> getBestTimes()
Returns the current list of Best Times


requestName

public java.lang.String requestName()
The weird part to this method is that it will only return the name of the first Observer in the list. Since their probably isn't more than one Observer, and if there is it probably doesn't care about a name, it seems to work out OK anyway...


updateBoard

public void updateBoard(int row,
                        int col,
                        Cell cell)
Updates the Cell at the location [row][col] and sets it to the passed Cell argument


clear

public void clear()
Clears all of the user's placed Cells on the board, resets the Hints, and then notifies all Observers


setDifficulty

public void setDifficulty(BoardDifficulties difficulty)
Sets the current difficulty level


getDifficulty

public BoardDifficulties getDifficulty()
Returns the current difficulty level


getBoard

public Board getBoard()
Returns the current board


getSolution

public Board getSolution()
Returns the current board's solution


getStatus

public int getStatus()
Returns the current status


setStatusIdle

public void setStatusIdle()
Sets the status of this Model to IDLE. Outside classes are only allowed this option for setting the status of the Model - because it wouldn't be safe to allow any and everyone could decide when the Model's game was WON or LOST.


setStatusPlaying

public void setStatusPlaying()
Sets the status of this Model to PLAYING.


isLegalID

public boolean isLegalID(int ID)
Helper method to determine the legality of a candidate ID number


getRange

public java.lang.String getRange()
Returns a String representation of the current range of legal Board ID numbers


getIDLimit

public int getIDLimit()
Returns the highest possible legal ID


checkBoard

public void checkBoard()
Checks the current board for any mistakes, populates an int[] with the coordinates of all the incorrectly placed Cells, and then notifies all Observers


getCellType

public boolean getCellType(int row,
                           int col)
Returns the type of the Cell at the position [row][col]


getCellValue

public int getCellValue(int row,
                        int col)
Returns the value of the Cell at the position [row][col]


getBoardID

public int getBoardID()
Returns the ID number of the current Board. This returns 1 larger then the ACTUAL ID number so that it can be used easily for presenting the number to the user!


isLegal

public boolean isLegal(int row,
                       int col)
Returns whether or not it is legal for a user to try to place a Cell at the given location [row][col] on the Board


setChanged

public void setChanged()
Sets the changed boolean to True


clearChanged

public void clearChanged()
Sets the changed boolean to False


setBestTimeAllowed

public void setBestTimeAllowed()
Sets the bestTimeAllowed boolean to True


clearBestTimeAllowed

public void clearBestTimeAllowed()
Sets the bestTimeAllowed boolean to False


setStatsAllowed

public void setStatsAllowed()
Sets the statsAllowed boolean to True


clearStatsAllowed

public void clearStatsAllowed()
Sets the statsAllowed boolean to False


printBoard

public void printBoard()
                throws java.lang.Exception
Prints the current Board

Throws:
java.lang.Exception

printSolution

public void printSolution()
                   throws java.lang.Exception
Prints the current Solution

Throws:
java.lang.Exception