Class Player


  • public class Player
    extends java.lang.Object
    A class that implements a player for the ConnectN (and possibly other) games.

    You do not need to modify this class (except to fix one checkstyle error). But you are encouraged to study it since it does some of the things that you need to do to complete ConnectN.

    See Also:
    MP3 Documentation
    • Constructor Summary

      Constructors 
      Constructor Description
      Player(java.lang.String setName)
      Create a new player with the given name.
      Player(java.lang.String setName, int setScore, int setID)
      Create a new player from a full set of fields.
      Player(Player other)
      Create a copy of an existing player.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addScore()
      Add one to this player's score.
      boolean equals(java.lang.Object obj)
      Define equality for the Player class.
      int getId()
      Get the ID of this player.
      java.lang.String getName()
      Get the player's name.
      int getScore()
      Get this player's score.
      int hashCode()
      Define the hash code for the Player class.
      void setName(java.lang.String setName)
      Set the player's name.
      • Methods inherited from class java.lang.Object

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

      • Player

        public Player(java.lang.String setName)
        Create a new player with the given name.

        Each player's score begins at zero, and each receives a monotonically increasing unique ID.

        Parameters:
        setName - the name for the new player
      • Player

        public Player(Player other)
        Create a copy of an existing player.

        This copy constructor creates a copy of an existing player, so that player information can be exposed without allowing the copy to modify the state of the original player.

        The id is copied, meaning that the copy will initially be equal to the original. However, future modifications to the score field will not change the copy and will result in it being considered non-equal to the original.

        Parameters:
        other - the other
      • Player

        public Player(java.lang.String setName,
                      int setScore,
                      int setID)
        Create a new player from a full set of fields.

        This is entirely for deserialization by the web frontend. Unlike the other constructors, it does not increment the player count, since the assumption is that you are recovering an existing player.

        Parameters:
        setName - the name for the new player
        setScore - the score for the new player
        setID - the ID for the new player
    • Method Detail

      • getName

        public java.lang.String getName()
        Get the player's name.
        Returns:
        the player's name
      • setName

        public void setName(java.lang.String setName)
        Set the player's name.
        Parameters:
        setName - the player's new name
      • getScore

        public int getScore()
        Get this player's score.
        Returns:
        this player's score
      • addScore

        public void addScore()
        Add one to this player's score.
      • getId

        public int getId()
        Get the ID of this player. Used during serialization, so do not remove.
        Returns:
        the ID of this player.
      • hashCode

        public int hashCode()
        Define the hash code for the Player class.

        This method should only use the id field of the instance. This implements what is known as entity semantics. Two players with the same name and same score may not be equal if they were created at different times. Note that IntelliJ can auto-generate this and equals.

        Overrides:
        hashCode in class java.lang.Object
        See Also:
        Object.hashCode()
      • equals

        public final boolean equals(java.lang.Object obj)
        Define equality for the Player class.

        This method should only use the id field of the instance. This implements what is known as entity semantics. Two players with the same name and same score may not be equal if they were created at different times. Note that IntelliJ can auto-generate this and hashCode().

        Overrides:
        equals in class java.lang.Object
        See Also:
        Object.equals(java.lang.Object)