예제 #1
0
파일: player.py 프로젝트: Ceditor/fiveai
 def next_loc(self, checkerboard: CheckerBoard,
              win_length: int) -> Location:
     wins = {}
     state = checkerboard.state
     if len(checkerboard.empty_locations) > 0:
         locations = set(checkerboard.empty_locations)
         for loc in locations:
             res = self.simulate(checkerboard, loc, self.color, self.depth,
                                 win_length)
             checkerboard.state = state
             wins[loc] = res
             print(loc, res)
         return min(wins,
                    key=lambda k: (wins[k][-1 * self.color]) /
                    (sum(wins[k].values())))
     else:
         raise Exception
예제 #2
0
파일: player.py 프로젝트: Ceditor/fiveai
 def simulate(self, checkerboard: CheckerBoard, loc: Location, color: int,
              depth: int, win_length: int):
     res = {1: 0, -1: 0, 0: 0}
     checkerboard.move(color, loc)
     if checkerboard.judge(color, win_length):
         # win the game
         res[color] += 1
         return res
     elif depth == 0:
         # random play
         player = player1 = Player(-color)
         player2 = Player(color)
         while True:
             try:
                 loc = player.next_loc(checkerboard, win_length)
                 checkerboard.move(player.color, loc)
             except:
                 res[0] += 1
                 return res
             if checkerboard.judge(player.color, win_length):
                 res[player.color] += 1
                 return res
             player = player == player1 and player2 or player1
     else:
         state = checkerboard.state
         locations = set(checkerboard.empty_locations)
         if len(locations) > 0:
             for loc in locations:
                 result = self.simulate(checkerboard, loc, -color,
                                        depth - 1, win_length)
                 res[0] += result[0]
                 res[1] += result[1]
                 res[-1] += result[-1]
                 checkerboard.state = state
         else:
             return res
     return res