Permalink
Browse files

Merge pull request #13 from a1k0n/endgame-fix

Track endgame conditions more accurately in the test framework
  • Loading branch information...
2 parents 8ec7ffc + 9b24687 commit 4fef54c5451aeb14fcd1475e7cdcb684d4e2490c @jennylin jennylin committed Jun 5, 2012
Showing with 35 additions and 16 deletions.
  1. +33 −6 assets/js/board.js
  2. +2 −10 assets/js/player.js
View
@@ -176,15 +176,42 @@ var Board = {
Board.history[Board.oppX][Board.oppY] |= 2;
},
- noMoreItems: function() {
- for (var i=0; i<WIDTH; i++) {
- for (var j=0; j<HEIGHT; j++) {
- if (Board.board[i][j] != 0) {
- return false;
+ checkGameOver: function() {
+ var item_type_score_max = 0,
+ item_type_score_min = 0;
+ var item_types_left = Board.numberOfItemTypes;
+ for (var i=0; i < Board.numberOfItemTypes; i++) {
+ var diff = Board.myBotCollected[i] - Board.simpleBotCollected[i];
+ var numleft = Board.totalItems[i] - Board.myBotCollected[i] - Board.simpleBotCollected[i];
+ var item_score_max = diff + numleft;
+ var item_score_min = diff - numleft;
+ if (item_score_min == 0 && item_score_max == 0) { // tie
+ item_types_left --;
+ } else if (item_score_min >= 0) {
+ item_type_score_max ++; // player 1 could win or tie
+ if (item_score_min > 0) {
+ item_type_score_min ++; // player 1 wins for this type of fruit
+ item_types_left --;
}
+ } else if (item_score_max <= 0) {
+ item_type_score_min --; // player 2 could win or tie
+ if (item_score_max < 0) {
+ item_type_score_max --; // player 2 wins
+ item_types_left --;
+ }
+ } else if(numleft != 0) { // still up in the air
+ item_type_score_min --;
+ item_type_score_max ++;
}
}
- return true;
+ if (item_type_score_max < 0) {
+ return item_type_score_max;
+ } else if (item_type_score_min > 0) {
+ return item_type_score_min;
+ } else if (item_types_left == 0) {
+ return 0;
+ }
+ return;
},
initRandom: function(boardNumber) {
// Create a random number generator (PRNG) for board
View
@@ -86,16 +86,8 @@ var GamePlay = {
GamePlay.drawPlayerOne(ctx, Board.board);
GamePlay.displayScore(ctx, Board.board);
if (GamePlay.mode == "play") {
- if (Board.noMoreItems()) {
- var score = 0;
- for (var i=0; i<GamePlay.itemTypeCount; i++) {
- if (Board.myBotCollected[i] > Board.simpleBotCollected[i]) {
- score = score + 1;
- }
- if (Board.myBotCollected[i] < Board.simpleBotCollected[i]) {
- score = score - 1;
- }
- }
+ var score = Board.checkGameOver();
+ if (score !== undefined) {
if (score > 0) {
ctx.font = "30px Arial";
ctx.fillStyle = "#000";

0 comments on commit 4fef54c

Please sign in to comment.