コード例 #1
0
 def test_adding_starting_settlements(self):
     # Create game
     g = Game();
     # Make sure creating a starting settlement does not use any cards
     g.players[0].add_cards([
         ResCard.WOOD,
         ResCard.BRICK,
         ResCard.SHEEP,
         ResCard.WHEAT
     ])
     # Test adding a starting settlement, i.e. no cards needed
     res = g.add_settlement(0, 0, 0, True)
     assert res == Statuses.ALL_GOOD
     assert g.board.points[0][0].building != None
     assert g.board.points[0][0].building.type == Building.BUILDING_SETTLEMENT
     assert g.board.points[0][0].building.point is g.board.points[0][0]
     assert len(g.players[0].cards) == 4
     # Test adding a settlement too close to another settlement
     res = g.add_settlement(1, 0, 1, True)
     assert res == Statuses.ERR_BLOCKED
     # Test adding a settlement the correct distance away
     res = g.add_settlement(2, 0, 2, True)
     assert res == Statuses.ALL_GOOD
     # Try creating a settlement on a point that does not exist
     res = g.add_settlement(0, 100, 0, True)
     assert res == Statuses.ERR_BAD_POINT
コード例 #2
0
ファイル: test_board.py プロジェクト: samwheeler99/PyCatan
 def test_robber_prevents_yield(self):
     random.seed(1)
     game = Game()
     board = game.board
     # Move robber to top-left corner
     board.robber = board.tiles[0][0]
     # Add settlement
     game.add_settlement(0, game.board.points[0][0], True)
     # Roll an 8
     board.add_yield(8)
     # Ensure the robber prevented the player from getting the card
     assert not game.players[0].has_cards([ResCard.Brick])
コード例 #3
0
 def test_give_proper_yield(self):
     # Set seeed to ensure the board is the same as the testcase
     random.seed(1)
     # Create new game and get the board
     game = Game()
     board = game.board
     # Make sure robber is not on the top-left hex
     board.robber = [1, 1]
     # add settlement
     game.add_settlement(0, 0, 0, True)
     # give the roll
     board.add_yield(8)
     # check the board gave the cards correctly
     assert game.players[0].has_cards([ResCard.BRICK])
コード例 #4
0
 def test_add_settlement(self):
     g = Game()
     # Try to add a settlement without the cards
     g.add_settlement(0, 0, 0)
     # Add cards to build a settlement
     g.players[0].add_cards([
         ResCard.WOOD,
         ResCard.BRICK,
         ResCard.SHEEP,
         ResCard.WHEAT
     ])
     # Try adding an isolated settlement
     res = g.add_settlement(0, 0, 0)
     assert res == Statuses.ERR_ISOLATED
     assert g.board.points[0][0].building == None
     # Try adding a settlement at a point that is not on the board
     res = g.add_settlement(0, 500, 0)
     assert res == Statuses.ERR_BAD_POINT
     # Add starting settlement and two roads to ensure there is an available position
     assert g.add_settlement(0, 0, 2, True) == Statuses.ALL_GOOD
     assert g.add_road(0, [0, 2], [0, 1], True) == Statuses.ALL_GOOD
     assert g.add_road(0, [0, 0], [0, 1], True) == Statuses.ALL_GOOD
     res = g.add_settlement(0, 0, 0)
     assert res == Statuses.ALL_GOOD
     assert g.board.points[0][0].building != None
     assert g.board.points[0][0].building.type == Building.BUILDING_SETTLEMENT
コード例 #5
0
ファイル: test_board.py プロジェクト: samwheeler99/PyCatan
 def test_give_proper_yield(self):
     # Set seeed to ensure the board is the same as the testcase
     random.seed(1)
     # Create new game and get the board
     game = Game()
     board = game.board
     # Make sure robber is not on the top-left tile
     board.robber = [1, 1]
     # add settlement
     game.add_settlement(0, game.board.points[0][0], True)
     # give the roll
     board.add_yield(8)
     # check the board gave the cards correctly
     assert game.players[0].has_cards([ResCard.Brick])
コード例 #6
0
 def test_trade_in_cards_through_bank(self):
     g = Game()
     # Add 4 wood cards to player 0
     g.players[0].add_cards([ResCard.WOOD] * 4)
     # Try to trade in for 1 wheat
     res = g.trade_to_bank(player=0, cards=[ResCard.WOOD] * 4, request=ResCard.WHEAT)
     assert res == Statuses.ALL_GOOD
     assert not g.players[0].has_cards([ResCard.WOOD])
     assert g.players[0].has_cards([ResCard.WHEAT])
     # Try to trade in cards the player doesn't have
     res = g.trade_to_bank(player=0, cards=[ResCard.BRICK] * 4, request=ResCard.ORE)
     assert res == Statuses.ERR_CARDS
     assert not g.players[0].has_cards([ResCard.ORE])
     # Try to trade in with less than 4 cards, but more than 0
     g.players[0].add_cards([ResCard.BRICK] * 3)
     res = g.trade_to_bank(player=0, cards=[ResCard.BRICK] * 4, request=ResCard.SHEEP)
     assert res == Statuses.ERR_CARDS
     assert g.players[0].has_cards([ResCard.BRICK] * 3)
     assert not g.players[0].has_cards([ResCard.SHEEP])
コード例 #7
0
ファイル: test_game.py プロジェクト: samwheeler99/PyCatan
 def test_adding_starting_settlements(self):
     # Create game
     g = Game()
     # Make sure creating a starting settlement does not use any cards
     g.players[0].add_cards(
         [ResCard.Wood, ResCard.Brick, ResCard.Sheep, ResCard.Wheat])
     # Test adding a starting settlement, i.e. no cards needed
     res = g.add_settlement(0, g.board.points[0][0], True)
     assert res == Statuses.ALL_GOOD
     assert g.board.points[0][0].building != None
     assert g.board.points[0][
         0].building.type == Building.BUILDING_SETTLEMENT
     assert g.board.points[0][0].building.point is g.board.points[0][0]
     assert len(g.players[0].cards) == 4
     # Test adding a settlement too close to another settlement
     res = g.add_settlement(1, g.board.points[0][1], True)
     assert res == Statuses.ERR_BLOCKED
     # Test adding a settlement the correct distance away
     res = g.add_settlement(2, g.board.points[0][2], True)
     assert res == Statuses.ALL_GOOD
コード例 #8
0
ファイル: test_game.py プロジェクト: samwheeler99/PyCatan
 def test_moving_robber(self):
     random.seed(1)
     g = Game()
     # Move the robber
     g.move_robber(g.board.tiles[0][0], None, None)
     assert g.board.robber is g.board.tiles[0][0]
     # Build a settlement at 1, 1
     g.add_settlement(player=0,
                      point=g.board.points[1][1],
                      is_starting=True)
     # Roll an 8
     g.add_yield_for_roll(8)
     # Ensure the player got nothing since the robber was there
     assert len(g.players[0].cards) == 0
     # Give the player a brick to steal
     g.players[0].add_cards([ResCard.Brick])
     # Move the robber to 1, 0 and steal the brick
     g.move_robber(g.board.tiles[1][0], 1, 0)
     # Make sure they stole the brick
     assert g.players[1].has_cards([ResCard.Brick])
コード例 #9
0
 def test_adding_starting_roads(self):
     # Create game
     game = Game()
     # Add starting settlement
     game.add_settlement(0, 0, 0, True)
     # Try adding a road
     res = game.add_road(0, [0, 0], [0, 1], True)
     assert res == Statuses.ALL_GOOD
     res = game.add_road(0, [1, 1], [0, 0], True)
     assert res == Statuses.ALL_GOOD
     # Try adding a disconnected road
     res = game.add_road(0, [2, 0], [2, 1], True)
     assert res == Statuses.ERR_ISOLATED
     # Try adding a road whose point's are not connected
     res = game.add_road(0, [0, 0], [5, 5], True)
     assert res == Statuses.ERR_NOT_CON
     # Try adding a road connected to another player's settlement
     game.add_settlement(1, 2, 2, True)
     res = game.add_road(0, [2, 2], [2, 3], True)
     assert res == Statuses.ERR_ISOLATED
コード例 #10
0
 def test_trade_in_cards_through_harbor(self):
     g = Game();
     # Add Settlement next to the harbor on the top
     res = g.add_settlement(player=0, r=0, i=2, is_starting=True)
     assert res == Statuses.ALL_GOOD
     # Make the harbor trade in ore for testing
     for h in g.board.harbors:
         if g.board.points[0][2] in h.get_points():
             h.type = HarborType.ORE
     g.players[0].add_cards([ResCard.ORE] * 2)
     # Try to use harbor
     res = g.trade_to_bank(player=0, cards=[ResCard.ORE] * 2, request=ResCard.WHEAT)
     assert res == Statuses.ALL_GOOD
     assert g.players[0].has_cards([ResCard.WHEAT])
     assert not g.players[0].has_cards([ResCard.ORE])
     # Try to trade in to a harbor that the player does not have access to
     g.players[0].add_cards([ResCard.BRICK] * 2)
     res = g.trade_to_bank(player=0, cards=[ResCard.BRICK] * 2, request=ResCard.SHEEP)
     assert res == Statuses.ERR_HARBOR
     assert g.players[0].has_cards([ResCard.BRICK] * 2)
     assert not g.players[0].has_cards([ResCard.SHEEP])
     # Try to trade without the proper cards
     assert not g.players[0].has_cards([ResCard.ORE])
     res = g.trade_to_bank(player=0, cards=[ResCard.ORE] * 2, request=ResCard.SHEEP)
     assert res == Statuses.ERR_CARDS
     assert not g.players[0].has_cards([ResCard.SHEEP])
     # Try to trade with more cards than the player has
     g.players[0].add_cards([ResCard.ORE])
     res = g.trade_to_bank(player=0, cards=[ResCard.ORE] * 2, request=ResCard.SHEEP)
     assert res == Statuses.ERR_CARDS
     assert not g.players[0].has_cards([ResCard.SHEEP])
     assert g.players[0].has_cards([ResCard.ORE])
コード例 #11
0
ファイル: test_game.py プロジェクト: samwheeler99/PyCatan
 def test_add_settlement(self):
     g = Game()
     # Try to add a settlement without the cards
     g.add_settlement(0, g.board.points[0][0])
     # Add cards to build a settlement
     g.players[0].add_cards(
         [ResCard.Wood, ResCard.Brick, ResCard.Sheep, ResCard.Wheat])
     # Try adding an isolated settlement
     res = g.add_settlement(0, g.board.points[0][0])
     assert res == Statuses.ERR_ISOLATED
     assert g.board.points[0][0].building == None
     # Add starting settlement and two roads to ensure there is an available position
     assert g.add_settlement(0, g.board.points[0][2],
                             True) == Statuses.ALL_GOOD
     assert g.add_road(0, g.board.points[0][2], g.board.points[0][1],
                       True) == Statuses.ALL_GOOD
     assert g.add_road(0, g.board.points[0][0], g.board.points[0][1],
                       True) == Statuses.ALL_GOOD
     res = g.add_settlement(0, g.board.points[0][0])
     assert res == Statuses.ALL_GOOD
     assert g.board.points[0][0].building != None
     assert g.board.points[0][
         0].building.type == Building.BUILDING_SETTLEMENT
コード例 #12
0
ファイル: test_game.py プロジェクト: samwheeler99/PyCatan
 def test_trade_in_cards_through_harbor(self):
     g = Game()
     # Add Settlement next to the harbor on the top
     res = g.add_settlement(0, g.board.points[0][2], is_starting=True)
     assert res == Statuses.ALL_GOOD
     # Make the harbor trade in ore for testing
     for h in g.board.harbors:
         if g.board.points[0][2] in h.get_points():
             h.type = HarborType.Ore
             print("found harbor lmao")
     g.players[0].add_cards([ResCard.Ore] * 2)
     # Try to use harbor
     res = g.trade_to_bank(player=0,
                           cards=[ResCard.Ore] * 2,
                           request=ResCard.Wheat)
     assert res == Statuses.ALL_GOOD
     assert g.players[0].has_cards([ResCard.Wheat])
     assert not g.players[0].has_cards([ResCard.Ore])
     # Try to trade in to a harbor that the player does not have access to
     g.players[0].add_cards([ResCard.Brick] * 2)
     res = g.trade_to_bank(player=0,
                           cards=[ResCard.Brick] * 2,
                           request=ResCard.Sheep)
     assert res == Statuses.ERR_HARBOR
     assert g.players[0].has_cards([ResCard.Brick] * 2)
     assert not g.players[0].has_cards([ResCard.Sheep])
     # Try to trade without the proper cards
     assert not g.players[0].has_cards([ResCard.Ore])
     res = g.trade_to_bank(player=0,
                           cards=[ResCard.Ore] * 2,
                           request=ResCard.Sheep)
     assert res == Statuses.ERR_CARDS
     assert not g.players[0].has_cards([ResCard.Sheep])
     # Try to trade with more cards than the player has
     g.players[0].add_cards([ResCard.Ore])
     res = g.trade_to_bank(player=0,
                           cards=[ResCard.Ore] * 2,
                           request=ResCard.Sheep)
     assert res == Statuses.ERR_CARDS
     assert not g.players[0].has_cards([ResCard.Sheep])
     assert g.players[0].has_cards([ResCard.Ore])
コード例 #13
0
import math

# Get an integer from standard input and return it
# If a non-integer value is entered, continue to prompt the user
def integer_input(str_prompt):
    while True:
        try:
            return int(input(str_prompt))
        except ValueError:
            print("Please enter a valid integer")
            continue

if __name__ == "__main__":
    terminal = blessings.Terminal()
    # Create a new game of Catan
    game = Game()
    # Set up board to render
    br = BoardRenderer(board=game.board, center=[math.floor(terminal.width / 2), math.floor(terminal.height / 2 - 1)])
    # Draw the board
    br.render()
    # Starting phase
    # Twice for each player
    for p in game.players + list(reversed(game.players)):
        # Render the board
        br.render()
        # Place a free settlement
        with terminal.location(0, terminal.height - 1):
            # Prompt the player for input
            print("Player %s, it is your turn" % p.num)
            while True:
                # Get the row
コード例 #14
0
ファイル: test_game.py プロジェクト: samwheeler99/PyCatan
 def test_adding_starting_roads(self):
     # Create game
     g = Game()
     # Add starting settlement
     g.add_settlement(0, g.board.points[0][0], True)
     # Try adding a road
     res = g.add_road(0, g.board.points[0][0], g.board.points[0][1], True)
     assert res == Statuses.ALL_GOOD
     res = g.add_road(0, g.board.points[1][1], g.board.points[0][0], True)
     assert res == Statuses.ALL_GOOD
     # Try adding a disconnected road
     res = g.add_road(0, g.board.points[2][0], g.board.points[2][1], True)
     assert res == Statuses.ERR_ISOLATED
     # Try adding a road whose point's are not connected
     res = g.add_road(0, g.board.points[0][0], g.board.points[5][5], True)
     assert res == Statuses.ERR_NOT_CON
     # Try adding a road connected to another player's settlement
     g.add_settlement(1, g.board.points[2][2], True)
     res = g.add_road(0, g.board.points[2][2], g.board.points[2][3], True)
     assert res == Statuses.ERR_ISOLATED
コード例 #15
0
ファイル: test_game.py プロジェクト: samwheeler99/PyCatan
 def test_game_starts_with_variable_players(self):
     game = Game(num_of_players=5)
     assert len(game.players) == 5
コード例 #16
0
        if hex_type == HexType.HILLS:
            return "H"
        elif hex_type == HexType.MOUNTAINS:
            return "M"
        elif hex_type == HexType.PASTURE:
            return "P"
        elif hex_type == HexType.FOREST:
            return "F"
        elif hex_type == HexType.FIELDS:
            # Since F is already used, use W for "wheat"
            return "W"
        elif hex_type == HexType.DESERT:
            return "D"
        else:
            raise Exception(
                "Unknown HexType %s passed to get_hex_type_string" % hex_type)


if __name__ == "__main__":
    g = Game()
    br = BoardRenderer(g.board, [50, 10])
    # Add some settlements
    g.add_settlement(player=0, r=0, i=0, is_starting=True)
    g.add_settlement(player=1, r=2, i=3, is_starting=True)
    g.add_settlement(player=2, r=4, i=1, is_starting=True)
    # Add some roads
    g.add_road(player=0, start=[0, 0], end=[0, 1], is_starting=True)
    g.add_road(player=1, start=[2, 3], end=[2, 2], is_starting=True)
    g.add_road(player=2, start=[4, 1], end=[4, 0], is_starting=True)
    br.render()