Package loveletter

Class State

  • All Implemented Interfaces:
    java.lang.Cloneable

    public class State
    extends java.lang.Object
    implements java.lang.Cloneable
    This class represents the observable state of the game. The class comes in two modes, one for the players, whcih has update operations disabled, and one for the game engine, that can update the state. States of players in the same game will have common data, allowing for an efficient representation.
    • Constructor Summary

      Constructors 
      Constructor Description
      State​(java.util.Random random, Agent[] agents)
      Default constructor to build the initial observed state for a player First player in the array will always start
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int deckSize()
      gives the remaining size of the deck, including the burnt card
      Card drawCard()
      Draws a card for a player from the shuffled deck.
      boolean eliminated​(int player)
      returns true if the nominated player is eleiminated in the round
      boolean gameOver()  
      int gameWinner()  
      Card getCard​(int playerIndex)
      get the card of the specified player, if known.
      java.util.Iterator<Card> getDiscards​(int player)
      returns an iterator to go through a players discard pile, from most recent to earliest.
      int getPlayerIndex()
      returns the index of the observing player, or -1 for perfect information
      boolean handmaid​(int player)
      helper method to determine if the nominated player is protected by the handmaid
      boolean legalAction​(Action act, Card drawn)
      Checks to see if an action is legal given the current state of the game, for an agent who has just drawn a card.
      void newRound()
      Resets state for a new round, with new deck of cards, and everyone's hand and discards reset.
      int nextPlayer()
      Gives the next player to play in the round
      int numPlayers()
      Gives the number of players in the game
      State playerState​(int player)
      Produces a state object for a player in the game.
      boolean roundOver()
      Tests to see if the round is over, either by all but one player being eleiminated or by all but one card being drawn from the deck.
      int roundWinner()
      helper method to determine the winner of the round.
      int score​(int player)  
      Card[] unseenCards()
      returns an array of the remaining cards that haven't been played yet.
      java.lang.String update​(Action act, Card card)
      Executes the given action of a player.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • State

        public State​(java.util.Random random,
                     Agent[] agents)
        Default constructor to build the initial observed state for a player First player in the array will always start
        Parameters:
        random - the random number generator for the deals.
        agents - the array of players who start the game (must be of size 2,3 or 4)
        Throws:
        java.lang.IllegalArgumentException - if the array is of the wrong size.
    • Method Detail

      • playerState

        public State playerState​(int player)
                          throws IllegalActionException
        Produces a state object for a player in the game. The update methods will be disabled for that State object.
        Parameters:
        player - the player for who the State object is created.
        Throws:
        IllegalActionException - if this is a player state.
        java.lang.IllegalArgumentException - if player is not between 0 and numPlayers
      • legalAction

        public boolean legalAction​(Action act,
                                   Card drawn)
        Checks to see if an action is legal given the current state of the game, for an agent who has just drawn a card. That is a) the player a must hold card c, b) it must be player a's turn c) if the player holds the countess, they cannot play the Prince or the King d) if the action has a target, they cannot be eliminated e) if the target is protected by the handmaid and their is some player other than the target and a not protected, then that player must be targetted instead. f) if all players are protected by the handmaid and the player a plays a Prince, they must target themselves There are other rules (such as a player not targetting themselves) that is enforced in the Action class.
        Parameters:
        act - the action to be performed
        drawn - the card drawn by the playing agent.
        Throws:
        IllegalActionException - if any of these conditions hold.
      • drawCard

        public Card drawCard()
                      throws IllegalActionException
        Draws a card for a player from the shuffled deck. May only be performed in the game state. The card is no longer available on the top of the deck.
        Returns:
        the top card of the deck
        Throws:
        IllegalActionException - if an agent attempts to access this from a player state.
      • update

        public java.lang.String update​(Action act,
                                       Card card)
                                throws IllegalActionException
        Executes the given action of a player. May only be called for non-player states (i.e. the omniscient game engine state)
        Parameters:
        act - the action to be performed
        card - the card drawn by the actor
        Returns:
        a plain English decsription of the action
        Throws:
        IllegalActionAxception - if the state is a player state, or if the action is against the rules.
        IllegalActionException
      • getPlayerIndex

        public int getPlayerIndex()
        returns the index of the observing player, or -1 for perfect information
        Returns:
        the index of the observing player or -1 for perfect information.
      • getDiscards

        public java.util.Iterator<Card> getDiscards​(int player)
        returns an iterator to go through a players discard pile, from most recent to earliest.
        Parameters:
        player - the index of the player whos discard pile is sought.
        Returns:
        an iterator to go through the discard pile, from most recently discarded to oldest discard
      • getCard

        public Card getCard​(int playerIndex)
        get the card of the specified player, if known.
        Parameters:
        playerIndex - the player for which we seek the card
        Returns:
        the card the player currently holds, or null, if it is not known
        Throws:
        ArrayIndexoutOfBoundsException - if the playerIndex is out of range.
      • eliminated

        public boolean eliminated​(int player)
        returns true if the nominated player is eleiminated in the round
        Parameters:
        player - the player being checked
        Returns:
        true if and only if the player has been eliminated in the round.
        Throws:
        ArrayIndexoutOfBoundsException - if the playerIndex is out of range.
      • nextPlayer

        public int nextPlayer()
        Gives the next player to play in the round
        Returns:
        the index of the next player to play
      • numPlayers

        public int numPlayers()
        Gives the number of players in the game
        Returns:
        the number of players in the game
      • handmaid

        public boolean handmaid​(int player)
        helper method to determine if the nominated player is protected by the handmaid
        Returns:
        true if and only if the index corresponds to a player who is protected by the handmaid
      • deckSize

        public int deckSize()
        gives the remaining size of the deck, including the burnt card
        Returns:
        the number of cards not in players hands or discarded.
      • unseenCards

        public Card[] unseenCards()
        returns an array of the remaining cards that haven't been played yet.
        Returns:
        an array of all cards not in the discard piles
      • roundOver

        public boolean roundOver()
        Tests to see if the round is over, either by all but one player being eleiminated or by all but one card being drawn from the deck.
        Returns:
        true if and only if the round is over
      • roundWinner

        public int roundWinner()
        helper method to determine the winner of the round. In the unlikely event of a total draw, the player with the smallest index is the winner.
        Returns:
        the index of the winner, or -1 if the round is not yet over.
      • score

        public int score​(int player)
      • gameOver

        public boolean gameOver()
      • gameWinner

        public int gameWinner()