Skip to content

Commit

Permalink
Added autotester
Browse files Browse the repository at this point in the history
  • Loading branch information
damondoucet committed Nov 30, 2014
1 parent 06a5cc5 commit 66d1882
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 15 deletions.
102 changes: 102 additions & 0 deletions autotest.py
@@ -0,0 +1,102 @@
#!/usr/bin/python

import random
import time
import logging
import src.game as game
from src import game, log
from src.card_decoder.decoder import CardDecoder
from src.strategies.basic_strategy import BasicStrategy
from src.strategies.toy.basic_estimating_strategy import BasicEstimatingStrategy

# Let the number of strategies be N. This will run N^2 * GAMES_PER_PAIR games.
GAMES_PER_PAIR = 4

STRATEGY_TYPES = [
BasicStrategy,
BasicStrategy,
BasicEstimatingStrategy,
BasicEstimatingStrategy
]

NUM_PLAYERS = 2

# Returns a dictionary where key is (i, j) meaning staregy i is first player
# and strategy j is second player (note i can equal j). The value is a tuple
# (p1 wins, p2 wins, ties)
def generate_win_count_matrix(seed = None):
if seed is None:
seed = int(time.time())

card_dictionary = CardDecoder().decode_cards()
win_counts = {}

for i in xrange(len(STRATEGY_TYPES)):
for j in xrange(len(STRATEGY_TYPES)):
a = STRATEGY_TYPES[i]
b = STRATEGY_TYPES[j]

# play GAMES_PER_PAIR games, record win counts
p1_wins = 0
p2_wins = 0
ties = 0

for k in xrange(GAMES_PER_PAIR):
seed += 1
random.seed(seed)

p0 = a(0, NUM_PLAYERS, card_dictionary)
p1 = b(1, NUM_PLAYERS, card_dictionary)
print "Using seed %d for %s (player 0) vs %s (player 1)" % (seed, p0.tag, p1.tag)
victor = game.play_game([p0, p1])
if victor == "tie":
ties += 1
elif victor == "0":
p1_wins += 1
elif victor == "1":
p2_wins += 1
else:
raise Exception("Illegal victor: %s" % victor)

win_counts[(i, j)] = (p1_wins, p2_wins, ties)

return win_counts

if __name__ == "__main__":
log.initialize_logging(logging.ERROR) # don't show log messages

win_counts = generate_win_count_matrix()

COLUMN_WIDTH = 20

# If a (p1 win, p2 win, tie) tuple has three two-digit numbers, it takes 11
# characters to print. Add two spaces to either side and there is a total of
# 15 columns to use.
def pad_tuple(T):
return str(T).center(COLUMN_WIDTH, ' ')

print
print "*" * 50
print
print "Tuples are in the form of (p0 wins, p1 wins, ties)"
print

card_dictionary = CardDecoder().decode_cards()

# Print column headings
print (COLUMN_WIDTH + 3) * ' ',
for strat in STRATEGY_TYPES:
# instantiate to get tag, lol...
tag = strat(0, 1, card_dictionary).tag
print tag.center(COLUMN_WIDTH - 1, ' '),
print

for i in xrange(len(STRATEGY_TYPES)):
# Print row heading
tag = STRATEGY_TYPES[i](0, 1, card_dictionary).tag
print tag.rjust(COLUMN_WIDTH, ' '), '| ',

# Print row
print ''.join(str(win_counts[i, j]).center(COLUMN_WIDTH, ' ')
for j in xrange(len(STRATEGY_TYPES)))

15 changes: 9 additions & 6 deletions main.py
Expand Up @@ -2,6 +2,7 @@

import logging
from src import game, log
from src.card_decoder.decoder import CardDecoder
from src.strategies.basic_strategy import BasicStrategy
from src.strategies.user_strategy.user_strategy import UserStrategy
from src.strategies.toy.basic_estimating_strategy import BasicEstimatingStrategy
Expand All @@ -14,15 +15,17 @@ def main():
log_level = logging.INFO if VERBOSE else logging.ERROR
log.initialize_logging(log_level)

card_dictionary = CardDecoder().decode_cards()

strategies = [
BasicStrategy(0), # player index 0
BasicStrategy(1) # player index 1
# BasicStrategy(0, NUM_PLAYERS, card_dictionary), # player index 0
# BasicStrategy(1, NUM_PLAYERS, card_dictionary) # player index 1

# BasicEstimatingStrategy(0, NUM_PLAYERS),
# BasicEstimatingStrategy(1, NUM_PLAYERS)
BasicEstimatingStrategy(0, NUM_PLAYERS, card_dictionary),
BasicEstimatingStrategy(1, NUM_PLAYERS, card_dictionary)

# UserStrategy(0),
# UserStrategy(1)
# UserStrategy(0, NUM_PLAYERS, card_dictionary),
# UserStrategy(1, NUM_PLAYERS, card_dictionary)
]

victor = game.play_game(strategies)
Expand Down
4 changes: 2 additions & 2 deletions src/strategies/basic_strategy.py
Expand Up @@ -21,8 +21,8 @@
GAIN_POWER_EFFECT = 5

class BasicStrategy(Strategy):
def __init__(self, player_index):
super(BasicStrategy, self).__init__(TAG, player_index)
def __init__(self, player_index, num_players, card_dictionary):
super(BasicStrategy, self).__init__(TAG, player_index, num_players, card_dictionary)

def play_turn(self, board, opponents_previous_moves):
assert board.current_player() == board.players[self.player_index]
Expand Down
6 changes: 4 additions & 2 deletions src/strategies/strategy.py
Expand Up @@ -13,10 +13,12 @@


class Strategy(object):
def __init__(self, tag, player_index):
def __init__(self, tag, player_index, num_players, card_dictionary):
self.tag = tag
self.player_index = player_index
self.card_dictionary = CardDecoder().decode_cards()
self.num_players = num_players

self.card_dictionary = card_dictionary

logger_name = "p%d.%s" % (player_index, tag)
color = PLAYER_INDEX_TO_COLOR[player_index]
Expand Down
5 changes: 2 additions & 3 deletions src/strategies/toy/basic_estimating_strategy.py
Expand Up @@ -21,12 +21,11 @@
TAG = "basic_estimating"

class BasicEstimatingStrategy(Strategy):
def __init__(self, player_index, num_players):
super(BasicEstimatingStrategy, self).__init__(TAG, player_index)
def __init__(self, player_index, num_players, card_dictionary):
super(BasicEstimatingStrategy, self).__init__(TAG, player_index, num_players, card_dictionary)
def make_metric_tracker():
return MetricTracker(num_players * Board.HONOR_PER_PLAYER)

self.num_players = num_players
self.my_tracker = make_metric_tracker()

self.opponent_trackers = defaultdict(make_metric_tracker)
Expand Down
4 changes: 2 additions & 2 deletions src/strategies/user_strategy/user_strategy.py
Expand Up @@ -33,8 +33,8 @@ def read_string():
return s[:len(s) - 1]

class UserStrategy(Strategy):
def __init__(self, player_index):
super(UserStrategy, self).__init__(TAG, player_index)
def __init__(self, player_index, num_players, card_dictionary):
super(UserStrategy, self).__init__(TAG, player_index, num_players, card_dictionary)

def play_card(self, cards_not_played):
targets = {GAIN_RUNES_EFFECT: [], GAIN_HONOR_EFFECT: [], GAIN_POWER_EFFECT: []}
Expand Down

0 comments on commit 66d1882

Please sign in to comment.