无法将输入与预期结果匹配

时间:2018-04-06 18:00:39

标签: vb.net random

我正在制作一个名为Mastermind的游戏的Visual Basic程序,其中有一定数量的引脚(由用户定义)和每个引脚的许多颜色选项(也由用户定义)。用户试图猜测随机生成的代码"通过选择每个引脚的颜色,系统会告诉您正确的引脚数量,以及正确的颜色数量,但引脚错误。

问题是,当我搜索我的数组并告诉它显示正确的引脚数时,它总是显示1或0,即使我将它设置为只有一个颜色选项(这意味着我应该让它们全部正确默认情况下)。这是我的代码(是的,我知道ColorsCorrect函数是空的,但这里没有关注):

 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

为什么它没有按预期工作?

1 个答案:

答案 0 :(得分:0)

首先,您需要启用Option Strict On。这将指出一些问题,但还不足以使其发挥作用。

接下来,那些While循环被要求改为For..Next循环。当我这样做时,我可能会或可能不会纠正一两个错误,看看我是否可以修复它。一些变量重命名是有序的 - 在“int”之前的所有内容上都没有任何意义。一些变量的范围也太广了。

随着代码的清理,现在更容易调试并发现RndInt()函数的行为不像您希望的那样 - 银行家的舍入不是那里所需要的;相反,你需要:

Function RndInt() As Integer
    Return CInt(Math.Truncate(nColours * Rnd())) + 1

End Function

对于它的价值,这是我最终让它发挥作用的地方:

Public Class Form1

    Dim nPegs As Integer = 0
    Dim nColours As Integer = 0

    Dim hiddenColours(-1) As Integer
    Dim userGuesses(-1) As Integer

    Dim GroupBox(-1) As GroupBox
    Dim RadioButton(-1, -1) As RadioButton

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim inputsValid As Boolean
        Do
            nPegs = CInt(InputBox("Enter The Number of Pegs (1 - 10):", "Number of Pegs"))
            nColours = CInt(InputBox("Enter The Number of Colors (1 - 9):", "Number of Colors"))
            inputsValid = nColours >= 1 AndAlso nColours <= 9 AndAlso nPegs >= 1 AndAlso nPegs <= 9

            If inputsValid Then
                MessageBox.Show("You Have Selected " & nPegs & " Pegs and " & nColours & " Colors")
            Else
                MessageBox.Show("Invalid Input. Please Try Again", "Error")
            End If

        Loop While Not inputsValid

        ReDim userGuesses(nPegs - 1)
        ReDim hiddenColours(nPegs - 1)
        ReDim GroupBox(nPegs - 1)
        ReDim RadioButton(nColours - 1, nPegs - 1)

        Randomize()
        ChooseColors()

        For pegNumber = 0 To nPegs - 1
            GroupBox(pegNumber) = New GroupBox
            GroupBox(pegNumber).Name = "grpPeg" & pegNumber
            GroupBox(pegNumber).Text = "Peg " & (pegNumber + 1)
            GroupBox(pegNumber).Size = New System.Drawing.Size(64, 238)
            GroupBox(pegNumber).Location = New System.Drawing.Point(59 + (70 * pegNumber), 12)

            For colourNumber = 0 To nColours - 1
                Dim rb As New RadioButton
                rb.Size = New System.Drawing.Size(31, 17)
                rb.Name = "radPeg" & pegNumber & "Color" & colourNumber
                rb.Text = (colourNumber + 1).ToString()
                rb.Location = New System.Drawing.Point(17, 29 + 23 * colourNumber)

                RadioButton(colourNumber, pegNumber) = rb
                GroupBox(pegNumber).Controls.Add(RadioButton(colourNumber, pegNumber))

            Next

            Me.Controls.Add(GroupBox(pegNumber))

        Next

    End Sub

    Private Sub btnCheckGuess_Click(sender As Object, e As EventArgs) Handles btnCheckGuess.Click
        For peg = 0 To nPegs - 1
            For colourNum = 0 To nColours - 1
                If RadioButton(colourNum, peg).Checked Then
                    userGuesses(peg) = colourNum + 1
                End If
            Next
        Next

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

    End Sub

    Sub ChooseColors()
        For i = 0 To nPegs - 1
            hiddenColours(i) = RndInt()
        Next

    End Sub

    Function RndInt() As Integer
        Return CInt(Math.Truncate(nColours * Rnd())) + 1

    End Function

    Function PegsCorrect() As Integer
        Dim nCorrect As Integer = 0

        For i = 0 To nPegs - 1
            If hiddenColours(i) = userGuesses(i) Then
                nCorrect += 1
            End If
        Next

        Return nCorrect

    End Function

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

        Return intColorsCorrect

    End Function

End Class