Пример #1
0
def main():
    ### debug options (see https://docs.python.org/2/howto/logging.html) ###
    if debugMode:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.ERROR) 
    #logging.basicConfig(filename='Debug.log', level=logging.DEBUG)

    
    ### initialization part ###
    logging.debug( "Initialization" )
    
    
    logging.debug( "Parsed file paths\n%s\n%s\n%s\n%s\nCreate History=%s" % (pathToInitialBoard, pathToPlayerFile, pathToConfigFile, pathToOutputDirectory, history) )
                 
    
    # parse config file
    configs = parseConfigFile( pathToConfigFile )
    logging.debug("Parsed config file %s\nParameters are stopCriterion:%s\tNoOfCoins:%s\tWaitingTime:%s\n" % (pathToConfigFile,configs.stopCriterion,configs.numberOfCoins,configs.waitingTime))
    
    # construct initial board by reading in board and player files
    board = Board()
    board.init( pathToInitialBoard, configs, pathToPlayerFile)
    rememberedBoards = list()
    
    # init gui
    if displayGUI:
        guiGame = GUI(onGridInitialized)
        guiGame.Map = board.matrix
        guiGame.start()
        # waiting for gui to be initialized
        print("waiting for gui to be initialized...")
        while(not IsGridInitialized):
            time.sleep(0.1)
        #draw legend
        guiGame.updateCoinsRemaining(int(configs.numberOfCoins))
    else:
        # guiGame should be None, such that we can call the update functions of the module board
        guiGame = None
        
    for i in range(len(board.players)):
        playerName = board.players[i][1]
        playerName = os.path.basename(playerName)
        if guiGame != None:
            guiGame.addPlayerToLegend(i,playerName)
                
    #add players to gui
    #draw player
    for k in board.playerInfo.keys():
        player = board.playerInfo[k]
        playerIndex = board.players.index((player.Name,player.FilePath))
        position = player.Position
        if guiGame != None:
            guiGame.addPlayerToCell(position, playerIndex)
        
    # set first coin on board and draw it to gui
    board.placeNewCoinIfNothingOnBoard(guiGame)
    
    #set outputPaths
    boardFile = os.path.join(pathToOutputDirectory, "boardCurrent.txt")
    historyFile = os.path.join(pathToOutputDirectory, "history.txt")
        
    ###  iteratitive rounds part ###
    logging.debug( "Iterative rounds" )
    while( not gameIsFinished( configs, board ) ):
        logging.debug( "nextRound")
        board.initRound()

        # determine order of players in this round
        playersSequence = [player for player, path in board.getPlayersSequence()]
        logging.debug( "Player sequence: " + ','.join(playersSequence) )
        
        # write out current board with all information (board, players, player sequence, coins per player, remaining coins, general playing configs)
        writeCurrentBoard(boardFile, board, configs)
        
        #f=raw_input()
        # forget last history
        #del rememberedBoards[:] # why should we forget the last history, ji would suggest we store every board until the game is finished in rememberedBoards
        
        for aPlayer in playersSequence:
            board.placeNewCoinIfNothingOnBoard(guiGame)
            
            logging.debug( "Player\n  " + str(aPlayer) )
            
            # remember start time of this round
            start = time.time()
            
            # ask player for her move
            move = board.requestPlayersMove( aPlayer, configs, boardFile, historyFile)
            logging.debug( "Move\n  " + str(move) )
        
            # perform player's move and consider all possible cases: moving to empty field, water, mountain, other player, coin
            board.movePlayer( aPlayer, move, guiGame)
            
            # remember updated board for writing it to history file after the round is finished
            rememberedBoards.append( board.copy() )
            
            # draw updated board
            if not displayGUI:
                clearScreen()
                writeCurrentBoardToSTDOUT(board, configs)
            
            # wait if necessary to enable watching the game in real time
            #   time.sleep would not waste cpu resources but is less accurate
            end = time.time()
            while end - start < float(configs.waitingTime) / 1000000.:
                end = time.time()
        
        
        board.distributeCoins(board.coinsOfDrownedPlayers, guiGame)
        board.coinsOfDrownedPlayers = 0                   
        
        # if a player died, place her onto the board again after all player's moves are finished and then distribute her coins
        if board.containsSuspendedPlayers():
            pass
            
        # write out history (like current board) with all remembered intermediate states of the board
        if(history == "1"):
            writeHistory(historyFile, rememberedBoards, configs) #here we write the whole board history to the file
        
        pass
        
    
    
    
    ### finishing part ###
    logging.debug( "Initialization" )