Exemplo n.º 1
0
 def action(self, game, player):
     target = game.get_nearest(player, self.tiletype)
     player.move_to(target)
     tile = game.get_tile(target)
     tile.card_rent()
     dice = Dice()
     dice.roll()
     player.land_on(tile, dice)
Exemplo n.º 2
0
 def test_repeats_distribution(self):
     dice = Dice()
     expected_mean = 1 / 6
     expected_variance = 5 / 36
     for _ in range(1000):
         dice.roll()
     mean = dice.repeats / 1000
     variance = (24 * mean + 1) / 36
     self.assertAlmostEqual(mean, expected_mean, delta=0.1, msg=mean)
     self.assertAlmostEqual(variance,
                            expected_variance,
                            delta=0.1,
                            msg=variance)
Exemplo n.º 3
0
 def next(self):
     if self.can_roll:
         raise MustRollError
     elif not self.current_tile_owned():
         raise TileNotOwnedError
     else:
         if not self.get_current_player().positive_balance():
             self.game.players.kill(self.get_current_player())
         self.current_player_index += 1
         self.current_player_index %= self.game.get_player_count()
         if self.game.get_player_count() == 1:
             self.game.end(self.get_current_player())
             return
         self.can_roll = True
         self.dice = Dice()
Exemplo n.º 4
0
 def test_one_distribution(self):
     dice = Dice()
     prob = [0] * 6
     expected_mean = 3.5
     expected_variance = 17.5 / 6
     for _ in range(1000):
         dice.roll()
         prob[dice.first - 1] += 1
     for i in range(6):
         prob[i] /= 1000
     mean = 0
     variance = 0
     for i in range(6):
         mean += prob[i] * (i + 1)
         variance += prob[i] * (i + 1 - expected_mean)**2
     self.assertAlmostEqual(mean, expected_mean, delta=0.3, msg=mean)
     self.assertAlmostEqual(variance,
                            expected_variance,
                            delta=0.2,
                            msg=variance)
Exemplo n.º 5
0
 def test_total_distribution(self):
     dice = Dice()
     prob = [0] * 11
     expected_mean = 7
     expected_variance = 35 / 6
     for _ in range(1000):
         dice.roll()
         prob[dice.value() - 2] += 1
     for i in range(11):
         prob[i] /= 1000
     mean = 0
     variance = 0
     for i in range(11):
         mean += prob[i] * (i + 2)
         variance += prob[i] * (i + 2 - expected_mean)**2
     self.assertAlmostEqual(mean, expected_mean, delta=0.7, msg=mean)
     self.assertAlmostEqual(variance,
                            expected_variance,
                            delta=0.6,
                            msg=variance)
Exemplo n.º 6
0
 def __init__(self, game):
     self.game = game
     self.dice = Dice()
     self.can_roll = True
     self.current_player_index = 0
     self.jail_mgr = JailManager(self)
Exemplo n.º 7
0
class TurnManager:
    def __init__(self, game):
        self.game = game
        self.dice = Dice()
        self.can_roll = True
        self.current_player_index = 0
        self.jail_mgr = JailManager(self)

    def copy(self, game):
        turn_copy = TurnManager(game)
        turn_copy.dice = deepcopy(self.dice)
        turn_copy.can_roll = self.can_roll
        turn_copy.current_player_index = self.current_player_index
        return turn_copy

    def roll(self):
        if not self.can_roll:
            raise NoThrowsLeftError
        elif not self.current_tile_owned() and self.dice.repeats > 0:
            raise TileNotOwnedError
        else:
            self.dice.roll()
            current_player = self.get_current_player()
            if self.dice.repeats < 3:
                current_player.start_from(self.get_current_tile(), self.dice)
                current_player.land_on(self.get_current_tile(), self.dice)
            else:
                self.jail_mgr.put_in_jail(self.get_current_player())
            self.can_roll = self.dice.same() and not current_player.in_jail

    def next(self):
        if self.can_roll:
            raise MustRollError
        elif not self.current_tile_owned():
            raise TileNotOwnedError
        else:
            if not self.get_current_player().positive_balance():
                self.game.players.kill(self.get_current_player())
            self.current_player_index += 1
            self.current_player_index %= self.game.get_player_count()
            if self.game.get_player_count() == 1:
                self.game.end(self.get_current_player())
                return
            self.can_roll = True
            self.dice = Dice()

    def use_card(self):
        self.jail_mgr.use_card()

    def buy_out(self):
        self.jail_mgr.buy_out()

    def current_tile_owned(self):
        try:
            return self.get_current_tile().owned()
        except AttributeError:
            return True

    def get_current_tile(self):
        return self.game.get_tile(self.get_current_player().position)

    def get_current_player(self):
        return self.game.get_player(self.current_player_index)

    def info_dice(self):
        print(self.dice)

    def info_current_player(self):
        print(self.get_current_player())