How to use a nested For Each loop

Hi Forum,

I am trying to build a Mastermind Game sequence where

for (i=0, i<UserGuess.length, i++)
if UserGuessArray(i) = RandomNumber(i)
RandomNumberIndexBoolean(i) = True
Cows Counter +1

else
for (j=0, j<UserGuess.length, j++)
if UserGuessArray(i) = RandomNumber(j) AND RandomNumberIndexBoolean(j) = False
Bulls Counter +1

else
RandomNumberIndexBoolean(j) = True

I am having difficulties anchoring (i) into (j), I am currently using a nested For Each loop

I hope I have explained the problem clearly.
Sorry for the tiny screenshot, I want to share as much code as possible
Any advice, even to re-write would be appreciated :slight_smile:

HI,

I think it should be as the following.

if UserGuessArray(i) = RandomNumber(j) AndAlso i<>j

Can you also try the following sample?

Sample20231016-4L.zip (3.4 KB)

Regards,

1 Like

Hi Yoichi,

This works!
Thank you so much for the advice :slight_smile:

My final code is:
if UserGuessArray(i) = RandomNumber(j) AND RandomNumberIndexBoolean(j) = False AND i<>j

This prevents a double count in the event where
RandomNumber = 1123
UserGuessArray = 0001
It will count RandomNumber(0) only once

I hope this helps others!

1 Like

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