Mastermind检查结果python

时间:2017-12-12 13:05:47

标签: python algorithm

目前我正在尝试制作一个类似游戏的策划类型代码。 我目前正在努力检查用户猜测是否正确。

colours = ["R","O","Y","G","B","I","V"]

n = 1
ans = random.choices(colours, k = 4)
print(*ans)
guess = input("what is your guess? ")

[guess[i:i+n] for i in range(0, len(guess), n)]

guess = (guess.upper())

def check():
    correct = False
    right = 0
    wrong = 0
    if guess == ans:
        correct = True
    elif guess != ans:
        correct = False
        for i in range(0,(len(guess))):
            for x in range(0,(len(ans))):
                if guess[i] in ans:
                    wrong += 1

                    break


    print(right,"black pegs")
    print(wrong,"white pegs")
    return
check()

如果ans = "R O Y Y"guess = "R G O O" 我希望输出

1 white pegs
1 black pegs

实际上给我的是

3 white pegs
0 black pegs

白色钉子意味着正确的颜色,错误的地方,黑色的钉子意味着正确的颜色,正确的位置

我如何编写黑钉的编码?

4 个答案:

答案 0 :(得分:1)

我认为你的部分问题在于你没有将逻辑分成功能,这样可以更容易理解需要做什么,所以在下面我将工作分成count_whitecount_blackcheck

您注意到的第一件事是["R","O","Y","Y"]["R","G","O","O"]给出了3个白钉,因为R被归类为白色(尽管它应该是黑色的)并且O被计算在O中的ans因此3,白钉实际上比黑钉更难计算。

可以使用

计算黑钉
black = 0
for i in range(len(guess)):
    if guess[i] == ans[i]:
        black += 1

或更多pythonic和函数:

def count_black(ans, guess):
    return sum([ans[i] == guess[i] for i in range(len(ans))])

对于白人,您只需要检查两个列表中是否出现颜色,如果有,请将其从ans中删除,这样您就不会多次检查它:

def count_white(ans, guess):
    temp_ans = ans[:]
    white = 0
    for i in guess:
        if i in temp_ans:
            temp_ans.remove(i)
            white += 1
    return white

请注意,如果一个挂钩处于正确的位置,上面的会忽略,这没关系,因为我们知道有多少位于黑色计数的正确位置。

现在已经将这些功能转移到了check只能是:

def check(ans, guess):
    white = count_white(ans, guess)
    black = count_black(ans, guess)
    white -= black

    print(black,"black pegs")
    print(white,"white pegs")

    return black == 4

导致:

>>> check(["R","O","Y","Y"],["R","G","O","O"])
1 black pegs
1 white pegs
False

请注意,返回的False是否正确。

因此,您的所有代码都可以写成:

import random
colours = ["R","O","Y","G","B","I","V"]
ans = [random.choice(colours) for i in range(4)]
print(''.join(ans))

def count_white(ans, guess):
    temp_ans = ans[:]
    pegs = 0
    for i in guess:
        if i in temp_ans:
            temp_ans.remove(i)
            pegs += 1
    return pegs

def count_black(ans, guess):
    return sum([ans[i] == guess[i] for i in range(len(ans))])

def check(ans, guess):
    white = count_white(ans, guess)
    black = count_black(ans, guess)
    white -= black

    print(black,"black pegs")
    print(white,"white pegs")

    return black == 4

guess = []
while guess != ans:
    guess = list(input("what is your guess?").upper())
    check(ans, guess)

答案 1 :(得分:0)

guess[i] == ans 

不应该吗?

guess[i] == ans[x]

因为你比较字符,而不是字符串整数:)

而且我不确定这是否能真正解决您的问题,因为您必须跟踪您已计算的颜色以及从现在开始的颜色

答案 2 :(得分:0)

事实上,它对于黑钉来说是微不足道的,但对于白钉来说稍微复杂一些,因为你当前的算法不能正确处理重复的颜色。所以恕我直言,你可以检查猜测的每个元素2次,首先是正确位置的正确颜色,第二个是错误位置的正确颜色删除找到的元素以避免多次计数。一种简单的方法是在循环之前采用可修改的副本。

假设ansguess都是可迭代的,您的代码可能会变为:

def check(ans, guess):
    correct = False
    right = 0
    wrong = 0
    if guess == ans:
        correct = True
        right = len(guess)
    else:
        correct = False
        test = list(ans)      # take a modifiable copy
        for i, colour in enumerate(guess):
            if ans[i] == colour:     # is it at the correct place?
                right += 1
            elif colour in test:     # if not is it present?
                test.remove(colour)  # count it only one
                wrong += 1
    print(right,"black pegs")
    print(wrong,"white pegs")
    return correct

答案 3 :(得分:0)

我不完全理解你要用你的代码完成什么,因为你没有指定如果一个角色在猜测中出现多次而在ans中只出现一次会发生什么。无论如何,这应该可以为您提供所需的输出。

def check():
    ans = "ROYY"
    guess = "RGOO"
    correct = False
    right = 0
    wrong = 0
    if guess == ans:
        correct = True

    for i in ans:
        j = 0
        while(j < len(guess)):
            if i in guess and i == guess[j]:
                right += 1
                break
            elif i in guess and i != guess[j]:
                wrong += 1
                break
            elif i not in guess:
                break;
        j += 1

    print(right,"black pegs")
    print(wrong,"white pegs")
    return
check()