I have been working on a C++ Tic Tac Toe game. 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.
Ive only recently got it semi working, but it classes something as a 3 in a row when it isnt. The reason why is that it is checking for matches next to a piece, then checking for matches to that match, but for the second check, i need it to only check in the same direction.
Anyone got any ideas how to achieve this?
int setsize = 3
int chsize = 3;
int x, y, check, breakloop, i, dist, currentch;
int xco, yco, set[20][20], yloop, xloop, posy, posx;
int turn = 2;
check = 0;
for ( xloop=1; xloop<=setsize; xloop++ ) { // These first two loops start the position
for ( yloop=1; yloop<=setsize; yloop++ ) { // of where to start checking from
currentch = set[xloop][yloop];
if ( (currentch==turn) ) {
posx=xloop;
posy=yloop;
for ( i=1; i<=4; i++ ) { // Sets a different direction to try
for ( dist=1; dist<=chsize; dist++ ) { // Sets distance to go 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]!=turn)) {
break;
}
else {
check=check+1; // Only continues loop if there are values in that direction
}
}
posx=xloop;
posy=yloop;
}
}
}
}
Question
justin89h
I have been working on a C++ Tic Tac Toe game. 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.
Ive only recently got it semi working, but it classes something as a 3 in a row when it isnt. The reason why is that it is checking for matches next to a piece, then checking for matches to that match, but for the second check, i need it to only check in the same direction.
Anyone got any ideas how to achieve this?
int setsize = 3 int chsize = 3; int x, y, check, breakloop, i, dist, currentch; int xco, yco, set[20][20], yloop, xloop, posy, posx; int turn = 2; check = 0; for ( xloop=1; xloop<=setsize; xloop++ ) { // These first two loops start the position for ( yloop=1; yloop<=setsize; yloop++ ) { // of where to start checking from currentch = set[xloop][yloop]; if ( (currentch==turn) ) { posx=xloop; posy=yloop; for ( i=1; i<=4; i++ ) { // Sets a different direction to try for ( dist=1; dist<=chsize; dist++ ) { // Sets distance to go 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]!=turn)) { break; } else { check=check+1; // Only continues loop if there are values in that direction } } posx=xloop; posy=yloop; } } } }Here is what happens:
Link to comment
Share on other sites
8 answers to this question
Recommended Posts