Skip to content

Commit

Permalink
fix #13
Browse files Browse the repository at this point in the history
  • Loading branch information
duckpunch committed Sep 17, 2015
1 parent 0e91f38 commit 430d5c2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 25 deletions.
32 changes: 30 additions & 2 deletions src/analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ export function group(board, position) {

while (!queue.isEmpty()) {
const current = queue.first();
const more_matching = matchingAdjacentPositions(board, position);
const more_matching = matchingAdjacentPositions(board, current);

queue = queue.rest().union(more_matching.subtract(found));
found = found.add(current);
queue = queue.rest().union(more_matching.subtract(found));
}

return found;
Expand Down Expand Up @@ -176,6 +176,34 @@ export function isLegalMove(board, position, color) {
}


/**
* @private
*/
export function prettyString(board) {
const size = board.get(SIZE_KEY);
let pretty_string = '';

for (var i = 0; i < size; i++) {
for (var j = 0; j < size; j++) {
let color = board.get(List.of(i, j), EMPTY);
switch(color) {
case BLACK:
pretty_string += 'O';
break;
case WHITE:
pretty_string += 'X';
break;
case EMPTY:
pretty_string += '+';
break;
}
}
pretty_string += '\n';
}
return pretty_string;
}


export default {
isLegalMove, oppositeColor, liberties, group
};
24 changes: 2 additions & 22 deletions src/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Map, List} from 'immutable';
import {isPositiveInteger} from './utils';
import {
SIZE_KEY, allPossibleMoves, group, liberties, isLegalMove,
BLACK, WHITE, EMPTY,
BLACK, WHITE, EMPTY, prettyString,
} from './analysis';
import {
emptyBoard, addBlackMove, addWhiteMove, addMove, removeMoves,
Expand Down Expand Up @@ -204,26 +204,6 @@ export class Board {
* @returns {string} ASCII board
*/
toPrettyString() {
const size = this.board_size;
let pretty_string = '';

for (var i = 0; i < this.board_size; i++) {
for (var j = 0; j < this.board_size; j++) {
let color = this.positions.get(List.of(i, j), EMPTY);
switch(color) {
case BLACK:
pretty_string += 'O';
break;
case WHITE:
pretty_string += 'X';
break;
case EMPTY:
pretty_string += '+';
break;
}
}
pretty_string += '\n';
}
return pretty_string;
return prettyString(this.positions);
}
}
1 change: 1 addition & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {isNumber} from 'lodash';


/**
* @private
*/
Expand Down
30 changes: 29 additions & 1 deletion test/test_transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from 'assert';
import {List, Set} from 'immutable';

import {emptyBoard, addMove, removeMoves} from '../src/transforms';
import {SIZE_KEY, BLACK, WHITE} from '../src/analysis';
import {SIZE_KEY, BLACK, WHITE, prettyString} from '../src/analysis';


describe('empty board', function() {
Expand Down Expand Up @@ -71,6 +71,34 @@ describe('add move', function() {
]).equals(Set(new_board.keys()))
);
});

it('kills 3 stone group', function() {
const board = emptyBoard(5)
.set(List.of(0, 0), BLACK)
.set(List.of(0, 1), BLACK)
.set(List.of(0, 2), BLACK)
.set(List.of(1, 0), WHITE)
.set(List.of(1, 1), WHITE)
.set(List.of(1, 2), WHITE)
.set(List.of(2, 0), BLACK)
.set(List.of(2, 1), BLACK)
.set(List.of(2, 2), BLACK);

const new_board = addMove(board, List.of(1, 3), BLACK);

assert.ok(
Set([
SIZE_KEY,
List.of(0, 0),
List.of(0, 1),
List.of(0, 2),
List.of(2, 0),
List.of(2, 1),
List.of(2, 2),
List.of(1, 3),
]).equals(Set(new_board.keys()))
);
});
});


Expand Down

0 comments on commit 430d5c2

Please sign in to comment.