-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.test.ts
129 lines (118 loc) · 3.5 KB
/
index.test.ts
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
import { assert } from "chai";
import Shapes, { CardShape } from "../src/shapes";
import Game from "../src";
import createLogger from "../src/logger";
import Card, { GetWhot } from "../src/card";
import Player from "../src/player";
const logger = createLogger("index.test.js");
const makeRandomPlay = (player: Player, game: Game) => {
if (player.canPlay()) {
const compatibles = player
.hand()
.filter((card) => card.matches(game.pile.top()));
const compatibleCardIndex = player
.hand()
.indexOf(compatibles[Math.floor(Math.random() * compatibles.length)]);
let iNeed: CardShape | undefined = undefined;
if (player.hand()[compatibleCardIndex].shape === Shapes.Whot) {
const eligibleCards = player
.hand()
.filter((card) => card.shape != Shapes.Whot);
iNeed =
(eligibleCards[Math.floor(Math.random() * eligibleCards.length)] || {})
.shape || Shapes.Circle;
}
player.play(compatibleCardIndex, iNeed);
game.turn.execute(game.pile.top());
} else {
const marketCards = player.pick();
game.turn.switch();
}
};
describe("Game", () => {
describe("Constructor", () => {
it("should throw InvalidArgumentTypeError", () => {
try {
const game = new Game({
noOfDecks: 1,
// @ts-ignore
noOfPlayers: null
});
} catch (err: any) {
assert.equal(err.name, "InvalidArgumentTypeError");
assert.equal(err.message, "props.noOfPlayers");
}
});
it("should work", () => {
const game = new Game({
noOfDecks: 2,
noOfPlayers: 4,
});
let endGame = false;
game.emitter.on("player:play", (player, card) => {
logger.log(
player.render(),
"| old:",
game.pile.top().render(),
"| play:",
card.render()
);
});
game.emitter.on("player:market", (player: Player, marketCards: Card[]) => {
logger.warn(
player.render(),
"| old:",
game.pile.top().render(),
"| market:",
marketCards.map((card) => card.render()).join(", ")
);
});
logger.warn(
"Game Start: top:",
game.pile.top().render(),
" | ",
game.turn?.next()?.render()
);
for (let i = 1; i <= 50; i++) {
if (!endGame) {
const nextPlay = game.turn?.next();
if (!nextPlay) {
throw new Error("nextPlay cannot be undefined");
}
makeRandomPlay(nextPlay, game);
}
}
});
it("should have Whot! as its first card", () => {
const game = new Game({
noOfDecks: 1,
noOfPlayers: 2,
firstCard: GetWhot({
value: 20
}),
});
assert.equal(game.pile.top().shape, Shapes.Whot);
});
it("should play !Whot card when whot card is first in the pile", () => {
const game = new Game({
noOfDecks: 1,
noOfPlayers: 2,
firstCard: GetWhot({
value: 20
}),
});
let player = game.turn.next();
while (!player?.canPlay()) {
const marketCards = player?.pick();
game.turn.switch();
player = game.turn.next();
}
const compatibleCardIndex = player
.hand()
.findIndex((card) => Shapes.Whot !== card.shape);
const card = player.hand()[compatibleCardIndex];
assert.notEqual(card.shape, Shapes.Whot);
player.play(compatibleCardIndex);
});
});
});