Permalink
Browse files

Track endgame conditions more accurately in the test framework

Just like on the server, you win if you have the most fruit in the most
types of fruit.  This ends the game as soon as that happens, or as soon
as a draw is certain.
  • Loading branch information...
1 parent 8ec7ffc commit 9b2468765caee696b96b435a04d3b3bb33a37d85 @a1k0n a1k0n 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
39 assets/js/board.js
@@ -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
12 assets/js/player.js
@@ -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 9b24687

Please sign in to comment.