Class Player

Direct Known Subclasses:
AlienPlayer, ArcherPlayer, KnightPlayer, RebornPlayer

public abstract class Player extends Entity
Base class for all 4 player characters (Knight, Archer, Reborn, Alien). Manages inventory, skills, movement, and the defend system. calculateAttack() is left to subclasses to override because the values differ.
  • Field Details

    • row

      protected int row
      Row position on the map.
    • col

      protected int col
      Column position on the map.
    • inventory

      protected List<Item> inventory
      List of items currently held.
    • maxInventorySize

      protected int maxInventorySize
      Maximum number of items that can be held; default is 4.
    • skill

      protected Skill skill
      The character's special skill.
    • isDefending

      protected boolean isDefending
      Whether the player is currently defending (halves incoming damage this turn).
    • playerNumber

      protected int playerNumber
      Player number (1 or 2).
    • selectedItemIndex

      protected int selectedItemIndex
      Index of the selected item in the inventory.
  • Constructor Details

    • Player

      public Player(String name, int maxHp, int currentHp, int attackPower, int defense, Skill skill)
      Creates a player with the specified starting stats.
      Parameters:
      name - Character name
      maxHp - Maximum HP
      currentHp - Starting HP
      attackPower - Starting attack power
      defense - Starting defense
      skill - The character's skill
  • Method Details

    • takeDamage

      public void takeDamage(int dmg)
      Takes damage, halving it first if the player is currently defending. The defending state is always reset to false after damage is applied.
      Overrides:
      takeDamage in class Entity
      Parameters:
      dmg - Raw damage before reduction
    • canMoveTo

      public boolean canMoveTo(Cell cell)
      Checks whether this character can move to the given cell. By default, only walkable cells (not ROCK, RIVER, or TREE) are allowed. Subclasses such as Archer or Reborn override this to add exceptions.
      Parameters:
      cell - Target cell
      Returns:
      true if movement is allowed
    • isValidMove

      public boolean isValidMove(int fromRow, int fromCol, int toRow, int toCol)
      Checks whether moving from (fromRow, fromCol) to (toRow, toCol) is valid. By default, only straight-line moves of exactly 1 step are allowed (no diagonal). Alien overrides this to permit diagonal movement.
      Parameters:
      fromRow - Starting row
      fromCol - Starting column
      toRow - Destination row
      toCol - Destination column
      Returns:
      true if the move is valid
    • move

      public void move(int newRow, int newCol)
      Moves the character to a new position on the map.
      Parameters:
      newRow - New row
      newCol - New column
    • pickUpItem

      public boolean pickUpItem(Item item)
      Picks up an item and adds it to the inventory if there is space.
      Parameters:
      item - Item to pick up
      Returns:
      true if picked up successfully, false if inventory is full
    • useFirstItem

      public boolean useFirstItem()
      Uses the first item in the inventory and removes it.
      Returns:
      true if successful, false if the inventory is empty
    • getRow

      public int getRow()
      Returns:
      Current row on the map
    • getCol

      public int getCol()
      Returns:
      Current column on the map
    • getInventory

      public List<Item> getInventory()
      Returns:
      All items currently in the inventory
    • getSkill

      public Skill getSkill()
      Returns:
      The character's skill
    • isDefending

      public boolean isDefending()
      Returns:
      true if the player is defending this turn
    • setDefending

      public void setDefending(boolean d)
      Sets the defending state.
      Parameters:
      d - true to start defending
    • getPlayerNumber

      public int getPlayerNumber()
      Returns:
      Player number (1 or 2).
    • setPlayerNumber

      public void setPlayerNumber(int n)
      Sets the player number.
      Parameters:
      n - Player number (1 or 2)
    • getMaxInventorySize

      public int getMaxInventorySize()
      Returns:
      Maximum number of items that can be held
    • isInventoryFull

      public boolean isInventoryFull()
      Returns:
      true if the inventory is full
    • getSelectedItemIndex

      public int getSelectedItemIndex()
      Returns:
      Index of the currently selected item
    • setSelectedItemIndex

      public void setSelectedItemIndex(int index)
      Selects an item by its index in the inventory.
      Parameters:
      index - Index of the item in the inventory