以下代码试图将棋子放在棋盘上的随机位置:
Function selectPlayer()
Dim rand As New Random()
Dim player As Integer = rand.Next(0, 3)
Return player
End Function
Function selectPiece()
Dim rand As New Random()
Dim piece As Integer = rand.Next(0, 4)
Return piece
End Function
Sub randomiseBoard(ByRef Board(,) As String)
Dim players() As String = {"W", "B", " "}
Dim pieces() As String = {"G", "E", "N", "M"}
For RankNo = 1 To BoardDimension
For FileNo = 1 To BoardDimension
Board(RankNo, FileNo) = players(selectPlayer) & pieces(selectPiece)
Next
Next
End Sub
片段的类型从数组中选择,包含代表它们的字符,并与播放器(B / W)组合以唯一地定义片段。
执行时,会生成电路板,但每个正方形都放置相同的电路板。
每次运行代码时,都会生成并显示新的部分。 然而 - 它并没有为每次迭代选择一个独特的部分。
这是为什么? 我怎么解决这个问题?
答案 0 :(得分:0)
初始化随机数生成器。
Randomize()
然后生成随机值