I'm trying to do a tic-tac-toe console application for practice. I created a case statement for the user to pick which square they want to fill in.
Public Class TwoDi
Shared Sub twodimension()
Console.WriteLine("Do you want to go first?")
first = Console.ReadLine()
If first = "yes" Then
Console.WriteLine("Please enter a X or O.")
x2 = Console.ReadLine()
Console.WriteLine("Pick a spot.")
pickone(x2)
End If
End Sub
Shared Sub pickone(ByVal x As String)
Dim choice As String
Dim tic(2, 2) As开发者_如何学编程 String
Dim x2 As String
Dim bound0 As Integer = tic.GetUpperBound(0)
Dim bound1 As Integer = tic.GetUpperBound(1)
Dim i As Integer
Dim x1 As Integer
Dim populate As String = "n"
For i = 0 To bound0
For x1 = 0 To bound1
Console.Write("{0}", show)
Next
Console.WriteLine()
Next
x2 = x
choice = Console.ReadLine()
Select Case choice
Case "middle" //putting in one case statement for example.
choice = "middle"
tic(1, 1) = x2
End Select
Console.Write("" & vbLf & "Pick a spot.")
pickone(x2)
End Sub
End Class
Now I tried a few loops to update the display to show the 3x3 2-d array. Specifically this one:
For i = 0 To bound0
For x1 = 0 To bound1
Console.Write("{0}", tic(bound0, bound1))
Next
Console.WriteLine()
Next
I prepopulate the 3x3 grid with "n" but when I run the loop it just shows a single x or o and not even spaced correctly.
Also a somewhat seperate question, when I loop to run a block of code again, is it best practice to just to call the Sub/Function again?
Addressing the "all filled with X or O depending if I pick a X or O" part:
Did you really mean to type Console.Write("{0}", tic(1, 1))
instead of maybe this?
For i = 0 To bound0
For x1 = 0 To bound1
Console.Write("{0}", tic(i, x1))
Next
Console.WriteLine()
Next
精彩评论