Guessing Game on UIPath Double Counting issue

Hi Forum,

I am building a Mastermind game in StudioX workflow, and I am encountering an issue where my values are being double counted

For example
RandomNumber is 2370
UserGuess is 7070

The desired output is 0P 2N
(0 correct numbers in wrong position), (2 correct numbers in correct position)

The current output is 2P 2N
(2 correct numbers in wrong position), (2 correct numbers in correct position)

My current code sequence is as follows

declare variables
RandomNumberArray
RandomNumberArrayBoolean

While UserTries < MaxNumberOfTries
Receive UserGuess into UserGuessArray
Reset RandomNumberArrayBoolean to {False, False, False, False}

for each Enumerable.Range(0,4)
item i
if UserGuessArray(i) = RandomNumberArray(i)
then assign RandomNumberArrayBoolean(i) = True
N+1
else do nothing

for each Enumerable.Range(0,4)
item j
if UserGuessArray(i) = RandomNumberArray(j) AND RandomNumberArrayBoolean(j) = False And i <> j
then assign RandomNumberArrayBoolean(j) = True
P+1
break;
else do nothing

I have attached my code if I did not explain very well. I have annotated the sequences inside too
Any advice would be very appreciated :slight_smile:
Any advice on best practices would be readily appreciated too!

Declare variables:

  • RandomNumberArray
  • RandomNumberArrayBoolean
  • UserGuessArray
  • UserTries
  • MaxNumberOfTries
  • P (Initialize to 0)
  • N (Initialize to 0)

While UserTries < MaxNumberOfTries
Receive UserGuess into UserGuessArray
Reset RandomNumberArrayBoolean to {False, False, False, False}

For Each i in Enumerable.Range(0, 4)
    If UserGuessArray(i) = RandomNumberArray(i)
    Then
        Assign RandomNumberArrayBoolean(i) = True
        Assign P = P + 1
    End If

For Each i in Enumerable.Range(0, 4)
    For Each j in Enumerable.Range(0, 4)
        If UserGuessArray(i) = RandomNumberArray(j) And RandomNumberArrayBoolean(j) = False And i <> j
        Then
            Assign RandomNumberArrayBoolean(j) = True
            Assign N = N + 1
            Break
        End If
    End For
End For

Log Message "Result: " + P + "P " + N + "N"
If P = 4
Then
    Log Message "Congratulations! You've guessed the correct number."
    Break
End If

Assign UserTries = UserTries + 1

End While

1 Like

Dear Kumar,

I was indeed missing another (i) loop before my (j) iteration to set the Booleans to True.
Your solution has helped solve this issue.

Thank you so much for your input and effort in helping me out!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.