Package Model

Class HostModel

All Implemented Interfaces:
ScrabbleModelFacade

public class HostModel extends Observable implements ScrabbleModelFacade
  • Field Details

    • board

      protected Board board
    • players

      protected List<Player> players
    • name

      protected String name
    • myTurn

      protected boolean myTurn
    • gameStarted

      public boolean gameStarted
  • Constructor Details

    • HostModel

      public HostModel(String name) throws IOException
      The HostModel function is the constructor for the HostModel class. It initializes all the variables that are used in this class, and starts a server to listen for incoming connections from guests.
      Parameters:
      name - String -Set the name of the host
      Throws:
      IOException
  • Method Details

    • isGameStarted

      public boolean isGameStarted()
      The isGameStarted function returns a boolean value that indicates whether the game has started or not.
      Specified by:
      isGameStarted in interface ScrabbleModelFacade
      Returns:
      A boolean value
    • isMyTurn

      public boolean isMyTurn()
      The isMyTurn function returns a boolean value that indicates whether it is the player's turn.
      Specified by:
      isMyTurn in interface ScrabbleModelFacade
      Returns:
      A boolean value
    • isGameOver

      public boolean isGameOver()
      The isGameOver function checks to see if the game is over.
      Specified by:
      isGameOver in interface ScrabbleModelFacade
      Returns:
      True if the game is over, and false otherwise
    • addObserver

      public void addObserver(ScrabbleViewModel vm)
      The addObserver function adds a new observer to the list of observers.
      Specified by:
      addObserver in interface ScrabbleModelFacade
      Parameters:
      vm - An ScrabbleViewModel -Pass the view model to the super class
    • startGame

      The startGame function is responsible for starting the game. It shuffles the players, and then sends a message to all of them that the game has started. Then it waits 2 seconds before sending a message to either host or guest depending on who's turn it is first.
      Specified by:
      startGame in interface ScrabbleModelFacade
      Returns:
      A list of characters
      Throws:
      IOException
      ClassNotFoundException
    • submitWord

      public boolean submitWord(String word, int row, int col, boolean isVertical) throws IOException, ClassNotFoundException
      The submitWord function takes in a word, row, column and boolean value to determine if the word is vertical or horizontal. It then converts the string into a Word object using the stringToWord function. The tryPlaceWord function from Board is called with this new Word object as an argument and returns an integer score which represents how many points were earned by placing that word on the board. If no points are earned (score == 0), then false is returned because it was not possible to place that word on the board at those coordinates. Otherwise, true is returned because it was possible to place that word
      Specified by:
      submitWord in interface ScrabbleModelFacade
      Parameters:
      word - String -Create a word object to be placed on the board
      row - int -Determine the row of the first letter in a word
      col - int -Determine the column of the first letter in a word
      isVertical - boolean -Determine whether the word is placed vertically or horizontally on the board
      Returns:
      A boolean, which is true if the word was successfully placed on the board
      Throws:
      IOException
      ClassNotFoundException
    • update

      public void update()
      The update function is used to notify the observers that a change has been made. This function will be called by the model whenever it needs to update its observers.
    • isDisconnected

      public boolean isDisconnected()
      The isDisconnected function checks to see if the client is disconnected.
      Specified by:
      isDisconnected in interface ScrabbleModelFacade
      Returns:
      A boolean value that indicates whether the client is disconnected
    • getScore

      public String getScore() throws IOException
      The getScore function returns a string of the players' names and scores.
      Specified by:
      getScore in interface ScrabbleModelFacade
      Returns:
      A string with the scores of all players in the game
      Throws:
      IOException
    • getBoard

      public Character[][] getBoard() throws IOException, ClassNotFoundException
      The getBoard function is used to return the board as a 2D array of characters.
      Specified by:
      getBoard in interface ScrabbleModelFacade
      Returns:
      A character[][]
      Throws:
      IOException
      ClassNotFoundException
    • getBoardToCharacters

      public Character[][] getBoardToCharacters()
      The getBoardToCharacters function is a helper function that converts the board's tiles into characters.
      Returns:
      A 2d array of characters
    • getNewPlayerTiles

      public ArrayList<Character> getNewPlayerTiles(int amount) throws IOException, ClassNotFoundException
      The getNewPlayerTiles function is used to get a new set of tiles for the player.
      Specified by:
      getNewPlayerTiles in interface ScrabbleModelFacade
      Parameters:
      amount - int -Determine how many tiles the player will receive
      Returns:
      An arraylist of characters
      Throws:
      IOException
      ClassNotFoundException
    • nextTurn

      public void nextTurn() throws IOException, InterruptedException
      The nextTurn function is used to determine which player's turn it is. It also determines when the game ends, and if so, call the endGame function.
      Specified by:
      nextTurn in interface ScrabbleModelFacade
      Throws:
      IOException
      InterruptedException
    • disconnect

      public void disconnect()
      The disconnect function is used to notify the other players that this player has disconnected. It also closes the client socket and stops listening for messages from the server.
      Specified by:
      disconnect in interface ScrabbleModelFacade
    • closeClient

      public void closeClient()
      The closeClient function closes the client's connection to the server. It does this by closing the DictionaryCommunication instance, which in turn closes its socket and input/output streams.