0

Currently I'm trying to fill a 3x3 square with random x's and o's to make a tic tac toe game. Unfortunately the game doesn't seem to output all the x's and o's. Logically, from what I can see, it should be able to but it's not. Any help would be appreciated.

Shared Sub twodimension()
    Dim tic(2, 2) As String
    Dim min As Integer
    Dim x As String
    Dim random As New Random()
    Dim i As Integer
    Dim x1 As Integer

    Dim bound0 As Integer = tic.GetUpperBound(0)
    Dim bound1 As Integer = tic.GetLowerBound(1)

    For i = 0 To bound0
        For x1 = 0 To bound1
            min = random.Next(2)

            If min = 0 Then
                x = "x"
                Console.WriteLine("{0}", x)
            Else
                x = "o"
                Console.WriteLine("{0}", x)
            End If
            Console.Write(" "c)
        Next
        Console.WriteLine()
    Next
End Sub
2
  • What result do you have right now? Commented Jul 29, 2011 at 16:29
  • It just displays either: x o, x x, o x, o o Commented Jul 29, 2011 at 16:54

1 Answer 1

3

So presumably you've got this declaration somewhere, right?

Public Shared Tic(2, 2) As String

In your code you've got GetLowerBound which will (almost) always returns zero and instead you should have GetUpperBound().

    Dim bound0 As Integer = tic.GetUpperBound(0)
    Dim bound1 As Integer = Tic.GetUpperBound(1)

EDIT (in response to comment)

GetUpperBound(int) returns the highest number that you can use for the dimension that you specify.

So for the following array:

    Dim MyArray(4, 6, 8) As Integer

    Trace.WriteLine(MyArray.GetUpperBound(0)) ''//Returns 4
    Trace.WriteLine(MyArray.GetUpperBound(1)) ''//Returns 6
    Trace.WriteLine(MyArray.GetUpperBound(2)) ''//Returns 8

GetLowerBound(int) returns the lowest number that you can use for the dimension that you specify. In almost every case this is zero but in older versions of VB (and using some COM interop) you can create arrays that don't "start" at zero and instead start at whatever you wanted. So in old VB you could actually say Dim Bob(1 To 4) As Integer and GetLowerBound(0) would return 1 instead of 0. For the most part there is no reason to even be aware that GetLowerBound exists even.

Sign up to request clarification or add additional context in comments.

4 Comments

Oops I missed that in the original post, thanks I'll add it to make it more clear
wow that upperbound works! Now it displays all the x and o. Thanks.
Just a question, why would GetUpperBound work instead of GetLowerBound since GetUpper returns the upper bound for the indexes of the first dimension of the Array? Also looking through msdn.microsoft.com/en-us/library/… It doesn't explain what the GetUpperBound(1) one mean.
That is a superb answer, excellent. Good to know that GetLowerBound isn't that used. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.