samedi 21 juillet 2018

test an array against another to see if the positions match, one has the right value in the wrong spot, or a value isn't in the array in VB

I'm making the game Mastermind in visual basic, and am still fairly new to programming. I am up to the part where I need to test the user's guess against the hidden code, both of which are currently in 2d arrays. The program needs to test whether the user's 4 colour guess has the colours in the correct spot, the correct colours in the wrong spot, or the incorrect colour altogether. It will then randomise this and give the user feedback (green for completely correct, red for completely incorrect & black for partially correct). Currently, I am just using if statements and testing a whole bunch of values for each, but this is one: not efficient, and two: the black will not show, merely giving red if it isn't 100% correct. This is the current code:

While i <= 3
        If UserGuessArray(NoofGuesses, i) = HiddenCode(NoofGuesses, i) Then
            UserFeedbackArray(NoofGuesses, i) = "Green"
        ElseIf ((UserGuessArray(NoofGuesses, i) <> HiddenCode(NoofGuesses, 0)) AndAlso (UserGuessArray(NoofGuesses, i) <> HiddenCode(NoofGuesses, 1)) AndAlso (UserGuessArray(NoofGuesses, i) <> HiddenCode(NoofGuesses, 2)) AndAlso (UserGuessArray(NoofGuesses, i) <> HiddenCode(NoofGuesses, 3))) Then
            UserFeedbackArray(NoofGuesses, i) = "Red"
        ElseIf UserGuessArray(NoofGuesses, i) = HiddenCode(NoofGuesses, 0) And HiddenCode(NoofGuesses, 0) <> HiddenCode(NoofGuesses, i) Or HiddenCode(NoofGuesses, 1) And HiddenCode(NoofGuesses, 1) <> HiddenCode(NoofGuesses, i) Or HiddenCode(NoofGuesses, 2) And HiddenCode(NoofGuesses, 2) <> HiddenCode(NoofGuesses, i) Or HiddenCode(NoofGuesses, 3) And HiddenCode(NoofGuesses, 3) <> HiddenCode(NoofGuesses, i) Then
            UserFeedbackArray(NoofGuesses, i) = "Black"
        End If

        i = i + 1
End While

Any help would be much appreciated. Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire