Beispiel #1
0
 def initial_placement_run(self):
     """Allow players to place original two houses and two roads"""
     # Order:  start with self.turn_order_first, proceed to everyone in UID
     # order.  Then start with the last person, going back to self.t_o_f...
     self.placement_order = [self.turn_order_first] # add the first person
     for u in sorted(self.all_uids): # add everyone after the first person
         if u > self.turn_order_first:
             self.placement_order.append(u)
     for u in sorted(self.all_uids): # add everyone before the first person
         if u < self.turn_order_first:
             self.placement_order.append(u)
     for u in sorted(self.all_uids)[::-1]: # add everyone before the first person, reverse order
         if u < self.turn_order_first:
             self.placement_order.append(u)
     for u in sorted(self.all_uids)[::-1]: # add everyone after the first person, reverse order
         if u > self.turn_order_first:
             self.placement_order.append(u)
     self.placement_order.append(self.turn_order_first)
     
     print "Starting game initial placement..."
     time.sleep(5)
     
     self.initialization_round = 0
     for i in range(len(self.placement_order)):
         self.initialization_round = i
         players_turn = self.placement_order[self.initialization_round]
         if players_turn == self.uid:
             # Our turn!
             h = self.house_place(True)
             self.ui_board.set_vertex(h, ui.uid_to_friendly(self.uid, self.all_uids)) # set up the vertex for road checking
             r = self.road_place()
             self.broadcast_message({"initialization_round": self.initialization_round, "house_place": h, "road_place": r})
             self.init_rounds[self.initialization_round] = (self.uid, h, r)
         else:
             print "Waiting for another player to go!"
         while self.initialization_round not in self.init_rounds:
             time.sleep(1)
         # Verify the player's placements
         rnd = self.init_rounds[self.initialization_round]
         assert rnd[0] == players_turn # It was actually that player's turn
         if rnd[0] != self.uid:
             assert self.ui_board.can_build_house(rnd[1], ui.uid_to_friendly(rnd[0], self.all_uids), True)
             self.ui_board.set_vertex(rnd[1], ui.uid_to_friendly(rnd[0], self.all_uids))
             print "setting vertex ", rnd[1], "to player"
         self.ui_board.print_actual_board()
         assert self.ui_board.can_build_road(rnd[2], ui.uid_to_friendly(rnd[0], self.all_uids))
         # Looks all good - we didn't crash the game for a cheater.  Update our board and proceed.
         self.ui_board.set_edge(rnd[2], ui.uid_to_friendly(rnd[0], self.all_uids))
         print "The new board:"
         self.ui_board.print_actual_board()
     self.phase += 1
Beispiel #2
0
 def turn_order_run(self):
     """Detemine the turn order of the players by rolling the distributed dice.
        We roll the dice once for each player in UID order..."""
     print "Determining turn order!"
     die_order = sorted([c.their_uid for c in self.client_connections] + [self.uid])
     die_results = {}
     max_roll = -1
     max_roller = None
     while True:
         die_results = {}
         for current_uid in die_order:
             roll = self.run_die_roll() + self.run_die_roll() # Collect 2 die results per UID
             die_results[current_uid] = roll
         # Determine the winner - largest die roller.
         for uid, roll in die_results.iteritems():
             if roll > max_roll:
                 max_roll = roll
                 max_roller = uid
         # OK, but does this roll appear more than once?
         if die_results.values().count(max_roll) > 1:
             print "Highest roller was a tie, trying rolls again"
             continue
         break
     print "Turn order determined!"
     self.turn_order_first = max_roller
     print "Player", ui.uid_to_friendly(max_roller, self.all_uids), "goes first."
     self.phase += 1
Beispiel #3
0
 def turn_order_run(self):
     """Detemine the turn order of the players by rolling the distributed dice.
        We roll the dice once for each player in UID order..."""
     print "Determining turn order!"
     die_order = sorted([c.their_uid
                         for c in self.client_connections] + [self.uid])
     die_results = {}
     max_roll = -1
     max_roller = None
     while True:
         die_results = {}
         for current_uid in die_order:
             roll = self.run_die_roll() + self.run_die_roll(
             )  # Collect 2 die results per UID
             die_results[current_uid] = roll
         # Determine the winner - largest die roller.
         for uid, roll in die_results.iteritems():
             if roll > max_roll:
                 max_roll = roll
                 max_roller = uid
         # OK, but does this roll appear more than once?
         if die_results.values().count(max_roll) > 1:
             print "Highest roller was a tie, trying rolls again"
             continue
         break
     print "Turn order determined!"
     self.turn_order_first = max_roller
     print "Player", ui.uid_to_friendly(max_roller,
                                        self.all_uids), "goes first."
     self.phase += 1
Beispiel #4
0
 def road_place(self):
     while True:
         hin = raw_input("Please tell me an edge to place a road or ! for edge reference: ")
         if hin == "!":
             self.ui_board.print_board_edge_reference()
             continue
         if self.ui_board.can_build_road(hin, ui.uid_to_friendly(self.uid, self.all_uids)):
             print "OK!  Attempting to build a road there..."
             return hin
         print "Can't seem to build a road there... are you sure it's open, attached to other roads/houses?"
Beispiel #5
0
 def house_place(self, initial_case=False):
     while True:
         hin = raw_input("Please tell me a vertex to place a house or ! for vertex reference: ")
         if hin == "!":
             self.ui_board.print_vertex_reference()
             continue
         if self.ui_board.can_build_house(hin, ui.uid_to_friendly(self.uid, self.all_uids), initial_case):
             print "OK!  Attempting to build a house there..."
             return hin
         print "Can't seem to build a house there... are you sure it's open and not too close to others?"
Beispiel #6
0
 def welcome_run(self):
     """Welcome phase.  Tell all the other players that we're ready to play."""
     self.broadcast_message({'ready': True})
     print "Waiting for all players to be ready..."
     while not all(self.players_ready.values()):
         time.sleep(1)
         print "..."
     print "Ready to play!  Starting game in 3 seconds..."
     self.all_uids = [c.their_uid for c in self.client_connections] + [self.uid]
     self.player_number = ui.uid_to_friendly(self.uid, self.all_uids)
     print "You are player", self.player_number
     time.sleep(3)
     self.phase += 1
Beispiel #7
0
 def road_place(self):
     while True:
         hin = raw_input(
             "Please tell me an edge to place a road or ! for edge reference: "
         )
         if hin == "!":
             self.ui_board.print_board_edge_reference()
             continue
         if self.ui_board.can_build_road(
                 hin, ui.uid_to_friendly(self.uid, self.all_uids)):
             print "OK!  Attempting to build a road there..."
             return hin
         print "Can't seem to build a road there... are you sure it's open, attached to other roads/houses?"
Beispiel #8
0
 def house_place(self, initial_case=False):
     while True:
         hin = raw_input(
             "Please tell me a vertex to place a house or ! for vertex reference: "
         )
         if hin == "!":
             self.ui_board.print_vertex_reference()
             continue
         if self.ui_board.can_build_house(
                 hin, ui.uid_to_friendly(self.uid, self.all_uids),
                 initial_case):
             print "OK!  Attempting to build a house there..."
             return hin
         print "Can't seem to build a house there... are you sure it's open and not too close to others?"
Beispiel #9
0
 def welcome_run(self):
     """Welcome phase.  Tell all the other players that we're ready to play."""
     self.broadcast_message({'ready': True})
     print "Waiting for all players to be ready..."
     while not all(self.players_ready.values()):
         time.sleep(1)
         print "..."
     print "Ready to play!  Starting game in 3 seconds..."
     self.all_uids = [c.their_uid
                      for c in self.client_connections] + [self.uid]
     self.player_number = ui.uid_to_friendly(self.uid, self.all_uids)
     print "You are player", self.player_number
     time.sleep(3)
     self.phase += 1
Beispiel #10
0
 def game_play_run(self):
     """Everything's set up - actually play the game.  It took so long to get here!"""
     print "Playing the game! :)"
     self.turn_order = [self.turn_order_first] # add the first person
     for u in sorted(self.all_uids): # add everyone after the first person
         if u > self.turn_order_first:
             self.turn_order.append(u)
     for u in sorted(self.all_uids): # add everyone before the first person
         if u < self.turn_order_first:
             self.turn_order.append(u)
     time.sleep(2)
     while True:
         # Whose turn is it?  Start at the first player and go around forever
         current_player = self.turn_order[self.game_turn % len(self.turn_order)]
         print "It's player %s's turn!" % str(ui.uid_to_friendly(current_player, self.all_uids))
         if current_player == self.uid: print "(That's you!)"
         # Roll the dice for that player
         print "Fairly rolling the dice..."
         dice_roll = self.run_die_roll() + self.run_die_roll()
         print "The dice rolled to %s" % str(dice_roll)
         
         if dice_roll != 7:
             # Distribute resources based on the dice roll
             owed = self.ui_board.resources_owed(dice_roll)
             print "The bank distributes:", owed
             # Mark those items as locally claimed.  That user can 
             for player, resources in owed.iteritems():
                 for resource in resources:
                     the_resource = self.resources.get_next_resource(resource)
                     self.resources.set_resource_owner(the_resource, player)
                     if player == ui.uid_to_friendly(self.uid, self.all_uids):
                         # add to my hand
                         self.hand.append(the_resource)
         elif current_player == self.uid:
             inp = raw_input("You control the robber!  Where would you like to move it? (Hexagon index, ! for key)")
             while inp == "!":
                 self.ui_board.print_hex_reference()
                 inp = raw_input("You control the robber!  Where would you like to move it? (Hexagon index, ! for key)")
             inp = int(inp)
             # move robber to inp
             self.broadcast_message({"turn": self.game_turn, "robber": inp})
             # steal from someone?
             #steal = raw_input("From whom would you like to steal (among those you put the robber next to)?")
             #self.broadcast_message({"steal": ui.friendly_to_uid(steal, self.all_uids), "steal_turn": self.game_turn})
             # Wait for them to send their encrypted hand
             # Ask them for one of their keys
             # Add this item to our hand
             print "Stealing during robbery is unimplemented."
         # Is it our turn?
         if current_player == self.uid:
             print "It's your turn!  What would you like to do?"
             do = raw_input("1: try to make a trade, 2: buy a house, 3: buy a road, or nothing")
             if do != "":
                 if int(do) == 1:
                     # Ask a user for trade
                     # Broadcast ask
                     print "Trading is unimplemented"
                     pass
                 elif int(do) == 2:
                     # Choose the house
                     house = self.house_place()
                     # Spend resources
                     print "New house placement is unimplemented"
                     # Broadcast
                     pass
                 elif int(do) == 3:
                     # Choose the road
                     road = self.road_place()
                     # Spend resources
                     # Broadcast
                     print "New road placement is unimplemented"
                     pass
             self.broadcast_message({'turn_done': self.game_turn}) # end my turn
         else:
             print "Waiting for the player to complete their turn (purchases, trades, etc.)"
             while not self.game_turn in self.game_rounds or 'done' not in self.game_rounds[self.game_turn]:
                 time.sleep(1)
             # only the current player can end their turn
             assert self.game_rounds[self.game_turn]['done'][1] == current_player
             # do we move the robber?
             if 'robber' in self.game_rounds[self.game_turn]:
                 assert self.game_rounds[self.game_turn]['robber'][1] == current_player # verify the sender
                 self.ui_board.move_robber(self.game_rounds[self.game_turn]['robber'][0]) # move it
         self.ui_board.print_actual_board()
         self.game_turn += 1
Beispiel #11
0
    def game_play_run(self):
        """Everything's set up - actually play the game.  It took so long to get here!"""
        print "Playing the game! :)"
        self.turn_order = [self.turn_order_first]  # add the first person
        for u in sorted(self.all_uids):  # add everyone after the first person
            if u > self.turn_order_first:
                self.turn_order.append(u)
        for u in sorted(self.all_uids):  # add everyone before the first person
            if u < self.turn_order_first:
                self.turn_order.append(u)
        time.sleep(2)
        while True:
            # Whose turn is it?  Start at the first player and go around forever
            current_player = self.turn_order[self.game_turn %
                                             len(self.turn_order)]
            print "It's player %s's turn!" % str(
                ui.uid_to_friendly(current_player, self.all_uids))
            if current_player == self.uid: print "(That's you!)"
            # Roll the dice for that player
            print "Fairly rolling the dice..."
            dice_roll = self.run_die_roll() + self.run_die_roll()
            print "The dice rolled to %s" % str(dice_roll)

            if dice_roll != 7:
                # Distribute resources based on the dice roll
                owed = self.ui_board.resources_owed(dice_roll)
                print "The bank distributes:", owed
                # Mark those items as locally claimed.  That user can
                for player, resources in owed.iteritems():
                    for resource in resources:
                        the_resource = self.resources.get_next_resource(
                            resource)
                        self.resources.set_resource_owner(the_resource, player)
                        if player == ui.uid_to_friendly(
                                self.uid, self.all_uids):
                            # add to my hand
                            self.hand.append(the_resource)
            elif current_player == self.uid:
                inp = raw_input(
                    "You control the robber!  Where would you like to move it? (Hexagon index, ! for key)"
                )
                while inp == "!":
                    self.ui_board.print_hex_reference()
                    inp = raw_input(
                        "You control the robber!  Where would you like to move it? (Hexagon index, ! for key)"
                    )
                inp = int(inp)
                # move robber to inp
                self.broadcast_message({"turn": self.game_turn, "robber": inp})
                # steal from someone?
                #steal = raw_input("From whom would you like to steal (among those you put the robber next to)?")
                #self.broadcast_message({"steal": ui.friendly_to_uid(steal, self.all_uids), "steal_turn": self.game_turn})
                # Wait for them to send their encrypted hand
                # Ask them for one of their keys
                # Add this item to our hand
                print "Stealing during robbery is unimplemented."
            # Is it our turn?
            if current_player == self.uid:
                print "It's your turn!  What would you like to do?"
                do = raw_input(
                    "1: try to make a trade, 2: buy a house, 3: buy a road, or nothing"
                )
                if do != "":
                    if int(do) == 1:
                        # Ask a user for trade
                        # Broadcast ask
                        print "Trading is unimplemented"
                        pass
                    elif int(do) == 2:
                        # Choose the house
                        house = self.house_place()
                        # Spend resources
                        print "New house placement is unimplemented"
                        # Broadcast
                        pass
                    elif int(do) == 3:
                        # Choose the road
                        road = self.road_place()
                        # Spend resources
                        # Broadcast
                        print "New road placement is unimplemented"
                        pass
                self.broadcast_message({'turn_done':
                                        self.game_turn})  # end my turn
            else:
                print "Waiting for the player to complete their turn (purchases, trades, etc.)"
                while not self.game_turn in self.game_rounds or 'done' not in self.game_rounds[
                        self.game_turn]:
                    time.sleep(1)
                # only the current player can end their turn
                assert self.game_rounds[
                    self.game_turn]['done'][1] == current_player
                # do we move the robber?
                if 'robber' in self.game_rounds[self.game_turn]:
                    assert self.game_rounds[self.game_turn]['robber'][
                        1] == current_player  # verify the sender
                    self.ui_board.move_robber(self.game_rounds[self.game_turn]
                                              ['robber'][0])  # move it
            self.ui_board.print_actual_board()
            self.game_turn += 1
Beispiel #12
0
    def initial_placement_run(self):
        """Allow players to place original two houses and two roads"""
        # Order:  start with self.turn_order_first, proceed to everyone in UID
        # order.  Then start with the last person, going back to self.t_o_f...
        self.placement_order = [self.turn_order_first]  # add the first person
        for u in sorted(self.all_uids):  # add everyone after the first person
            if u > self.turn_order_first:
                self.placement_order.append(u)
        for u in sorted(self.all_uids):  # add everyone before the first person
            if u < self.turn_order_first:
                self.placement_order.append(u)
        for u in sorted(
                self.all_uids
        )[::-1]:  # add everyone before the first person, reverse order
            if u < self.turn_order_first:
                self.placement_order.append(u)
        for u in sorted(
                self.all_uids
        )[::-1]:  # add everyone after the first person, reverse order
            if u > self.turn_order_first:
                self.placement_order.append(u)
        self.placement_order.append(self.turn_order_first)

        print "Starting game initial placement..."
        time.sleep(5)

        self.initialization_round = 0
        for i in range(len(self.placement_order)):
            self.initialization_round = i
            players_turn = self.placement_order[self.initialization_round]
            if players_turn == self.uid:
                # Our turn!
                h = self.house_place(True)
                self.ui_board.set_vertex(
                    h, ui.uid_to_friendly(
                        self.uid,
                        self.all_uids))  # set up the vertex for road checking
                r = self.road_place()
                self.broadcast_message({
                    "initialization_round": self.initialization_round,
                    "house_place": h,
                    "road_place": r
                })
                self.init_rounds[self.initialization_round] = (self.uid, h, r)
            else:
                print "Waiting for another player to go!"
            while self.initialization_round not in self.init_rounds:
                time.sleep(1)
            # Verify the player's placements
            rnd = self.init_rounds[self.initialization_round]
            assert rnd[0] == players_turn  # It was actually that player's turn
            if rnd[0] != self.uid:
                assert self.ui_board.can_build_house(
                    rnd[1], ui.uid_to_friendly(rnd[0], self.all_uids), True)
                self.ui_board.set_vertex(
                    rnd[1], ui.uid_to_friendly(rnd[0], self.all_uids))
                print "setting vertex ", rnd[1], "to player"
            self.ui_board.print_actual_board()
            assert self.ui_board.can_build_road(
                rnd[2], ui.uid_to_friendly(rnd[0], self.all_uids))
            # Looks all good - we didn't crash the game for a cheater.  Update our board and proceed.
            self.ui_board.set_edge(rnd[2],
                                   ui.uid_to_friendly(rnd[0], self.all_uids))
            print "The new board:"
            self.ui_board.print_actual_board()
        self.phase += 1