确定哪位玩家在行动

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

Determine which player is on the move

问题

我正在尝试用Python构建一款卡牌游戏,以更深入了解面向对象编程。我正在寻找一种确定哪个玩家轮到行动的方法。在这个卡牌游戏中的规则是,四名玩家中的一名是庄家,接下来的玩家就会是首个出牌的玩家。我已经尝试过一些方法,但没有找到解决方案。目前我有以下代码:

player_list = [p1, p2, p3, p4]

def func_turn(first_player):
    for i in range(len(player_list)): 

        if first_player + i == 4:
            first_player = 0
        player_list[first_player + i].play_card()

是否有更好的方法可以在玩家类中实现或者一些我不知道的列表函数可以用来处理这个问题?

提前感谢!

英文:

I am trying to build a card game in python to get a little bit into OOP.
I am searching for a way to determine wchich player is on the move. The rule in the card game is that one of the four players is the dealer, and the next one is then the first to play a card.
I have searched for some ways to do that but I couldnt finde a solution for this.
At the moment I have something like this:

player_list = [p1, p2, p3, p4]

def func_turn(first_player):
    for i in range(len(player_list)): 

        if first_player + i == 4:
            first_player = 0
        player_list[first_player + i].play_card()

Is there a better way to do that something I can implement in the player class or some list functions I am not aware of?

thanks in advance

答案1

得分: 1

方法 1

你可以使用 itertools 模块中的一些方法来创建适用于你情况的生成器。让我们以示例列表为例:

a = [1, 2, 3, 4]

首先,我们希望对列表进行 cycle 操作,使其不断重复。这将给我们:

>>> b = itertools.cycle(a)    # 生成器对象,返回 [1, 2, 3, 4, 1, 2, ...]

接下来,我们想要跳过一定数量的元素。这是你的 first_player 变量,我们想要取 n 个元素,其中 n 是玩家数量。我们可以使用 islice 方法来实现:

>>> itertools.islice(b, 2, 2 + 4)   # 生成器对象,返回 [3, 4, 1, 2]

将所有这些组合起来,你可以得到:

import itertools as it

def func_turn(start):
    for player in it.islice(it.cycle(player_list), start, start + len(player_list)):
        player.play_card()

方法 2

你可以使用 collections.deque 及其 rotate 方法来在每轮之后更新玩家顺序:

from collections import deque

player_list = deque([p1, p2, p3, p4])

def func_turn():
    for player in player_list:
        player.play_card()

    player_list.rotate(-1)    # 在一次旋转后将变为 deque([p2, p3, p4, p1])
英文:

Method 1

You can use some methods from the itertools module to create the appropriate generator for your case. Let's work with the example list

a = [1, 2, 3, 4]

Firstly, we want to cycle the list so that it keeps repeating. This will give us

>>> b = itertools.cycle(a)    # generator object that gives [1, 2, 3, 4, 1, 2, ...]

Next, we want to skip ahead by a certain amount. This is your first_player variable and we want to take n elements where n is the number of players. We can accomplish this using the islice method

>>> itertools.islice(b, 2, 2 + 4)   # generator object that gives [3, 4, 1, 2]

Putting this all together, you get

import itertools as it

def func_turn(start):
    for player in it.islice(it.cycle(player_list), start, start + len(player_list)):
        player.play_card()

Method 2

You can use collections.deque and its rotate method to update the player order after every turn

from collections import deque

player_list = deque([p1, p2, p3, p4])

def func_turn():
    for player in player_list:
        player.play_card()

    player_list.rotate(-1)    # will be deque([p2, p3, p4, p1]) after 1 rotation

huangapple
  • 本文由 发表于 2020年1月6日 21:34:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613069.html
匿名

发表评论

匿名网友

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

确定