Module AM37

Class RuleSetBase

java.lang.Object
it.polimi.ingsw.model.rules.RuleSetBase
All Implemented Interfaces:
RuleSetStrategy
Direct Known Subclasses:
AffectMyTurnStrategy, AffectOpponentTurnStrategy, BuildingStrategy, MovementStrategy, WinConditionStrategy

public class RuleSetBase
extends java.lang.Object
implements RuleSetStrategy
The default rules

It defines the default rules for moving, building and setting up the turn:

  • Setup: using this ruleset, the player can place TWO workers at the beginning of the game (see Worker for details about the Workers placement)
  • Turn Order: the player MUST choose a worker, move the worker in another cell (see #isMoveActionValid()) and build (see #isBuildActionValid()) with THE SAME worker
  • Field Details

  • Constructor Details

    • RuleSetBase

      public RuleSetBase()
      Default constructor
      See Also:
      initialize()
    • RuleSetBase

      public RuleSetBase​(RuleSetStrategy ruleSetBase, Game game)
      Copy constructor

      Used to restore the game status in case of unfortunate events on the server side

      Parameters:
      ruleSetBase - the ruleSet to restore
      game - the game code
  • Method Details

    • initialize

      protected void initialize()
      Sets the parameters for a new turn

      Using this ruleSet, a player is granted one movement and one building action, which have to be used in this order and by the same worker

    • getMovesAvailable

      public int getMovesAvailable()
      movesAvailable getter
      Specified by:
      getMovesAvailable in interface RuleSetStrategy
      Returns:
      the number of moves available
    • getMovesUpAvailable

      public int getMovesUpAvailable()
      movesUpAvailable getter
      Specified by:
      getMovesUpAvailable in interface RuleSetStrategy
      Returns:
      the number of moves available on a taller building
    • setMovesUpAvailable

      public void setMovesUpAvailable​(int num)
      movesUpAvailable setter

      Used when an effect has a malus on other players' available moves

      Specified by:
      setMovesUpAvailable in interface RuleSetStrategy
      Parameters:
      num - the number of moves up to be made available
    • hasMovedUp

      public boolean hasMovedUp()
      hasMovedUp getter
      Specified by:
      hasMovedUp in interface RuleSetStrategy
      Returns:
      true if the player moved up during the last turn, false otherwise
    • getBuildsAvailable

      public int getBuildsAvailable()
      buildsAvailable getter
      Specified by:
      getBuildsAvailable in interface RuleSetStrategy
      Returns:
      the number of buildings the player can build
    • getMovedWorker

      public Worker getMovedWorker()
      movedWorker getter
      Specified by:
      getMovedWorker in interface RuleSetStrategy
      Returns:
      the worker which has been moved during the last turn (can be null)
    • setGame

      public void setGame​(Game game)
      game setter
      Specified by:
      setGame in interface RuleSetStrategy
      Parameters:
      game - the game in which the effect is used
    • getBlocks

      public java.util.List<Block> getBlocks​(Cell selectedCell)
      Provides the possible blocks buildable on a given cell

      Using this ruleSet, a worker will always be able to build only one kind of block, immediately higher than the one it is trying to build on

      Specified by:
      getBlocks in interface RuleSetStrategy
      Parameters:
      selectedCell - the cell to get the buildable blocks for
      Returns:
      a list of blocks that can be built on the given cell
    • doEffect

      public void doEffect()
      Applies end turn effects

      Using this ruleSet, the end turn effects simply resets the attributes changed during the turn

      Specified by:
      doEffect in interface RuleSetStrategy
    • isInsideWalkableCells

      protected boolean isInsideWalkableCells​(MoveAction action)
      Determines if the moveAction's target cell is a legal one
      Parameters:
      action - the action to analyze
      Returns:
      true if the cell is walkable, false otherwise
    • getPossibleActions

      public java.util.List<PossibleActions> getPossibleActions​(Worker worker)
      Provides a list of possible actions for a player to perform, based on the chosen worker

      g this ruleSet, the possible actions for a worker are:

      • Change Worker/Move, if the worker has not been moved yet
      • Build, if the worker has been moved
      • None, in any other case
      Specified by:
      getPossibleActions in interface RuleSetStrategy
      Parameters:
      worker - the worker to perform an action with
      Returns:
      a list of possible performable actions
    • isMoveActionValid

      public boolean isMoveActionValid​(MoveAction action)
      Determines if a moveAction is legal and applies it

      g this ruleSet, a movement action is considered valid if the following conditions are all true:

      • no worker has been moved yet during the turn
      • the target cell is a walkable cell (see getWalkableCells(Worker)) for the worker to be moved
      Specified by:
      isMoveActionValid in interface RuleSetStrategy
      Parameters:
      action - the movement action to validate
      Returns:
      true if the action has been applied, false otherwise
    • isBuildActionValid

      public boolean isBuildActionValid​(BuildAction action)
      Determines if a buildAction is legal and applies it

      g this ruleSet, a build action is considered valid if the following conditions are all true:

      • a worker has already been moved
      • the worker to perform the action is the same which has been moved
      • the cell to build on is a buildable cell (see getBuildableCells(Worker)) for the worker
      Specified by:
      isBuildActionValid in interface RuleSetStrategy
      Parameters:
      action - the build action to validate
      Returns:
      true if the action has been applied, false otherwise
    • isInsideBuildableCells

      protected boolean isInsideBuildableCells​(BuildAction action)
      Determines if the buildAction's target cell is a legal one
      Parameters:
      action - the action to analyze
      Returns:
      true if the cell is buildable, false otherwise
      See Also:
      isBuildActionValid(BuildAction)
    • canBuild

      protected boolean canBuild​(BuildAction action)
      Determines if the worker chosen for the build action can actually build
      Parameters:
      action - the action to analyze
      Returns:
      true if the worker can build, false otherwise
      See Also:
      isBuildActionValid(BuildAction)
    • isCorrectBlock

      protected boolean isCorrectBlock​(BuildAction action)
      Determines if the block to be built can actually be built
      Parameters:
      action - the action to analyze
      Returns:
      true if the block can be placed, false otherwise
      See Also:
      isBuildActionValid(BuildAction)
    • checkWinCondition

      public boolean checkWinCondition​(MoveAction action)
      Determines if the win conditions are satisfied upon a movement action

      Using this ruleSet, a player can win only upon moving (following the rules already defined in isMoveActionValid(MoveAction)) on a level 3 building

      Specified by:
      checkWinCondition in interface RuleSetStrategy
      Parameters:
      action - the action to analyze
      Returns:
      true if the action led to victory, false otherwise
    • checkLoseCondition

      public boolean checkLoseCondition​(MoveAction moveAction)
      Determines if the lose conditions are satisfied upon a movement action

      Using this ruleSet, a player can lose upon a movement action if the worker which has been moved cannot build any block around it

      Specified by:
      checkLoseCondition in interface RuleSetStrategy
      Parameters:
      moveAction - the action to analyze
      Returns:
      true if the action led to a loss, false otherwise
    • checkLoseCondition

      public boolean checkLoseCondition()
      Checks if the turn can begin

      Using this ruleSet, a player's turn can start if at least one of the player's workers can perform a movement action

      Specified by:
      checkLoseCondition in interface RuleSetStrategy
      Returns:
      true if there is at least one action to perform, false otherwise
    • checkLoseCondition

      public boolean checkLoseCondition​(BuildAction buildAction)
      Determines if the lose conditions are satisfied upon a build action

      Using this ruleSet, the player can never lose after it performs a build action; it might "trap itself", but in this case it will lose on the beginning of its next turn.

      Specified by:
      checkLoseCondition in interface RuleSetStrategy
      Parameters:
      buildAction - the action to analyze
      Returns:
      true if the action led to a loss, false otherwise
    • getWalkableCells

      public java.util.List<Cell> getWalkableCells​(Worker worker)
      Provides a list of cells on which the worker can walk on

      Using this ruleSet, a worker can walk on the cells adjacent to its starting cell which height difference is at most one compared to the starting cell (domes do not count), is not occupied by another worker and has no dome built on it

      Specified by:
      getWalkableCells in interface RuleSetStrategy
      Parameters:
      worker - the worker to be moved
      Returns:
      a list of walkable cells
    • addWalkableCells

      protected void addWalkableCells​(Worker worker, java.util.List<Cell> cells)
      Adds to a list the cells on which the given player can walk on
      Parameters:
      worker - the worker to be moved
      cells - the list of walkable cells
    • isCorrectDistance

      protected boolean isCorrectDistance​(Worker worker, Cell cell)
      Determines if a worker can reach a cell

      A cell is considered reachable if the height difference between the worker's cell and the target cell is at most 1, and the movesUpAvailable counter has not been decreased

      Parameters:
      worker - the worker to be moved
      cell - the target cell
      Returns:
      true if the cell is reachable, false otherwise
    • getBuildableCells

      public java.util.List<Cell> getBuildableCells​(Worker worker)
      Provides a list of cells on which the worker can build on

      Using this ruleSet, a worker can build on any cell adjacent to its starting cell

      Specified by:
      getBuildableCells in interface RuleSetStrategy
      Parameters:
      worker - the worker to build with
      Returns:
      a list of buildable cells
    • addBuildableCells

      protected void addBuildableCells​(Worker worker, java.util.List<Cell> cells)
      Adds to a list the cells on which the given player can build on
      Parameters:
      worker - the worker to be moved
      cells - the list of buildable cells
      See Also:
      getBuildableCells(Worker)
    • canEndTurn

      public boolean canEndTurn()
      Determines whether a player can end its turn

      Using this ruleSet, a player cannot end manually its turn

      Specified by:
      canEndTurn in interface RuleSetStrategy
      Returns:
      true if the player can end its turn, false otherwise
    • canEndTurnAutomatically

      public boolean canEndTurnAutomatically()
      Determines whether a player can end its turn

      Using this ruleSet, a player turn is ended automatically right after it performs its build action
      This method should never be invoked directly from the player

      Specified by:
      canEndTurnAutomatically in interface RuleSetStrategy
      Returns:
      true if the player can end its turn, false otherwise
    • cloneStrategy

      public RuleSetStrategy cloneStrategy​(Game game)
      Creates a clone of this object
      Specified by:
      cloneStrategy in interface RuleSetStrategy
      Parameters:
      game - the current game
      Returns:
      a clone of this object