• 0

C++ Tic Tac Toe


Question

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:

post-47-1087037529.jpg

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

How are you using check? What variable tells you that they have 3? Sounds like somehow it's getting that there's 3 on the grid so it says, "Hey he won, he has 3". Also, your check sounds incomplete since there's actually 8 possible directions to check (around the center) and you're only checking 4 of them.

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 (i==5) { posx = xloop - dist; }

if (i==6) { posy = yloop - dist; }

if (i==7) { posx = xloop + dist; posy = yloop - dist;}

if (i==8) { posx = xloop - dist; posy = yloop - dist;}

Plus, I'm not seeing any way of keeping track of how they get 3 in a row except for check which never seems to get reset. Not to mention a win can be a combination of different directions from a given piece, such as diagonal:

121
012
102

There's really no need to check the whole grid each time either, just check in each direction around where they last went.

Edited by kjordan2001
Link to comment
Share on other sites

  • 0

'check' only increments if the loop hasnt broken, and the loop breaks as soon as there is a piece that doesnt match.

Also, as it checks from every place on the grid, it doesnt need to check the back directions, as a position before it has already checked those squares.

Not to mention a win can be a combination of different directions from a given piece, such as diagonal:
This is what im trying to find out how to do through this thread :)
There's really no need to check the whole grid each time either, just check in each direction around where they last went.

hmmm, ill try that, does make sense, ill just save my code i already have incase i screw it up :)

Thanks muchso

And heres a little (paint drawn) picture so everyone can understand my loop.

post-47-1087095646.gif

Link to comment
Share on other sites

  • 0

Well, what I meant is check is never reset to 0 and I'm not sure how you're using it to check how many they have in a row. My code from the other thread will work for this (with changes from Java to C++ and changing the max). I'm still not sure how you're escaping Segmentation Faults since you don't have any check to make sure you're not checking to make sure you're not on the edge.

Link to comment
Share on other sites

  • 0

hmmm...i would set a recursive function to record the direction of checkage and then use that to check for the same direction so that it looks for a line. By direction, i meanthe increase to x compared to the increase to y. Also, to optimize this only go halfway around the board. ie. 0,0 , 1,0 , 2,0 , 2,1. Hope that wasnt too confusing :p

Link to comment
Share on other sites

  • 0

yay, i finally fixed it. I had to do something that kjordan2001 said, but with a few tweaks. First of all, check should have been set at 1 at the start. Second, i cant just reset it to 1 after the dist loop, because even if it found a match, it would be reset. So this simple if statement fixed all the problems :)

if (check<chsize) { check = 1; }

Located right after the dist loop finishes.

Thanx for all your help ppl!

:D :D :D :D

Link to comment
Share on other sites

  • 0

err.... phatzac, if your talking to me, i have no idea what assignment ur talking bout, and wheres mq? soz

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.