1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| import random
class Player: def __init__(self, name): self.name = name self.position = 0 self.money = 1500 self.properties = [] self.in_jail = False
class BoardCell: def __init__(self, cell_type, name, price=0, rent=0): self.cell_type = cell_type self.name = name self.price = price self.rent = rent self.owner = None
def initialize_board(): return [ BoardCell("start", "起点"), BoardCell("property", "地中海大道", 600, 50), BoardCell("community", "社区福利"), BoardCell("property", "北欧大道", 600, 50), BoardCell("tax", "所得税", price=200), BoardCell("railroad", "中央车站", 2000, 200), BoardCell("property", "东方大道", 1000, 100), BoardCell("chance", "机会"), BoardCell("property", "南方大道", 1000, 100), BoardCell("property", "西方大道", 1200, 150), BoardCell("jail", "监狱"), BoardCell("property", "北京路", 1400, 200), BoardCell("utility", "电力公司", 1500, 100), BoardCell("property", "南京路", 1400, 200), BoardCell("property", "上海路", 1600, 250), BoardCell("railroad", "东站", 2000, 200), BoardCell("property", "广州路", 1800, 300), BoardCell("community", "社区福利"), BoardCell("property", "深圳路", 1800, 300), BoardCell("property", "台北路", 2000, 350), BoardCell("free_parking", "免费停车"), BoardCell("property", "香港路", 2200, 400), BoardCell("chance", "机会"), BoardCell("property", "澳门路", 2200, 400), BoardCell("property", "西藏路", 2400, 450), BoardCell("railroad", "西站", 2000, 200), BoardCell("property", "新疆路", 2600, 500), BoardCell("property", "内蒙古路", 2600, 500), BoardCell("utility", "自来水厂", 1500, 100), BoardCell("property", "青海路", 2800, 550), BoardCell("jail", "监狱"), BoardCell("property", "甘肃路", 3000, 600), BoardCell("property", "宁夏路", 3000, 600), BoardCell("community", "社区福利"), BoardCell("property", "陕西路", 3200, 700), BoardCell("railroad", "北站", 2000, 200), BoardCell("chance", "机会"), BoardCell("property", "河南路", 3500, 800), BoardCell("tax", "奢侈税", price=500), BoardCell("property", "河北路", 4000, 1000) ]
def draw_chance_card(player): cards = [ ("move", "前进到起点", 0), ("money", "银行错误,获得200元", 200), ("move", "前进到中央车站", 5), ("money", "缴纳罚款100元", -100), ("jail", "直接入狱", None) ] card = random.choice(cards) print(f"机会卡:{card[1]}") if card[0] == "move": player.position = card[2] elif card[0] == "money": player.money += card[2] elif card[0] == "jail": player.in_jail = True player.position = 10
def handle_turn(player, board, players): print(f"\n{player.name}的回合,当前位置:{board[player.position].name}") dice = random.randint(1, 6) + random.randint(1, 6) print(f"掷出了 {dice} 点") player.position = (player.position + dice) % len(board) current_cell = board[player.position] print(f"移动到 {current_cell.name} ({player.position})") if current_cell.cell_type == "property": if current_cell.owner: if current_cell.owner != player: rent = current_cell.rent print(f"需要支付租金 {rent} 元给 {current_cell.owner.name}") player.money -= rent current_cell.owner.money += rent else: if player.money >= current_cell.price: buy = input(f"是否要购买 {current_cell.name}({current_cell.price}元)? (y/n) ").lower() if buy == 'y': player.money -= current_cell.price current_cell.owner = player player.properties.append(current_cell) print(f"购买成功!剩余资金:{player.money}") elif current_cell.cell_type == "chance": draw_chance_card(player) elif current_cell.cell_type == "tax": print(f"缴纳 {current_cell.price} 元税款") player.money -= current_cell.price if player.money < 0: print(f"{player.name} 破产了!") for prop in player.properties: prop.owner = None players.remove(player)
def main(): board = initialize_board() players = [Player("玩家1"), Player("玩家2")] while len(players) > 1: for player in list(players): if player in players: handle_turn(player, board, players) print(f"{player.name} 剩余资金:{player.money}") if len(players) == 1: print(f"\n游戏结束!胜利者:{players[0].name}") return
if __name__ == "__main__": main()
|