コード例 #1
0
ファイル: testing.py プロジェクト: eswald/parlance
 def test_weighted_choice(self):
     # After a large number of trials,
     # each one should be hit in this approximate ratio.
     choices = {"A": 200, "B": 500, "C": 900}
     results = dict.fromkeys(choices, 0)
     for iteration in xrange(sum(choices.itervalues())):
         results[weighted_choice(choices)] += 1
     
     for key in choices:
         self.failUnlessAlmostEqual(choices[key], results[key], -2)
コード例 #2
0
ファイル: games.py プロジェクト: eswald/parlance
 def generate(self):
     moves = defaultdict(int)
     board = self.game.board
     for x in range(9):
         if board[x]:
             # Only look at blank spaces
             continue
         
         for y in range(9):
             if y == x:
                 continue
             
             for z in range(9):
                 if z in (x, y):
                     continue
                 if x + y + z != 12:
                     # Only look at rows
                     continue
                 
                 # Count the number of rows x participates in,
                 # to make the center worth more.
                 moves[x] += 1
                 
                 if board[y] == board[z]:
                     if board[z] == self.player:
                         moves[x] += 1000
                     elif board[z] == 0:
                         moves[x] += 5
                     else:
                         moves[x] += 500
                 elif board[y] == self.player and board[z] == 0:
                     # Make this better than a blank-blank-blank row
                     moves[x] += 20
     
     self.game.output("%r", moves)
     return weighted_choice(moves)