Python 桌游:骰子街

骰子街是一款经典桌游。规则简单、易学。这里是规则讲解和游玩的视频。这个视频的作者桌游林吃素开了一家桌游店“小狮子桌游铺”(淘宝),我在这家桌游店买了骰子街豪华版,包含基础版和扩展。

笔者使用 Python 写了一个基于命令行的基础版的骰子街,无法添加扩展(笔者无能)。代码也少,如下:

from random import randint

from utils import *
from typing import ClassVar, Callable, Any, final


# 地标建筑
class GreatCard:
    is_enable: bool
    name: str
    cost: int
    owner: 'Player'
    msg: ClassVar[str]

    def __init__(self, player: 'Player'): self.owner, self.is_enable = player, False

    def enable(self): ...

    @final
    def buy(self):
        self.owner.money -= self.cost
        self.open()

    @final
    def open(self):
        self.is_enable = True
        self.enable()

    @classmethod
    def new_great_card(cls, cls_name: str, cls_cost: int, cls_msg: str, cls_enable):
        class Sub(cls):
            name = cls_name
            cost = cls_cost
            msg = cls_msg
            enable = cls_enable

        return Sub


class CardMeta(type):
    name: str
    dice_num: tuple[int, ...]

    def __str__(self): return f'card {
     self.name}[{
     any2str(self.dice_num)}]'


class Card(metaclass=CardMeta):
    owner: 'Player'

    dice_num: ClassVar[tuple[int, ...]]
    name: ClassVar[str]
    cost: ClassVar[int]
    describe: ClassVar[list[str]] = []

    def __init__(self, owner: 'Player'): self.owner = owner

    def can_work(self, dice: int, player: 'Player') -> bool: ...

    def work(self, now: 'Player'): ...

    def set_owner(self, new: 'Player'):
        raw = self.owner
        self.owner = new
        new.cards.append(self)
        raw.cards.remove(self)

    def __str__(self): return f'card {
     self.name}[{
     any2str(self.dice_num)}] of player {
     self.owner}\'s'

    def all_work(self, dice: int, now: 'Player'):
        if self.can_work(dice, now):
            self.work(now)

    @classmethod
    def new_card_cls(cls, cls_name: str, cls_dice_num: int | tuple[int, ...], cls_cost: int) -> type['Card']:
        class Sub(cls):
            name = cls_name
            dice_num = (cls_dice_num,) if isinstance(cls_dice_num, int) else cls_dice_num
            cost = cls_cost

        return Sub


# 根据触发方式(当前玩家)分类
class PrivateCard(Card):
    def can_work(self, dice: int, player: 'Player') -> bool: return dice in self.dice_num and player is self.owner


class PublicCard(Card):
    def can_work(self, dice: int, player: 'Player') -> bool: return dice in self.dice_num


class SoldOutCard(Card):
    def can_work(self, dice: int, player: 'Player') -> bool: return dice in self.dice_num and player is not self.owner


# 根据功能分类
class MoneyGetCard(Card):
    one_step: ClassVar[int]

    def one_make(self) -> int: ...

    def work(self, now: 'Player'):
        made = self.one_make()
        owner = self.owner
        if type(self) in affect and owner in affect_players:
            made += 1
        owner.money += made

    @classmethod
    def new_card_cls(cls, name: str, dice_num: int | tuple[int, ...], cost: int, one_step=0):
        res = super().new_card_cls(name, dice_num, cost)
        res.one_step = one_step
        return res


class SimpleCard(MoneyGetCard):
    def one_make
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值