我正在尝试创建一个数独游戏,在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
我也知道StreamReader ......
答案 0 :(得分:0)
这可能是与您的saveBoard对应的loadBoard 当然,这里需要进行一些测试,并采用更简单的方法来处理关键错误(例如,作为输入传递的不同文件)。
请注意,我已在Stream的开头添加了using statement。这也应该在上面的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