All Downloads are FREE. Search and download functionalities are using the official Maven repository.

uk.co.mruoc.nac.entities.Status Maven / Gradle / Ivy

There is a newer version: 0.1.42
Show newest version
package uk.co.mruoc.nac.entities;

import java.util.Optional;
import lombok.Builder;
import lombok.Data;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Builder(toBuilder = true)
@Data
public class Status {

  private final long turn;
  private final Players players;
  private final boolean complete;
  private final Player winner;

  public Status(Players players) {
    this(0, players, false, null);
  }

  public Status turnTaken() {
    return toBuilder().turn(nextTurn()).players(players.updateCurrentPlayer()).build();
  }

  public Status winningTurnTaken(char token) {
    return Status.builder()
        .turn(nextTurn())
        .players(players.clearCurrentPlayer())
        .complete(true)
        .winner(players.getPlayerByToken(token))
        .build();
  }

  public Status drawGameTurnTaken() {
    return Status.builder()
        .turn(nextTurn())
        .players(players.clearCurrentPlayer())
        .complete(true)
        .build();
  }

  public Optional getCurrentPlayer() {
    return players.getCurrentPlayer();
  }

  public void validate(Turn turn) {
    players.validate(turn);
  }

  public Optional getWinner() {
    return Optional.ofNullable(winner);
  }

  public boolean isDraw() {
    return complete && getWinner().isEmpty();
  }

  private long nextTurn() {
    return turn + 1;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy