Home > Software engineering >  Word search puzzle on vb, how do I show the completed grid after the words have been entered?
Word search puzzle on vb, how do I show the completed grid after the words have been entered?

Time:12-19

I'm trying to create a program that asks the user to enter 10 words they would like hidden in a word search puzzle then show the completed grid.

How do I show the completed grid after the words have been entered? when the words are entered the program doesn't do anything until I press enter which closes it

code

   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()
        'sets the grid
        For i = 0 To 14
            For j = 0 To 14
                grid(i, j) = ""
            Next j
        Next i
    End Sub
    Sub Main()
        setupgrid()
        outputgrid()
        Words()
        fillEmptyCells()
        Console.ReadLine()
    End Sub
    Sub Words()

        wordcount = 0
        Do
            Console.WriteLine("choose word")
            word = CInt(Console.ReadLine)
            Showdirection()
            Console.WriteLine("choose row")
            row = CInt(Console.ReadLine())
            Console.WriteLine("choose colunm")
            col = CInt(Console.ReadLine())
            grid(row, col) = word(0)
            For i = 0 To Len(word) - 1
                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 = CInt(Console.ReadLine())
    End Sub
    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

                    grid(i, j) = Chr(random.Next(65, 90)).ToString()
                End If

                grid(i, j) = grid(i, j).ToUpper()
            Next
        Next
        End

    End Sub

CodePudding user response:

I don't think I understand the problem well, but let's try to find a solution. You already have a method that allows you to print what you've entered into the grid, and that's outputgrid(). If you want to see the result at the end, you need to change the main as follows:

Sub Main()
    setupgrid()
    outputgrid() 'This the initial empty grid...
    Words()
    fillEmptyCells()
    outputgrid() 'This is the filled grid
    Console.ReadLine()
End Sub

If you want to see the result after each user action, you need to add the same thing in the sub Words() loop:

Sub Words()
    wordcount = 0
    Do
        '* your original code *
        outputgrid() 'Grid with current data
    Loop Until wordcount = 10
End Sub

One more thing... If I'm not misunderstood, you're trying to put words in the grid, right? With the lines

Console.WriteLine("choose word")
word = CInt(Console.ReadLine)

you are allowing the user to enter only numbers (due to CInt).

CodePudding user response:

I got rid of most of the Module level variables and moved them to where they are used. If a variable is only used in a single method it should be declared as a local variable.

I got rid of the setupgrid method and changed fillEmptyCells to check for Null or Empty.

Don't call outputgrid until after you filled the grid.

To stop the errors in Words method, I added an If statement that checked if you row and col were in range of the grid.

In the fillEmptyCells method I got rid of the line where you called ToUpper. The ASCII codes you provided are all uppercase characters. Also, in the call to Random.Next, I changed the second parameter to 91 so you could include Z. Next includes the first parameter and excludes the second.

It will be easier to pick out the words from the grid because they may be in lower case. This might be good while you are debugging but afterwards in the Words method Dim word = Console.ReadLine.ToUpper makes the puzzle harder.

I think you will see that input words may be overwritten. You could add checks to prevent this. If you want to allow an additional word when a word won't fit put wordcount -= 1 directly before the Exit For.

Private grid(14, 14) As String

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 Main()
    Words()
    fillEmptyCells()
    outputgrid()
    Console.ReadLine()
End Sub

Sub Words()
    Dim wordcount As Integer
    Do
        Console.WriteLine("choose word")
        Dim word = Console.ReadLine '.ToUpper after debugging
        Dim DirectionChoice = Showdirection()
        Console.WriteLine("choose row")
        Dim row = CInt(Console.ReadLine())
        Console.WriteLine("choose colunm")
        Dim col = CInt(Console.ReadLine())
        grid(row, col) = word(0) 'First letter of word
        For i = 1 To Len(word) - 1 'don't start at zero, you have already assigned the zero element
            Select Case DirectionChoice
                Case 1 'horizontal- left to right
                    col = col   1
                Case 2 'Horizontal -Right to Left
                    col = col - 1
                Case 3 'Vertical -Down
                    row = row   1
                Case 4 'Vertical -Up
                    row = row - 1
                Case 5 'Diagonal – Down L To R
                    col = col   1
                    row = row   1
                Case 6 'Diagonal – Down R To L
                    col = col - 1
                    row = row   1
                Case 7 'Diagonal – Up L To R
                    col = col   1
                    row = row - 1
                Case 8 'Diagonal – Up R To L
                    col = col - 1
                    row = row - 1
            End Select
            If col > 14 OrElse col < 0 OrElse row > 14 OrElse row < 0 Then
                Console.WriteLine("Word won't fit")
                Exit For
            End If
            grid(row, col) = word(i)
        Next
        wordcount  = 1
    Loop Until wordcount = 2 '10
End Sub
Private Function Showdirection() As Integer
    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 ")
    Dim choice = CInt(Console.ReadLine())
    Return choice
End Function
Private Sub fillEmptyCells()
    Dim random As New Random()
    For i = 0 To grid.GetUpperBound(0)
        For j = 0 To grid.GetUpperBound(1)
            If String.IsNullOrEmpty(grid(i, j)) Then '<---
                grid(i, j) = Chr(random.Next(65, 91)).ToString() 'Next includes the first parameter and excludes the second
            End If
        Next
    Next
End Sub
  • Related