Package Model

Class GuestModel

All Implemented Interfaces:
ScrabbleModelFacade

public class GuestModel extends Observable implements ScrabbleModelFacade
  • Field Details

    • server

      protected Socket server
  • Constructor Details

    • GuestModel

      public GuestModel(String name, String ip, int port) throws IOException
      The GuestModel function is the constructor for the GuestModel class. It takes in a String name, String ip and int port as parameters. The function creates a new socket with the given ip and port, then sends "Connect:name" to server to connect. If server responds with "GameIsFull", it throws an IOException saying that game is full. If server responds with "NameIsTaken", it throws an IOException saying that name is taken by another player already connected to game.
      Parameters:
      name - A String -Set the name of the player
      ip - A String -Connect to the server public void waitforgamestart() { try { bufferedreader br = new bufferedreader(new inputstreamreader(server
      port - An int -Connect to the server
      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
    • disconnect

      public void disconnect()
      The disconnect function is used to disconnect the client from the server. It sends a message to the server that it is disconnecting, and then closes its socket connection with the server.
      Specified by:
      disconnect in interface ScrabbleModelFacade
    • isDisconnected

      public boolean isDisconnected()
      The isDisconnected function checks to see if the client is disconnected.
      Specified by:
      isDisconnected in interface ScrabbleModelFacade
      Returns:
      The value of the disconnect boolean variable
    • waitForGameStart

      public void waitForGameStart()
      The waitForGameStart function is used to wait for the game to start. It waits until it receives a message from the server that says "GameStarted". Once this happens, it notifies all of its observers and then calls waitForTurn().
    • 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 - ScrabbleViewModel -Pass the view model to the super class
    • 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 of true if it is the player's turn and false otherwise
    • 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, false otherwise
    • waitForTurn

      public void waitForTurn()
      The waitForTurn function is used to wait for the server to notify the client that it's their turn. The function will continue running until either: 1) It receives a message from the server saying that it's their turn, or 2) The game has ended (in which case, we close our connection with the server).
    • nextTurn

      public void nextTurn()
      The nextTurn function is called when the current player's turn ends. It sends a message to the server that it is no longer their turn, and then waits for a message from the server indicating that it is now their turn again. This function also sets myTurn to false, so that any further attempts by this client to send messages will be disabled until they receive another "YourTurn" message from the server.
      Specified by:
      nextTurn in interface ScrabbleModelFacade
    • submitWord

      public boolean submitWord(String word, int row, int col, boolean isVertical)
      The submitWord function is used to submit a word to the game board.
      Specified by:
      submitWord in interface ScrabbleModelFacade
      Parameters:
      word - String -Pass the word that is being submitted to the server
      row - int -Specify 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 vertical or horizontal
      Returns:
      true if word was successfully placed on board, false if not.
    • getScore

      public String getScore()
      The getScore function returns a string containing the current score of all players in the game. The format of this string is as follows with a delimiter of ";" to separate between players.

      Example: Michal:104;Tal:98;Roie:57;Arik:82

      Specified by:
      getScore in interface ScrabbleModelFacade
      Returns:
      String
    • getBoard

      public Character[][] getBoard()
      The getBoard function is used to get the board from the server.
      Specified by:
      getBoard in interface ScrabbleModelFacade
      Returns:
      A 2d array of characters
    • disconnectInvoked

      public void disconnectInvoked()
      The disconnectInvoked function is used to notify the observers that a disconnection has been invoked. It sets the disconnect boolean to true, and notifies all of its observers.
    • getNewPlayerTiles

      public ArrayList<Character> getNewPlayerTiles(int amount)
      The getNewPlayerTiles function is used to get new tiles for the player.
      Specified by:
      getNewPlayerTiles in interface ScrabbleModelFacade
      Parameters:
      amount - int -Determine how many tiles the player needs to get from the bag
      Returns:
      An arraylist of characters
    • startGame

      The startGame function is called when the game begins. It returns an ArrayList of 7 characters, which are the tiles that will be given to a player at the start of a game. The function calls getNewPlayerTiles(7) to accomplish this task.
      Specified by:
      startGame in interface ScrabbleModelFacade
      Returns:
      An arraylist of character objects
      Throws:
      IOException
      ClassNotFoundException