如何检查列表中的值而不重复检查元素?

时间:2014-08-08 10:59:59

标签: python python-3.x

所以我正在做一个游戏,游戏的目标是猜测计算机随机生成的隐藏数字序列。

得分角色是:

  1. 对于每一个正确的猜测,右边的位置-i会将1加到黑色(开始为0)
  2. 对于每一个正确的猜测但不在正确的位置-i会将1加到白色(开始为0)
  3. 注意*每次猜测只应计算一次;要么+1黑色,+1白色,要么根本没有。对于1次猜测,它不能同时为+1黑色和+1白色(这是我当前的问题)

    问题是当我尝试计算分数时(我会举一个例子来说明这一点)

    this is the example (not the code, but someone indented it)
    the hidden sequence is [4, 1, 4, 9]
    
    entered sequence of guess: 1111
    score: [1, 3] #[black, white]
    expected score: [1, 0] #+1 on black cause of 2nd number 2 being in right position; +0 on white because there is only one 1 in the hidden sequence, and that one is in right position already
    
    entered sequence of guess: 4141
    score: [3, 1]
    expected score: [3, 0] #+3 on black cause 414 are in the right sequence; +0 on white because there is only one 1 in the hidden sequence, and that one is in right position already
    
    entered sequence of guess: 1414
    score: [0, 4]
    expected score: [0, 3] #+3 on white because there are two 4's and one 1 in the sequence; +0 on black because there are no correct numbers in right position
    

    以下是我目前拥有的以及理解和解决问题所需的代码

    import random
    
    highest = 9
    lowest = 1
    num1 = random.randint(lowest, highest)
    num2 = random.randint(lowest, highest)
    num3 = random.randint(lowest, highest)
    num4 = random.randint(lowest, highest)
    hidden = [num1, num2, num3, num4]
    
    #starting from this part, everything is nested in a while loop to let me guess until a certain amount of number
    #if i have exceeded the number of attempts allowed, or if i have guessed the right sequence; the loop will end
    guess = int(input("enter guess here: "))
    a = guess // 1000
    b = guess // 100 % 10
    c = guess // 10 % 10
    d = guess % 10
    P1guess = [a, b, c, d]
    
    white = 0
    black = 0
    
    checkctr = 0
    x = 0
    #this loop right here is my problem
    while checkctr < 4:
        if P1guess[x] == hidden[x]:
            black += 1
        elif P1guess[x] in hidden:
            white += 1
        x += 1
            checkctr += 1
    
    print("black: " + str(black))
    print("white: " + str(white))
    

2 个答案:

答案 0 :(得分:0)

如果我正确理解了问题,您只想对该号码进行一次评分。假设生成的列表和猜测具有相同的长度,解决它的最佳方法是迭代生成的列表:

for i, val in enumerate(hidden):
    if hidden[i] == P1guess[i]:
        black += 1
    #I assume we grade the number only once
    elif val in P1guess:
        white += 1

你需要澄清一下你的得分规则。考虑到生成的序列是[4,1,1,1],你的猜测是[1,9,9,9],它会给你[0,3]的分数。如果你希望在这种情况下它是[0,1],那么代码需要更新。

答案 1 :(得分:0)

我想你在看:http://en.wikipedia.org/wiki/Mastermind_%28board_game%29

white = 0
black = 0
hidden = [4, 1, 4, 9]
P1guess = [1, 1, 1, 4]
import copy
non_exact_hidden = copy.copy(hidden)
non_exact_match = copy.copy(P1guess)
checkctr = 0
x = 0
while checkctr < 4:
    if P1guess[x] == hidden[x]:
        black += 1
        # remove from the list that we consider non exact match
        non_exact_hidden.remove(hidden[x])
        non_exact_match.remove(hidden[x])
    x += 1
    checkctr += 1

# go over non exact matches and score them appropriately 
for guess in non_exact_match:
    if guess in non_exact_hidden:
        white += 1

这将给你1,1

我认为它更接近你的期望吗? 它虽然不太优雅......