欢迎光临散文网 会员登陆 & 注册

赛尔号:哀目黑子日记本破译密码小游戏的程序分享

2020-03-21 21:21 作者:摸鱼的橙汁  | 我要投稿

赛尔号“哀目黑子的日记本】破译密码”小游戏的程序分享

作者:橙汁


游戏背景

本周(2020.03.20)赛尔号更新了一个非常有意思的解密小游戏,“【哀目黑子的日记本】破译密码”。

游戏要求我们完成四位数字(颜色)解密小游戏,以便了解异世界的秘密,获得养成道具。

“破译密码”小游戏的游戏规则如下所示。简单来说就是选四个颜色,猜密码。

单机程序

我觉得这个小游戏挺好玩的,所以就把它做成单机版的小游戏了,无聊的时候可以随时随地玩。

这是电脑Windows的版本。(界面只是用了登录器的外观,核心只是小游戏,不是游戏网页。)

这是手机Android的版本。(安卓手机安装程序不需要上架应用商店,比较方便;但是苹果手机我就爱莫能助了,即使做出来也无法安装。)

这两个单机版小游戏的程序文件我已经分享到QQ群与百度网盘了,下载途径待会儿请见置顶评论吧。


解密程序

“密码破译”这种事情,交给计算机才是最方便的,所以我又写了一个解密程序,帮助玩家“解密”小游戏。

 玩家只需要根据提示,输入锁孔角数与每次提示的绿灯个数,程序就可以帮忙给出答案。

这小游戏的解密程序文件我已经分享到QQ群与百度网盘了,下载途径待会儿请见置顶评论吧。


程序代码

解密程序是使用Python写的,在这里我就献丑,分享一下源码吧。

大体的设计思路是这样的:

  1. 找出每个颜色的数量。

  2. 将颜色的数量组合进行分类。

  3. 选取数量最少的颜色,进行逐位排查,这里为了缩短步数,依次使用两种颜色,最少的颜色选1个,另一种颜色选多个。

  4. 整理分析情况,得出最终结果。

程序代码如下所示。


def reLoad():

    global colorNum

    colorNum = [0,0,0,0,0]

    global colorName

    colorName = ['黄','蓝','绿','红','紫']

    print('')


def str2num(s:str):

    s = s.replace(' ','')

    try:

       num = float(s)

    except:

        try:

            num = float(eval(s))

        except:

            num = 0.0

    return num


def getListSum(li):

    sum = 0

    for i in li:

        sum += i

    return sum

    

def getNumType():

    if 4 in colorNum:

        return 0

    if 3 in colorNum:

        return 1

    if colorNum.count(2) == 2:

        return 2

    if 1 in colorNum and 2 in colorNum:

       return 3

    return 4


def Do():

    NumType = getNumType()

    finish = False

    if NumType == 0:

        for i in range(5):

            if colorNum[i] == 4:

                print('【答案】' + colorName[i] + ' ' + colorName[i] + ' ' + colorName[i] + ' ' + colorName[i])

                finish = True

                break

    elif NumType == 1:

        big = -1

        small = -1

        for i in range(5):

            if colorNum[i] == 3:

                big = i

            elif colorNum[i] == 1:

                small = i

        for i in range(3):

            check = False

            guess = [colorName[big],colorName[big],colorName[big],colorName[big]]

            guess[i] = colorName[small]

            guess = ' '.join(guess)

            print('猜:' + guess)

            while not(check):

                green = int(str2num(input('请输入绿灯个数:')))

                if green == 2:

                    finish = False

                    check = True

                elif green == 4:

                    finish = True

                    check = True

                    print('【答案】' + guess)

                    break

                else:

                    print('输入错误,请重新输入。')

        if finish == False:

            guess = [colorName[big],colorName[big],colorName[big],colorName[big]]

            guess[3] = colorName[small]

            guess = ' '.join(guess)

            print('【答案】' + guess)

    elif NumType == 2:

        A = -1

        B = -1

        for i in range(5):

            if colorNum[i] == 2:

                if A < 0:

                    A = i

                else:

                    B = i

        check0 = False

        while not check0:

            isB = [0,0,0,0]

            for i in range(3):

                check = False

                guess = [colorName[A],colorName[A],colorName[A],colorName[A]]

                guess[i] = colorName[B]

                guess = ' '.join(guess)

                print('猜:' + guess)

                while not(check):

                    green = int(str2num(input('请输入绿灯个数:')))

                    if green == 3:

                        isB[i] = 1

                        finish = False

                        check = True

                    elif green == 1:

                        isB[i] = 0

                        finish = False

                        check = True

                    else:

                        print('输入错误,请重新输入。')

            sum = getListSum(isB)

            if sum == 1:

                isB[3] = 1

                check0 = True

            elif sum == 2:

                check0 = True

            else:

                print('输入错误,请重新输入。')

        guess = ''

        for i in range(4):

            if isB[i] == 1:

                guess += colorName[B] + ' '

            else:

                guess += colorName[A] + ' '

        finish = True

        print('【答案】' + guess)

    elif NumType == 3:

        big = -1

        sml1 = -1

        sml2 = -1

        for i in range(5):

            if colorNum[i] == 2:

                big = i

            elif colorNum[i] == 1:

                if sml1 < 0:

                    sml1 = i

                else:

                    sml2 = i

        check0 = False

        while not check0:

            arr = []

            greens = []

            for i in range(3):

                check = False

                guess = [colorName[big],colorName[big],colorName[big],colorName[big]]

                guess[i] = colorName[sml1]

                guess = ' '.join(guess)

                print('猜:' + guess)

                while not check:

                    green = int(str2num(input('请输入绿灯个数:')))

                    if green == 1:

                        arr.append(big)

                        greens.append(green)

                        finish = False

                        check = True

                    elif green == 2:

                        arr.append(sml2)

                        greens.append(green)

                        finish = False

                        check = True

                    elif green == 3:

                        arr.append(sml1)

                        greens.append(green)

                        finish = False

                        check = True

                    else:

                        print('输入错误,请重新输入。')

            if getListSum(greens) > 3:

                check0 = True

            else:

                print('输入错误,请重新输入。')

        if (sml1 in arr or sml2 in arr) and arr.count(big) < 2:

            arr.append(big)

        else:

            if sml1 in arr:

                arr.append(sml2)

            else:

                arr.append(sml1)

        guess = colorName[arr[0]] + ' ' + colorName[arr[1]] + ' ' + colorName[arr[2]] + ' ' + colorName[arr[3]]

        finish = True

        print('【答案】'+guess)

    else:

        A = -1

        B = -1

        C = -1

        D = -1

        for i in range(5):

            if colorNum[i] == 1:

                if A < 0:

                    A = i

                elif B < 0:

                    B = i

                elif C < 0:

                    C = i

                else:

                    D = i

        greens = []

        arr = []

        check = False

        while not check:

            guess = [colorName[B],colorName[A],colorName[A],colorName[A]]

            guess = ' '.join(guess)

            print('猜:' + guess)

            green = int(str2num(input('请输入绿灯个数:')))

            if green < 3:

                greens.append(green)

                check = True

            else:

                print('输入错误,请重新输入。')

        check = False

        while not check:

            guess = [colorName[C],colorName[A],colorName[A],colorName[A]]

            guess = ' '.join(guess)

            print('猜:' + guess)

            green = int(str2num(input('请输入绿灯个数:')))

            if green < 3 and greens[0] + green < 4:

                greens.append(green)

                check = True

            else:

                print('输入错误,请重新输入。')

        temp = []

        if greens[0] == 0 and greens[1] == 0:

            arr.append(A)

            temp = [A,C,B,B]

            tmp = D

        elif greens[0] == 2:

            arr.append(B)

            temp = [B,C,A,A]

            tmp = D

        elif greens[1] == 2:

            arr.append(C)

            temp = [C,B,A,A]

            tmp = D

        else:

            arr.append(D)

            temp = [D,B,A,A]

            tmp = C

        check = False

        while not check:

            guess = [colorName[temp[0]],colorName[temp[1]],colorName[temp[2]],colorName[temp[3]]]

            guess = ' '.join(guess)

            print('猜:' + guess)

            green = int(str2num(input('请输入绿灯个数:')))

            if green == 2:

                arr.append(tmp)

                check = True

            elif green == 3:

                arr.append(temp[1])

                check = True

            elif green == 1:

                arr.append(temp[2])

                check = True

            else:

                print('输入错误,请重新输入。')

        temp = [arr[0],arr[1]]

        for i in range(5):

            if i not in temp and colorNum[i] == 1:

                temp.append(i)

        check = False

        while not check:

            guess = [colorName[temp[0]],colorName[temp[1]],colorName[temp[2]],colorName[temp[3]]]

            guess = ' '.join(guess)

            print('猜:' + guess)

            green = int(str2num(input('请输入绿灯个数:')))

            if green == 2:

                arr.append(temp[3])

                arr.append(temp[2])

                check = True

            elif green == 4:

                arr.append(temp[2])

                arr.append(temp[3])

                check = True

            else:

                print('输入错误,请重新输入。')

        guess = colorName[arr[0]] + ' ' + colorName[arr[1]] + ' ' + colorName[arr[2]] + ' ' + colorName[arr[3]]

        finish = True

        print('【答案】'+guess)


print('本程序为赛尔号《【哀目黑子日记本】破译密码》小游戏的解密程序。')

print('作者:橙汁')

print('使用方法:根据程序的提示进行选取猜测,并输入绿灯个数。')

print('\n')

while True:

    reLoad()

    check = False

    while not check:

        n = int(str2num(input('请输入锁孔角的个数:')))

        if n > 2 and n < 6:

            check = True

        else:

            print('输入错误,请重新开始!')

    i = 0

    while i < 5:

        print('猜:' + colorName[i] + ' ' + colorName[i] + ' ' + colorName[i] + ' ' + colorName[i])

        colorNum[i] = str2num(input('请输入绿灯个数:'))

        colorNum[i] = int(colorNum[i])

        sum = getListSum(colorNum)

        if sum > 4:

            print('输入错误,请重新开始!')

            i = -1

        elif sum == 4:

            break

        elif i == n - 2:

            colorNum[i + 1] = 4 - sum

            break

        i += 1

    sum = getListSum(colorNum)

    if sum < 4:

        print('输入错误,请重新开始!')

    else:

       Do()



(作者橙汁,文章书写不易,转载或引用请注明出处,谢谢。)


赛尔号:哀目黑子日记本破译密码小游戏的程序分享的评论 (共 条)

分享到微博请遵守国家法律