Skip to content

Commit c0794c7

Browse files
committed
CIFRA: Try to fix playground issue
1 parent 8f8fc9d commit c0794c7

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

src/games/cifra.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface ICifraState extends IAPGameState {
2222
winner: playerid[];
2323
stack: Array<IMoveState>;
2424
startpos: Array<Shade>;
25+
firstChoice?: string;
2526
};
2627

2728
interface IBuffer {
@@ -74,6 +75,7 @@ export class CifraGame extends GameBase {
7475
public results: Array<APMoveResult> = [];
7576
public startpos: Array<Shade>;
7677
private highlights: string[] = [];
78+
public firstChoice?: string;
7779

7880
constructor(state?: ICifraState | string, variants?: string[]) {
7981
super();
@@ -110,6 +112,7 @@ export class CifraGame extends GameBase {
110112
this.winner = [...state.winner];
111113
this.variants = state.variants;
112114
this.stack = [...state.stack];
115+
this.firstChoice = state.firstChoice;
113116
}
114117
this.load();
115118
}
@@ -149,7 +152,7 @@ export class CifraGame extends GameBase {
149152
if (this.gameover) { return []; }
150153

151154
// side choosing
152-
if (this.stack.length === 1) {
155+
if (this.stack.length === 1 || this.firstChoice === undefined) {
153156
return ["light,top", "light,bottom", "dark,top", "dark,bottom","light,left", "light,right", "dark,left", "dark,right"];
154157
}
155158

@@ -197,7 +200,7 @@ export class CifraGame extends GameBase {
197200
}
198201

199202
public getButtons(): ICustomButton[] {
200-
if (this.stack.length === 1) {
203+
if (this.stack.length === 1 || this.firstChoice === undefined) {
201204
return [
202205
{ label: "cifra.lt", move: "light,top" },
203206
{ label: "cifra.lb", move: "light,bottom" },
@@ -213,7 +216,7 @@ export class CifraGame extends GameBase {
213216
}
214217

215218
public randomMove(): string {
216-
if (this.stack.length === 1) {
219+
if (this.stack.length === 1 || this.firstChoice === undefined) {
217220
const shuffled = shuffle(["light,top", "light,bottom", "dark,top", "dark,bottom","light,left", "light,right", "dark,left", "dark,right"]) as string[];
218221
return shuffled[0];
219222
} else if ( (this.variants.includes("sum") || this.variants.includes("king")) && this.stack.length <= 3) {
@@ -250,10 +253,10 @@ export class CifraGame extends GameBase {
250253
}
251254

252255
public getHomeRowCol(p: playerid): {col?: number; row?: number}|undefined {
253-
if (this.stack.length < 2) {
256+
if (this.stack.length < 2 || this.firstChoice === undefined) {
254257
return undefined;
255258
}
256-
const [, position] = this.stack[1].lastmove!.split(",");
259+
const [, position] = this.firstChoice.split(",");
257260
const num1 = (position === "top" || position === "left") ? 0 : this.boardSize - 1;
258261
const num2 = num1 === 0 ? this.boardSize - 1 : 0;
259262
if (position === "top" || position === "bottom") {
@@ -264,28 +267,28 @@ export class CifraGame extends GameBase {
264267
}
265268

266269
public get firstPos(): string|undefined {
267-
if (this.stack.length < 2) {
270+
if (this.stack.length < 2 || this.firstChoice === undefined) {
268271
return undefined;
269272
}
270-
const [, position] = this.stack[1].lastmove!.split(",");
273+
const [, position] = this.firstChoice.split(",");
271274
return position;
272275
}
273276

274277
public getPlayerColour(p: playerid): number|string {
275-
if (this.stack.length === 1) {
278+
if (this.stack.length === 1 || this.firstChoice === undefined) {
276279
return "#808080";
277280
}
278-
const [shade,] = this.stack[1].lastmove!.split(",");
281+
const [shade,] = this.firstChoice.split(",");
279282
const c1 = shade === "light" ? "_context_background" : 2;
280283
const c2 = c1 === 2 ? "_context_background" : 2;
281284
return p === 1 ? c1 : c2;
282285
}
283286

284287
public getPlayerShade(p: playerid): Shade|undefined {
285-
if (this.stack.length === 1) {
288+
if (this.stack.length === 1 || this.firstChoice === undefined) {
286289
return undefined
287290
}
288-
const [shade,] = this.stack[1].lastmove!.split(",");
291+
const [shade,] = this.firstChoice.split(",");
289292
const c1 = shade === "light" ? "L" : "D";
290293
const c2 = c1 === "L" ? "D" : "L";
291294
return p === 1 ? c1 : c2;
@@ -378,7 +381,7 @@ export class CifraGame extends GameBase {
378381

379382
if (m.length === 0) {
380383
let context = "play";
381-
if (this.stack.length === 1) {
384+
if (this.stack.length === 1 || this.firstChoice === undefined) {
382385
context = "choose";
383386
} else if ((this.variants.includes("king") || this.variants.includes("sum")) && this.stack.length <= 2) {
384387
context = "setup";
@@ -390,7 +393,7 @@ export class CifraGame extends GameBase {
390393
}
391394

392395
// setup scenarios first
393-
if (this.stack.length === 1) {
396+
if (this.stack.length === 1 || this.firstChoice === undefined) {
394397
const [shade, pos] = m.split(",")
395398
if ( (shade === "light" || shade === "dark") && (pos === "top" || pos === "bottom" || pos === "left" || pos === "right") ) {
396399
result.valid = true;
@@ -531,7 +534,8 @@ export class CifraGame extends GameBase {
531534
}
532535

533536
// choosing sides
534-
if (this.stack.length === 1) {
537+
if (this.stack.length === 1 || this.firstChoice === undefined) {
538+
this.firstChoice = m;
535539
this.results.push({type: "affiliate", which: m});
536540
// if in default Dash mode, populate the board
537541
if (!this.variants.includes("king") && !this.variants.includes("sum")) {
@@ -706,6 +710,7 @@ export class CifraGame extends GameBase {
706710
winner: [...this.winner],
707711
stack: [...this.stack],
708712
startpos: [...this.startpos],
713+
firstChoice: this.firstChoice,
709714
};
710715
if (opts !== undefined && opts.strip) {
711716
// only strip if we're still in the setup window

0 commit comments

Comments
 (0)