• 0

[c++] Checking with For Loops


Question

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 kerodeon
Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0
(set[posy][posy]==1)

The first posy wouldn't need to be posx would it?

Other thing I'm trying to figure out is if you could run into the problem of where the check goes off the end of the array (leading to a nasty error).

I actually wrote a program similar to this a long time ago in Java but didn't go for the simplistic looping approach instead I checked in each direction separately. Nastiest part was actually my diagonal check, which I don't see you actually checking here. Are you skipping diagonal?

Link to comment
Share on other sites

  • 0

oh woops, that was just a mistake when i put it in the forum, still doesnt work :) thx for looking though

diagonal checks:

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

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

Other thing I'm trying to figure out is if you could run into the problem of where the check goes off the end of the array (leading to a nasty error).

Yah, its done that before, but when it did it before the program had a major error and shutdown, and its not doing that now.

Link to comment
Share on other sites

  • 0

Here's mine from a while back, probably nastier than the checking needs to be, but not sure how else to avoid hitting the end of the array. Different style of game, but should work pretty much the same. Only difference I can see is the size of the grid is fixed on mine.

? ?public boolean pebblecheck(String symbol, int row, int column, JButton[][] b, int numCheck) {
 ? ? ? ?int left = 0; ?//Stores the number of buttons found to the horizontal left in radius numCheck that have the same symbol
 ? ? ? ?int right = 0; ?//Stores the number of buttons found to the horizontal right in radius numCheck that have the same symbol
 ? ? ? ?int down = 0; ?//Stores the number of buttons vertically downwards in radius numCheck that have the same symbol
 ? ? ? ?int up = 0; ?//Stores the number of buttons vertically upward in radius numCheck that have the same symbol
 ? ? ? ?int downleft = 0; ?//Stores the number of buttons diagonally down-left in radius numCheck that have the same symbol
 ? ? ? ?int upleft = 0; ?//Stores the number of buttons diagaonlly up-left in radius numCheck that have the same symbol
 ? ? ? ?int downright = 0; ?//Stores the number of buttons diagaonlly down-right in radius numCheck that have the same symbol
 ? ? ? ?int upright = 0; ?//Stores the number of buttons diagaonlly up-right in radius numCheck that have the same symbol
 ? ? ? ?//////////////////////////
 ? ? ? ?// ? Check horizontal ? //
 ? ? ? ?//////////////////////////
 ? ? ? ?if (column != 0) {
 ? ? ? ? ? ?//If the button isn't in column 0, the left side is checked
 ? ? ? ? ? ?for (int currcol = column - 1; ((currcol >= (column-numCheck)) && (currcol >= 0)); currcol--) {
 ? ? ? ? ? ? ? ?//Checks the left side of the button for radius numCheck, starting next to the button and going leftwards until it hits a different symbol or hits the edge
 ? ? ? ? ? ? ? ?if (b[row][currcol].getText().equals(symbol)) {
 ? ? ? ? ? ? ? ? ? ?left++; ?//Increments left if the button has the same symbol
 ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ? ? ?else {
 ? ? ? ? ? ? ? ? ? ?break; ?//If it doesn't have the same symbol, break out of the loop
 ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ?}
 ? ? ? ?}
 ? ? ? ?if (column != 5) {
 ? ? ? ? ? ?//If the column isn't 5, it checks to the right of the button
 ? ? ? ? ? ?for (int currcol = column + 1; ((currcol <= (column+numCheck)) && (currcol <= 5)); currcol++) {
 ? ? ? ? ? ? ? ?//Checks the right side of the button for radius numCheck, starting next to the button and going rightwards until it hits a different symbol or hits the edge
 ? ? ? ? ? ? ? ?if (b[row][currcol].getText().equals(symbol)) {
 ? ? ? ? ? ? ? ? ? ?right++; ?//Increments right if the button has the same symbol
 ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ? ? ?else {
 ? ? ? ? ? ? ? ? ? ?break; ?//If it doesn't have the same symbol, break out of the loop
 ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ?}
 ? ? ? ?}
 ? ? ? ?////////////////////////
 ? ? ? ?// ? Check vertical ? //
 ? ? ? ?////////////////////////
 ? ? ? ?if (row != 0) {
 ? ? ? ? ? ?//If the button isn't in row 0, the upward side is checked
 ? ? ? ? ? ?for (int currow = row - 1; ((currow >= (row-numCheck)) && (currow >= 0)); currow--) {
 ? ? ? ? ? ? ? ?//Checks the upward side of the button for radius numCheck, starting next to the button and going upwards until it hits a different symbol or hits the edge
 ? ? ? ? ? ? ? ?if (b[currow][column].getText().equals(symbol)) {
 ? ? ? ? ? ? ? ? ? ?up++; ?//Increments up if the button has the same symbol
 ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ? ? ?else {
 ? ? ? ? ? ? ? ? ? ?break; ?//If it doesn't have the same symbol, break out of the loop
 ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ?}
 ? ? ? ?}
 ? ? ? ?if (row != 5) {
 ? ? ? ? ? ?//If the button isn't in row 5, the downward side is checked
 ? ? ? ? ? ?for (int currow = row + 1; ((currow <= (row+numCheck)) && (currow <= 5)); currow++) {
 ? ? ? ? ? ? ? ?//Checks the downward side of the button for radius numCheck, starting next to the button and going downwards until it hits a different symbol or hits the edge
 ? ? ? ? ? ? ? ?if (b[currow][column].getText().equals(symbol)) {
 ? ? ? ? ? ? ? ? ? ?down++; ?//Increments down if the button has the same symbol
 ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ? ? ?else {
 ? ? ? ? ? ? ? ? ? ?break; ?//If it doesn't have the same symbol, break out of the loop
 ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ?}
 ? ? ? ?}
 ? ? ? ?////////////////////////
 ? ? ? ?// ? Check diagonal ? //
 ? ? ? ?////////////////////////
 ? ? ? ?if ((row != 5) && (column != 5)) {
 ? ? ? ? ? ?//If the button isn't in row 5 and column isn't 5, the down-right side is checked
 ? ? ? ? ? ?int currow = row + 1;
 ? ? ? ? ? ?int currcol = column + 1;
 ? ? ? ? ? ?while ((currow <= row+numCheck) && (currcol <= column+numCheck) && (currow <= 5) && (currcol <= 5)) {
 ? ? ? ? ? ? ? ?//Checks right downwards for radius numCheck until it hits an edge or hits a different symbol
 ? ? ? ? ? ? ? ?if (b[currow][currcol].getText().equals(symbol)) {
 ? ? ? ? ? ? ? ? ? ?downright++; ?//Increases downright if symbols are the same
 ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ? ? ?else {
 ? ? ? ? ? ? ? ? ? ?break; ?//Breaks if they aren't the same
 ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ? ? ?currow++;
 ? ? ? ? ? ? ? ?currcol++;
 ? ? ? ? ? ?}
 ? ? ? ?}

 ? ? ? ?if ((row != 0) && (column != 5)) {
 ? ? ? ? ? ?//If the button isn't in row 0 and isn't in column 5, the up-right side is checked
 ? ? ? ? ? ?int currow = row - 1;
 ? ? ? ? ? ?int currcol = column + 1;
 ? ? ? ? ? ?while ((currow >= (row-numCheck)) && (currcol <= (column+numCheck)) && (currow >= 0) && (currcol <= 5)) {
 ? ? ? ? ? ? ? ?//Checks right upwards for radius numCheck until it hits an edge or hits a different symbol
 ? ? ? ? ? ? ? ?if (b[currow][currcol].getText().equals(symbol)) {
 ? ? ? ? ? ? ? ? ? ?upright++; ?//Increases upright if symbols are the same
 ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ? ? ?else {
 ? ? ? ? ? ? ? ? ? ?break; ?//Breaks if they aren't
 ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ? ? ?currow--;
 ? ? ? ? ? ? ? ?currcol++;
 ? ? ? ? ? ?}
 ? ? ? ?}

 ? ? ? ?if ((row != 0) && (column != 0)) {
 ? ? ? ? ? ?//If the button isn't in row 5 and isn't in column 0, the up-right side is checked
 ? ? ? ? ? ?int currcol = column - 1;
 ? ? ? ? ? ?int currow = row - 1;
 ? ? ? ? ? ?while ((currow >= (row-numCheck)) && (currcol >= (column-numCheck)) && (currow >= 0) && (currcol >= 0)) {
 ? ? ? ? ? ? ? ?//Checks left upwards for radius numCheck until it hits an edge or hits a different symbol
 ? ? ? ? ? ? ? ?if (b[currow][currcol].getText().equals(symbol)) {
 ? ? ? ? ? ? ? ? ? ?upleft++; ?//Increases upleft if symbols are the same
 ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ? ? ?else {
 ? ? ? ? ? ? ? ? ? ?break; ?//Breaks if they aren't
 ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ? ? ?currow--;
 ? ? ? ? ? ? ? ?currcol--;
 ? ? ? ? ? ?}
 ? ? ? ?}

 ? ? ? ?if ((row != 5) && (column != 0)) {
 ? ? ? ? ? ?//If the button isn't in row 5 and isn't in column 0, the up-right side is checked
 ? ? ? ? ? ?int currcol = column - 1;
 ? ? ? ? ? ?int currow = row + 1;
 ? ? ? ? ? ?while ((currow <= (row+numCheck)) && (currcol >= (column-numCheck)) && (currow <=5) && (currcol >= 0)) {
 ? ? ? ? ? ? ? ?//Checks left downwards for radius numCheck until it hits an edge or hits a different symbol
 ? ? ? ? ? ? ? ?if (b[currow][currcol].getText().equals(symbol)) {
 ? ? ? ? ? ? ? ? ? ?downleft++; ?//Increases downleft if symbols are the same
 ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ? ? ?else {
 ? ? ? ? ? ? ? ? ? ?break; ?//Breaks if they aren't
 ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ? ? ?currow++;
 ? ? ? ? ? ? ? ?currcol--;
 ? ? ? ? ? ?}
 ? ? ? ?} ? ? ? ? ? ? ? 

 ? ? ? ?boolean hor = ((right + left) >= numCheck); ?//Checks to see if the horizontal numbers meets the numCheck requirements
 ? ? ? ?boolean ver = ((up + down) >= numCheck); ?//Checks to see if the vertical numbers meet the numCheck requirements
 ? ? ? ?boolean forwardslash = ((downleft+upright) >= numCheck); ?//Checks to see if the forward slash diagonals meet the numCheck requirements
 ? ? ? ?boolean backslash = ((upleft+downright) >= numCheck); ?//Checks to see if the black slash diagonals meet the numCheck requirements
 ? ? ? ?if (hor || ver || forwardslash || backslash) {
 ? ? ? ? ? ?return true; ?//If any of the directions meet the requirement, true is returned
 ? ? ? ?}
 ? ? ? ?return false; ?//Return false if all don't
 ? ?}

Other thing I'm not sure is what do the 2 and 1 mean? Does it mean player 1 or 2 has dropped one there? If so, I'm not sure I understand this check:

? ? ?if ((set[posx][posy]==2)||(set[posx][posy]==1)) {
 ? ? ? check=check+1;
 ? ? ?}

You're incrementing if either player 1 or 2 went there? I thought you only checked for one player (the one in currentch?), and so if his wasn't there, you'd break.

Link to comment
Share on other sites

  • 0

Yeah, you're right, im just checking for any player at the moment, (baby steps)

Anyway, i got it working, :D :D :D no idea how, im pretty sure i changed nothing, but heres my code for the whole thing:

#include <iostream>
using namespace std;

int main() {

	/* Changeable Variables */
	/**/ int setsize = 4; /**/
	/**/ int chsize	= 3;  /**/
	/**/      /**/
	/**/      /**/
	/* Changeable Variables */

	// Declare some Variables
	int xco, yco;
	int set[20][20], x, y, check, yloop, xloop, breakloop, i, dist, currentch, posy, posx, killer;
	int turn = 1;

	// Let's clear the board!
	for ( x=0; x<=setsize+1; x++ ) {
  for ( y=0; y<=setsize+1; y++ ) {
 	 set[x][y] = 0;
  }
	}

	// Perty Title
	cout << "Nac Tic Tac Toe\n" << endl;
	cout << "-----------------------------------------";
	cout << "---------------------------------------" << endl;

	while ( breakloop != 1 ) {

  // Interchanges Turn
  if ( turn==2 ) { turn = 1; }
  else { turn = 2; }

  // Where ya wanna go?
  cout << "X Co-ordinate: ";
  cin >> xco;
  cout << "Y Co-ordinate: ";
  cin >> yco;
  cout << "\n";

  // Places Piece
  set[xco][yco] = turn;

  // Prints out Board
  for ( x=1; x<=setsize; x++ ) {
 	 for ( y=1; y<=setsize; y++ ) {
    cout << set[x][y];
 	 }
 	 cout << "\n";
  }


  // Anyone win?  
  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==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 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]!=2)&&(set[posx][posy]!=1)) {
              break;
            }
            else {
           	 check=check+1; // Only continues loop if there are values in that direction
     	 }
      }
      posx=xloop;
      posy=yloop;
   	 }	
    }
 	 }
  }

  if (check>chsize) {
    cout<<"\nYou Won!"<<endl;
    breakloop = 1;
  }


  // Perty Line
  cout << "\n---------------------------------------------" << endl;


	}

  


	fflush (stdin);
	cout << "\n\nPress any key to continue . . . ";
	cin.get();


	return 0;

}

I did put this in

     posx=xloop;
      posy=yloop;

But i cant figure out how that fixed it, oh well, thx for the help kjordan2001, might use your example to neaten up mine so i can understand it :).

Link to comment
Share on other sites

  • 0

Ah, now actually that I see what you just put in, that does make sense. You have to reset where your starting point is otherwise it keeps going from where you left off. Mine's probably a little lighter on the CPU too as it only checks around where they went. And now that I think about it, probably about the same amount of loops (maybe fewer?).

Edited by kjordan2001
Link to comment
Share on other sites

  • 0

ohhh, i do see. Anyways, i just did mine the way i did, as i wanted to have it adustable, board size and checking length, cause my brother did it with his connect 4 game (php)

src: http://www.webpathways.com/damien/connect4/src

game: http://www.webpathways.com/damien/connect4/

Anyway, thank you much for your help, i just need to neaten it up now, putting turns and such in. After that i can start work on AI. :o

Link to comment
Share on other sites

  • 0
ohhh, i do see. Anyways, i just did mine the way i did, as i wanted to have it adustable, board size and checking length, cause my brother did it with his connect 4 game (php)

src: http://www.webpathways.com/damien/connect4/src

game: http://www.webpathways.com/damien/connect4/

Anyway, thank you much for your help, i just need to neaten it up now, putting turns and such in. After that i can start work on AI. :o

AI is never something I really got around to with mine, although it was an assignment and AI wasn't necessary (in fact it would have been overkill). It might be fun to put AI in it though :D

I tried to put AI in a robot maze assignment, but it didn't turn out all that well, was going to try to get around to fixing that but never really got around to it. Never could get Java to slow down the refresh either, if he succeeded, he'd jump right to the end point.

Hmm, guess mine was a connect 4 game too, pretty much the same as tic-tac-toe though, just a different way to put the moves in.

Edited by kjordan2001
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.