def __init__(self, players, board=None, tiles=None): self.players = players self.board = TsuroBoard() if board is None else board self.tiles = [TsuroTile(i, tileInfo) for i, tileInfo in enumerate(tileData)] if tiles is None else tiles
class TsuroGame(object): def __init__(self, players, board=None, tiles=None): self.players = players self.board = TsuroBoard() if board is None else board self.tiles = [TsuroTile(i, tileInfo) for i, tileInfo in enumerate(tileData)] if tiles is None else tiles def transform(self, tile, location, boardOnly=False): #print "Transforming by placing tile %d at %s" % (tile.index, str(location)) newBoard = copy.deepcopy(self.board) newBoard.placeTile(tile, location) if boardOnly: return newBoard else: newGame = self.cloneGame(copy.deepcopy(self.players), newBoard, copy.deepcopy(self.tiles)) newGame.moveAllPlayers() return newGame def playCard(self, tile, location): self.board = self.transform(tile, location, boardOnly=True) def gameOver(self): livingPlayerCount = len(self.activePlayers()) #print "there are %d living players out of %d total" % (livingPlayerCount, len(self.players)) return livingPlayerCount == 1 or livingPlayerCount == 0 or self.board.isFull() def playTurn(self, turn): print "--------------------------------------------------------" print "it is turn %d" % turn for player in self.players: print "%d: %s" % (player.id, (str([tile.index for tile in player.hand])) if player.alive() else "DEAD") print "--------------------------------------------------------" currentPlayer = self.players[turn % len(self.players)] print "player %d who is at %s" % (currentPlayer.id, str(currentPlayer.position)) if not currentPlayer.alive(): print "Player %d is eliminated, skipping their turn" % currentPlayer.id return 1 else: print "Their hand:" print currentPlayer.print_hand() print "selecting a tile \n-----------------------------" selectedTile = currentPlayer.play(turn) print "------------------------------" print "the selected tile is %d rotated %d times" % (selectedTile.index, selectedTile.rotation) #print "the base card paths are %s" % TsuroTilesAllTiles[selectedTile.index].easyPrint() #print "The rotated paths are: %s" % selectedTile.easyPrint() if self.isSuicide(currentPlayer, selectedTile): print "this is suicide :( " else: print "this is not suicide, good move" self.playCard(selectedTile, currentPlayer.play_position()) self.board.printBoard() print "moving all players" self.moveAllPlayers(report=True) print "wrapping up turn" self.wrapUpTurn(currentPlayer) print "--------------------------------------------------------------" #raw_input("<>") def printDeck(self): print "Deck remaining: %d cards" % len(self.tiles) print str([tile.index for tile in self.tiles]) def isSuicide(self, player, tile): tileLocation = (player.position[0] + positions_adders[player.position[2]][0], player.position[1] + positions_adders[player.position[2]][1]) tryBoard = self.transform(tile, tileLocation, boardOnly=True) testLocation = tryBoard.followPath(player.position, player.position[2]) return not testLocation[0] def moveAllPlayers(self, report=False): for player in self.activePlayers(): newLocation = self.board.followPath(player.position, player.position[2]) if report: print "player %d has moved from %s to %s" % (player.id, str(player.position), str(newLocation)) player.position = (newLocation[1][0], newLocation[1][1], newLocation[2]) if not newLocation[0]: if report: print "Player %d has been eliminated" % player.id self.handlePlayerDeath(player, report) def activePlayers(self): return [livingPlayer for livingPlayer in self.players if livingPlayer.alive()] def active_players(self): return self.activePlayers() """ Anything after a turn, but mostly for drawing cards. Be sure to check whether or not the player is dead, because if a player is killed on his own turn, we will still call this on him """ def wrapUpTurn(self, player): raise NotImplementedError("Should be overwritten") """ When a player dies, this function will be called. Deal with handling recollection of tiles etc. """ def handlePlayerDeath(self, player): raise NotImplementedError("Should be overwritten")