I am using a bunch of for loops to check values for a tic tac toe game. I have made it much more complex then needed, so a user could specify the size of the board for tic tac toe, and make it so you have to get 7 in a row for example.
I have worked on the code below for days, trying to figure out what is stopping it from working, but i cannot find the problem. If there is anything obvious i should change that you can see, please tell, it would help a great deal. The problem at the moment that i can see is it is continuing to check in the wrong direction, even though it has an if statement to stop it.
for ( xloop=0; xloop<=setsize; xloop++ ) { // These first two loops start the position
for ( yloop=0; yloop<=setsize; yloop++ ) { // of where to start checking from
currentch = set[xloop][yloop];
if ( (currentch==1) || (currentch==2) ) {
posx=xloop;
posy=yloop;
for ( i=1; i<=4; i++ ) { // Sets a different direction to try
for ( dist=1; dist<=chsize; dist++ ) { // Sets how far to check in that direction
if (i==1) { posx = xloop + dist; }
if (i==2) { posx = xloop + dist; posy = yloop + dist; }
if (i==3) { posy = yloop + dist; }
if (i==4) { posx = xloop - dist; posy = yloop + dist; }
if ((set[posx][posy]==2)||(set[posx][posy]==1)) {
check=check+1;
}
else {
break; // Only continues loop if there are values in that direction
}
}
}
}
}
}
I commented on the less obvious parts and what they do, so it should be fairly easy to understand, please help if you can see anything wrong.
Question
justin89h
I am using a bunch of for loops to check values for a tic tac toe game. I have made it much more complex then needed, so a user could specify the size of the board for tic tac toe, and make it so you have to get 7 in a row for example.
I have worked on the code below for days, trying to figure out what is stopping it from working, but i cannot find the problem. If there is anything obvious i should change that you can see, please tell, it would help a great deal. The problem at the moment that i can see is it is continuing to check in the wrong direction, even though it has an if statement to stop it.
for ( xloop=0; xloop<=setsize; xloop++ ) { // These first two loops start the position for ( yloop=0; yloop<=setsize; yloop++ ) { // of where to start checking from currentch = set[xloop][yloop]; if ( (currentch==1) || (currentch==2) ) { posx=xloop; posy=yloop; for ( i=1; i<=4; i++ ) { // Sets a different direction to try for ( dist=1; dist<=chsize; dist++ ) { // Sets how far to check in that direction if (i==1) { posx = xloop + dist; } if (i==2) { posx = xloop + dist; posy = yloop + dist; } if (i==3) { posy = yloop + dist; } if (i==4) { posx = xloop - dist; posy = yloop + dist; } if ((set[posx][posy]==2)||(set[posx][posy]==1)) { check=check+1; } else { break; // Only continues loop if there are values in that direction } } } } } }I commented on the less obvious parts and what they do, so it should be fairly easy to understand, please help if you can see anything wrong.
Edited by kerodeonLink to comment
Share on other sites
8 answers to this question
Recommended Posts