if statement problem
Started by phil67rpg, Feb 24 2012 04:25 PM
18 replies to this topic
#1 Members - Reputation: 94
Posted 24 February 2012 - 04:25 PM
for some reason this if statement never executes even if all arrays are true.let me know if you need more code
if (((board[0,0]) == true) && ((board[0,1]) == true) && ((board[0,2]) == true))
{
Pen pn = new Pen(Color.Blue, 3);
g.DrawLine(pn, 0, 50, 200, 50);
}
if (((board[0,0]) == true) && ((board[0,1]) == true) && ((board[0,2]) == true))
{
Pen pn = new Pen(Color.Blue, 3);
g.DrawLine(pn, 0, 50, 200, 50);
}
Ad:
#2 Members - Reputation: 158
Posted 24 February 2012 - 04:47 PM
How is the array declared? Is it passed around to multiple classes? I once had a problem that was caused by actually having 2 versions of the array kicking around. Also how do you knwo that the statements are actually true, did you look at the array with a debugger?
Just a side note the brackets around your array access are just taking up space.
Just a side note the brackets around your array access are just taking up space.
Remember to mark someones post as helpful if you found it so.
#3 Members - Reputation: 845
Posted 24 February 2012 - 04:47 PM
1. It's easier to just use the value, rather than comparing to true (do you say "is it true that we have stopped?" or "have we stopped?" - albeit the names aren't quite so nice in this case)
2. Place a breakpoint on the if, run with debugging enabled and inspect the values of board[0,0] etc.
2. Place a breakpoint on the if, run with debugging enabled and inspect the values of board[0,0] etc.
[TheUnbeliever]
#4 Members - Reputation: 94
Posted 24 February 2012 - 04:52 PM
here is some more code for clarification of my problem.
int pX = e.X;
int pY = e.Y;
int[] board = new int[9];
if (pX >= 10 && pX <= 20 && pY >= 30 && pY <= 40)
{
board[0] = 1;
}
if (pX >= 80 && pX <= 90 && pY >= 30 && pY <= 40)
{
board[1] = 1;
}
if (pX >= 160 && pX <= 170 && pY >= 30 && pY <= 40)
{
board[2] = 1;
}
if (board[0]==1 & board[1]==1 & board[2]==1)
{
Pen pn = new Pen(Color.Blue, 3);
g.DrawLine(pn, 0, 50, 200, 50);
}
this one has only a 1d array and it still does not work
int pX = e.X;
int pY = e.Y;
int[] board = new int[9];
if (pX >= 10 && pX <= 20 && pY >= 30 && pY <= 40)
{
board[0] = 1;
}
if (pX >= 80 && pX <= 90 && pY >= 30 && pY <= 40)
{
board[1] = 1;
}
if (pX >= 160 && pX <= 170 && pY >= 30 && pY <= 40)
{
board[2] = 1;
}
if (board[0]==1 & board[1]==1 & board[2]==1)
{
Pen pn = new Pen(Color.Blue, 3);
g.DrawLine(pn, 0, 50, 200, 50);
}
this one has only a 1d array and it still does not work
#5 Members - Reputation: 158
Posted 24 February 2012 - 05:12 PM
That's because your 2nd statement can never be true if the fisrt one is;
if (pX >= 10 && pX <= 20 ...
if (pX >= 80 && pX <= 90
From the sounds of it I think you might be looking for an OR statement not an AND statement, if that is the case than change it to this;
if (board[0]==1 || board[1]==1 || board[2]==1)
Also using the & operator forces it to equate the whole statement its a binary operation, you want to use && 99% of the time instead of &.
if (pX >= 10 && pX <= 20 ...
if (pX >= 80 && pX <= 90
From the sounds of it I think you might be looking for an OR statement not an AND statement, if that is the case than change it to this;
if (board[0]==1 || board[1]==1 || board[2]==1)
Also using the & operator forces it to equate the whole statement its a binary operation, you want to use && 99% of the time instead of &.
Remember to mark someones post as helpful if you found it so.
#7 Members - Reputation: 158
Posted 24 February 2012 - 05:33 PM
It depends what you are trying to achieve. A switch structure will not achieve anything that your if structure isn't already doing. The problem is this code;
if (board[0]==1 & board[1]==1 & board[2]==1)
{
Pen pn = new Pen(Color.Blue, 3);
g.DrawLine(pn, 0, 50, 200, 50);
}
If you change the &'s to || (OR's) than you will at least be drawing that static position you have defined (0,50,200,50). Then you can go from there.
if (board[0]==1 & board[1]==1 & board[2]==1)
{
Pen pn = new Pen(Color.Blue, 3);
g.DrawLine(pn, 0, 50, 200, 50);
}
If you change the &'s to || (OR's) than you will at least be drawing that static position you have defined (0,50,200,50). Then you can go from there.
Remember to mark someones post as helpful if you found it so.
#8 Members - Reputation: 94
Posted 24 February 2012 - 05:40 PM
XXChester, on 24 February 2012 - 05:33 PM, said:
If you change the &'s to || (OR's) than you will at least be drawing that static position you have defined (0,50,200,50). Then you can go from there.
#11 Members - Reputation: 166
Posted 24 February 2012 - 05:50 PM
Try printing the values of board right before the if statement and let us know what the output is and if you can get the values to match what you expect.
"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ
My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^
My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^
#12 Members - Reputation: 158
Posted 24 February 2012 - 05:54 PM
Okay, than you need to track the board over multiple updates. Meaning right now you have int[] board = new int[9];
declared locally to your method. You need to move this outside of your method, then it will draw once you have 3 in a row. Also make sure you change your &'s to &&'s.
declared locally to your method. You need to move this outside of your method, then it will draw once you have 3 in a row. Also make sure you change your &'s to &&'s.
Remember to mark someones post as helpful if you found it so.
#14 Members - Reputation: 158
Posted 24 February 2012 - 06:04 PM
Don't take this wrong because I am in no way trying to be an ass but I think you need to do some reading on the core concepts of programming before trying to go any further. You are going to keep stumbling over things like this until you have the core basics down but reading even 1 programming book would set you a lot further ahead.
Either way, glad I could help and best of luck.
Either way, glad I could help and best of luck.
Remember to mark someones post as helpful if you found it so.
#16 Members - Reputation: 158
Posted 24 February 2012 - 06:25 PM
That is good if you are, and there is nothing wrong with asking questions. More times than not though we see people asking questions that have read 2 tutorials and think they are a programmer now. All I was trying to say is variable scoping is a basic fundamental programming concept and that you might want to step back and study a book a little more before attempting a game...especially a visual game. Personally (and this is where I did start many years ago), if you are still set on making a game, I would still make your tic tac toe, but make it console based. That way what you are learning and practicing is the core programming concepts, looping, variables, data types, logical operations etc without all of the nitty gritty graphics stuff. Besides the DirectDraw API built into the .NET language you will not use (someone will say they do but most) for game development so in essence you are wasting your time with it.
Either way best of luck, you are in for a great ride
Either way best of luck, you are in for a great ride
Remember to mark someones post as helpful if you found it so.
#19 Members - Reputation: 845
Posted 24 February 2012 - 11:02 PM
phil67rpg, on 24 February 2012 - 06:31 PM, said:
I have one more question,where on the net do I find help on optimizing code.
You really don't want to make this your concern as a beginner. Just write code that seems elegant, or at least straightforward, to you and that will likely be performant enough. You would have to try quite hard to have performance difficulties with Tic Tac Toe! :-) Even once you do start to have problems, you don't just start blindly 'optimizing', you use a profiler to identify where your bottlenecks are and apply specific solutions to those.
[TheUnbeliever]


















