Skip to content

Commit

Permalink
Treat T&B hexes as equiv to underlying terrain
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisrandy committed Feb 18, 2024
1 parent 19f39d3 commit 2877ee2
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/logic/shuffle.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { Hex, HexType, Port, PortOrientation, FishTile } from "../types/hexes";
import {
Hex,
HexType,
Port,
PortOrientation,
FishTile,
FishTileOrientation,
} from "../types/hexes";
import { BinaryConstraints, NumericConstraints } from "../types/constraints";
import { CatanBoard, Neighbors } from "../types/boards";
import { HexGroups } from "./HexGroups";
import { hexToPipCount } from "../utils/catan";
import { compareHexType, hexToPipCount } from "../utils/catan";

// structuredClone isn't available on older devices. use this as a polyfill
if (typeof globalThis.structuredClone === "undefined") {
Expand Down Expand Up @@ -142,7 +149,7 @@ function getShuffledTerrain(
// will still be shuffled, unless the lower neighbor is fixed
if (
(neighbor > currentIndex || hexes[neighbor].fixed) &&
hexes[currentIndex].type === hexes[neighbor].type
compareHexType(hexes[currentIndex], hexes[neighbor])
)
stack.push(neighbor);
}
Expand Down
41 changes: 40 additions & 1 deletion src/tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { hexToPipCount, numberToPipCount } from "../utils/catan";
import {
compareHexType,
hexToPipCount,
numberToPipCount,
} from "../utils/catan";

describe("Catan utils", () => {
it("should return the correct number of pips for every valid number", () => {
Expand All @@ -25,4 +29,39 @@ describe("Catan utils", () => {
hexToPipCount({ type: "mountains", number: 10, secondNumber: 10 })
).toBe(6);
});

it("should correctly compare all hex type pairs", () => {
expect(
compareHexType({ type: "mountains" }, { type: "riverMountains" })
).toBe(true);
expect(
compareHexType({ type: "riverMountains" }, { type: "riverMountains" })
).toBe(true);
expect(compareHexType({ type: "hills" }, { type: "riverHills" })).toBe(
true
);
expect(compareHexType({ type: "riverHills" }, { type: "riverHills" })).toBe(
true
);
expect(compareHexType({ type: "pasture" }, { type: "riverPasture" })).toBe(
true
);
expect(
compareHexType({ type: "riverPasture" }, { type: "riverPasture" })
).toBe(true);
expect(compareHexType({ type: "desert" }, { type: "oasis" })).toBe(true);
expect(
compareHexType({ type: "mountains" }, { type: "riverPasture" })
).toBe(false);
expect(compareHexType({ type: "mountains" }, { type: "riverHills" })).toBe(
false
);
expect(compareHexType({ type: "hills" }, { type: "riverMountains" })).toBe(
false
);
expect(
compareHexType({ type: "pasture" }, { type: "riverMountains" })
).toBe(false);
expect(compareHexType({ type: "fields" }, { type: "fields" })).toBe(true);
});
});
20 changes: 19 additions & 1 deletion src/utils/catan.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NumberChitValue, Hex } from "../types/hexes";
import { NumberChitValue, Hex, HexType } from "../types/hexes";

/**
* Given a valid `NumberChitValue` `num`, return the number of pips that appear
Expand All @@ -21,3 +21,21 @@ export function hexToPipCount(hex: Hex): number {
res += numberToPipCount(hex.secondNumber!);
return res;
}

/**
* Return `true` if `h1` and `h2` are the same terrain type and `false`
* otherwise. With the introduction of river hexes and the oasis from Traders &
* Barbarians, it is no longer sufficient to compare `Hex.type` directly
*/
export function compareHexType(h1: Hex, h2: Hex): boolean {
const mapping: Partial<Record<HexType, HexType>> = {
riverHills: "hills",
riverMountains: "mountains",
riverPasture: "pasture",
oasis: "desert",
};
let [t1, t2] = [h1.type, h2.type];
if (t1 in mapping) t1 = mapping[t1]!;
if (t2 in mapping) t2 = mapping[t2]!;
return t1 === t2;
}

0 comments on commit 2877ee2

Please sign in to comment.