我正在与4名玩家一起制作宫廷牌游戏,我不知道如何判断谁在投掷卡牌?我应该使用类似计数器的东西吗?
答案 0 :(得分:1)
使用简单的计数器模数玩家数量。
class Game(object):
def __init__(self):
self.count = 4
self.current_player = 0
def next_turn(self):
self.current_player += 1
self.current_player %= self.count
答案 1 :(得分:0)
由于不同的牌可能导致不同的玩家进行下一轮,并且游戏在顺时针和逆时针之间切换,我建议您将决定留给卡片对象。
from abc import ABCMeta, abstractmethod
class Card(object):
__metaclass__ = ABCMeta
def __init__(self, color, value):
"""
Color is either "r", "b", "g", or "y". Value is 0-9.
"""
self.color = color
self.value = value
def is_valid(self, color, value=None):
return color in self.color or value == self.value
@abstractmethod
def card_effect(self, game):
pass
所有卡片的这个基类是一个类必须满足才能被视为卡片的“合约”。由于我们没有计划实例化这个类,所以我们将它作为一个抽象类。
class Normal(Card):
def card_effect(self, game):
game.increment_player_index(1)
class Skip(Card):
def __init__(self, color):
super(Skip, self).__init__(color, "skip")
def card_effect(self, game):
game.increment_player_index(2)
class Turn(Card):
def __init__(self, color):
super(Turn, self).__init__(color, "turn")
def card_effect(self, game):
game.toggle_direction()
game.increment_player_index(2)
class PlusTwo(Card):
def __init__(self, color):
super(PlusTwo, self).__init__(color, "+2")
def card_effect(self, game):
game.increment_player_index(1)
game.current_player().punish(2)
game.increment_player_index(1)
对于黑卡,我们这样做:
class PlusFour(Card):
def __init__(self):
super(PlusFour, self).__init__("rbgy", "+4")
def card_effect(self, game):
color = game.current_player().choose_color()
game.set_color(color)
game.increment_player_index(1)
game.current_player().punish(4)
game.increment_player_index(1)