当前位置: 首页>>代码示例>>Python>>正文


Python State.result方法代码示例

本文整理汇总了Python中State.State.result方法的典型用法代码示例。如果您正苦于以下问题:Python State.result方法的具体用法?Python State.result怎么用?Python State.result使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在State.State的用法示例。


在下文中一共展示了State.result方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from State import State [as 别名]
# 或者: from State.State import result [as 别名]
class Driver:
    def __init__(self, Alpha, Beta, Depth, Ratio, PruningType, FF, CC):
        self.Alpha = Alpha
        self.Beta  = Beta 
        self.Depth = Depth
        self.Ratio = Ratio
        if PruningType.lower() in ["co","cuttingoff"]:
            self.AI1 = CuttingOff(self.Alpha, self.Beta, self.Depth)
        elif PruningType.lower() in ["fc","forwardpruning"]:
            self.AI1 = RandomForwardPruning(self.Alpha, self.Beta, self.Depth, self.Ratio)
        else:
            print("Error: {0} is not a valid Pruning Type".format(PruningType))
            sys.exit(1)
        if CC:
            self.AI1 = CuttingOff(self.Alpha, self.Beta, self.Depth)
            self.AI2 = CuttingOff(self.Alpha, self.Beta, self.Depth)
        elif FF:
            self.AI1 = RandomForwardPruning(self.Alpha, self.Beta, self.Depth, self.Ratio)
            self.AI2 = RandomForwardPruning(self.Alpha, self.Beta, self.Depth, self.Ratio)
        else:
            self.AI1 = CuttingOff(self.Alpha, self.Beta, self.Depth)
            self.AI2 = RandomForwardPruning(self.Alpha, self.Beta, self.Depth, self.Ratio)
            
        self.__construct__()
            
    def __construct__(self):
        w =  [0,0,0,0,0,0,5,0,3,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,2] 
        r =  [0,2,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,3,0,5,0,0,0,0,0]
        self.state = State(r,w)
    
    def start(self):
        #Initial game sequence
        input("Press Enter to Begin...")
        not_set = True
        player  = ""
        while not_set:
            print("Select your color...(R or W)")
            player = input("Red or White? ")
            if player.lower() in ["r","red"]:
                self.player = 0
                self.enemy  = 1
                not_set = False
            elif player.lower() in ["w","white"]:
                self.player = 1
                self.enemy  = 0
                not_set = False
            else:
                print("{0} is not a valid choice".format(player))
        
        
        #Print the board for the player to see!
        writeBoard(self.state.redBoard, self.state.whiteBoard)
        tie = True
        dice = [0, 0]
        input("Press enter to roll the dice and see who goes first...")
        #Loop until somebody wins the dice roll
        while tie:
            dice = [random.randint(1,6),random.randint(1,6)]
            print("\n\nYour roll:  {0}\nTheir roll: {1}".format(dice[0],dice[1]))
            if dice[0] != dice[1]:
                tie = False
            else:
                input("It's a tie! Press Enter to reroll...")
        #if AI's die was higher, have them go first
        if dice[1] > dice[0]:
            print("They go first...")
            #AI Action
            print("Their Roll: {0}".format(', '.join([str(x) for x in dice])))
            dice            = [random.randint(1,6), random.randint(1,6)]
            action          = self.AI1.Search(self.state, self.enemy, dice)
            self.state.result(action, self.enemy)
            print("Computer has performed the following moves: {0}".format(action))
        else:
            print("You go first...")
        
        #Begin Game Loop with player starting
        game_not_over = True
        while game_not_over:
            if self.state.isGameOver():
                not_game_over = False
                break
            #Player Input Loop and values
            dice            = [random.randint(1,6),random.randint(1,6)]
            invalid = True
            moves = 2
            while moves > 0:
                possible_moves = []
                for die in dice:
                    possible_moves += self.state.moves(die, self.player)
                if self.state.isGameOver():
                    not_game_over = False
                    break
                #Print the board for the player to see!
                writeBoard(self.state.redBoard, self.state.whiteBoard)
                if len(possible_moves) == 0:
                    print("No valid moves exist...passing turn")
                    break
                print("Your Roll: {0}".format(', '.join([str(x) for x in dice])))
                str_i = input("Which piece would like to move? ")
                str_j = input("To where would you like to move it? ")
#.........这里部分代码省略.........
开发者ID:DnLKnR,项目名称:Backgammon_AI,代码行数:103,代码来源:Driver.py

示例2: __init__

# 需要导入模块: from State import State [as 别名]
# 或者: from State.State import result [as 别名]
class Run_Time:
    def __init__(self,alpha,beta,depth,ratio,runs,FF,CC):
        self.runs = runs
        self.Alpha = alpha
        self.Beta  = beta
        self.Depth = depth
        self.Ratio = ratio
        self.names = []
        if CC:
            self.AI1 = CuttingOff(self.Alpha, self.Beta, self.Depth)
            self.AI2 = CuttingOff(self.Alpha, self.Beta, self.Depth)
            self.names = ["CuttingOff", "CuttingOff"]
        elif FF:
            self.AI1 = RandomForwardPruning(self.Alpha, self.Beta, self.Depth, self.Ratio)
            self.AI2 = RandomForwardPruning(self.Alpha, self.Beta, self.Depth, self.Ratio)
            self.names = ["ForwardPruning","ForwardPruning"]
        else:
            self.AI1 = CuttingOff(self.Alpha, self.Beta, self.Depth)
            self.AI2 = RandomForwardPruning(self.Alpha, self.Beta, self.Depth, self.Ratio)
            self.names = ["CuttingOff","ForwardPruning"]
            
    def __construct__(self):
        w =  [0,0,0,0,0,0,5,0,3,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,2] 
        r =  [0,2,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,3,0,5,0,0,0,0,0]
        self.state = State(r,w)
    
    def faceoff(self):
        self.__construct__()
        self.player = 0
        self.enemy  = 1
        moves_r,moves_w,times_r,times_w = 0,0,0,0
        not_game_over = True
        while not_game_over:
            if self.state.isGameOver():
                not_game_over = False
                break
            #AI1 Action
            dice            = [random.randint(1,6), random.randint(1,6)]
            start = time.time()
            action1          = self.AI1.Search(self.state, self.player, dice)
            end   = time.time()
            moves_r += 1
            times_r  += (end - start)
            if action1 != None:
                self.state.result(action1, self.enemy)
            #Check if the game is over
            if self.state.isGameOver():
                not_game_over = False
                break
            #AI2 Action
            dice            = [random.randint(1,6), random.randint(1,6)]
            start = time.time()
            action2          = self.AI2.Search(self.state, self.enemy, dice)
            end   = time.time()
            moves_w += 1
            times_w  += (end - start)
            if action2 != None:
                self.state.result(action2, self.enemy)
        
        return (moves_r,moves_w,times_r,times_w)
    
    def get_analysis(self):
        r = 0
        w = 0
        moves = [0,0]
        times = [0,0]
        runs = self.runs
        while runs > 0:
            moves_r,moves_w,total_r,total_w = self.faceoff()
            print("Game {0} completed for Run-Time Test".format(self.runs - (runs - 1)))
            moves[0] += moves_r
            moves[1] += moves_w
            times[0] += total_r
            times[1] += total_w
            runs -= 1
        
        ## COMPUTE TOTAL AVG RUNTIME AND AVG MOVE TIME FOR EACH ##
        avg_tot_time = [times[0]/self.runs,times[1]/self.runs]
        avg_mov_time = [times[0]/moves[0],times[1]/moves[1]]
        
        ## CREATE OUTPUT RUN-TIME ANALYSIS ##
        analysis =  ' vs. '.join(self.names)+ " Run-Time Analysis\n\n"
        analysis += "\tAverage Total Time over " + str(self.runs) + " runs:\n"
        analysis += "\t\t" + self.names[0] + ":\t" + str(avg_tot_time[0]) + "\n"
        analysis += "\t\t" + self.names[1] + ":\t" + str(avg_tot_time[1])  + "\n\n"
        analysis += "\tAverage Time per Move:\n"
        analysis += "\t\t" + self.names[0] + ":\t" + str(avg_mov_time[0]) + "\n"
        analysis += "\t\t" + self.names[1] + ":\t" + str(avg_mov_time[1])  + "\n\n"
        
        return analysis
开发者ID:DnLKnR,项目名称:Backgammon_AI,代码行数:92,代码来源:Test.py


注:本文中的State.State.result方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。