help with 3D tic-tac-toe diagonal checking

This is a discussion on help with 3D tic-tac-toe diagonal checking within the C++ Programming forums, part of the General Programming Boards category; i need some help with figuring out how to check diagonals in my 3D Tic-Tac-Toe board. i have the horizontal, ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    44

    help with 3D tic-tac-toe diagonal checking

    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 problem is that im making it to that you choose what cubic dimensions you want and i dont know how to go through the board and only check diagonal. i can put more code up if you need it too.

    Code:
    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;
    
    		}
    	}
    	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;
    }

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    726
    Well, without looking at your code yet (hint: too long methinks!), I'd suggest something along the lines the following:

    Code:
    Dimensions x,y,z
    Foreach(x)
        test diagonal in plane x
    Foreach(y)
        test diagonal in plane y
    Foreach(z)
        test diagonal in plane z
    ie. you could extract each plane from each dimension (3 * n times where n is cube length) and test for winDiagonal() on each of them.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,515
    Why is your board a template? What could you possible store in a tic tac toe board other than integers and why?

    You've got a mess there.

    Suggestion:

    1. Make a board class that encapsulate everything possible a board should/can do.
    2. Make another class to encapsulate the layers of boards. Use a std vector or list to maintain the boards or if you know how many boards, create an array of class Board.
    3. Dump the nested loop. It's hideous.
    Arrogance breeds bad code

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    44
    its a template cause thats what the assignment is suppose to have. right now im just trying to get it working then i worry about making the code look perty.

    jafet could u elaborate a little more i dont quite understand what u mean by multiply by 3*n how would that work?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with my simple Tic tac toe prog
    By maybnxtseasn in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 06:25 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  4. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 08:33 AM
  5. checking for win in tic tac toe
    By Leeman_s in forum C++ Programming
    Replies: 1
    Last Post: 04-13-2002, 01:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21