Skip to content

案例-卡牌游戏

(对象: 牌, 玩家⼀和二, 骰子,复活牌组,上帝之手)

两位玩家, 共 8 张牌。

8 张牌的任务和各自能力分别为:

牌号⼈物智力力量速度攻击
1白雪公主5422
2睡美人7515
3灰姑娘9387
4美人鱼67101
5花木兰81096
6阿拉丁4639
7爱莎3844
8勇敢骑士1953

游戏规则:

  1. 游戏开始两位玩家分别随机发到 4 张牌,注意⼿中的牌是扣过去的,玩家只能知道牌号不知道⼈物和能力值。
  2. 第⼀轮,掷骰⼦,谁的数字⼤,谁先开始出牌,并从 4 张牌中任意选⼀个号码并选出⽤哪⼀个能力进行比拼,出牌
  3. 之后对方也应并从 4 张牌中任意选⼀个号码并对上家所选的能力进行比拼,出牌, 谁的能力值⼤谁得⼀分
  4. 第⼀轮结束,两张牌都要回收到“复活牌组”中
  5. 下⼀轮由上⼀轮获胜的玩家开始游戏
  6. 直到其中⼀个玩家所有牌都出完,或双方都出完牌,游戏结束
  7. 游戏结束时,谁得分越高,谁是赢家
  8. 每个玩家有机会选择上帝之⼿和复活牌各仅⼀次

特殊牌:

  1. 上帝之⼿:每轮获得开始权利的玩家在选择好下⼀轮想要出的牌和能力后,可以选择是否⽤上帝之⼿; 上帝之⼿的作⽤是可以迫使对家打出任何⼀张赢家说出的牌号,能力值⼤的⼀方获胜。
  2. 复活牌:每轮获得开始权利的玩家可以在选择下⼀轮想要出的牌和能力之前,要求使⽤复活牌; 复活牌的作⽤是系统随机从复活牌组中抽出⼀张牌,并会添加在自己的牌桌上,并且自己必须在这轮中出这张牌
  3. 特殊情况为,如果玩家⼀选择使⽤上帝之⼿,玩家⼆就不可以同时选择上帝之⼿; 然⽽玩家⼆可以选择复活牌,在复活牌添加完成后,玩家⼀有权选择对家打出上帝之⼿还是复活牌

答案

python
import random

card_data = [
    ['1', '⽩雪公主', '5', '4', '2', '2'],
    ['2', '睡美⼈', '7', '5', '1', '5'],
    ['3', '灰姑娘', '9', '3', '8', '7'],
    ['4', '美⼈⻥', '6', '7', '10', '1'],
    ['5', '花⽊兰', '8', '10', '9', '6'],
    ['6', '阿拉丁', '4', '6', '3', '9'],
    ['7', '爱莎', '3', '8', '4', '4'],
    ['8', '勇敢骑⼠', '1', '9', '5', '3'],
]


class Dice:
    @staticmethod
    def dice():
        return random.randint(1, 6)


class Card:
    attr_name = ['iq', 'power', 'speed', 'attack']

    def __init__(self, no, name, iq, power, speed, attack):
        self.no = no
        self.name = name
        self.iq = iq
        self.power = power
        self.speed = speed
        self.attack = attack

    def __repr__(self):
        return f'<Card no:{self.no} name:{self.name}>'

    def attack_by_attr(self, attr, other_card):
        return True if eval('self.' + attr) > eval('other_card.' + attr) else False


class Person(Dice):
    def __init__(self, name, cards):
        self.name = name
        self.chars = cards
        self.score = 0

    def select_card_and_attr(self):
        card_index = self.select_card_by_no()
        attr = self.select_card_attr()
        return attr, card_index

    def select_card_by_no(self):
        no = input(f'{self.name} 需要对比的牌的序号({",".join([card.no for card in self.chars])}):')
        return self._search_card_by_no(no)

    def select_card_attr(self):
        return input(f'{self.name} 请指定对比属性({",".join(Card.attr_name)}):')

    def _search_card_by_no(self, card_no):
        for card in self.chars:
            if card.no == card_no:
                self.chars.remove(card)
                return card


class Game:
    def __init__(self):
        self.round = 0
        self.next_first = None
        self.card_numbers = 4
        # 初始牌
        self.card_list = [Card(*data) for data in card_data]
        # 随机分发四张牌
        # 对象一
        self.person_1 = self.create_person('玩家一')
        self.person_2 = self.create_person('玩家二')

    def create_person(self, person_name):
        person_cards = []
        for i in range(self.card_numbers):
            person_cards.append(self.card_list.pop(random.randint(0, len(self.card_list) - 1)))

        return Person(person_name, person_cards)

    def get_first_hand(self, person_1, person_2):
        self.round += 1
        print(f'正在进行第{self.round}轮游戏')
        if self.round == 1:
            if person_1.dice() >= person_2.dice():
                return True
            else:
                return False

        return True if (self.next_first is self.person_1) else False

    def play_game(self):
        while self.person_1.chars and self.person_2.chars:
            flag = self.get_first_hand(self.person_1, self.person_2)
            if flag:
                # 选择属性
                attr, card_1 = self.person_1.select_card_and_attr()
                card_2 = self.person_2.select_card_by_no()
                ret = card_1.attack_by_attr(attr, card_2)
                if ret:
                    self.next_first = self.person_1
                    self.person_1.score += 1
                else:
                    self.next_first = self.person_2
                    self.person_2.score += 1
            else:
                attr, card_2 = self.person_2.select_card_and_attr()
                card_1 = self.person_1.select_card_by_no()
                ret = card_2.attack_by_attr(attr, card_1)
                if ret:
                    self.next_first = self.person_2
                    self.person_2.score += 1
                else:
                    self.next_first = self.person_1
                    self.person_1.score += 1

        print(self.person_1.score)
        print(self.person_2.score)


if __name__ == '__main__':
    g = Game()
    g.play_game()