Skip to content

Commit d41f066

Browse files
authored
Remove duplication in handling puzzle modes (#198)
1 parent 07b9130 commit d41f066

2 files changed

Lines changed: 33 additions & 50 deletions

File tree

functions/src/game.ts

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -170,27 +170,21 @@ function checkSet4Set(a: string, b: string, c: string, d: string) {
170170
return [a, b, c, d];
171171
}
172172

173-
function findSetNormal(deck: string[], gameMode: GameMode, state: FindState) {
173+
function* findSetNormal(deck: string[], gameMode: GameMode, state: FindState) {
174174
const deckSet = new Set(deck);
175175
const first =
176176
modes[gameMode].chain && state.lastSet!.length > 0 ? state.lastSet! : deck;
177-
const foundSets = modes[gameMode].puzzle && state.foundSets!;
178177
for (let i = 0; i < first.length; i++) {
179178
for (let j = first === deck ? i + 1 : 0; j < deck.length; j++) {
180179
const c = conjugateCard(first[i], deck[j]);
181180
if (deckSet.has(c)) {
182-
const set = [first[i], deck[j], c];
183-
if (!(foundSets && foundSets.has(set.sort().join("|")))) {
184-
return set;
185-
}
181+
yield [first[i], deck[j], c];
186182
}
187183
}
188184
}
189-
return null;
190185
}
191186

192-
function findSetUltra(deck: string[], gameMode: GameMode, state: FindState) {
193-
const foundSets = modes[gameMode].puzzle && state.foundSets!;
187+
function* findSetUltra(deck: string[], gameMode: GameMode, state: FindState) {
194188
const cutoff = modes[gameMode].chain ? state.lastSet!.length : 0;
195189
const deckSet = new Set(deck);
196190
let first, second, prevSet;
@@ -213,18 +207,14 @@ function findSetUltra(deck: string[], gameMode: GameMode, state: FindState) {
213207
c2 !== second[j] &&
214208
c2 !== deck[k]
215209
) {
216-
const set = [first[i], second[j], deck[k], c2];
217-
if (!(foundSets && foundSets.has(set.sort().join("|")))) {
218-
return set;
219-
}
210+
yield [first[i], second[j], deck[k], c2];
220211
}
221212
}
222213
}
223214
}
224-
return null;
225215
}
226216

227-
function findSetGhost(deck: string[], gameMode: GameMode, state: FindState) {
217+
function* findSetGhost(deck: string[], gameMode: GameMode, state: FindState) {
228218
for (let i = 0; i < deck.length; i++) {
229219
for (let j = i + 1; j < deck.length; j++) {
230220
for (let k = j + 1; k < deck.length; k++) {
@@ -239,40 +229,41 @@ function findSetGhost(deck: string[], gameMode: GameMode, state: FindState) {
239229
deck[m],
240230
deck[n]
241231
);
242-
if (cand) return cand;
232+
if (cand) yield cand;
243233
}
244234
}
245235
}
246236
}
247237
}
248238
}
249-
return null;
250239
}
251240

252-
function findSet4Set(deck: string[], gameMode: GameMode, state: FindState) {
241+
function* findSet4Set(deck: string[], gameMode: GameMode, state: FindState) {
253242
const deckSet = new Set(deck);
254243
const first =
255244
modes[gameMode].chain && state.lastSet!.length > 0 ? state.lastSet! : deck;
256-
const foundSets = modes[gameMode].puzzle && state.foundSets!;
257245
for (let i = 0; i < first.length; i++) {
258246
for (let j = i + 1; j < first.length; j++) {
259247
for (let k = first === deck ? j + 1 : 0; k < deck.length; k++) {
260248
const c = conjugateCard4Set(first[i], first[j], deck[k]);
261249
if (deckSet.has(c)) {
262-
const set = [first[i], first[j], deck[k], c];
263-
if (!(foundSets && foundSets.has(set.sort().join("|")))) {
264-
return set;
265-
}
250+
yield [first[i], first[j], deck[k], c];
266251
}
267252
}
268253
}
269254
}
270-
return null;
271255
}
272256

273257
/** Find a set in an unordered collection of cards, if any, depending on mode. */
274258
export function findSet(deck: string[], gameMode: GameMode, state: FindState) {
275-
return setTypes[modes[gameMode].setType].findFn(deck, gameMode, state);
259+
const foundSets = modes[gameMode].puzzle && state.foundSets;
260+
const sets = setTypes[modes[gameMode].setType].findFn(deck, gameMode, state);
261+
for (const set of sets) {
262+
if (!(foundSets && foundSets.has(set.sort().join("|")))) {
263+
return set;
264+
}
265+
}
266+
return null;
276267
}
277268

278269
/** Get the array of cards from a GameEvent */

src/game.js

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -174,27 +174,21 @@ export function cardTraits(card) {
174174
};
175175
}
176176

177-
function findSetNormal(deck, gameMode, state) {
177+
function* findSetNormal(deck, gameMode, state) {
178178
const deckSet = new Set(deck);
179179
const first =
180180
modes[gameMode].chain && state.lastSet.length > 0 ? state.lastSet : deck;
181-
const foundSets = modes[gameMode].puzzle && state.foundSets;
182181
for (let i = 0; i < first.length; i++) {
183182
for (let j = first === deck ? i + 1 : 0; j < deck.length; j++) {
184183
const c = conjugateCard(first[i], deck[j]);
185184
if (deckSet.has(c)) {
186-
const set = [first[i], deck[j], c];
187-
if (!(foundSets && foundSets.has(set.sort().join("|")))) {
188-
return set;
189-
}
185+
yield [first[i], deck[j], c];
190186
}
191187
}
192188
}
193-
return null;
194189
}
195190

196-
function findSetUltra(deck, gameMode, state) {
197-
const foundSets = modes[gameMode].puzzle && state.foundSets;
191+
function* findSetUltra(deck, gameMode, state) {
198192
const cutoff = modes[gameMode].chain ? state.lastSet.length : 0;
199193
const deckSet = new Set(deck);
200194
let first, second, prevSet;
@@ -217,18 +211,14 @@ function findSetUltra(deck, gameMode, state) {
217211
c2 !== second[j] &&
218212
c2 !== deck[k]
219213
) {
220-
const set = [first[i], second[j], deck[k], c2];
221-
if (!(foundSets && foundSets.has(set.sort().join("|")))) {
222-
return set;
223-
}
214+
yield [first[i], second[j], deck[k], c2];
224215
}
225216
}
226217
}
227218
}
228-
return null;
229219
}
230220

231-
function findSetGhost(deck, gameMode, state) {
221+
function* findSetGhost(deck, gameMode, state) {
232222
for (let i = 0; i < deck.length; i++) {
233223
for (let j = i + 1; j < deck.length; j++) {
234224
for (let k = j + 1; k < deck.length; k++) {
@@ -243,39 +233,41 @@ function findSetGhost(deck, gameMode, state) {
243233
deck[m],
244234
deck[n]
245235
);
246-
if (cand) return cand;
236+
if (cand) yield cand;
247237
}
248238
}
249239
}
250240
}
251241
}
252242
}
253-
return null;
254243
}
255244

256-
function findSet4Set(deck, gameMode, state) {
245+
function* findSet4Set(deck, gameMode, state) {
257246
const deckSet = new Set(deck);
258247
const first =
259248
modes[gameMode].chain && state.lastSet.length > 0 ? state.lastSet : deck;
260-
const foundSets = modes[gameMode].puzzle && state.foundSets;
261249
for (let i = 0; i < first.length; i++) {
262250
for (let j = i + 1; j < first.length; j++) {
263251
for (let k = first === deck ? j + 1 : 0; k < deck.length; k++) {
264252
const c = conjugateCard4Set(first[i], first[j], deck[k]);
265253
if (deckSet.has(c)) {
266-
const set = [first[i], first[j], deck[k], c];
267-
if (!(foundSets && foundSets.has(set.sort().join("|")))) {
268-
return set;
269-
}
254+
yield [first[i], first[j], deck[k], c];
270255
}
271256
}
272257
}
273258
}
274-
return null;
275259
}
276260

261+
/** Find a set in an unordered collection of cards, if any, depending on mode. */
277262
export function findSet(deck, gameMode, state) {
278-
return setTypes[modes[gameMode].setType].findFn(deck, gameMode, state);
263+
const foundSets = modes[gameMode].puzzle && state.foundSets;
264+
const sets = setTypes[modes[gameMode].setType].findFn(deck, gameMode, state);
265+
for (const set of sets) {
266+
if (!(foundSets && foundSets.has(set.sort().join("|")))) {
267+
return set;
268+
}
269+
}
270+
return null;
279271
}
280272

281273
export function eventFromCards(cards) {

0 commit comments

Comments
 (0)