所以我正在建立一个记忆游戏。这是一个2部分的问题。我有16个盒子使用,需要两次数字1-8。我已经将每个盒子设置为一个结构,并有一个随机数生成器,随机选择一个盒子的数字,然后随机将数字1-8放入其中。我遇到的问题是这个。我正在为我的盒子重复数字,并且对于将被放置在该盒子中的数字超过2次,有时没有一起使用的序列号。如何确保创建所有16个框,而不必为16个不同的实例创建代码。还需要确保使用1-8之间的每个单位数字并且只使用两次?
我的问题的第二部分是我在用户选择一个框时发生点击事件的问题。现在我无法弄清楚如何关联用户点击的框。我不想为16个不同的框编写代码,将数组链接到每个框,然后填充每个框的数组的猜测数。有没有办法可以将代码简化为简短的代码?
到目前为止,我已经包含了所有代码。
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Structure MemoryBox
Public intBox As Integer
Public intGuess As Integer
Public strGuess As String
End Structure
Private gameScore() As Integer
Private memoryGame(15) As MemoryBox
Dim countLoad As Integer = 0
Dim countScore As Integer = 0
Dim intScore As Integer
Private Sub LoadGame()
'this is where I am using the random numbers to make each box and populate it with a guess
Dim randomGen As New Random
Dim intMemBox As Integer
Dim intMemGuess As Integer
intScore = 0
If countLoad <= 15 Then
intMemBox = randomGen.Next(1, 16)
intMemGuess = randomGen.Next(1, 8)
memoryGame(countLoad).intBox = intMemBox
memoryGame(countLoad).intGuess = intMemGuess
memoryGame(countLoad).strGuess = intMemGuess.ToString
countLoad += 1
End If
End Sub
Private Sub GuessClick()
'trying to use this area for click event for each box click
lblMemory1.BackColor = Color.Green
lblMemory1.Text = memoryGame()
intScore += 1
lblScore.Text = intScore.ToString
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call LoadGame()
End Sub
Private Sub btnNewGame_Click(sender As Object, e As EventArgs) Handles btnNewGame.Click
gameScore(countScore) = intScore
countScore += 1
Dim outFile As IO.StreamWriter
outFile = IO.File.AppendText("score.txt")
outFile.WriteLine(intScore)
outFile.Close()
Call LoadGame()
End Sub
Private Sub btnHighScore_Click(sender As Object, e As EventArgs) Handles btnHighScore.Click
Array.Sort(gameScore)
lblHighScore.Text = gameScore(0).ToString
End Sub
Private Sub btnAllScores_Click(sender As Object, e As EventArgs) Handles btnAllScores.Click
Dim inFile As IO.StreamReader
Dim strInfo As String
If IO.File.Exists("score.txt") Then
inFile = IO.File.OpenText("score.txt")
Do Until inFile.Peek = -1
strInfo = inFile.ReadLine
Loop
inFile.Close()
Else
MessageBox.Show("Can't find the score.txt file", "High Score", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
MessageBox.Show(strInfo, "All Scores", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
End Class
答案 0 :(得分:0)
一个选项是使用您想要的数字填充数组,然后随机随机播放。使用至少运行数组长度一倍的循环,并使用迭代器值作为一个索引,并将其与随机选择的索引交换。
这样的事情:
Dim memoryGame(15) As MemoryBox
Dim randomarray() As Integer = {1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8}
Dim rnd As New Random(Now.Millisecond)
For I = 0 To 15
Dim randindex As Integer = rnd.Next(0, 16)
Dim temp As Integer = randomarray(I)
randomarray(I) = randomarray(randindex)
randomarray(randindex) = temp
Next
For I = 0 To 15
memoryGame(I).intBox = randomarray(I)
Next
从这里简单地遍历数组并将值分配给您的框
处理点击事件:
为每个框使用相同的处理程序。一种方法是为每个框添加Handles子句。有16个盒子,这可能会有点笨重。我建议在load事件处理程序中迭代这些框并使用AddHandler语句将处理程序添加到每个框中。此时sender
将始终指向已单击的框。只需将发件人转换为盒子的任何类型,即可访问盒子的所有属性。
如果框是按钮(Box1,Box2等),它将看起来像这样:
For Each b As Button In Me.Controls.OfType(Of Button).Where(Function(x) x.Name.StartsWith("Box"))
AddHandler b.Click, AddressOf Button_Click
Next
Private Sub Button_Click(sender As Object, e As EventArgs)
Dim clickedbutton As Button = DirectCast(sender, Button)
'access the properties here
End Sub