Vb.net VB 2D数组可能没有返回正确的值/需要帮助计算2D数组中布尔数
我试图用VisualBasic编写Conways生命游戏,但计算周围活细胞数量的代码似乎有问题。代码如下所示:Vb.net VB 2D数组可能没有返回正确的值/需要帮助计算2D数组中布尔数,vb.net,conways-game-of-life,Vb.net,Conways Game Of Life,我试图用VisualBasic编写Conways生命游戏,但计算周围活细胞数量的代码似乎有问题。代码如下所示: Module Module1 Dim ScreenHeight As Integer = 30 Dim ScreenWidth As Integer = 30 Dim Field(ScreenWidth - 1, ScreenHeight - 1) As Boolean Dim FieldBuffer(ScreenWidth - 1, ScreenHeight - 1) As Boo
Module Module1
Dim ScreenHeight As Integer = 30
Dim ScreenWidth As Integer = 30
Dim Field(ScreenWidth - 1, ScreenHeight - 1) As Boolean
Dim FieldBuffer(ScreenWidth - 1, ScreenHeight - 1) As Boolean
Sub Main()
Console.SetWindowSize(ScreenWidth, ScreenHeight)
Field(10, 9) = True
Field(10, 10) = True
Field(10, 11) = True
While True
Dim x = 0
While (x < ScreenWidth)
Dim y = 0
While (y < ScreenHeight)
Console.SetCursorPosition(x, y)
If ((x <> 0 And x <> ScreenWidth - 1) And (y <> 0 And y <> ScreenWidth - 1)) Then
Dim count = Field(x + 1, y) + Field(x - 1, y) + Field(x, y + 1) + Field(x, y - 1) _
+ Field(x + 1, y + 1) + Field(x - 1, y - 1) + Field(x - 1, y + 1) + Field(x + 1, y - 1)
count = count * -1
If (Field(x, y) = True) Then
If (count < 2) Then
FieldBuffer(x, y) = False
End If
If (count > 3) Then
FieldBuffer(x, y) = False
End If
If (count = 3 Or count = 2) Then
FieldBuffer(x, y) = True
End If
Console.BackgroundColor = ConsoleColor.Blue
Else
If (count = 3) Then
FieldBuffer(x, y) = True
End If
Console.BackgroundColor = ConsoleColor.Black
End If
Console.Write(count)
Else
Console.BackgroundColor = ConsoleColor.Red
Console.Write("X")
End If
y += 1
End While
x += 1
End While
Console.ReadKey()
Field = FieldBuffer
End While
End Sub
End Module
对于程序的第一次迭代,它返回字段数组中单元格结构的正确值。
但是在第二次迭代中,应用规则后,它不会返回正确的值,应该是:
12321
11211
12321
规则正确地应用于数组,因为在第一次迭代之后,显示的结构是正确的。当单元格周围的每个位置都有“If(Field(x,y)=true)then Count+=1”时,也会出现此错误
如果有任何帮助,我们将不胜感激,因为这个bug已经让我疯狂了大约两周了。您的规则是错误的
' Any live cell with two or three neighbours lives
' Any dead cell with exactly three live
If count = 3 Then ' If (count = 3 Or count = 2) Then
FieldBuffer(x, y) = True
End If
您也在复制引用。这意味着,在第二次传递时,Field和FieldBuffer是相同的
Field = FieldBuffer
您需要复制每个项目。使用循环,类似或
Field = FieldBuffer