-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
189 lines (167 loc) · 5.75 KB
/
index.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
export type MancalaTurn = {
player: number;
pocketIndex: number;
explanation: string;
};
export type MancalaResolution = {
winner: number | null;
scores: [number, number];
};
export type MancalaTurnStep = {
// prettier-ignore
pockets: [
number, number, number, number, number, number,
number, number, number, number, number, number,
];
stores: [number, number];
};
export type MancalaGameState = {
// prettier-ignore
pockets: [
number, number, number, number, number, number,
number, number, number, number, number, number,
];
stores: [number, number];
activePlayer: 0 | 1;
turns: Array<MancalaTurn>;
lastTurnSteps: Array<MancalaTurnStep>;
resolution?: MancalaResolution;
};
const NEW_GAME: MancalaGameState = {
pockets: [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
stores: [0, 0],
activePlayer: 0,
turns: [],
lastTurnSteps: [],
};
export function newMancalaGameState(): MancalaGameState {
return structuredClone(NEW_GAME);
}
exports.newMancalaGameState = newMancalaGameState;
export function applyTurnGameState(
incomingGameState: MancalaGameState,
pocketIdx: number
): MancalaGameState {
if (!playerCanMakeMove(incomingGameState, pocketIdx)) {
throw new Error(
`IllegalMoveError - Player ${incomingGameState.activePlayer} cannot select pocket ${pocketIdx}`
);
}
const outgoingGameState = structuredClone(incomingGameState);
outgoingGameState.lastTurnSteps = [];
const turn: MancalaTurn = {
player: incomingGameState.activePlayer,
pocketIndex: pocketIdx,
explanation: "",
};
let numberOfSeeds = outgoingGameState.pockets[pocketIdx];
let whatJustHappened = "";
let shouldSwitchActivePlayer = true;
// take all the stones from the selcected pocket
outgoingGameState.pockets[pocketIdx] = 0;
while (numberOfSeeds > 0) {
// should we add it to the active player's store?
if (
(pocketIdx === 0 && outgoingGameState.activePlayer === 0) ||
(pocketIdx === 6 && outgoingGameState.activePlayer === 1)
) {
numberOfSeeds -= 1;
outgoingGameState.stores[outgoingGameState.activePlayer] += 1;
outgoingGameState.lastTurnSteps.push({
// save it in the steps
pockets: [...outgoingGameState.pockets],
stores: [...outgoingGameState.stores],
});
// we landed on the AP's store, so they get to go again
if (numberOfSeeds === 0) {
whatJustHappened = `Player ${
outgoingGameState.activePlayer + 1
}'s last seed went into their mancala, they get to go again.`;
shouldSwitchActivePlayer = false;
continue;
}
}
pocketIdx = pocketIdx === 0 ? 11 : pocketIdx - 1;
numberOfSeeds -= 1;
outgoingGameState.pockets[pocketIdx] += 1;
outgoingGameState.lastTurnSteps.push({
// save it in the steps
pockets: [...outgoingGameState.pockets],
stores: [...outgoingGameState.stores],
});
// if we added to an empty pocket take from the opposite pocket
if (numberOfSeeds === 0 && outgoingGameState.pockets[pocketIdx] === 1) {
const oppositeIdx = Math.abs(pocketIdx - 11);
const newStore =
1 +
outgoingGameState.stores[outgoingGameState.activePlayer] +
outgoingGameState.pockets[oppositeIdx];
whatJustHappened = `Player ${
outgoingGameState.activePlayer + 1
}'s last seed went into an empty pocket, they get to keep that seed and they capture seeds from the opposite pocket.`;
outgoingGameState.stores[outgoingGameState.activePlayer] = newStore;
outgoingGameState.pockets[pocketIdx] = 0;
outgoingGameState.lastTurnSteps.push({
// save it in the steps
pockets: [...outgoingGameState.pockets],
stores: [...outgoingGameState.stores],
});
outgoingGameState.pockets[oppositeIdx] = 0;
outgoingGameState.lastTurnSteps.push({
// save it in the steps
pockets: [...outgoingGameState.pockets],
stores: [...outgoingGameState.stores],
});
}
}
// end the game if either side is all 0
const postUpdateGameOver = isGameOver(outgoingGameState.pockets);
if (postUpdateGameOver) {
// add all pocketedStones to the proper store
const adds = [0, 0];
for (let i = 0; i < outgoingGameState.pockets.length; i++) {
adds[i < 6 ? 0 : 1] += outgoingGameState.pockets[i];
outgoingGameState.pockets[i] = 0;
}
outgoingGameState.stores[0] += adds[0];
outgoingGameState.stores[1] += adds[1];
outgoingGameState.lastTurnSteps.push({
// save it in the steps
pockets: [...outgoingGameState.pockets],
stores: [...outgoingGameState.stores],
});
whatJustHappened = `Player 1 added ${adds[0]} seed from their side, and Player 2 added ${adds[1]} seeds from their side.`;
outgoingGameState.resolution = {
scores: [...outgoingGameState.stores],
winner:
outgoingGameState.stores[0] === outgoingGameState.stores[1]
? null
: outgoingGameState.stores[0] > outgoingGameState.stores[1]
? 0
: 1,
};
} else if (shouldSwitchActivePlayer) {
outgoingGameState.activePlayer =
outgoingGameState.activePlayer === 0 ? 1 : 0;
}
turn.explanation = whatJustHappened;
outgoingGameState.turns.push(turn);
return outgoingGameState;
}
exports.applyTurnGameState = applyTurnGameState;
export function isGameOver(pockets: Array<number>): boolean {
return (
pockets.slice(0, 6).every((val) => val === 0) ||
pockets.slice(6).every((val) => val === 0)
);
}
exports.isGameOver = isGameOver;
export function playerCanMakeMove(
gameState: MancalaGameState,
pocketIndex: number
): boolean {
return gameState.pockets[pocketIndex] > 0 && gameState.activePlayer === 0
? pocketIndex < 6
: pocketIndex > 5;
}
exports.playerCanMakeMove = playerCanMakeMove;