Example #1
0
File: ai.py Project: quilan1/TM
    def recBestMove(self, gameState, evalAsPlayer, options, depth=0, st=""):
        if depth == 0: return [0,None,None,gameState];

        arr = [];
        for commandList in options.enumerateAll(deferToPlayer=evalAsPlayer):
            newGameState = GameState();
            newGameState.copyState(gameState);
            outputList = [];
            anyChoices = False;
            for i,command in enumerate(commandList):
                if isinstance(command, Command):
                    outputList.append(command);
                    newGameState.processCommand(command.command);
                    continue;
                #it's a CommandOption
                score, _, chosenList, newGameState = self.recBestMove(newGameState, command.evalAsPlayer, command, depth=1);
                outputList.extend(chosenList);
                anyChoices = True;

            score = self.evaluateGameState(evalAsPlayer, newGameState);
            if anyChoices == False: del outputList; outputList = commandList;
            arr.append( [score, commandList, outputList, newGameState] );

        arr.sort(reverse=True);

        if depth > 1:
            for i in xrange(len(arr)):
                oldGameState = arr[i][-1];
                if oldGameState.currentPlayer == gameState.currentPlayer: oldGameState.nextPlayer();

                options = oldGameState.generateOperations(oldGameState.currentPlayer);
                _, _, _, newGameState = self.recBestMove(oldGameState, oldGameState.currentPlayer, options, depth-1);
                if self.keepCalculating() == False: break;
                if oldGameState == newGameState: continue;
                arr[i][0] = self.evaluateGameState(evalAsPlayer, newGameState, display=False);
            arr.sort(reverse=True);

        return arr[0];
Example #2
0
class TerraMystica(object):
    def __init__(self):
        self.gameState = GameState();
        self.worldMap = worldMap;

    def gameLoop(self, log=False, logToFile=""):
        self.gameState.logComment("\n", False, logToFile);

        while True:
            #Game is over after 6 turns
            if self.gameState.currentTurn >= 6: break;

            results = self.gameState.generateOperations(self.gameState.currentPlayer);
            print "== Turn %s =="%(self.gameState.currentTurn);
            for playerNumber, player in sorted(self.gameState.players.items()): print "%s %s\t%s"%(playerNumber, "**" if self.gameState.currentPlayer == playerNumber else "", player);
            print;
            print UI(self.gameState, (UI.DisplayWorld,[]));

            earlierTurn = self.gameState.currentTurn;
            if self.gameState.players[self.gameState.currentPlayer].isComputer:
                clist = AI(timeout=30.0).calculateBestMove(self.gameState);
                for command in clist:
                    if isinstance(command, list):
                        self.gameState.processCommand(command, log=False, logToFile=logToFile);
                        continue;

                    print;
                    _, clist = UI(self.gameState).navigateCommandMenu(command);
                    for subcommand in clist: self.gameState.processCommand(subcommand, log=False, logToFile=logToFile);
            else:
                _, clist = UI(self.gameState).navigateCommandMenu(results);
                for command in clist: self.gameState.processCommand(command, log=False, logToFile=logToFile);

            self.gameState.logComment("\n", False, outputFile);

            #Hack to ensure we have the correct player action rotations
            if earlierTurn == self.gameState.currentTurn: self.gameState.nextPlayer();

        print "Game Over!";