Skip to content
Settlers Journal
Go back

Computing Available Actions for the `TURN_SPEND_RESOURCES` State

Edit page

TLDR

Improving the automated testing setup I used for local development. Implement checks in CatanBankService that ensure a player can make particular purchases. Checking which purchases a player can make before returning available Actions.

if is_manual_execution:
  # ask for user confirmation
  user_input = input("Do you want to execute this command? (y/n): ").strip().lower()

  # default to "yes" if input is empty
  if user_input == '' or user_input == 'y':
      os.system(bash_command)
  elif user_input == 'n':
      print("Skipping command.")
  else:
        print("Invalid input. Skipping command.")

alt text

Avoiding manually confirming 20+ commands to get board into proper testing state.

public static boolean isDevCardPurchasePossible(CatanGame catanGame, int userId) {
  int totalDevCardsAvailable = CatanGame.DEV_CARD_COUNTS
      .values()
      .stream()
      .mapToInt(Integer::intValue)
      .sum();
  long totalDevCardsPurchased = catanGame
      .getPlayers()
      .stream()
      .map(Player::getDevCards)
      .mapToLong(List::size)
      .sum();
  boolean isDevCardAvailable = totalDevCardsPurchased < totalDevCardsAvailable;

  boolean doesPlayerHaveSufficientResources = catanGame
      .getPlayerByUserId(userId)
      .getResources()
      .isGreaterThanOrEqualTo(CatanGame.REQUIREMENTS_DEV_CARD);

  return isDevCardAvailable && doesPlayerHaveSufficientResources;
}

alt text

Showing the available purchase Actions during TURN_SPEND_RESOURCES state.

Main work

Challenges

Learnings

Why Is new ActionEnum[0] Needed Here?

Consider the setAvailableActions method and its usage.

// call to method, requiring array type with "strange" parameter
catanGame.getActivePlayer().setAvailableActions(
  availablePurchaseActions.toArray(new ActionEnum[0])
);

// method signature, taking variable num of args
public void setAvailableActions(ActionEnum... availableActions) {
  this.availableActions.clear();
  Collections.addAll(this.availableActions, availableActions);
}

Edit page
Share this post on:

Previous Post
Revisiting Catan With a Fresh Perspective
Next Post
City Placement Validation, Double Resource Collection, and Robber Blocking