Python 在进行板球比赛时,交换球员和记录每个球员得分的问题

Python 在进行板球比赛时,交换球员和记录每个球员得分的问题,python,Python,我想创建一个非常简单的基于文本的板球游戏。但是我被卡住了 from random import randint class Player(): def __init__(self): pass def run(self, player, score): # some code that will take a player and a score and associate that # score with the player

我想创建一个非常简单的基于文本的板球游戏。但是我被卡住了

from random import randint
class Player():
    def __init__(self):
        pass

    def run(self, player, score):
        # some code that will take a player and a score and associate that
        # score with the player and store it (in a dictionary)

    def switch(self, player):
        # some code that will take a player name and change the current 
        # batsmen to the next one, for e.g this should change "a" to "b" or 
        # vice versa but not "c"

team_players = ["a", "b", "c"]

player = Player()

position = 0
run = randint(0,6)
current_batsman = team_players[position]

if run%2 == 0: # which means run is even
    player.run(current_batsman, run) # sending the current player and their run
else: # if the run is odd
    player.run(current_batsman, run) # still doing the same stuff as before but...
    player.switch(current_batsman) # the current batsman should now be switched
也许在
Player
类中调整
位置可能会有所帮助


我希望我的代码能彻底解释我的问题。顺便说一句,在板球比赛中,得分被称为跑动,如果一名球员(击球手)跑了一圈(1,3,5),下一名击球手就会上场,在一名击球手出局之前,场上只有两名击球手,但我现在希望我的比赛非常简单。非常感谢您的帮助。谢谢。

我不知道板球规则,但在课堂上,我会保留球员的名字和分数。它还可以具有函数
run()
,该函数将随机值添加到他的分数中(或者使用参数-value创建方法,您希望将其添加到分数中)

我还添加了
\uuuu str\uuuu
以方便显示播放器

接下来,我将创建一个类
Team
,该类保留所有球员,保留信息,哪个球员是当前击球手,切换击球手,并对当前击球手使用
run()

class Team():

    def __init__(self, players):
        self.players = players
        self.current_batsman = 0
        self.current_run = 0

    def set_next_batsman(self):
        self.current_batsman += 1
        if self.current_batsman >= len(self.players):
            self.current_batsman = 0

    def get_current_batsman(self):
        return self.players[self.current_batsman]

    def run(self):
        self.players[self.current_batsman].run()

        if self.current_run % 2 != 0:
            self.set_next_batsman()

        self.current_run += 1

    def __str__(self):
        return "Player: " + ", ".join(str(p) for p in self.players)

    def total_score(self):
        return sum(p.score for p in self.players)
然后两个队可以比赛:

team1 = Team( [Player("a"), Player("b"), Player("c")] )
team2 = Team( [Player("x"), Player("y"), Player("z")] )

print('Team1:', team1)
print('Team2:', team2)

for number in range(1, 5):
    print('Round:', number)
    print('Team1 current batsman:', team1.get_current_batsman())
    team1.run()
    print('Team2 current batsman:', team2.get_current_batsman())
    team2.run()

print('Team1:', team1)
print('Team2:', team2)

print('Team1 total score:', team1.total_score())
print('Team2 total score:', team2.total_score())

我不知道板球的规则,但在课堂上,我会保留球员的名字和分数。它还可以具有函数
run()
,该函数将随机值添加到他的分数中(或者使用参数-value创建方法,您希望将其添加到分数中)

我还添加了
\uuuu str\uuuu
以方便显示播放器

接下来,我将创建一个类
Team
,该类保留所有球员,保留信息,哪个球员是当前击球手,切换击球手,并对当前击球手使用
run()

class Team():

    def __init__(self, players):
        self.players = players
        self.current_batsman = 0
        self.current_run = 0

    def set_next_batsman(self):
        self.current_batsman += 1
        if self.current_batsman >= len(self.players):
            self.current_batsman = 0

    def get_current_batsman(self):
        return self.players[self.current_batsman]

    def run(self):
        self.players[self.current_batsman].run()

        if self.current_run % 2 != 0:
            self.set_next_batsman()

        self.current_run += 1

    def __str__(self):
        return "Player: " + ", ".join(str(p) for p in self.players)

    def total_score(self):
        return sum(p.score for p in self.players)
然后两个队可以比赛:

team1 = Team( [Player("a"), Player("b"), Player("c")] )
team2 = Team( [Player("x"), Player("y"), Player("z")] )

print('Team1:', team1)
print('Team2:', team2)

for number in range(1, 5):
    print('Round:', number)
    print('Team1 current batsman:', team1.get_current_batsman())
    team1.run()
    print('Team2 current batsman:', team2.get_current_batsman())
    team2.run()

print('Team1:', team1)
print('Team2:', team2)

print('Team1 total score:', team1.total_score())
print('Team2 total score:', team2.total_score())

如果你在课堂上使用name
Player
,那么我希望你有
player1=Player(“b”)
player2=Player(“a”)
,等等,或者作为列表
team=[Player(“a”)、Player(“b”),…]
。而且
Player不需要在
run()中使用
Player`in
,因为它应该将所有信息(针对每个播放器)保存在不同的实例中。在我看来,你的职业玩家应该有名字
Team
,因为你使用外部玩家作为参数。如果你在职业中使用名字
Player
,那么我希望你有
player1=Player(“b”)
player2=Player(“a”)
,等等。或者作为列表
Team=[Player(“a”)、Player(“b”),…]
。而且
Player不需要在
run()中使用
Player`in
,因为它应该将所有信息(针对每个播放器)保存在不同的实例中。在我看来,你的类玩家应该有名字
Team
,因为你使用外部玩家作为参数。