我正在尝试创建一个数独游戏,在VB中加载和保存游戏功能,我想知道如何加载保存文件(数独板和时间分数上的数字),并使文件内的数字对应于他们在新板上的确切位置。保存文件,我有这样的:
Private Sub saveBoard(fileName As String)
Dim f As StreamWriter = New StreamWriter(fileName)
For col = 0 To 8
Dim sudokuLine = ""
For row = 0 To 8
If (board(row, col).Text = "") Then
sudokuLine += "0"
Else : sudokuLine += board(row, col).Text
End If
sudokuLine += " "
Next
f.WriteLine(sudokuLine)
Next
f.WriteLine(lblMinutes.Text + " " + lblSeconds.Text)
f.Close()
End Sub
这可能是loadBoard对应于你的saveBoard
当然,这里需要进行一些测试,并且需要一种更可靠的方法来处理严重错误(例如作为输入传递的不同文件)。
注意,我在流的开头添加了using语句。这也应该在上面的saveBoard中完成,以避免在异常情况下锁定文件的问题。
Private Sub loadBoard(fileName As String)
Using f = New StreamReader(fileName)
For col = 0 To 8
Dim sudokuColumn = f.ReadLine()
Dim cells() = sudokuColum.Split()
For row = 0 To 8
If cells(row, col) = "0") Then
boards(row, col).Text = ""
Else
board(row, col).Text = cells(row,col)
End If
Next
Next
lblMinutes.Text = f.ReadLine()
End Using
End Sub