停止我制作的Python游戏的循环。

huangapple go评论69阅读模式
英文:

Stopping my python loop for a game I made

问题

我正在尝试使用Python制作一个棋盘游戏。这是我的第一个项目。

需要整理一下,但在我这样做之前,我想让基本功能正常运行,包括停止游戏。

在所有属性都被购买之后,游戏就陷入了一个无限循环。

我想要做的是:一旦所有属性都被购买,就停止游戏。获胜者是拥有最多资金的玩家。

以下是你的代码翻译:

# 导入语句
from random import random
from random import randint

# 定义棋盘大小
board_size = 20

# 玩家
# 创建玩家
num_players = 4

# 每个玩家的资金
player_funds = [250] * num_players

# 每个玩家的起始位置
players_position = [0] * num_players

# 财产
# 价格、租金(20%)、类别(每个正方形棋盘长度的3个属性)、所有者
property_prices = {
    "Property 1": {"cost": 100, "rent": 20, "category": "Category 1", 'owner': None},
    "Property 2": {"cost": 150, "rent": 30, "category": "Category 1", 'owner': None},
    "Property 3": {"cost": 200, "rent": 40, "category": "Category 1", 'owner': None},
    "Property 4": {"cost": 250, "rent": 50, "category": "Category 2", 'owner': None},
    "Property 5": {"cost": 200, "rent": 40, "category": "Category 2", 'owner': None},
    "Property 6": {"cost": 120, "rent": 24, "category": "Category 2", 'owner': None},
    "Property 7": {"cost": 200, "rent": 40, "category": "Category 3", 'owner': None},
    "Property 8": {"cost": 180, "rent": 36, "category": "Category 3", 'owner': None},
    "Property 9": {"cost": 100, "rent": 20, "category": "Category 3", 'owner': None},
    "Property 10": {"cost": 150, "rent": 30, "category": "Category 4", 'owner': None},
    "Property 11": {"cost": 200, "rent": 40, "category": "Category 4", 'owner': None},
    "Property 12": {"cost": 200, "rent": 40, "category": "Category 4", 'owner': None},
}

# 根据棋盘大小的财产所有权
property_ownership = [-1] * board_size

# 创建骰子
# 创建掷骰子的函数
def roll_dice():
    return randint(1, 6)

# 效果
effect_tiles = ['前进2格',
                '前进3格',
                '获得50',
                '支付10的费用',
                '回到起点并获得50',
                '再次掷骰子']

# 效果函数
def effect_function(player, players_position):
    # 根据玩家位置相对于棋盘大小确定效果索引
    effect_index = players_position[player] % board_size
    # 确定索引的效果
    effect = effect_tiles[effect_index]

    # 为每个效果创建语句
    if effect == '前进2格':
        players_position[player] += 2
    elif effect == '前进3格':
        players_position[player] += 3
    elif effect == '获得50':
        player_funds[player] += 50
    elif effect == '支付10的费用':
        player_funds[player] -= 10
    elif effect == '回到起点并获得50':
        players_position[player] = 0
        player_funds[player] += 50
    elif effect == '再次掷骰子':
        return True

    print(f'玩家{player+1}落在了一个效果格上:{effect}')

# 游戏
# 定义当前玩家位置
current_player = 0

# 循环
while True:
    # 遍历每个玩家
    # 定义玩家和掷骰子结果
    player = current_player % num_players
    # 掷骰子,确保骰子结果小于3
    dice_roll = roll_dice()

    # 更新玩家的位置,使其适应棋盘大小
    players_position[player] += dice_roll
    players_position[player] %= board_size

    # 如果玩家的位置大于等于棋盘大小,重置为0并获得50
    if players_position[player] == 0:
        player_funds[player] += 50

    # 如果玩家落在效果格上
    if players_position[player] < len(effect_tiles):
        # effect_function(player, players_position)
        roll_again = effect_function(player, players_position)

        if roll_again:
            rolls = 1
            while rolls < 3:
                dice_roll = roll_dice()
                players_position[player] += dice_roll
                players_position[player] %= board_size

                if players_position[player] < len(effect_tiles):
                    roll_again = effect_function(player, rolls, players_position)

                    if not roll_again:
                        break

                rolls += 1

    # 检查玩家是否落在财产上
    property_index = players_position[player]
    property_name = f'Property {property_index + 1}'

    # 如果财产已经被拥有
    if property_name in property_prices and property_ownership[property_index] == 0:
        # 解析所有权
        if property_ownership[property_index] != player:
            owner = property_ownership[property_index]
            # 确定租金金额
            property_rent = property_prices[property_name]["rent"]

            # 检查玩家是否拥有同一类别的所有财产
            category = property_prices[property_name]['category']
            category_properties = [prop for prop in property_prices if property_prices[prop]['category'] == category]
            owns_all_in_category = all(property_ownership[property_ownership[player]] == player for prop in category_properties)

            # 如果拥有同一类别

<details>
<summary>英文:</summary>

I&#39;m trying to make a board game in Python. It&#39;s my very first project.

It needs to be cleaned up, but before I do that, I&#39;d like to have the basic things working, which includes stopping the game.

After all of the properties have been bought, the game just goes into an infinite loop.

What I am trying to do: Once all of the properties have been purchased, stop the game. Winner is the player with the most funds.

#IMPORTING STATEMENTS
from random import random
from random import randint

#DEFINE BOARD SIZE
board_size = 20

#PLAYERS
#Creating the players
num_players = 4

#Player funds for each player
player_funds = [250] * num_players

#players starting positions for each player
players_position = [0] * num_players

#PROPERTY
#Prices, rent(20%), category (3 properties of each length of square board), owner
property_prices = {
"Property 1": {"cost": 100, "rent": 20, "category": "Category 1", 'owner': None},
"Property 2": {"cost": 150, "rent": 30, "category": "Category 1", 'owner': None},
"Property 3": {"cost": 200, "rent": 40, "category": "Category 1", 'owner': None},
"Property 4": {"cost": 250, "rent": 50, "category": "Category 2", 'owner': None},
"Property 5": {"cost": 200, "rent": 40, "category": "Category 2", 'owner': None},
"Property 6": {"cost": 120, "rent": 24, "category": "Category 2", 'owner': None},
"Property 7": {"cost": 200, "rent": 40, "category": "Category 3", 'owner': None},
"Property 8": {"cost": 180, "rent": 36, "category": "Category 3", 'owner': None},
"Property 9": {"cost": 100, "rent": 20, "category": "Category 3", 'owner': None},
"Property 10": {"cost": 150, "rent": 30, "category": "Category 4", 'owner': None},
"Property 11": {"cost": 200, "rent": 40, "category": "Category 4", 'owner': None},
"Property 12": {"cost": 200, "rent": 40, "category": "Category 4", 'owner': None},
}

#property ownership as per board size
property_ownership = [-1] * board_size

#category_ownership = category[player]

#CREATING THE DICE
#creating a function to roll the dice
def roll_dice():
return randint(1,6)

#EFFECTS
effect_tiles = ['Go forward 2 spaces',
'Go forward 3 spaces',
'Gain 50',
'Pay 10 in fees',
'Go to start and gain 50',
'Roll again']

#Effects functions
def effect_function(player, players_position):
#establish effect index with players position relative to board size
effect_index = players_position[player] % board_size
#establish the effect for the index
effect = effect_tiles[effect_index]

#Create statements for each effect
if effect == 'Go forward 2 spaces':
players_position[player] += 2
elif effect == 'Go forward 3 spaces':
players_position[player] += 3
elif effect == 'Gain 50':
player_funds[player] += 50
elif effect == 'Pay 10 in fees':
player_funds[player] -= 10
elif effect == 'Go to start and gain 50':
players_position[player] = 0
player_funds[player] += 50
elif effect == 'Roll again':
return True

print(f&#39;Player {player+1} landed on an effects tile: {effect}&#39;)

#THE GAME ----

#define current players position
current_player = 0

#The loop
while True:
#Iterating over each player
#define players and rolls
player = current_player % num_players
#roll the dice, ensuring rolls are < 3

dice_roll = roll_dice()
#update the players position appropriate to board size
players_position[player] += dice_roll
players_position[player] %= board_size
#if player &gt;19 places on the board size, reset to 0 and give 50
if players_position[player] == 0:
player_funds[player] += 50
#if a player lands on an effects tile
if players_position[player] &lt; len(effect_tiles):
#effect_function(player, players_position)
roll_again = effect_function(player, players_position)
if roll_again:
rolls = 1
while rolls &lt;3:
dice_roll = roll_dice()
players_position[player] += dice_roll
players_position[player] %= board_size
if players_position[player] &lt; len(effect_tiles):
roll_again = effect_function(player, rolls, players_position)
if not roll_again:
break
rolls +=1
#Checking if a player lands on a property
property_index = players_position[player]
property_name = f&#39;Property {property_index + 1}&#39;
#IF A PROPERTY IS OWNED
if property_name in property_prices and property_ownership[property_index] == 0:
#decipher ownership
if property_ownership[property_index] != player:
owner = property_ownership[property_index]
#define rent amount
property_rent = property_prices[property_name][&quot;rent&quot;]
#check if a player owns all properties in a category
category = property_prices[property_name][&#39;category&#39;]
category_properties = [prop for prop in property_prices if property_prices[prop][&#39;category&#39;] == category]
owns_all_in_category = all(property_ownership[property_ownership[player]] == player for prop in category_properties)
#if owns all in one category, doulbe the rent
if owns_all_in_category:
property_rent*2
print(f&#39;Rent has doubled.&#39;)
else:
property_rent
#pay rent
player_funds[player] -= property_rent
player_funds[owner] += property_rent
#Update the display with rent paid, and new fund amount
print(f&#39;Player {player+1}: You have paid Player{owner+1} {property_rent} in rent. You now have {player_funds[player]}.&#39;)
print(f&#39;Player{owner+1}, you now have {player_funds[owner]}.&#39;)

#IF A PROPERTY IS UNOWNED
if property_name in property_prices and property_ownership[property_index] == -1:
#Ask the player if they would like to purchase the property
#define property cost at appropriate index
property_cost = property_prices[property_name]["cost"]
#property_cost = property_prices[property_name]["cost"]
purchase_choice = input(f'Player {player+1}, you have landed on an unowned property at {property_index+1}\n'
f'This property costs {property_cost}. You have {player_funds[player]}. Do you want to buy it? (y/n)')

    if purchase_choice.lower() == &#39;y&#39;:
#Check the player has enough money to buy
#if player has enough money:
if player_funds[player] &gt;= property_cost:
#deduct from players funds
player_funds[player] -= property_cost
#update ownership
property_ownership[property_index] = player
#update owner value
property_prices[property_name][&#39;owner&#39;] = player
#confirm the property has been bought
print(f&#39;You have purchased {property_name}&#39;)
#if not enough money
else:
print(&#39;You do not have enough money to purchase this property.&#39;)
#display the player, dice roll and current position
print(f&quot;Player {player+1}: Your current position is {players_position[player]}. You have {player_funds[player]}&quot;)
#Make display easier to navigate &amp; ready
print(&#39;-&#39;)
#update current player
current_player += 1
#Kicking players out of the game
#If a player has no more money
if player_funds[player] &lt; 0:
print(f&#39;Player {player+1} is out of money and has been removed from the game.&#39;)
#set properties as unowned
for prop in category_properties:
#accessing the property index and splitting it
property_index = int(prop.split()[1]) -1
property_ownership[property_index] = None
#remove player from game
del player_funds[player]
del players_position[player]
num_players -= 1
current_player -=1
#check if there is one player left (the winner)
if num_players == 1:
winner = player_funds.index(max(player_funds))
print(f&#39;Player {winner+1} has won! with {player_funds[player]} in their bank!&#39;)
#WINNING CONDITIONS
if all(property_ownership[index] != -1 for index in range(board_size)):
print(&#39;All properties are owned. You have 10 rounds. The player with the most money at the end, wins&#39;)
#End the game and determine winner
max_money = player_funds[player]
winners = [i + 1 for i, fund in enumerate(player_funds) if fund == max_money]
if len(winners)== 1:
print(f&#39;Player {winners[0]} wins the game with {player_funds[winners[0]]}&#39;)
print(&#39;Player {player+1} ended with {player_funds[player]}&#39;)
else:
print(f&#39;Its a tie.&#39;)
break

#The end.


</details>
# 答案1
**得分**: 0
以下是翻译好的部分:
"Instead of `while true`, you could use a variable, like `running`:
running = True
while running:
..."
"Then when you want the game to end, you can just set `running = False`.
if len(winners)== 1:
running = False"
"Edit:
I have a few suggestions, when I was looking through your code,
- you seem to have `player` and `current_player` doing the same thing, or at least it is difficult to see the logic.
I suggest using `current_player` and at the start of each loop you set the `current_player` and do all calculations in reference to them:
#update current player
current_player += 1
current_player %= num_players"
"`property_prices` could just be a list, and when you want `property_name` you could use `&quot;Property&quot; + str(property_index + 1)`"
"The roll checking if the dice has been rolled up to 3 times could be simplified with something like:
#if a player lands on an effects tile
if players_position[current_player] &lt; len(effect_tiles):
rolls = 1
while players_position[current_player] &lt; len(effect_tiles) and rolls &lt; 3:
effect_function(current_player, players_position)
dice_roll = roll_dice()
players_position[current_player] += dice_roll
players_position[current_player] %= board_size
rolls +=1"
"You might be able to use `&#39;owner&#39;: None` in `property_prices` to set ownership, and it saves using the data structure `property_ownership = [-1] * board_size`."
<details>
<summary>英文:</summary>
Instead of `while true`, you could use a variable, like `running`:
running = True
while running:
...
Then when you want the game to end, you can just set `running = False`.
if len(winners)== 1:
running = False
Edit:
I have a few suggestions, when I was looking through your code,
- you seem to have `player` and `current_player` doing the same thing, or at least it is difficult to see the logic.
I suggest using `current_player` and at the start of each loop you set the `current_player` and do all calculations in reference to them:
:
#update current player
current_player += 1
current_player %= num_players
- `property_prices` could just be a list, and when you want `property_name` you could use `&quot;Property&quot; + str(property_index + 1)`
- The roll checking if the dice has been rolled up to 3 times could be simplified with something like:
:
#if a player lands on an effects tile
if players_position[current_player] &lt; len(effect_tiles):
rolls = 1
while players_position[current_player] &lt; len(effect_tiles) and rolls &lt; 3:
effect_function(current_player, players_position)
dice_roll = roll_dice()
players_position[current_player] += dice_roll
players_position[current_player] %= board_size
rolls +=1
- You might be able to use `&#39;owner&#39;: None` in `property_prices` to set ownership, and it saves using the data structure `property_ownership = [-1] * board_size`.
Feel free to discard this if I misinterpreted.
</details>
# 答案2
**得分**: 0
属性索引永远不会返回1或5。并且因为:
property_name = f&#39;属性 {property_index + 1}&#39;
属性2和6不可购买。
<details>
<summary>英文:</summary>
Property index never returns as 1 or 5. And because: 
property_name = f&#39;Property {property_index + 1}&#39;
Properties 2 and 6 are not purchasable.
</details>

huangapple
  • 本文由 发表于 2023年6月25日 20:37:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定