Skip to content

Commit

Permalink
WeightedRandomPlayer
Browse files Browse the repository at this point in the history
  • Loading branch information
bcollazo committed Oct 26, 2020
1 parent f1d49d3 commit b9679f5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
Empty file added catanatron/players/__init__.py
Empty file.
22 changes: 22 additions & 0 deletions catanatron/players/weighted_random.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import random

from catanatron.models.player import Player
from catanatron.models.actions import ActionType


WEIGHTS_BY_ACTION_TYPE = {
ActionType.BUILD_CITY: 10000,
ActionType.BUILD_SETTLEMENT: 1000,
ActionType.BUY_DEVELOPMENT_CARD: 100,
}


class WeightedRandomPlayer(Player):
def decide(self, game, playable_actions):
bloated_actions = []
for action in playable_actions:
weight = WEIGHTS_BY_ACTION_TYPE.get(action.action_type, 1)
bloated_actions.extend([action] * weight)

index = random.randrange(0, len(bloated_actions))
return bloated_actions[index]
3 changes: 2 additions & 1 deletion play.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
from catanatron.game import Game
from catanatron.json import GameEncoder
from catanatron.models.player import RandomPlayer, Color, SimplePlayer
from catanatron.players.weighted_random import WeightedRandomPlayer


@click.command()
@click.option("--num", default=5, help="Number of greetings.")
def simulate(num):
"""Simple program simulates NUM Catan games."""
player_classes = [RandomPlayer, RandomPlayer, RandomPlayer, SimplePlayer]
player_classes = [WeightedRandomPlayer, RandomPlayer, RandomPlayer, SimplePlayer]
colors = [Color.RED, Color.BLUE, Color.ORANGE, Color.WHITE]
pseudonyms = ["Foo", "Bar", "Baz", "Qux"]

Expand Down

0 comments on commit b9679f5

Please sign in to comment.