TicTacToe Win Condition, 2d Array
Page 1 of 112 Replies - 230 Views - Last Post: 09 February 2013 - 11:00 PM
#1
TicTacToe Win Condition, 2d Array
Posted 07 February 2013 - 01:53 PM
Public arrayWin(3, 3) As Object = {btnBoard1, btnBoard2, btnBoard3, btnBoard4, btnBoard5, btnBoard6, btnBoard7, btnBoard8, btnBoard9}
^Doesn't like the (3,3)
Replies To: TicTacToe Win Condition, 2d Array
#2
Re: TicTacToe Win Condition, 2d Array
Posted 07 February 2013 - 02:13 PM
0 | 1 | 2 ---+---+--- 3 | 4 | 5 ---+---+--- 6 | 7 | 8
This post has been edited by AdamSpeight2008: 07 February 2013 - 02:13 PM
#3
Re: TicTacToe Win Condition, 2d Array
Posted 07 February 2013 - 04:43 PM
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?
#4
Re: TicTacToe Win Condition, 2d Array
Posted 07 February 2013 - 06:05 PM
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.
#5
Re: TicTacToe Win Condition, 2d Array
Posted 07 February 2013 - 07:40 PM
Chas3down, on 07 February 2013 - 04:43 PM, said:
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.
#6
Re: TicTacToe Win Condition, 2d Array
Posted 07 February 2013 - 07:45 PM
LilGhost, on 07 February 2013 - 07:40 PM, said:
Chas3down, on 07 February 2013 - 04:43 PM, said:
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.
#7
Re: TicTacToe Win Condition, 2d Array
Posted 07 February 2013 - 11:20 PM
LilGhost, on 07 February 2013 - 08:40 PM, said:
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.
#8
Re: TicTacToe Win Condition, 2d Array
Posted 08 February 2013 - 06:29 PM
lar3ry, on 07 February 2013 - 11:20 PM, said:
LilGhost, on 07 February 2013 - 08:40 PM, said:
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.
#9
Re: TicTacToe Win Condition, 2d Array
Posted 08 February 2013 - 09:16 PM
LilGhost, on 08 February 2013 - 07:29 PM, said:
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
#10
Re: TicTacToe Win Condition, 2d Array
Posted 09 February 2013 - 07:35 PM
lar3ry, on 08 February 2013 - 09:16 PM, said:
LilGhost, on 08 February 2013 - 07:29 PM, said:
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.
#11
Re: TicTacToe Win Condition, 2d Array
Posted 09 February 2013 - 09:04 PM
LilGhost, on 09 February 2013 - 08:35 PM, said:
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
If (Part1 & Part2 & Part3) = "XXX" Then
Little bit simpler in my opinion.
That would work, of course.
#12
Re: TicTacToe Win Condition, 2d Array
Posted 09 February 2013 - 10:25 PM
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
#13
Re: TicTacToe Win Condition, 2d Array
Posted 09 February 2013 - 11:00 PM
AdamSpeight2008, on 09 February 2013 - 11:25 PM, said:
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"
|
|

New Topic/Question
Reply



MultiQuote





|