Sign in to follow this  

if statement problem

This topic is 2119 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

for some reason this if statement never executes even if all arrays are true.let me know if you need more code
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] (((board[0,0]) == [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]true[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]) && ((board[0,1]) == [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]true[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]) && ((board[0,2]) == [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]true[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]))[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/size][/font][/size][/font]
[font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Pen[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] pn = [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]new[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/size][/font][/size][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Pen[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]([/size][/font][/size][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Color[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"].Blue, 3);[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]g.DrawLine(pn, 0, 50, 200, 50);[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/size][/font][/size][/font]

Share this post


Link to post
Share on other sites
[sup]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?[/sup]

[sup]Just a side note the brackets around your array access are just taking up space.[/sup]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
here is some more code for clarification of my problem.
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] pX = e.X;[/size][/font][/size][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] pY = e.Y;[/size][/font][/size][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"][] board = [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]new[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"][9];[/size][/font][/size][/font]

[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] (pX >= 10 && pX <= 20 && pY >= 30 && pY <= 40)[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]board[0] = 1;[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/size][/font][/size][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] (pX >= 80 && pX <= 90 && pY >= 30 && pY <= 40)[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]board[1] = 1;[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/size][/font][/size][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] (pX >= 160 && pX <= 170 && pY >= 30 && pY <= 40)[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]board[2] = 1;[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/size][/font][/size][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] (board[0]==1 & board[1]==1 & board[2]==1)[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/size][/font][/size][/font]
[font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Pen[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] pn = [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]new[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/size][/font][/size][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Pen[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]([/size][/font][/size][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Color[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"].Blue, 3);[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]g.DrawLine(pn, 0, 50, 200, 50);[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]this one has only a 1d array and it still does not work[/size][/font][/size][/font]

Share this post


Link to post
Share on other sites
That's because your 2nd statement can never be true if the fisrt one is;

[font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] (pX >= 10 && pX <= 20 [/size][/font][/size][/font]...

[font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] (pX >= 80 && pX <= 90[/size][/font][/size][/font]


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;
[font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] (board[0]==1 || board[1]==1 || board[2]==1)[/size][/font][/size][/font]


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 &.

Share this post


Link to post
Share on other sites
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;
[font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] (board[0]==1 & board[1]==1 & board[2]==1)[/size][/font][/size][/font]
[font=Consolas][size=2][font=Consolas][size=2]{[/size][/font][/size][/font]
[font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]Pen[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] pn = [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]new[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] [/size][/font][/size][/font][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]Pen[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2]([/size][/font][/size][/font][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]Color[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2].Blue, 3);[/size][/font][/size][/font]
[font=Consolas][size=2][font=Consolas][size=2]g.DrawLine(pn, 0, 50, 200, 50);[/size][/font][/size][/font]
[font=Consolas][size=2][font=Consolas][size=2]}[/size][/font][/size][/font]
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.

Share this post


Link to post
Share on other sites
[quote name='XXChester' timestamp='1330126384' post='4916368']
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.
[/quote]
I have tried the || operator.but it draws the line before all values in the array are filled.

Share this post


Link to post
Share on other sites
Okay, than you need to track the board over multiple updates. Meaning right now you have [font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2][] board = [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]new[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2][9];[/size][/font][/size][/font]

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
[quote name='phil67rpg' timestamp='1330129910' post='4916398']
I have one more question,where on the net do I find help on optimizing code.
[/quote]

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.

Share this post


Link to post
Share on other sites

This topic is 2119 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this