All files AllPlayerStates.ts

57.14% Statements 32/56
0% Branches 0/5
66.66% Functions 10/15
56.6% Lines 30/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142        1x           1x               34071x 34071x                               22925x 22925x 91700x   22925x             141x 141x   141x                         17201x 17201x 68804x   17201x       5583x 5583x   5583x       483x       11649x       5583x                                         5583x 5583x 22332x 22332x       5583x                                         5553x 5553x 22212x   5553x      
/**
 * @author Timo Lehnertz
 */
import { BoardPosition } from "./BoardPosition";
import { PlayerState } from "./PlayerState";
import { Treasure } from "./Treasure";
 
/**
 * Immutable AllPlayerStates class containing the states of all players
 */
export class AllPlayerStates {
  private readonly allPlayerStates: PlayerState[];
  public readonly playerIndexToMove: number;
 
  public constructor(
    allPlayerStates: PlayerState[],
    playerIndexToMove: number
  ) {
    this.allPlayerStates = allPlayerStates;
    this.playerIndexToMove = playerIndexToMove;
  }
 
  public get playerCount(): number {
    return this.allPlayerStates.length;
  }
 
  public getPlayerPositions(): BoardPosition[] {
    const positions: BoardPosition[] = [];
    for (const playerState of this.allPlayerStates) {
      positions.push(playerState.position);
    }
    return positions;
  }
 
  private copyPlayerStates(): PlayerState[] {
    const newAllPlayerStates: PlayerState[] = [];
    for (const playerState of this.allPlayerStates) {
      newAllPlayerStates.push(playerState);
    }
    return newAllPlayerStates;
  }
 
  public collectTreasure(
    playerIndex: number,
    foundTreasure: Treasure
  ): AllPlayerStates {
    const newAllPlayerStates = this.copyPlayerStates();
    newAllPlayerStates[playerIndex] =
      newAllPlayerStates[playerIndex].collectTreasure(foundTreasure);
    return new AllPlayerStates(newAllPlayerStates, this.playerIndexToMove);
  }
 
  public removeLastTreasure(playerIndex: number) {
    const newAllPlayerStates = this.copyPlayerStates();
    newAllPlayerStates[playerIndex] =
      newAllPlayerStates[playerIndex].removeLastTreasure();
    return new AllPlayerStates(newAllPlayerStates, this.playerIndexToMove);
  }
 
  public mutateAll(
    mutationCallback: (playerState: PlayerState) => PlayerState
  ): AllPlayerStates {
    const newAllPlayerStates = this.copyPlayerStates();
    for (let i = 0; i < newAllPlayerStates.length; i++) {
      newAllPlayerStates[i] = mutationCallback(newAllPlayerStates[i]);
    }
    return new AllPlayerStates(newAllPlayerStates, this.playerIndexToMove);
  }
 
  public movePlayer(playerIndex: number, to: BoardPosition): AllPlayerStates {
    const newAllPlayerStates = this.copyPlayerStates();
    newAllPlayerStates[playerIndex] =
      newAllPlayerStates[playerIndex].setPosition(to);
    return new AllPlayerStates(newAllPlayerStates, this.playerIndexToMove);
  }
 
  public getPlayerToMoveState(): PlayerState {
    return this.getPlayerState(this.playerIndexToMove);
  }
 
  public getPlayerState(playerIndex: number): PlayerState {
    return this.allPlayerStates[playerIndex];
  }
 
  public nextPlayer(): AllPlayerStates {
    return new AllPlayerStates(
      this.allPlayerStates,
      (this.playerIndexToMove + 1) % this.allPlayerStates.length
    );
  }
 
  public prevPlayer(): AllPlayerStates {
    let newPlayerIndexToMove = this.playerIndexToMove - 1;
    Iif (newPlayerIndexToMove < 0) {
      newPlayerIndexToMove = this.allPlayerStates.length - 1;
    }
    return new AllPlayerStates(this.allPlayerStates, newPlayerIndexToMove);
  }
 
  public getPlayerStatesWithAllTreasures(): {
    playerIndex: number;
    playerState: PlayerState;
  }[] {
    const playerStates: {
      playerIndex: number;
      playerState: PlayerState;
    }[] = [];
    for (let i = 0; i < this.allPlayerStates.length; i++) {
      const playerState = this.allPlayerStates[i];
      Iif (playerState.remainingTreasureCount === 0) {
        playerStates.push({ playerState, playerIndex: i });
      }
    }
    return playerStates;
  }
 
  public equals(other: AllPlayerStates): boolean {
    Iif (this.playerIndexToMove !== other.playerIndexToMove) {
      return false;
    }
    Iif (this.allPlayerStates.length !== other.allPlayerStates.length) {
      return false;
    }
    for (let i = 0; i < this.allPlayerStates.length; i++) {
      const a = this.allPlayerStates[i];
      const b = other.allPlayerStates[i];
      Iif (!a.equals(b)) {
        return false;
      }
    }
    return true;
  }
 
  public static create(instance: AllPlayerStates): AllPlayerStates {
    const allPlayerStates: PlayerState[] = [];
    for (const playerState of instance.allPlayerStates) {
      allPlayerStates.push(PlayerState.create(playerState));
    }
    return new AllPlayerStates(allPlayerStates, instance.playerIndexToMove);
  }
}