Permalink
Browse files

Improve performance of checking whether a buy causes a loss

Before cloning the game state, we first check to see if buying the
card in question would cause a pile to empty, and whether the current
player would be in the lead after gaining it.  This cuts the total
Dominiate execution time nearly in half.
  • Loading branch information...
1 parent c6dc60f commit 797a171ab517f7f8c3a1beae44ee81b7c4121051 @bilts bilts committed Feb 14, 2012
Showing with 13 additions and 3 deletions.
  1. +13 −3 gameState.coffee
View
@@ -669,11 +669,21 @@ class State
total
buyCausesToLose: (player, state, card) ->
- if (state.gainsToEndGame() > 1)
+ if not card? || @supply[card] > 1 || state.gainsToEndGame() > 1
return false
- if (not card?)
+
+ # Check to see if the player would be in the lead after buying this card
+ maxOpponentScore = -Infinity
+ for status in this.getFinalStatus()
+ [name, score, turns] = status
+ if name == player.ai.toString()
+ myScore = score + card.getVP(player)
+ else if score > maxOpponentScore
+ maxOpponentScore = score
+
+ if myScore > maxOpponentScore
return false
-
+
# One level of recursion is enough for first
if (this.depth==0)
[hypState, hypMy] = state.hypothetical(player.ai)

0 comments on commit 797a171

Please sign in to comment.