"use strict";

var assert = require('assert');

var Board = function(rows) {

    var opposite = function(turn) {
	return turn === 'B' ? 'W' : 'B';
    };

    var isValidMoveLeft = function(i,j,turn) {
	var w = j-1;
	while (w >= 0 && rows[i][w] === opposite(turn))
	    w--;

	if (w >= 0 && w !== j-1)
	    return (rows[i][w] === turn);
	else
	    return false;
    };

    this.isValidMove = function(i,j, turn) {
	if (rows[i][j] !== '-')
	    return false;

	return isValidMoveLeft(i,j,turn);

	return false;
    };

    this.legalMoves = function(turn) {

	var moves = [];

	for (var i=0;i<rows.length;++i)
	    for (var j=0;j<rows[i].length;++j)
		if (this.isValidMove(i,j,turn)) 
		    moves.push({row: i, col: j});

	return moves;
    };

    return this;
};

var createBoard = function(rows) {
    return new Board(rows);
};

describe('reversi', function() {
    it('should create a board', function() {
	var board = createBoard(['BW','BW']);
	assert(board !== undefined);
    });

    it('should return no valid moves', function() {
	var board = createBoard(['BW',
				 'BW']);

	assert.equal(0, board.legalMoves('B').length);
    });

    it('should return the single valid move (left)', function() {
	var board = createBoard(['BW-',
				 'BBB',
				 'BBB']);

	assert.equal(1, board.legalMoves('B').length);
    });

    it('should not return any valid moves (left)', function() {
	var board = createBoard(['BW-',
				 'BBB',
				 'BBB']);

	assert.equal(0, board.legalMoves('W').length);
    });

    it('should return the single valid move (right)', function() {
	var board = createBoard(['-WB',
				 'BBB',
				 'BBB']);

	assert.equal(1, board.legalMoves('B').length);
    });

    it('should not return any valid moves (right)', function() {
	var board = createBoard(['-WB',
				 'BBB',
				 'BBB']);

	assert.equal(0, board.legalMoves('W').length);
    });

});

@@ -356,32 +356,273 @@
  false;%0A    %7D;%0A%0A
+    var isValidMoveRight = function(i,j,turn) %7B%0A%09var w = j+1;%0A%09while (w %3C rows%5Bi%5D.length && rows%5Bi%5D%5Bw%5D === opposite(turn))%0A%09    w++;%0A%0A%09if (w %3C rows%5Bi%5D.length && w !== j+1)%0A%09    return (rows%5Bi%5D%5Bw%5D === turn);%0A%09else%0A%09    return false;%0A    %7D;%0A%0A%0A
     this.isValid
@@ -732,24 +732,46 @@
 urn)
-;%0A%0A%09return false
+ %7C%7C%0A%09       isValidMoveRight(i,j,turn)
 ;%0A  

make tests pass