Skip to content

Commit ede9bc4

Browse files
authored
Merge pull request #265 from th555/sunspot
Sunspot update
2 parents 911145d + 2677270 commit ede9bc4

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

src/games/sunspot.ts

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export class SunspotGame extends GameBase {
180180
try {
181181
const cell = this.graph.coords2algebraic(col, row);
182182
let newaction;
183-
if (this.board.has(cell)) {
183+
if (this.board.has(cell) || move.startsWith(cell)) { // Have to accomodate counter-flipping the initially placed stone
184184
newaction = "X" + cell;
185185
} else {
186186
newaction = cell;
@@ -262,24 +262,20 @@ export class SunspotGame extends GameBase {
262262
return [true, newBoard]
263263
}
264264

265-
private placeAction = {
266-
action: this.doPlaceAction
267-
}
268-
269-
private flipAction = {
270-
action: this.doFlipAction
271-
}
272-
273-
private counterFlipAction = {
274-
action: this.doCounterFlipAction
275-
}
276-
277265
/* Conditions */
278266

279267
private checkPlaceIsNotPossible = (board: Map<string, cellcontent>): boolean => {
280268
return board.size === this.graph.graph.order;
281269
}
282270

271+
private checkCounterFlipIsPossible = (board: Map<string, cellcontent>, previousAction: string): boolean => {
272+
if (!previousAction.startsWith('X')) {
273+
return false;
274+
}
275+
const cell = previousAction.slice(1);
276+
return this.isInteriorStoneInCombinedGroup(cell, this.currplayer, board);
277+
}
278+
283279
private checkCounterFlipIsNotPossible = (board: Map<string, cellcontent>, previousAction: string): boolean => {
284280
if (!previousAction.startsWith('X')) {
285281
return false;
@@ -289,6 +285,34 @@ export class SunspotGame extends GameBase {
289285
return !possible;
290286
}
291287

288+
private checkFlipIsPossible = (board: Map<string, cellcontent>): boolean => {
289+
for (const [cell, content] of board) {
290+
if(content === this.otherPlayer() && !this.isEdgeStone(cell, board)){
291+
for(const cell2 of this.graph.neighbours(cell)){
292+
if(this.board.has(cell2) && this.board.get(cell2) === this.otherPlayer()
293+
&& this.isEdgeStone(cell2, board)){
294+
return true;
295+
}
296+
}
297+
}
298+
}
299+
return false;
300+
}
301+
302+
private placeAction = {
303+
action: this.doPlaceAction
304+
}
305+
306+
private flipAction = {
307+
condition: this.checkFlipIsPossible,
308+
action: this.doFlipAction
309+
}
310+
311+
private counterFlipAction = {
312+
condition: this.checkCounterFlipIsPossible,
313+
action: this.doCounterFlipAction
314+
}
315+
292316
private placeIsNotPossible = {
293317
condition: this.checkPlaceIsNotPossible
294318
}
@@ -297,7 +321,6 @@ export class SunspotGame extends GameBase {
297321
condition: this.checkCounterFlipIsNotPossible
298322
}
299323

300-
301324
public validateMove(m: string): IValidationResult {
302325
if (m.length === 0) {
303326
return {

test/games/sunspot.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import "mocha";
44
import { expect } from "chai";
55
import { SunspotGame } from '../../src/games';
66

7-
describe.only("Sunspot", () => {
7+
describe("Sunspot", () => {
88
it("p1 win by encircling an opponent group", () => {
99
const g = [
1010
"b2", "h6",

0 commit comments

Comments
 (0)