Word search puzzle on vb



我正在尝试创建一个程序,要求用户输入他们想隐藏在单词搜索谜题中的10个单词。如何在网格中放置单词?

这就是我所做的,但我一直得到这个错误

messageSystem。IndexOutOfRangeException
Message=索引超出了数组的界限。

代码:

Sub Words()
wordcount = 0
Do
Console.WriteLine("choose word")
word = Console.ReadLine
Showdirection()
Console.WriteLine("choose row")
row = Console.ReadLine
Console.WriteLine("choose column")
col = Console.ReadLine
grid(row, col) = word(0)
For i = 1 To Len(word)
Select Case choice
Case "1"
col = col + 1
Case "2"
col = col - 1
Case "3"
row = row - 1
Case "4"
row = row + 1
Case "5"
col = col + 1
row = row - 1
Case "6"
col = col - 1
row = row + 1
Case "7"
col = col + 1
row = row + 1
Case "8"
col = col - 1
row = row + 1
End Select
grid(row, col) = word(i)
Next
wordcount = wordcount + 1
Loop Until wordcount = 10
End Sub

此外,一旦所有单词都放在网格中,我如何用随机字母填充剩余的空格?

这是我迄今为止的代码


Dim grid(14, 14) As String
Dim choice As Integer
Dim word As String
Dim wordcount As Integer = 0
Dim row As Integer
Dim col As Integer
Sub outputgrid()
For i = 0 To 14
For j = 0 To 14
Console.Write(grid(i, j))
Next j
Console.WriteLine()
Next i
End Sub
Sub setupgrid()
For i = 0 To 14
For j = 0 To 14
grid(i, j) = ""
Next j
Next i
End Sub
Sub Main(args As String())
setupgrid()
outputgrid()
Words()
Console.ReadLine()
End Sub
Sub Words()
wordcount = 0
Do
Console.WriteLine("choose word")
word = Console.ReadLine
Showdirection()
Console.WriteLine("choose row")
row = Console.ReadLine
Console.WriteLine("choose column")
col = Console.ReadLine
grid(row, col) = word(0)
For i = 1 To Len(word)
Select Case choice
Case "1"
col = col + 1
Case "2"
col = col - 1
Case "3"
row = row - 1
Case "4"
row = row + 1
Case "5"
col = col + 1
row = row - 1
Case "6"
col = col - 1
row = row + 1
Case "7"
col = col + 1
row = row + 1
Case "8"
col = col - 1
row = row + 1
End Select
grid(row, col) = word(i)
Next
wordcount = wordcount + 1
Loop Until wordcount = 10
End Sub
Sub Showdirection()
Console.WriteLine("choose direction")
Console.WriteLine("1 horizontal- left to right")
Console.WriteLine("2 Horizontal -Right to Left")
Console.WriteLine("3 Vertical -Down")
Console.WriteLine("4 Vertical -Up")
Console.WriteLine("5 Diagonal – Down L To R")
Console.WriteLine("6 Diagonal – Down R To L ")
Console.WriteLine("7 Diagonal – Up L To R ")
Console.WriteLine("8 Diagonal – Up R To L ")
choice = Console.ReadLine()
End Sub

首先,您应该将Option Strict On添加到代码的顶部。您正在执行从字符串到Integer再到Integer的隐式转换。一旦你这样做了,你会发现有些东西需要一些显式的强制转换(我不打算这样做,但你应该验证用户的输入(

choice = CInt(Console.ReadLine())
' there are more examples of this you need to fix
' but what if the user enters a non-number? You should handle that case

case语句对Integer进行操作,但您的case是字符串。更改为Integers

Select Case choice
Case 1
col = col + 1
Case 2
col = col - 1
...

至于你的代码不起作用,我会帮你做出解释(这是你应该在问题中提出的(:

系统。IndexOutOfRangeException
Message=索引超出了数组的界限。

很容易看出您的For循环设置不正确。请记住,数组在vb.net中是零基础的。

更改

For i = 1 To Len(word)

For i = 0 To Len(word) - 1

要填充剩余的空单元格,请使用方法

Private Sub fillEmptyCells()
Dim random As New Random()
For i = 0 To grid.GetUpperBound(0)
For j = 0 To grid.GetUpperBound(1)
If grid(i, j) = "" Then
' ascii characters 97 to 122 are all the lower-case letters
grid(i, j) = Chr(random.Next(97, 123)).ToString()
End If
' this will clean up your entry in the case the user entered a capital letter
grid(i, j) = grid(i, j).ToLower()
Next
Next
End Sub

在最后调用

Sub Main(args As String())
setupgrid()
outputgrid()
Words()
fillEmptyCells()
Console.ReadLine()
End Sub

最新更新