Esempio n. 1
0
  def _readGame(self, debug):
    playerCountChar = self.stream.read(1)
    if len(playerCountChar) == 0:
      return False

    players = []
    teams = []
    playerNames = []

    # Read the game header.
    (playerCount,) = struct.unpack("!B", playerCountChar)
    for i in xrange(playerCount):
      (teamNo, playerNameLen) = struct.unpack("!BH", self.stream.read(3))
      playerNames.append(self.stream.read(playerNameLen))

      player = Player()
      player.number = i
      player.teamNumber = teamNo
      players.append(player)
      
    for teamNo in set(map(lambda player: player.teamNumber, players)):
      team = Team(number = teamNo)
      team.playerNumbers = [player.number
                            for player in players
                            if player.teamNumber == teamNo]
      teams.append(team)

    self.game = Game(None, teams = teams, players = players, debug = debug)
    self.gameStartFn(self.game, playerNames)
    return True
Esempio n. 2
0
games = 10000
debug = False
Constants.DEBUG = False
PerfConstants.SAVE_POPULATION = True


# End configurable parameters

players = []
competitor = 0
for i in range(numPlayers):
  players.append(competitors[competitor]())
  competitor += 1
  if competitor >= len(competitors):
    competitor = 0

scores = {}
gamesPlayed = 0
startTime = time.time()
while True:
  g = Game(players, debug)
  winners = g.play()
  for winner in winners:
    if not scores.has_key(winner):
      scores[winner] = 0
    scores[winner] += 1
  gamesPlayed += 1
  print "Played %d games in %d seconds." % (gamesPlayed, int(time.time() - startTime))

print scores
Esempio n. 3
0
class TranscriptReader(object):
  def __init__(self, path, gameStartFn, handStartFn, moveFn, handEndFn, gameEndFn):
    """Args:
         path: File to read transcripts from.
         gameStartFn: Function to call when a new game is seen.  Function will be passed arguments:
           game: A Game object.
           playerNames: A list of the player names, in player number order.
         handStartFn: Function to call when a hand has begun.  Function will be passed a Game object.
         moveFn: Function to call when a move is seen.  Function will be passed arguments:
           game: A Game object.
           player: Player number of the player making the move.
           drawnCard: Card drawn for this move.
           move: Move object representing the player's action.
         handEndFn: Function to call when all a hand's moves have been seen.  Function will be passed a Game object.
         gameEndFn: Function to call when all a game's hands have been seen.  Function will be passed a Game object.
    """
    self.stream = open(path, "rb")
    self.gameStartFn = gameStartFn
    self.handStartFn = handStartFn
    self.moveFn = moveFn
    self.handEndFn = handEndFn
    self.gameEndFn = gameEndFn

  def read(self, debug = False):
    """Read as much of the transcript as possible."""

    while self._readGame(debug):
      while self._readNextHandStart():
        self.handStartFn(self.game)
        while self._readMove():
          pass
        self.game.computeHandScores()
        self.handEndFn(self.game)
      self.gameEndFn(self.game)

  def _readGame(self, debug):
    playerCountChar = self.stream.read(1)
    if len(playerCountChar) == 0:
      return False

    players = []
    teams = []
    playerNames = []

    # Read the game header.
    (playerCount,) = struct.unpack("!B", playerCountChar)
    for i in xrange(playerCount):
      (teamNo, playerNameLen) = struct.unpack("!BH", self.stream.read(3))
      playerNames.append(self.stream.read(playerNameLen))

      player = Player()
      player.number = i
      player.teamNumber = teamNo
      players.append(player)
      
    for teamNo in set(map(lambda player: player.teamNumber, players)):
      team = Team(number = teamNo)
      team.playerNumbers = [player.number
                            for player in players
                            if player.teamNumber == teamNo]
      teams.append(team)

    self.game = Game(None, teams = teams, players = players, debug = debug)
    self.gameStartFn(self.game, playerNames)
    return True

  def _readNextHandStart(self):
    self.game.resetHandState()
    for playerNo in xrange(len(self.game.players)):
      cards = struct.unpack("!BBBBBB", self.stream.read(6))
      if cards[0] == GAME_OVER_CARD:
        return False
      self.game.players[playerNo].hand = list(cards)
    return True

  def _readMove(self, coupFourre = False, declaredExtension = False):
    (num,) = struct.unpack(MOVE_STRUCT, self.stream.read(2))
    player = num & 0b111
    if player == PLAYERNO_HAND_OVER:
      return False
    elif player == PLAYERNO_SPECIAL_MOVE:
      if num & SPECIAL_MOVE_COUP_FOURRE:
        return self._readMove(coupFourre = True)
      elif num & SPECIAL_MOVE_EXTENSION:
        return self._readMove(declaredExtension = True)

    drawnCard = (num >> 3) & 0b11111
    if drawnCard == NO_DRAW_CARD:
      drawnCard = None

    moveType = (num >> 8) & 0b1
    moveCard = (num >> 9) & 0b11111
    moveTarget = (num >> 14) & 0b11
    move = Move(moveType, moveCard, moveTarget, coupFourre)

    if drawnCard is None:
      self.game.delayedAction = True
    else:
      self.game.players[player].hand.append(drawnCard)

    self.game.players[player].hand.remove(moveCard)

    playerObj = self.game.players[player]
    self.game.handleMove(playerObj,
                         self.game.teams[playerObj.teamNumber],
                         move,
                         forceExtension = declaredExtension)
    self.moveFn(self.game, player, drawnCard, move)
    return True
Esempio n. 4
0
            "Transcript move: %d drew %s %s\n" %
            (player, Cards.cardToString(drawnCard)
             if drawnCard else "nothing", move)),
        lambda *args: stdout.write("Transcript hand end!\n"),
        lambda game: stdout.write("Transcript game end!  Scores: %s\n" % (
            " ".join(map(lambda team: "%d" % team.totalScore, game.teams)))))
    reader.read(debug=True)
    exit(0)

players = []
competitor = 0
for i in range(numPlayers):
    players.append(competitors[competitor]())
    competitor += 1
    if competitor >= len(competitors):
        competitor = 0

scores = {}
transcriptWriter = None
for i in range(games):
    g = Game(players, debug)
    if writeTranscript:
        transcriptWriter = TranscriptWriter(writeTranscript, g)
    winners = g.play()
    for winner in winners:
        if not scores.has_key(winner):
            scores[winner] = 0
        scores[winner] += 1

print scores