Skip to content

Commit

Permalink
Return who played
Browse files Browse the repository at this point in the history
  • Loading branch information
ghay committed Oct 19, 2011
1 parent fab4af3 commit 6247827
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
16 changes: 11 additions & 5 deletions scripts/controller.js
Expand Up @@ -9,8 +9,8 @@ TICTACTOE.controller = function(umpire){
board = [[],[],[]];
turnsPlayed = 0;
gameOver = false;
currentPlayer = "0";
return "Player 0's turn";
currentPlayer = "0";
return { message : "Player 0's turn" };
},

play : function(x, y) {
Expand All @@ -33,19 +33,25 @@ TICTACTOE.controller = function(umpire){

board[x][y] = currentPlayer;

var result = { playedBy : currentPlayer };

var hasAWinner = umpire.checkForWinner(board);
if (hasAWinner) {
gameOver = true;
return "Player " + currentPlayer + " rules!";
result.message = "Player " + currentPlayer + " rules!";
return result;
}

if (turnsPlayed >= 9){
gameOver = true;
return "Game is a draw";
result.message = "Game is a draw";
return result;
}

currentPlayer = currentPlayer === "0" ? "X" : "0";
return "Player " + currentPlayer + "'s turn";

result.message = "Player " + currentPlayer + "'s turn";
return result;
}
};
};
25 changes: 16 additions & 9 deletions unit-tests/spec/ControllerSpec.js
Expand Up @@ -21,17 +21,24 @@

it("should return 'Player X's turn' if it's player X's turn next", function() {
var result = controller.play(1, 1);
expect(result).toEqual("Player X's turn");
expect(result.message).toEqual("Player X's turn");
});

it("should return 'Player 0's turn' if it's player 0's turn next", function() {
var result = controller.play(1, 1);
result = controller.play(1, 2);
expect(result).toEqual("Player 0's turn");
expect(result.message).toEqual("Player 0's turn");
});

});

describe("returns played by", function () {
it("should return who played, innit", function() {
var result = controller.play(1, 1);
expect(result.playedBy).toEqual("0");
});
});

describe("draw", function(){
it("should return expected message if game is drawn", function(){
var result;
Expand All @@ -40,7 +47,7 @@
result = controller.play(i, j);
}
}
expect(result).toEqual("Game is a draw");
expect(result.message).toEqual("Game is a draw");
});
});

Expand All @@ -57,10 +64,10 @@

it("should set current player to 0 when game is reset", function(){
controller.play(1,2);
var currentPlayer = controller.reset();
expect(currentPlayer).toEqual("Player 0's turn");
currentPlayer = controller.play(2,2);
expect(currentPlayer).toEqual("Player X's turn");
var result = controller.reset();
expect(result.message).toEqual("Player 0's turn");
result = controller.play(2,2);
expect(result.message).toEqual("Player X's turn");
});
});

Expand Down Expand Up @@ -125,7 +132,7 @@
return true;
}
var result = controller.play(0, 0);
expect(result).toEqual("Player 0 rules!");
expect(result.message).toEqual("Player 0 rules!");
});

it("asks the umpire if there's a winner after the 2nd play is recorded", function(){
Expand All @@ -134,7 +141,7 @@
return true;
}
var result = controller.play(1, 1);
expect(result).toEqual("Player X rules!");
expect(result.message).toEqual("Player X rules!");
});
});

Expand Down

0 comments on commit 6247827

Please sign in to comment.