Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
There is a game. You guess 4 digits of a random number. It outputs the amount of correct digits in the correct place but also correct digits in the wrong place. Ex if the code was 2188 and guess output would be 1888 1 and 1. This piece of code causes trouble logically. Assume tag_guess is an array of 4 falses.

The problem is that it outputs incorrectly. When the code was 4405 and guess 4444 it output 2 correct BUT 2 correct in wrong place. Logically speaking it should output 2 correct 0 correct in wrong place. It should break out of the if statement and ignore the 44 in 4405 as it has already been compared and marked as true. I need to make an array named tag_code and sum it all up using a for and while loop. How do I do so?


Python
wronPlace= 0
if tag_guess[0]== False:
    if guess[0]== code[1]:
        tag_guess[0]== True
        wronPlace= (wronPlace+1)
    elif guess[0]== code[2]:
        tag_guess[0]== True
        wronPlace= (wronPlace+1)
    elif guess[0]== code[3]:
        tatg_guess[0]== True
        wronPlace= (wronPlace+1)
if tag_guess[1]== False:
    if guess[1]== code[0]:
        tag_guess[1]== True
        wronPlace= (wronPlace+1)
    elif guess[1]== code[2]:
        tag_guess[1]== True
        wronPlace= (wronPlace+1)
    elif guess[1]== code[3]:
        tag_guess[1]== True
        wronPlace= (wronPlace+1)
if tag_guess[2]== False:
    if guess[2]== code[0]:
        tag_guess[2]== True
        wronPlace= (wronPlace+1)
    elif guess[2]== code[1]:
        tag_guess[2]== True
        wronPlace= (wronPlace+1)
    elif guess[2]== code[3]:
        tag_guess[2]== True
        wronPlace= (wronPlace+1)
if tag_guess[3]== False:
   if guess[3]== code[0]:
       tag_guess[3]== True
       wronPlace= (wronPlace+1)
   elif guess[3]== code[1]:
       tag_guess[3]== True
       wronPlace= (wronPlace+1)
   elif guess[3]== code[2]:
       tag_guess[3]== True
       wronPlace= (wronPlace+1)


What I have tried:

I've tried doing a for loop but I suck at doing anything more complex until I can lay down a basic layer of understanding toward the simple code first. The code should skip over numbers in the code and guess which have been marked as true/correct as the other. Help!
Posted
Updated 3-Apr-18 6:03am
v3

Looks like you have donne a step in right direction, but it is still more complicated.
When a guess match a code in correct position, you have to make sure the same guess will not be checked for wrong position and that same code will not be checked for wrong position either. And when a guess match a code for wrong position, you have to make sure you will not check that code again.

Advice: make guess an array just like code and arr, it open the door for loops and code size reduction once it works.

[Update]
Quote:
Could you please help me with doing so? I already have an arr but can't seem to use it to do so

you have created arr to prevent matching 2 times the same guess, you need another array to prevent matching 2 times the same code.
- naming an array arr is a bad idea, since it is used to tag matched guess, you can name it tag_guess.
- create another array named tag_code and you use it to prevent marching 2 times the same code.
- Rather than using variables for each guess, use an array. It will make your code suited for use of loops (and code size reduction).

[Update]
Your code should look like:
Python
if tag_guess[0]== False:
    if guess[0]== code[1] and tag_code[1]== False:
        tag_guess[0]== True
        tag_code[1]== True
        wronPlace= (wronPlace+1)
 
Share this answer
 
v3
Comments
Member 13672432 17-Feb-18 13:13pm    
Could you please help me with doing so? I already have an arr but can't seem to use it to do so
Patrice T 17-Feb-18 16:44pm    
see solution update.
Member 13672432 20-Feb-18 16:06pm    
how should the array tag_code be ordered?
Patrice T 20-Feb-18 16:32pm    
Same as code
Create a list that contains the answer.
Loop1:
    Compare each digit of the guess with each digit of the list.
    For each point in the answer where the digits match do:
        Remove the digit from the list, and the guess, or replace them with some other character
        Add 1 to the "right digit in the right place" count
    Repeat Loop1 for all digits in the guess

Loop2:
    For each remaining digit in the guess
    If the digit can be found in the answer then do:
        Add 1 to the "right digit in the wrong place" count
        remove the digit from the answer.
    Repeat Loop2 until all comparisons have been made, or no digits remain in either list
 
Share this answer
 
Comments
Member 13672432 17-Feb-18 13:14pm    
Thanks I'll try doing this but am still a little confused as how to program it. Should it be a while loop or a for?
Richard MacCutchan 18-Feb-18 3:02am    
I used a for loop in each case, using a range() based on the length of the list. You need a while loop round the whole thing to allow the user to enter more guesses.
Member 13672432 20-Feb-18 16:16pm    
thank you for the help!
You don't really need 2 for loops. Try this:

from collections import Counter


def main():
    answers = [3, 3, 4, 4]
    guesses = [3, 5, 6, 4]

    answers_counter = Counter(answers)
    guesses_counter = Counter(guesses)
    diff_counter = answers_counter - guesses_counter
    number_of_all_correct_digits = len(answers) - len(diff_counter.values())

    number_of_correct_digits_in_correct_place = len(
        list(filter(lambda x: x == 0, [a - g for a, g in zip(answers, guesses)])))

    number_of_correct_digits_in_wrong_place = number_of_all_correct_digits - number_of_correct_digits_in_correct_place

    return number_of_correct_digits_in_correct_place, number_of_correct_digits_in_wrong_place

if __name__ == "__main__":
    r1, r2 = main()
    print(r1)
    print(r2)

""" r1 and r2 are the correct digits in wrong place and correct digits in correct place

You of course need to make "guesses" and actual input for the array but here's the solution.
 
Share this answer
 
v2
Comments
Member 13672432 3-Apr-18 12:10pm    
Thank you!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900