python - python 中的宾果游戏逻辑

标签 python python-3.x

我正在构建一个脚本来检查玩家宾果卡与随机选择的数字。在测试之前我以为我有足够的逻辑。 无论顺序如何,我如何检查解决方案是否有效?下面是我的较大程序的逻辑 block 。

        chosennumbers = ['B8', 'O69', 'I27', 'G56']

    jordonsboard = ['B8', 'I18', 'N38', 'G47', 'O66',
                    'B10', 'I27', 'N44', 'G53', 'O71',
                    'B3', 'I19', 'fre', 'G48', 'O67',
                    'B4', 'I25', 'N39', 'G56', 'O65',
                    'B9', 'I30', 'N34', 'G58', 'O69']
    hitnodes = [12]  # this list houses hits on your board added 12 because its a free space
    win = False
    iterator = 0
    for i in chosennumbers:
        if i in jordonsboard:
            hitnodes.append(iterator)
        iterator += 1

if 0 in hitnodes:
    if 6 in hitnodes:
        if 12 in hitnodes:
            if 18 in hitnodes:
                if 24 in hitnodes:
                    win = True
                    print('Bingo!')
    elif 2 in hitnodes:
        if 3 in hitnodes:
            if 4 in hitnodes:
                win = True
                print('Bingo!')
    elif 5 in hitnodes:
        if 10 in hitnodes:
            if 15 in hitnodes:
                if 20 in hitnodes:
                    win = True
                    print('Bingo!')
if 1 in hitnodes:
    if 6 in hitnodes:
        if 11 in hitnodes:
            if 16 in hitnodes:
                if 21 in hitnodes:
                    win = True
                    print('Bingo!')
if 2 in hitnodes:
    if 7 in hitnodes:
        if 12 in hitnodes:
            if 17 in hitnodes:
                if 22 in hitnodes:
                    win = True
                    print('Bingo!')
if 3 in hitnodes:
    if 8 in hitnodes:
        if 13 in hitnodes:
            if 18 in hitnodes:
                if 23 in hitnodes:
                    win = True
                    print('Bingo!')
if 4 in hitnodes:
    if 8 in hitnodes:
        if 16 in hitnodes:
            if 20 in hitnodes:
                win = True
                print('Bingo!')
    elif 9 in hitnodes:
        if 14 in hitnodes:
            if 19 in hitnodes:
                if 24 in hitnodes:
                    win = True
                    print('Bingo!')

最佳答案

您需要set.intersection()。您可以将所有数字放在一个集合中,然后检查它与您的列表的交集是否等于该集合。

my_set = {0, 6, 12, 18, 24}

if my_set.intersection(hitnodes) == my_set:
    # do stuff

关于python - python 中的宾果游戏逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42452560/

相关文章:

python - django教程manage.py sql polls不产生输出

python - Pandas:concat 函数删除了数据帧的先前排序

python - SQLAlchemy 使用路由查询错误的表

python-3.x - 使用规则在主页面完成后抓取 'next' 页面

python - 为什么FormSet在django2中只保存一个表单的数据

python - 如何使用turtle-graphics模块同步动画?

转换为字符串时, float 的 Python 列表以 ':' 结尾

Python:解析 SGML

python - 将传感器值从Beaglebone Black发送到Windows上的套接字服务器

python - TableauException(40200):服务器未在Docker容器中回叫我们Python 3.6