i need some help with figuring out how to check diagonals in my 3D Tic-Tac-Toe board. i have the horizontal, vertical and depth done fine but i cant figure out how to get diagonals. i only put in the check part since the full code is really long. the peoblem is that im making it to that you choose wha cubic dimensions you want and i dont know how to go through the board and only check diagonal.

template <class TYPE>
class Grid : public Darray<TYPE> {
private:
	int currentRow,
		currentDepth,
		currentCol;
public:
	Grid();
	Grid(TYPE depth, TYPE row, TYPE col);
	void arrow_keys(Player *player);
	bool win();
	void clear_screen(void);
	void draw ();
	void draw (TYPE d);
	void topBorder();
	void bottomBorder();
	bool winHorizontal(Player* player);
	bool winVertical(Player* player);
	bool winDiagonal(Player* player);
	bool winCubic(Player* player);
};

template <class TYPE>
bool Grid<TYPE>::winHorizontal(Player* player)
{
	int count;
	for(int d = 0; d <DEPTH; d++){
		for(int r = 0; r<ROW; r++){
			for(int c = 0; c< COL; c++)
			{
				if(*player == *array[d][r][c].getPlayer())
					count++;
			}
			if(count == COL)
				return true;
			count = 0;	
		}
	}	
		cout << "Player " << symbol << " Wins!!" << endl;
		return false;
}
template <class TYPE>
bool Grid<TYPE>::winVertical(Player* player)
{
	int count;
	for(int d = 0; d <DEPTH; d++){
		for(int r = 0; r<ROW; r++)
		{
			if(*player == *array[d][r][c].getPlayer())
				count++;
		}
		if(count == ROW)
			return true;
		count = 0;
	}
	cout << "Player " << symbol << " Wins!!" << endl;
	return false;
}
template <class TYPE>
bool Grid<TYPE>::winDiagonal(Player* player)
{
	int count;
	for(int d = 0; d <DEPTH; d++){
		for(int r = 0; r<ROW; r++){
			for(int c = 0; c< COL; c++)
			{
				if(*player == *array[d][r][c].getPlayer() || *player == *array[d][r][c].getPlayer() )
					count++;
			}
			if()
				return true;
			count = 0;
			currentDepth--;
		}
	}
	cout << "Player " << symbol << " Wins!!" << endl;
	return false;
}
template <class TYPE>
bool Grid<TYPE>::winCubic(Player* player)
{
	int count;
	for(int d = 0; d < DEPTH; d++)
	{
		if(*player == *array[d][r][c].getPlayer())
			count++;
	}
	if(count == DEPTH)
		return true;
	count  = 0;

	cout << "Player " << symbol << " Wins!!" << endl;
	return false;
}
template <class TYPE>
bool Grid<TYPE>::win()
{
	if(winHorizontal())
		return true;
	if(winVertical())
		return true;
	if(winDiagonal())
		return true;
	if(winCubic())
		return true;
	else
		return false;
}
Member Avatar for iamthwee

hmmm,

Perhaps this?

for (int i=0; i<3; i++)
    {
        for(int j=0; j<3; j++)
        {
            for(int k=0; k<3; k++)
            {
                //check within_grid() 
                //check no_repeats()
                //check diagonals() I reckon this to be 16 checks
            }
        }
    }
  

// for and nxn cube there should always be
// only 24 possible solutions for winning diagonally
// not really sure though.

I'll think about it some more?

i have another question too, i cant get my static character to work right either. i start the first player off at 'A' which i get fine but when it goes to hte next player i want the player to be 'B' and so on for the number of players casue there can be more than 2 players. but after the 'A' gets printed the second player is not 'B' it just prints some wierd character instead.

class to make characters

class Player
{
private:
	char symbol;

public:
	static char players;

	Player() {symbol = players++;}
	~Player();
	char getMark() {return symbol;}
	bool operator == (Player& player); 
};


Player::~Player(){}

bool Player::operator == (Player& player)
{
	return symbol == player.symbol; 
}
// Global
char Player::players = 'A';

class to hold # of players

class Playerlist
{
private:

	int size;
	Player* player; // array 

public:
	Playerlist();
	~Playerlist();
	Playerlist (const Playerlist& playerlist);
	int getSize() {return size;}

};

Playerlist::Playerlist()
{
	player = NULL;

	cout << "How many players will be playing? " << endl;

	cout << "* Number of players should not exceed the cubic dimensions * " << endl;
	cin >> size;
}

Playerlist::~Playerlist()
{
	delete [] player;
}

Playerlist::Playerlist (const Playerlist& playerlist)
{
	size = playerlist.size;
	player = new Player [size];
	for(int s = 0; s < size; s++)
		player[s] = playerlist.player[s];
}

this is a switch statement you hin enter to mark the place you want on the board

.
.
.
.
case ENTER:
		//if(array[currentDepth][currentRow][currentCol].mark(player))
		//	cout << "Space already taken choose again" << endl;
		//else
		array[currentDepth][currentRow][currentCol].mark(player);
		player++;
		break;
Member Avatar for iamthwee

Actually I was wrong about there being 24 wins diagonally for an n x n cube.

It is actually only true for a 3 x 3 cube. 24 wins diagonally.

Actually I was wrong about there being 24 wins diagonally for an n x n cube.

It is actually only true for a 3 x 3 cube. 24 wins diagonally.

yea im stumped on this so for now dont worry about it but what do u see wrong with my static character im more concerned about that right now cause its almost working

Member Avatar for iamthwee

Actually, I think the wins for the diagonals on the 3 x 3 cube is 22.

I think the extended formula to calculate the diagonal wins for an arbitary n x n cube is:

diagonals = 6n + 4

If that's still wrong I'm giving up. :cry:


>what do u see wrong with my static character ...it's printing junk

Sorry bout that, perhaps someone else will help you. I'm just thinking about this from a generic point of view. ;)

actually i got the static character goin but ill let ya know if i get the diagonal working

Assuming you're only doing odd-number size cubes (3x3, 5x5, 7x7, etc):

I suggest the following approach.

For the cube itself, there are two full-diagonal wins.
If you think of the n x n cube in terms of horizontal and vertical layers, you get 2n.
For example: a 3x3 cube has six layers.
You have three layers left to right, and three layers top to bottom.

Every layer has 2 potential diagonal wins.

Ergo, the total number of diagonals will be 4n+2. (n = 3, 5, 7...)

However: Why bother with that? You can write three functions (one to check vertical layers, one to check horizontal layers, one to check the cube's diagonals) and call them iteratively by passing in the layer (probably best as a pointer) you're checking.

That way, you can also check for vertical and horizontal wins in any layer at the same time, and you only need to search by way of:

for(int i = 0; i<n;i++)
{
     horizCheck(layer(i));
     vertCheck(layer(i));
}
cubeDiagCheck(cubematrix);

and be done with it.

this is what i did and it seems to work

template <class TYPE>
bool Grid<TYPE>::winDiagonal(Player* player)	//diagonals
{
	Playerlist plist;
	int count = 0;
	int size = plist.getSize();
	for(int i = 0; i < size; i++)
	{
		//cubic diagonal top front left to bottom back right
		if(*player == *array [i][i][i].getPlayer())	
			count++;

		//cubic diagonal bottom front left to top back right
		else if(*player == *array[i][size-1-i][i].getPlayer())
			count++;

		//cubic diagonal top front right to bottom back left
		else if(*player == *array[i][i][size-1-i].getPlayer())
			count++;

		//cubic diagonal bottom front right to top back left
		else if(*player == *array[i][size-1-i][size-1-i].getPlayer())
			count++;
	}
	for(int d = 0; d < DEPTH; d++){
		for(int i = 0; i < size; i++)
		{
		// '\' diagonal check
		if(*player == *array[d][i][i].getPlayer())
			count++;

		// '/' diagonal check
		else if(*player == *array[d][i][size-1-i].getPlayer())
			count++;
		}
			
	}
			if(count == size)
				return true;

			count = 0;
			return false;
}
Member Avatar for iamthwee

>Ergo, the total number of diagonals will be 4n+2. (n = 3, 5, 7...)

Me thinks no, even if you're only considering odd values for n.

The number of diagonals should still be 6n+ 4

In any case why would you want to restrict it to odd values anyway, even values for n work just as well? The game is a total mind warp tho.

http://www.mathcats.com/explore/puzzles/3dtictactoe.html

Member Avatar for iamthwee

>lol yea the game is suppoes to crazy is there a source code for that game on there i couldnt find it if there was

I've had a look and found some code for your game in basic.
I've found this source in java, which is the most impressive so far:
http://www.ozpolitics.info/java/Qubic.htm

Although if you've never used java before some of the semantics might be confusing.

I'm switching from one language to another at the moment, so I concern myself more with the principle ideas behind a problem rather than the syntax of a particular language.

If I find any useful code relevant to c++ with class and template design I'll let you know.

>Ergo, the total number of diagonals will be 4n+2. (n = 3, 5, 7...)

Me thinks no, even if you're only considering odd values for n.

The number of diagonals should still be 6n+ 4

In any case why would you want to restrict it to odd values anyway, even values for n work just as well? The game is a total mind warp tho.

Okay, I'll conceed the +4, and that the equation 4n+4 will apply to evens as well as odds.

But could you please explain how a square can have more than two unique diagonals?

Member Avatar for iamthwee

Okay, I'll conceed the +4, and that the equation 4n+4 will apply to evens as well as odds.

But could you please explain how a square can have more than two unique diagonals?

Square or cube, are we thinking in 3 dimensions here kiddo? ;)

Dude it's simple think about it some more. I know da answer.
Nah nah ne nah nah.

... There's 12 boards. Fine. 6n+4 is correct, And my approach won't quite work. You'd need a single function that checks a single layer, then feed in the layers appropriately...

Or am I thinking too much in terms of Matlab?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.