12 Replies - 483 Views - Last Post: 09 February 2013 - 11:00 PM Rate Topic: -----

#1 Chas3down  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-February 13

TicTacToe Win Condition, 2d Array

Posted 07 February 2013 - 01:53 PM

Hey, I am trying to setup my win condition as a 2d array. However, I am unable to store objects as a 2d array, so How would I do this?


Public arrayWin(3, 3) As Object = {btnBoard1, btnBoard2, btnBoard3, btnBoard4, btnBoard5, btnBoard6, btnBoard7, btnBoard8, btnBoard9}


^Doesn't like the (3,3)
Is This A Good Question/Topic? 0
  • +

Replies To: TicTacToe Win Condition, 2d Array

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: TicTacToe Win Condition, 2d Array

Posted 07 February 2013 - 02:13 PM

Ask yourself does really need to be a 2D array? Could it be 1D?

 
  0 | 1 | 2 
 ---+---+---
  3 | 4 | 5
 ---+---+---
  6 | 7 | 8



This post has been edited by AdamSpeight2008: 07 February 2013 - 02:13 PM

Was This Post Helpful? 0
  • +
  • -

#3 Chas3down  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-February 13

Re: TicTacToe Win Condition, 2d Array

Posted 07 February 2013 - 04:43 PM

Alright thanks got it... One more question

If strWin(0) And strWin(1) And strWin(2) = "X"

Why do I have to do

If strWin(0) = "X" And strWin(1) = "X" And strWin(2) = "X"


What is wrong with the first line?
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: TicTacToe Win Condition, 2d Array

Posted 07 February 2013 - 06:05 PM

Cos vb.net is not written English, it's code and the code has relativily simple grammar and semantic rule (as compared to English).

If strWin(0) And strWin(1) And strWin(2) = "X" 


Different operators have different order of precedence

So the = is evaluated first, the rest are the same order so are evaluated left to right.
               ( AND )
              /       \
             /         \
            /           \
           /             \
          /               \
    ( AND )                (  =  )
    /     \                /      \
strWin(0) strWin(1) strWin(2)      "X" 



If you mean If (strWin(0) = "X") And (strWin(1) = "X") And (strWin(2) = "X") Then then write that.
Was This Post Helpful? 0
  • +
  • -

#5 LilGhost  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 98
  • Joined: 12-October 12

Re: TicTacToe Win Condition, 2d Array

Posted 07 February 2013 - 07:40 PM

View PostChas3down, on 07 February 2013 - 04:43 PM, said:

Alright thanks got it... One more question

If strWin(0) And strWin(1) And strWin(2) = "X"

Why do I have to do

If strWin(0) = "X" And strWin(1) = "X" And strWin(2) = "X"


What is wrong with the first line?

Basically, to summarize the post above mine and put it in my own words, it's saying "If (StrWin(0) = True) And (StrWin(1) = True) And (StrWin(2) = "X") Then"

Although these all will eventually equate to a true/false value, we can't say "Does this string = True" and expect it to return back whether or not the aspect of the array is = to "X" or not. However, someone correct me if i'm wrong, i believe you could use a select case statement in place of this if statement and then say something similar to what you have.
Was This Post Helpful? 0
  • +
  • -

#6 Chas3down  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-February 13

Re: TicTacToe Win Condition, 2d Array

Posted 07 February 2013 - 07:45 PM

View PostLilGhost, on 07 February 2013 - 07:40 PM, said:

View PostChas3down, on 07 February 2013 - 04:43 PM, said:

Alright thanks got it... One more question

If strWin(0) And strWin(1) And strWin(2) = "X"

Why do I have to do

If strWin(0) = "X" And strWin(1) = "X" And strWin(2) = "X"


What is wrong with the first line?

Basically, to summarize the post above mine and put it in my own words, it's saying "If (StrWin(0) = True) And (StrWin(1) = True) And (StrWin(2) = "X") Then"

Although these all will eventually equate to a true/false value, we can't say "Does this string = True" and expect it to return back whether or not the aspect of the array is = to "X" or not. However, someone correct me if i'm wrong, i believe you could use a select case statement in place of this if statement and then say something similar to what you have.
Okay, thanks.. I am really new to code, could you give a brief example of what that may look like?
Was This Post Helpful? 0
  • +
  • -

#7 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: TicTacToe Win Condition, 2d Array

Posted 07 February 2013 - 11:20 PM

View PostLilGhost, on 07 February 2013 - 08:40 PM, said:

Basically, to summarize the post above mine and put it in my own words, it's saying "If (StrWin(0) = True) And (StrWin(1) = True) And (StrWin(2) = "X") Then"

Although these all will eventually equate to a true/false value,...

The first two will not equate to a True/False value because they are not Boolean variables. They are strings, and a string cannot have a value of True or False.

In order to create a Boolean value using StrWin(0), we must specifically ask if it has a particular string property, such as a specific string, a string.length, etc.

You can test this yourself. Put this code into a project and run it (just stick it into a Button event somewhere).

        Dim str1 As String = ""
        Dim str2 As String = "X"
        If str1 Then Debug.Print("1T")
        If str2 Then Debug.Print("2T")
        If str1 = True Then Debug.Print("str1 is True")
        If str2 = True Then Debug.Print("str2 is True")


Each of the If statements will generate an error. If you have Option Strict On set, you will not be able to compile. Yu will get a BUILD FAILED, with an error message "Option Strict On disallows implicit conversions from 'String' to 'Boolean'". Without that Option, the error will occur at run time, with a similar error message.
Was This Post Helpful? 0
  • +
  • -

#8 LilGhost  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 98
  • Joined: 12-October 12

Re: TicTacToe Win Condition, 2d Array

Posted 08 February 2013 - 06:29 PM

View Postlar3ry, on 07 February 2013 - 11:20 PM, said:

View PostLilGhost, on 07 February 2013 - 08:40 PM, said:

Basically, to summarize the post above mine and put it in my own words, it's saying "If (StrWin(0) = True) And (StrWin(1) = True) And (StrWin(2) = "X") Then"

Although these all will eventually equate to a true/false value,...

The first two will not equate to a True/False value because they are not Boolean variables. They are strings, and a string cannot have a value of True or False.

In order to create a Boolean value using StrWin(0), we must specifically ask if it has a particular string property, such as a specific string, a string.length, etc.

You can test this yourself. Put this code into a project and run it (just stick it into a Button event somewhere).

        Dim str1 As String = ""
        Dim str2 As String = "X"
        If str1 Then Debug.Print("1T")
        If str2 Then Debug.Print("2T")
        If str1 = True Then Debug.Print("str1 is True")
        If str2 = True Then Debug.Print("str2 is True")


Each of the If statements will generate an error. If you have Option Strict On set, you will not be able to compile. Yu will get a BUILD FAILED, with an error message "Option Strict On disallows implicit conversions from 'String' to 'Boolean'". Without that Option, the error will occur at run time, with a similar error message.


The code will try to equate them to boolean values because they're in the if statement. As could be assumed, since it's a string, it'll proceed to the else or elseif statement because a string will always equate to False. Thus, in a roundabout way, the string parts of the if statement are being equated to booleans.
:detective:
Was This Post Helpful? 0
  • +
  • -

#9 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: TicTacToe Win Condition, 2d Array

Posted 08 February 2013 - 09:16 PM

View PostLilGhost, on 08 February 2013 - 07:29 PM, said:

The code will try to equate them to boolean values because they're in the if statement. As could be assumed, since it's a string, it'll proceed to the else or elseif statement because a string will always equate to False. Thus, in a roundabout way, the string parts of the if statement are being equated to booleans.
:detective:/>/>

Did you actually try the code in my example? The program will not compile with Option Strict On, and if that Option is not on, it will compile, but will not get past any of those If statements. Give it a try, commenting out three of the If statements, and trying each one.

This post has been edited by lar3ry: 08 February 2013 - 09:17 PM

Was This Post Helpful? 0
  • +
  • -

#10 LilGhost  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 98
  • Joined: 12-October 12

Re: TicTacToe Win Condition, 2d Array

Posted 09 February 2013 - 07:35 PM

View Postlar3ry, on 08 February 2013 - 09:16 PM, said:

View PostLilGhost, on 08 February 2013 - 07:29 PM, said:

The code will try to equate them to boolean values because they're in the if statement. As could be assumed, since it's a string, it'll proceed to the else or elseif statement because a string will always equate to False. Thus, in a roundabout way, the string parts of the if statement are being equated to booleans.
:detective:/>/>/>

Did you actually try the code in my example? The program will not compile with Option Strict On, and if that Option is not on, it will compile, but will not get past any of those If statements. Give it a try, commenting out three of the If statements, and trying each one.

No, i did not try the code. I just know what in some languages, it'll return false. I assume vb.net isn't quite that robust. Either way, as i stated above, the proper way would be to simply say:
If (Part1 = "X") And (Part2 = "X") And (Part3 = "X") Then



Or you could say:
If (Part1 & Part2 & Part3) = "XXX" Then


Little bit simpler in my opinion.
Was This Post Helpful? 0
  • +
  • -

#11 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: TicTacToe Win Condition, 2d Array

Posted 09 February 2013 - 09:04 PM

View PostLilGhost, on 09 February 2013 - 08:35 PM, said:

No, i did not try the code. I just know what in some languages, it'll return false. I assume vb.net isn't quite that robust. Either way, as i stated above, the proper way would be to simply say:
If (Part1 = "X") And (Part2 = "X") And (Part3 = "X") Then


VB.Net is not "less robust" just because it has a way of indicating True or False that differs from another language.

Yes, you did say that it would be proper to be specific in an If statement, and you were right... just not for the reason you stated.

Quote

Or you could say:
If (Part1 & Part2 & Part3) = "XXX" Then


Little bit simpler in my opinion.

That would work, of course.
Was This Post Helpful? 0
  • +
  • -

#12 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: TicTacToe Win Condition, 2d Array

Posted 09 February 2013 - 10:25 PM

It wouldn't since & is a Binary Operand, ie it takes two parameters and return something. Used for string concatenation.

The closest you're going to get to that syntax.
If {part1, part2, part3}.All(Function(part) part="XXX" Then



But since you're struggling with even the basic stuff, lambda function will confuse you.


Start with Console Applications first.
It help you focus and create the core essence of the program.
To learn the basics of the language, without the extra complexities mutli-threaded message-passaging GUI bring.
It's a lot simple to prototype
Was This Post Helpful? 0
  • +
  • -

#13 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: TicTacToe Win Condition, 2d Array

Posted 09 February 2013 - 11:00 PM

View PostAdamSpeight2008, on 09 February 2013 - 11:25 PM, said:

It wouldn't since & is a Binary Operand, ie it takes two parameters and return something. Used for string concatenation.

Assuming that Part1, Part2, and Part3 are all strings, and assuming that they will all contain "X" or "O" or "", it will definitely work. Those are the assumptions I made, given the topic and previous conversation.

Part1 & Part 2 & Part3, if they each contain an "X" will certainly result in "XXX"
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1