def play(self, gamer, data): """ Move player on the map to certain block """ operation.push2all(self.description) if isinstance(self._destination, list): tmp = self._destination[0] for dest in self._destination: if gamer.position < dest: tmp = dest operation.push2all("Do not pass Go, no cash collected.") gamer.move(steps=None, position=tmp) break else: continue go_block = data["chess_board"][0] operation.push2all("Passing Go, Gain %d" % go_block.reward) operation.pay(data['epic_bank'], gamer, go_block.reward, data) gamer.move(steps=None, position=tmp) dest_block = data["chess_board"][tmp] dest_block.display(gamer, data, 0) else: if gamer.position < self._destination: operation.push2all("Do not pass Go, no cash collected.") else: go_block = data["chess_board"][0] operation.push2all("Passing Go, Gain %d" % go_block.reward) operation.pay(data['epic_bank'], gamer, go_block.reward, data) if self._destination == 10: # in jail gamer.cur_status = 0 gamer.move(steps=None, position=self._destination) dest_block = data["chess_board"][self._destination] dest_block.display(gamer, data, 0)
def roll(gamer): """ Roll a dice :param gamer: The player who roll dices Returns: :int: The number of first dice :int: The number of second dice :bool: The station of end_flag """ # Whether pass Go a, b = roll_dice() if a == b: end_flag = False else: end_flag = True step = a + b current_gamer_position = gamer.position if current_gamer_position + step > 40: operation.push2all("Passing Go, Gain 200") operation.pay(data['epic_bank'], gamer, 200, data) gamer.move(step) end_position = gamer.position current_block = data['chess_board'][end_position] if isinstance(current_block, block.Go_To_Jail): end_flag = True operation.push2all("At %s" % current_block.name) current_block.display(gamer, data, step) return a, b, end_flag
def play(self, gamer, data): """ :param from_role: a player or bank or rest players :param data: global game data """ operation.push2all(self.description) if self._card_type == 2: to_role = data['epic_bank'] from_role = gamer operation.pay(from_role, to_role, self._amount, data) elif self._card_type == 8: # need to check to_role = [] all_role = data["player_dict"] for role_id in all_role.keys(): if role_id != gamer.id: to_role.append(all_role[role_id]) from_role = gamer total_amount = self._amount * len(to_role) for role in to_role: operation.pay(from_role, role, self._amount, data) elif self._card_type == 3: import estate from_role = gamer total_amount = 0 for e in from_role.assets: if isinstance(estate, estate.Estate): house_repair_amount = int( e.house_num % 6) * self._amount[0] hotel_repair_amount = int( e.house_num / 6) * self._amount[1] total_amount += house_repair_amount + hotel_repair_amount operation.pay(from_role, data['epic_bank'], self._amount, data)
def display(self, gamer, data, dice_result): """ docstring here :type gamer: player.Player :param gamer: :param data: :param dice_result: """ operation.push2all("Move to Jail") gamer.move(steps=0, position=10) gamer.cur_status = 0
def _init_player(self): self._player_dict_data = self._player_dict_data["data"] for i in range(len(self._player_dict_data)): p = player.Player(self._player_dict_data[i]['id'], self._player_dict_data[i]['name'], self._player_dict_data[i]['cash'], self._player_dict_data[i]['alliance']) output_str = "{0} {1} {2} {3}".format(p.cash, p.id, p.name, p.alliance) operation.push2all(output_str) self._player_dict[self._player_dict_data[i]['id']] = p
def display(self, gamer, data, dice_result): """ docstring here :type gamer: player.Player :param gamer: :param data: :param dice_result: """ bank = data['epic_bank'] payment = gamer.calculate_asset_value() * self.rate operation.pay(gamer, bank, payment, data) operation.push2all("%s pay %d for tax" % (gamer.name, payment))
def play(self, gamer, data): """ :param from_role: a player or bank or rest players :param from_role: player or bank or rest players """ operation.push2all(self.description) if self._card_type == 0: to_role = gamer operation.pay(data['epic_bank'], to_role, self._amount, data) elif self._card_type == 1: to_role = gamer from_role = [] all_role = data["player_dict"] for role_id in all_role.keys(): if role_id != gamer.id: from_role.append(role_id) for role_id in from_role: payer = all_role[role_id] operation.pay(payer, gamer, self._amount, data)
def play(self, gamer, data): """ Move player on the map to certain block """ operation.push2all(self.description) if gamer.position < self._destination: operation.push2all("Do not pass Go, no cash collected.") else: operation.push2all("Passing Go, Gain 200") operation.pay(data['epic_bank'], gamer, 200, data) if self._destination == 10: # in jail gamer.cur_status = 0 gamer.move(steps=None, position=self._destination)
def roll_dice(): operation.push2all("Rolling") a = random.randint(1, 6) b = random.randint(1, 6) operation.push2all("Dice number is %d %d" % (a, b)) return a, b
def display(self, gamer, data, dice_result): """ Display description :type data: dict :return: """ import operation player_dict = data['player_dict'] bank = data['epic_bank'] if self._status == 1: # Some body own it owner_id = self.owner owner = player_dict[owner_id] if owner_id == gamer.id: # Owner pass this station # print("%s own %s" % (gamer.name, self.name)) operation.push2all("%s own %s" % (gamer.name, self.name)) else: # Other pass this station # print("%s own %s" % (owner.name, self.name)) operation.push2all("%s own %s" % (owner.name, self.name)) fee = self.payment(gamer.station_num) operation.pay(gamer, owner, fee, data) elif self._status == -1: # Nobody own while True: operation.push2all("Nobody own %s do you want to buy it?" % self.name) operation.push2all("1: Buy it") operation.push2all("2: Do not buy it") while True: # input_str = input("Please enter the number of your decision:") input_str = operation.wait_choice() try: choice = int(input_str) break except ValueError: operation.push2all("Please enter a number.") operation.push2all(" ") if choice == 1: price = self.value if price > gamer.cash: operation.push2all("You do not have enough money") break else: operation.pay(gamer, bank, price, data) operation.trade_asset(self, bank, gamer) operation.push2all("%s buy %s for %d" % (gamer.name, self.name, price)) break elif choice == 2: break else: operation.push2all("Invalid operation") elif self._status == 0: # In mortgage operation.push2all("%s is in mortgaged" % self.name) else: raise ValueError("Invalid estate status")
def start_game(mess_hand): global data data = init_game(mess_hand) living_list = list(data["player_dict"].keys()) data["living_list"] = living_list num_round = 0 update_period = 1 while len(living_list) != 1: if num_round % update_period == 0: operation.update_value(data) num_round += 1 for gamer_id in living_list: gamer = data["player_dict"][gamer_id] operation.push2all("Now is %s turn" % gamer.name) operation.push2all("Gamer current cash %d" % gamer.cash) if gamer.cur_status == 0: # In jail operation.push2all("%s are in jail" % gamer.name) a, b = roll_dice() if a == b: gamer.cur_status = 1 gamer._in_jail = 0 turn(gamer, data) else: while True: operation.push2all("1: Bail. Get out of prison.") operation.push2all("2: Stay in prison.") while True: input_str = operation.wait_choice( "Please enter the number of your decision:") if (True): input_data = data["msg"].get_json_data("input") input_str = input_data[0]["request"] try: choice = int(input_str) break except ValueError: operation.push2all("Please enter a number.") operation.push2all() if choice == 1: if operation.bail(gamer, data): gamer.cur_status = 1 gamer._in_jail = 0 turn(gamer, data) break else: operation.push2all("Please stay in jail") gamer.count_in_jail() break elif choice == 2: gamer.count_in_jail() break else: operation.push2all("Invalid choice") elif gamer.cur_status == 1: # Normal Status turn(gamer, data) else: pass operation.push2all()
def play(self, gamer, data): """ Baild card, can be collected by players """ to_role = gamer operation.push2all(self.description) if to_role.bail_card_num == 0: operation.push2all("1. Keep it yourself.") operation.push2all("2. Sell to others.") while True: operation.push2all("Please enter the number of your decision:") input_str = operation.wait_choice() try: choice = int(input_str) if choice == 1 or choice == 2: break elif choice == -1: return False else: operation.push2all( "Invaild choice, please input again.") except ValueError: operation.push2all( "Please enter a number. Enter -1 to quit") if choice == 1: to_role.bail_card_num = to_role.bail_card_num + 1 elif choice == 2: while True: operation.push2all( "Please enter the player of you want to sell the card to:" ) input_str = operation.wait_choice() try: choice = str(input_str) if choice in data['player_dict'].keys( ) and choice != gamer.name: break elif choice == 'q': return False else: operation.push2all( "Invaild choice, please input again.") except ValueError: operation.push2all( "Please enter a player name. Enter q to quit") to_role = data['player_dict'][choice] from_role = gamer # TODO: need to implement trade pass
def game_main(): data = init_game() living_list = list(data["player_dict"].keys()) data["living_list"] = living_list while len(living_list) != 1: for gamer_id in living_list: gamer = data["player_dict"][gamer_id] operation.push2all("Now is %s turn" % gamer.name) operation.push2all("Gamer current cash %d" % gamer.cash) if gamer.cur_status == 0: # In jail operation.push2all("%s are in jail" % gamer.name) a, b = roll_dice() if a == b: gamer.cur_status = 1 gamer._in_jail = 0 turn(gamer) else: while True: operation.push2all("1: Bail. Get out of prison.") operation.push2all("2: Stay in prison.") while True: operation.push2all( "Please enter the number of your decision:") input_str = operation.wait_choice() try: choice = int(input_str) break except ValueError: operation.push2all("Please enter a number.") operation.push2all(" ") if choice == 1: if operation.bail(gamer, data): gamer.cur_status = 1 gamer._in_jail = 0 turn(gamer) break else: operation.push2all("Please stay in jail") gamer.count_in_jail() break elif choice == 2: gamer.count_in_jail() break else: operation.push2all("Invalid choice") elif gamer.cur_status == 1: # Normal Status turn(gamer) else: pass operation.push2all(" ")
def turn(gamer): """ :param gamer: players :return: """ global data end_flag = False while True: operation.push2all("1: Trade with others") operation.push2all("2: Roll dices") operation.push2all("3: Construct building") operation.push2all("4: Mortgage asset") operation.push2all("5: End turn") while True: operation.push2all("Please enter the number of your decision:") input_str = operation.wait_choice() try: choice = int(input_str) break except ValueError: operation.push2all("Please enter a number.") operation.push2all(" ") if choice == 1: operation.trade() elif choice == 2 and end_flag is False: dice_a, dice_b, end_flag = roll(gamer) elif choice == 3: operation.construct_building(gamer, data) elif choice == 4: operation.mortgage_asset(gamer, data) elif choice == 5: if end_flag is True: break else: operation.push2all("Please roll a dice") else: operation.push2all("Invalid choice")
def turn(gamer, data): """ :param gamer: players :return: """ end_flag = False while True: operation.push2all("1: Trade with others") operation.push2all("2: Roll dices") operation.push2all("3: Construct building") operation.push2all("4: Remove building") operation.push2all("5: Mortgage asset") operation.push2all("6: End turn") while True: input_str = operation.wait_choice( "Please enter the number of your decision:") if (True): input_data = data["msg"].get_json_data("input") input_str = input_data[0]["request"] print("input_str", input_str) try: choice = int(input_str) break except ValueError: operation.push2all("Please enter a number.") operation.push2all() if choice == 1: # operation.trade(data, trade_data) pass elif choice == 2 and end_flag is False: dice_a, dice_b, end_flag = roll(gamer, data) elif choice == 3: operation.construct_building(gamer, data) elif choice == 4: operation.remove_building(gamer, data) elif choice == 5: operation.mortgage_asset(gamer, data) elif choice == 6: if end_flag is True: break else: operation.push2all("Please roll a dice") else: operation.push2all("Invalid choice")
def display(self, gamer, data, dice_result): """ Display description :param dice_result: :return: """ player_dict = data['player_dict'] epic_bank = data['epic_bank'] if self._status == 1: # Some body own it owner_id = self.owner owner = player_dict[owner_id] if owner_id == gamer.id: # Owner pass this estate operation.push2all("%s own %s" % (gamer.name, self.name)) else: # Other pass this estate operation.push2all("%s own %s" % (owner.name, self.name)) payment = self.payment operation.pay(gamer, owner, payment, data) elif self._status == -1: # Nobody own while True: operation.push2all("Nobody own %s do you want to buy it?" % self.name) operation.push2all("Price: %d" % self.value) operation.push2all("1: Buy it") operation.push2all("2: Do not buy it") while True: input_str = operation.wait_choice( "Please enter the number of your decision:") if (True): input_data = data["msg"].get_json_data("input") input_str = input_data[0]["request"] try: choice = int(input_str) break except ValueError: operation.push2all("Please enter a number.") operation.push2all() if choice == 1: price = self.value cur_cash = gamer.cash if price > cur_cash: operation.push2all("You do not have enough money") break else: operation.pay(gamer, epic_bank, price, data) operation.trade_asset(self, epic_bank, gamer) operation.push2all("%s buy %s for %d" % (gamer.name, self.name, price)) break elif choice == 2: break else: operation.push2all("Invalid operation") elif self._status == 0: # In mortgage operation.push2all("%s is in mortgaged" % self.name) else: raise ValueError("Invalid estate status")
def init_game(): """ Initialize the game with map, players and bank 1. generate a map 2. initialize players 3. initialize bank all of the data :return: map, players list and bank """ # generate a map parent_addr = os.path.abspath(os.pardir) block_list_data = json_reader( os.path.join(parent_addr, 'Data/block_data.json')) station_list_data = json_reader( os.path.join(parent_addr, 'Data/station_data.json')) utility_list_data = json_reader( os.path.join(parent_addr, 'Data/utility_data.json')) estate_list_data = json_reader( os.path.join(parent_addr, 'Data/estate_data.json')) chest_list_data = json_reader( os.path.join(parent_addr, 'Data/chest_data.json')) chance_list_data = json_reader( os.path.join(parent_addr, 'Data/chance_data.json')) block_list = [0 for x in range(40)] station_list = [] utility_list = [] estate_list = [] corner_list = [] chest_block_list = [] chance_block_list = [] tax_list = [] # initialize bank epic_bank = bank.Bank('99', 'EpicBank', 32, 12) json_writer(os.path.join(parent_addr, 'Data/bank_data.json'), { "house_number": epic_bank.cur_house, "hotel_number": epic_bank.cur_hotel }) for b in block_list_data["data"]: if b['block_type'] == 0: # ["Go", "Go to Jail", "In Jail", "Free Parking"] if b['name'] == "Go": corner_block = block.Go(b['name'], b['block_id'], b['position']) elif b['name'] == "Go to Jail": corner_block = block.Go_To_Jail(b['name'], b['block_id'], b['position']) elif b['name'] == "In Jail": corner_block = block.In_Jail(b['name'], b['block_id'], b['position']) elif b['name'] == "Free Parking": corner_block = block.Free_Parking(b['name'], b['block_id'], b['position']) else: pass block_list[corner_block.position] = corner_block corner_list.append(corner_block) elif b['name'] == "Community Chest": # "Community Chest" new_block = cardpile.Community_Chest(b['name'], b['block_id'], b['position']) block_list[new_block.position] = new_block chest_block_list.append(new_block) elif b['name'] == "Chance": # "Chance" new_block = cardpile.Chance(b['name'], b['block_id'], b['position']) block_list[new_block.position] = new_block chance_block_list.append(new_block) elif b['block_type'] == 3: # ["Income Tax", "Super Tax"] if b['name'] == "Income Tax": new_block = tax.Income_Tax(b['name'], b['block_id'], b['position'], 0.10) elif b['name'] == "Super Tax": new_block = tax.Super_Tax(b['name'], b['block_id'], b['position'], 0.10) else: pass block_list[new_block.position] = new_block tax_list.append(new_block) # name, position, uid, estate_value, status, street_id for s in station_list_data["data"]: new_block = station.Station(s['name'], s['block_id'], s['position'], s['uid'], s['estate_value'], s['status']) station_list.append(new_block) block_list[new_block.position] = new_block epic_bank.add_asset(new_block) # name, position, uid, estate_value, status, street_id for u in utility_list_data["data"]: new_block = utility.Utility(u['name'], u['block_id'], u['position'], u['uid'], u['estate_value'], u['status']) utility_list.append(new_block) block_list[new_block.position] = new_block epic_bank.add_asset(new_block) for e in estate_list_data["data"]: new_block = estate.Estate(e['name'], e['block_id'], e['position'], e['uid'], e['estate_value'], e['status'], e['street_id'], e['house_value']) estate_list.append(new_block) block_list[new_block.position] = new_block epic_bank.add_asset(new_block) # initialize players player_dict_data = json_reader( os.path.join(parent_addr, 'Data/player_list.json')) player_dict = {} player_dict_data = player_dict_data["data"] for i in range(len(player_dict_data)): p = player.Player(player_dict_data[i]['id'], player_dict_data[i]['name'], player_dict_data[i]['cash'], player_dict_data[i]['alliance']) out_put_line = "%d %d %s %s" % (p.cash, p.id, p.name, p.alliance) operation.push2all(out_put_line) player_dict[player_dict_data[i]['id']] = p # initialize chest cards chest_list = [] for chest in chest_list_data["data"]: # 0: Collection, 1: Collect_from_players if chest['card_type'] == 0 or chest['card_type'] == 1: chest_list.append( card.CollectCard(chest['card_id'], chest['card_type'], chest['description'], chest['amount'])) elif chest['card_type'] == 2 or chest[ 'card_type'] == 3: # 2: Pay, 3: Pay_for_repair # or chest['card_type'] == 8 8: Pay_to_players chest_list.append( card.PayCard(chest['card_id'], chest['card_type'], chest['description'], chest['amount'])) # elif chest['card_type'] == 4 or chest['card_type'] == 6: # 4: Move_indicate_position, 6: Move_nearby # chest_list.append(card.MoveCard(chest['card_id'], chest['card_type'], chest['description'], # chest['block_id'])) # elif chest['card_type'] == 7: # Move # chest_list.append(card.MoveCard(chest['card_id'], chest['card_type'], chest['description'], # chest['steps'])) elif chest['card_type'] == 5: # Bailcard chest_list.append( card.BailCard(chest['card_id'], chest['card_type'], chest['description'])) # initialize chance cards chance_list = [] for chance in chance_list_data["data"]: if chance['card_type'] == 0: # 0: Collection # or chance['card_type'] == 1, 1: Collect_from_players chance_list.append( card.CollectCard(chance['card_id'], chance['card_type'], chance['description'], chance['amount'])) elif chance['card_type'] == 2 or chance['card_type'] == 3 or chance[ 'card_type'] == 8: # 2: Pay, # 3: Pay_for_repair # 8: Pay_to_players chance_list.append( card.PayCard(chance['card_id'], chance['card_type'], chance['description'], chance['amount'])) # 4: Move_indicate_position, 6: Move_nearby elif chance['card_type'] == 4 or chance['card_type'] == 6: chance_list.append( card.MoveCard(chance['card_id'], chance['card_type'], chance['description'], chance['block_id'])) elif chance['card_type'] == 7: # Move chance_list.append( card.MoveCard(chance['card_id'], chance['card_type'], chance['description'], chance['steps'])) elif chance['card_type'] == 5: # Bailcard chance_list.append( card.BailCard(chance['card_id'], chance['card_type'], chance['description'])) # initialize chess board two_block_street = [] three_block_street = [] for e in estate_list: if e.street_id == 1 or e.street_id == 8: two_block_street.append(e) else: three_block_street.append(e) chess_board_object = board.Board(two_block_street, three_block_street, station_list, utility_list, block_list, corner_list, chest_block_list, chance_block_list, tax_list) global data data['chess_board'] = block_list data['player_dict'] = player_dict data['epic_bank'] = epic_bank data['chest_list'] = chest_list data['chance_list'] = chance_list data['station_list'] = station_list data['utility_list'] = utility_list data['estate_list'] = estate_list data['corner_list'] = corner_list data['chest_block_list'] = chest_block_list data['chance_block_list'] = chance_block_list data['tax_list'] = tax_list return data