All files PathTile.ts

68.75% Statements 22/32
55.55% Branches 5/9
88.88% Functions 8/9
68.75% Lines 22/32

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      1x 1x   1x 1x 1x 1x           1x                             291313x 291313x 291313x 291313x       972371x       291313x     291313x 2754x   291313x       17201x 4418x   12783x                 40x                 340x                                                 277650x                
/**
 * @author Timo lehnertz
 */
import { OpenSides } from "./OpenSides";
import { Treasure } from "./Treasure";
 
export enum TileType {
  STREIGHT,
  L,
  T,
}
 
/**
 * Immutable Tile class representing a single tile
 */
export class PathTile {
  /**
   * Key
   */
  public readonly tileType: TileType;
  public readonly treasure: Treasure | null;
  public readonly rotation: number;
  public readonly homeOfPlayerIndex: number | null;
 
  public constructor(
    tileType: TileType,
    treasure: Treasure | null,
    rotation: number,
    homeOfPlayerIndex: number | null
  ) {
    this.tileType = tileType;
    this.treasure = treasure;
    this.rotation = PathTile.normalizeRotation(rotation);
    this.homeOfPlayerIndex = homeOfPlayerIndex;
  }
 
  public get openSides(): OpenSides {
    return new OpenSides(this.tileType, this.rotation);
  }
 
  private static normalizeRotation(rotation: number): number {
    while (rotation < 0) {
      rotation += 4;
    }
    while (rotation > 4) {
      rotation -= 4;
    }
    return rotation;
  }
 
  public rotate(repeat: number): PathTile {
    if (repeat === 0) {
      return this;
    }
    return new PathTile(
      this.tileType,
      this.treasure,
      this.rotation + repeat,
      this.homeOfPlayerIndex
    );
  }
 
  public setHomeOfPlayerIndex(homeOfPlayerIndex: number): PathTile {
    return new PathTile(
      this.tileType,
      this.treasure,
      this.rotation,
      homeOfPlayerIndex
    );
  }
 
  public setTreasure(treasure: Treasure | null): PathTile {
    return new PathTile(
      this.tileType,
      treasure,
      this.rotation,
      this.homeOfPlayerIndex
    );
  }
 
  public equals(other: PathTile): boolean {
    Iif (this.tileType !== other.tileType) {
      return false;
    }
    Iif (!Treasure.compare(this.treasure, other.treasure)) {
      return false;
    }
    Iif (this.rotation !== other.rotation) {
      return false;
    }
    Iif (this.homeOfPlayerIndex !== other.homeOfPlayerIndex) {
      return false;
    }
    return true;
  }
 
  public static create(instance: PathTile): PathTile {
    return new PathTile(
      instance.tileType,
      instance.treasure === null ? null : Treasure.create(instance.treasure),
      instance.rotation,
      instance.homeOfPlayerIndex
    );
  }
}