-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcompetition.py
72 lines (58 loc) · 1.88 KB
/
competition.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import matplotlib
from game import *
from util import *
import graphics
import minimax
def play_game(game, heuristic1, heuristic2):
while not end_game(game.board):
turn = game.turn.color
board = game.board
if has_valid_position(board, turn):
minimax.PLAYER = turn
if turn == "B":
position = minimax.minimax(board, 1, turn, heuristic1)[1]
else:
position = minimax.minimax(board, 1, turn, heuristic2)[1]
game.play(position)
else:
game.change_turn()
return game.winning_side(formatted=False)
def run(p1_name, heuristic1, p2_name, heuristic2, times):
rounds = times
#black
p1_color = "B"
p1_win = 0
#white
p2_color = "W"
p2_win = 0
ties = 0
for i in range(rounds):
print 'Started game %s' % i
game = Game(p1_color="B", p1_mode="C", p2_mode="C")
game.start()
winner = play_game(game, heuristic1, heuristic2)
if winner == p1_color:
p1_win += 1
print '>>>>>>>>>>>>>>>>>>> %s win!' % (p1_name)
elif winner == p2_color:
p2_win += 1
print '>>>>>>>>>>>>>>>>>>> %s win!'% (p2_name)
else:
print 'Tie.'
ties += 1
print 'finished game %s' % i
print 'After %s rounds:\n' % rounds
print '%s: %s ' % (p1_name, p1_win)
print '%s: %s' % (p2_name, p2_win)
print 'ties: %s' % ties
print 'Probabilities:'
print '%s: %s' % (p1_name, str(p1_win/float(rounds)))
print '%s: %s' % (p2_name, str(p2_win/float(rounds)))
print 'ties: %s' % str(ties/float(rounds))
graphics.plot(p1_name, p1_win, p2_name, p2_win, ties)
if __name__ == "__main__":
run(p1_name="weak",
heuristic1=minimax.weak,
p2_name="posicional",
heuristic2=minimax.posicional,
times=20000)