棋盘游戏需要20轮。棋盘本身是圆形的,这意味着玩家在第10位之后回到第1位。我定义了轮数,但在我的代码中,轮数仍然是随机的。我不明白我的代码有什么问题?
from dataclasses import dataclass
@dataclass
class Player:
firstname: str
lastname: str
coins: int
slot: int
def full_info(self) -> str:
return f"{self.firstname} {self.lastname} {self.coins} {self.slot}"
@classmethod
def from_user_input(cls) -> 'Player':
return cls(
firstname=input("Please enter your first name:"),
lastname=input("Please enter your second name: "),
coins=200,
slot= 0)
minplayer, maxplayer, n = 2, 5, -1
while not(minplayer <= n <= maxplayer):
n = int(input(f" Choose a number of players between {minplayer} and {maxplayer}: "))
playersingame = [] #storing it in the list
for i in range(n):
playersingame.append(Player.from_user_input())
print([player.full_info() for player in playersingame])
# The board
board = [[ ] for i in range(10)]
for player in playersingame:
board[ player.slot ].append(player)
print(board)
import random
for player in playersingame:
input(f"{player.firstname} {player.lastname}, please press enter to roll your die...")
die = random.randint(1,6)
print(f"You take {die} step{'s'* (die>1)} forward")
board[player.slot].remove(player)
player.slot += die
board[player.slot].append(player)
print(board)
def shift(seq, n=0):
a = n % len(seq)
return seq[-a:] + seq[:-a]
round_counter=1
rounds=30
while (round_counter <= rounds):
for player in playersingame:
input(f"{player.firstname} {player.lastname}, please press enter to roll your die...")
die = random.randint(1,6)
print(f"You take {die} step{'s'*(die>1)} forward")
board[player.slot].remove(player)
player.slot += die
if board[player.slot] == len(board):
shift(board)
board[player.slot-1].append(player)
print(board)
round_counter = round_counter+1
1条答案
按热度按时间qojgxg4l1#
代替:
尝试:
您的shift函数返回一个结果,但它从未被赋值,而且从原始代码中也不清楚您打算如何更新所有 * 其他 * 玩家的插槽。
如果有(未说明)为什么公共牌需要在玩家下方移动,请注意,玩家的空位可能超过公共牌的长度,您可能需要测试
board[player.slot] >= len(board)
。(例如,当player.slot
是8并且die
是4时,在player.slot += die
之后--player.slot
现在是12,因为加法是一次完成的并且永远不等于len(board)
)