vendredi 6 avril 2018

Not able to match input with expected result

I'm making a Visual Basic program for a game called Mastermind, where there are a certain number of pins (defined by the user) and a number of color options for each pin (also defined by the user). The user tries to guess a randomly generated "code" by selecting the color for each pin, and the system tells you how many pins you got correct, and how many colors you got correct, but on the wrong pin.

The issue is, when I search my array and tell it to display the number of correct pins, it always displays either 1 or 0, even when I set it to only one color option (meaning I should get all of them right by default). Here is my code (yes I know the ColorsCorrect function is empty, but that one isn't concerned here):

 Imports System.Drawing.Color
Public Class Form1
    Dim intNumbOfPegs As Integer = 0
    Dim intNumbOfColors As Integer = 0
    Dim intPegsGenned As Integer = 0
    Dim intColorsGenned As Integer = 0
    Dim intColorsToGuess(-1) As Integer
    Dim intColorsGuessed(-1) As Integer
    Dim GroupBox(-1) As GroupBox
    Dim RadioButton(-1, -1) As RadioButton
    Dim blnGenNewNumbs As Boolean = True

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Do
            intNumbOfPegs = InputBox("Enter The Number of Pegs (1 - 10):", "Number of Pegs")
            intNumbOfColors = InputBox("Enter The Number of Colors (1 - 9):", "Number of Colors")
            If intNumbOfColors = 0 Or intNumbOfColors > 9 Or intNumbOfPegs = 0 Or intNumbOfPegs > 10 Then
                MessageBox.Show("Invalid Input. Please Try Again", "Error")
            Else
                MessageBox.Show("You Have Selected " & intNumbOfPegs & " Pegs and " & intNumbOfColors & " Colors")
            End If
        Loop While intNumbOfColors = 0 Or intNumbOfColors > 9 Or intNumbOfPegs = 0 Or intNumbOfPegs > 10
        ReDim intColorsGuessed(intNumbOfPegs - 1)
        ReDim intColorsToGuess(intNumbOfPegs - 1)
        ReDim GroupBox(intNumbOfPegs - 1)
        ReDim RadioButton(intNumbOfColors - 1, intNumbOfPegs - 1)
        Do While intPegsGenned < intNumbOfPegs
            intPegsGenned = intPegsGenned + 1
            GroupBox(intPegsGenned - 1) = New GroupBox
            GroupBox(intPegsGenned - 1).Name = "grpPeg" & intPegsGenned
            GroupBox(intPegsGenned - 1).Text = "Peg " & intPegsGenned
            GroupBox(intPegsGenned - 1).Size = New System.Drawing.Size(64, 238)
            GroupBox(intPegsGenned - 1).Location = New System.Drawing.Point(59 + (70 * intPegsGenned), 12)
            Do While intColorsGenned < intNumbOfColors
                intColorsGenned = intColorsGenned + 1
                RadioButton(intColorsGenned - 1, intPegsGenned - 1) = New RadioButton
                RadioButton(intColorsGenned - 1, intPegsGenned - 1).Size = New System.Drawing.Size(31, 17)
                RadioButton(intColorsGenned - 1, intPegsGenned - 1).Name = "radPeg" & intPegsGenned & "Color" & intColorsGenned
                RadioButton(intColorsGenned - 1, intPegsGenned - 1).Text = intColorsGenned
                RadioButton(intColorsGenned - 1, intPegsGenned - 1).Location = New System.Drawing.Point(17, 6 + (23 * intColorsGenned))
                'AddHandler RadioButton(intColorsGenned - 1).CheckedChanged, AddressOf AllRadButtons_CheckedChanged
                GroupBox(intPegsGenned - 1).Controls.Add(RadioButton(intColorsGenned - 1, intPegsGenned - 1))
            Loop
            intColorsGenned = 0
            Controls.Add(GroupBox(intPegsGenned - 1))
        Loop
    End Sub

    Private Sub btnCheckGuess_Click(sender As Object, e As EventArgs) Handles btnCheckGuess.Click
        Dim intColorToCheck As Integer = 0
        Dim intPegToCheck As Integer = 0

        If blnGenNewNumbs = True Then
            ChooseColors()
            blnGenNewNumbs = False
        End If

        Do While intPegToCheck < intNumbOfPegs
            Do While intColorToCheck < intNumbOfColors
                If RadioButton(intColorToCheck, intPegToCheck).Checked = True Then
                    intColorsGuessed(intPegToCheck) = intColorToCheck + 1
                End If
                intColorToCheck = intColorToCheck + 1
            Loop
            intPegToCheck = intPegToCheck + 1
        Loop

        lblGuessDisplay.Text = lblGuessDisplay.Text & " Correct Pegs: " & PegsCorrect() & " Correct Colors: " & ColorsCorrect()

    End Sub

    Sub ChooseColors()
        Dim intNumbsFilled As Integer = 0
        Do While intNumbsFilled < intNumbOfPegs
            intColorsToGuess(intNumbsFilled) = RndInt()
            intNumbsFilled = intNumbsFilled + 1
        Loop
    End Sub
    Function RndInt()
        Dim intNumbToSend As Integer = 0
        Randomize()
        intNumbToSend = (Int(intNumbOfColors * Rnd()) + 1)
        Return intNumbToSend
    End Function

    Function PegsCorrect() As Integer
        Dim intPegsCorrect As Integer = 0
        Dim intPegCorrectToCheck As Integer = 0

        Do While intPegCorrectToCheck <= (intNumbOfPegs - 1)
            If intColorsToGuess(intPegCorrectToCheck) = intColorsGuessed(intPegCorrectToCheck) Then
                intPegsCorrect = intPegsCorrect + 1
            End If
            intPegCorrectToCheck = intPegCorrectToCheck + 1
        Loop
        Return intPegsCorrect
    End Function

    Function ColorsCorrect() As Integer
        Dim intColorsCorrect As Integer = 0

        Return intColorsCorrect
    End Function

End Class

Why is it not working as intended?




Aucun commentaire:

Enregistrer un commentaire