Thread: can someone pls tell me if u found bugs with my tic tac toe! thnx

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    6

    can someone pls tell me if u found bugs with my tic tac toe! thnx

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <time.h>
    
    class XOXboard
    {
    public:
    
    	XOXboard();
    	~XOXboard();
    
    	void draw(void);
    	void PosXOX(void);
    	void update(int x);
    	
    	int check(void);
    	
    private:
    
    	static int xwins;
    	static int owins;
    	char *xox;
    };
    
    int XOXboard::owins = 0;
    int XOXboard::xwins = 0;
    
    XOXboard::XOXboard()
    {
    	xox = new char[10];
    	for(int i = 0; i < 9; ++i)
    		xox[i] = ' ';
    	xox[i] = '\0';
    }
    
    XOXboard::~XOXboard()
    {
    	delete [] xox;
    }
    
    void XOXboard::draw()
    {
    	int i = 0;
    	system("cls");
    	cout << "\n\n\t\t  SAMPLE TIC TAC TOE MADE BY EON-SLASH"
    		 << "\n\n\n\t\t\t   " << ' ' << xox[0] << "  |  " << ' ' << xox[1] << "  |  " << ' '
    		 << xox[2] << endl
    		 << "\t\t\t   ----------------" << endl
    		 << "\t\t\t   " << ' ' << xox[3] << "  |  " << ' ' << xox[4] << "  |  " << ' ' 
    		 << xox[5] << endl
    		 << "\t\t\t   ----------------" << endl
    		 << "\t\t\t   " << ' ' << xox[6] << "  |  " << ' ' << xox[7] << "  |  " << ' ' 
    		 << xox[8] << endl << endl << endl
    		 << "\t\t\t\t\t\t    CURRENT SCORES X: " << xwins << "  O: " << owins;
    }
    
    void XOXboard::PosXOX()
    {
    	char offset;
    	while(1)
    	{
    		cout << "\nPOSITION ( 1 - 9 ) : ";
    		cin >> offset;
    		cin.ignore(255,'\n');
    
    		if(int(offset) < 49 || int(offset) > 57)
    		{
    			cout << "\nwrong choice\n";
    			continue;
    		}
    		else
    			if(xox[offset - 49] == 'X' || xox[offset - 49] == 'O')
    			{
    				cout << "\nposition " << offset << " is already " << xox[offset - 49];
    				continue;
    			}
    			else
    				break;
    	}
    	xox[int(offset - 49)] = 'X';
    }
    
    int XOXboard::check()
    {
    	char *x = new char[10];
    	for(int i = 0; i < 10; ++i)
    		x[i] = xox[i];
    
    	if((x[0] == x[1] && x[0] == x[2]) || (x[0] == x[4] && x[0] == x[8]) || \
    		(x[0] == x[3] && x[0] == x[6]))
    	{
    		if(x[0] != ' ')
    		{
    			cout << "\n\n\t\t\t\t" << x[0] << " wins\n\n";
    			if(x[0] == 'X')
    				xwins++;
    			else
    				owins++;
    			return 1;
    		}
    	}
    
    	if((x[1] == x[4] && x[1] == x[7]))
    	{
    		if(x[1] != ' ')
    		{
    			cout << "\n\n\t\t\t\t" << x[1] << " wins\n\n";
    			if(x[1] == 'X')
    				xwins++;
    			else
    				owins++;
    			return 1;
    		}
    	}
    
    	
    	if((x[2] == x[5] && x[2] == x[8]) || (x[2] == x[4] && x[2] == x[6]))
    	{
    		if(x[2] != ' ')
    			{
    				cout << "\n\n\t\t\t\t" << x[2] << " wins\n\n";
    				if(x[2] == 'X')
    					xwins++;
    				else
    					owins++;
    				return 1;
    			}
    	}
    
    	if((x[3] == x[4] && x[3] == x[5]))
    	{
    		if(x[3] != ' ')
    			{
    				cout << "\n\n\t\t\t\t" << x[3] << " wins\n\n";
    				if(x[3] == 'X')
    					xwins++;
    				else
    					owins++;
    				return 1;
    			}
    	}
    
    	if((x[6] == x[7] && x[6] == x[8]))
    	{
    		if(x[6] != ' ')
    			{
    				cout << "\n\n\t\t\t\t" << x[6] << " wins\n\n";
    				if(x[6] == 'X')
    					xwins++;
    				else
    					owins++;
    				return 1;
    			}
    	}
    
    	return 0;
    }
    
    void XOXboard::update(int x)
    {
    	srand(time(NULL));
    	int ai = rand() % 9;
    	
    	if(x >= 5)
    		return;
    	
    	while(1)
    	{
    		if(xox[ai] == 'X' || xox[ai] == 'O')
    		{
    			ai = rand() % 9;
    			continue;
    		}
    		else
    			break;
    	}
    	xox[ai] = 'O';
    }
    
    int check(XOXboard *eon, int shot);
    int main()
    {
    	int shot = 0;
    	int checker;
    	XOXboard *eon = new XOXboard;
    
    	while(shot++ < 5)
    	{
    		eon->draw();
    		eon->PosXOX();
    		checker = check(eon, shot);
    		if(checker == 1)
    		{
    			shot = 0;
    			eon = new XOXboard;
    			continue;
    		}
    		else
    			if(checker == 2)
    				break;
    				
    		eon->update(shot);
    		checker = check(eon, shot);
    		if(checker == 1)
    		{
    			shot = 0;
    			eon = new XOXboard;
    			continue;
    		}
    		else
    			if(checker == 2)
    				break;
    	}
    	eon->draw();
    	cout << "\nTHNX FOR PLAYING!\n\n";
    	system("pause");
    
    	return 0;
    }
    
    int check(XOXboard *eon, int shot)
    {
    	char choice;
    	if((eon->check()) || (!eon->check() && shot >= 5))
    		{
    			eon->draw();
    			if(!eon->check() && shot >= 5)
    				cout << "\nITS A DRAW!!!!";
    			cout << "\nDO YOU WANT TO PLAY AGAIN? (Y/N): ";
    			cin >> choice;
    			cin.ignore(255, '\n');
    			if(tolower(choice) == 'y')
    				return 1;
    			else
    				return 2;
    		}
    	return 0;
    }
    pls tell me about bugs if you found some!!!! THNX!!!

    hoy mark try mo toh ASTIG hehehehe! wala lang!!!

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    OMG I couldn't understand a word (those are words?) of what he's saying.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Dunno - but it sure marks it for someone to ignore.
    Sheesh, this isn't a "dump my mess for someone else to fix" board.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361
    especially since "thnx" is in the subject line
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Actually I will congratulate him on his layout - a rarity around here sometimes.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  6. #6
    *this
    Join Date
    Mar 2005
    Posts
    498
    I would have to disagree ^^^.

    1. You can initialize your private data members in your default constructor.
    2. You dont have to make private data members static, they already are.
    3. Use newer headers: iostream ctime etc...
    4. Comment so people can understand what you are doing.
    5. Seperate your class from the client code into its own header
    6. if((x[0] == x[1] && x[0] == x[2]) || (x[0] == x[4] && x[0] == x[8]) || \
      (x[0] == x[3] && x[0] == x[6]))
      - Turn this into a function and get rid of that slash
    Last edited by JoshR; 08-19-2005 at 04:11 PM.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Or, rather, backslash. He can leave it in if he wants to.

    Code:
            while(1)
    	{
    		if(xox[ai] == 'X' || xox[ai] == 'O')
    		{
    			ai = rand() % 9;
    			continue;
    		}
    		else
    			break;
    	}
    That else isn't needed.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Except if you take it out, how will the program leave the infinite while loop?

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    >> You dont have to make private data members static, they already are.

    As far as I know private members of a class arent static by default.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No they're not static. Why would they be? That would mean that all class instances share that same member. That's obviously incorrect.
    Code:
    class foo
    {
        private:
            int bar; //not static
    
        public:
            void setbar( int baz ) { bar = baz; };
            int getbar( void ) { return bar; }
    };
    
    foo f1, f2;
    
    f1.setbar( 1 );
    f2.setbar( 2 ); //if it were static, this would change 'f1.bar'
    cout << f1.getbar( );

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I mean the else keyword isn't needed. Just remove that, and leave the break in.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Positions 1 - 9? You're assuming the user will know what squares those positions correspond to? That's a poor assumption.

    Fix your memory leaks.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Look, what I mean is that
    Code:
            while(1)
    	{
    		if(xox[ai] == 'X' || xox[ai] == 'O')
    		{
    			ai = rand() % 9;
    			continue;
    		}
    		else
    			break;
    	}
    can be
    Code:
            while(1)
    	{
    		if(xox[ai] == 'X' || xox[ai] == 'O')
    		{
    			ai = rand() % 9;
    			continue;
    		}
    
    		break;
    	}
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    3
    even better than just removing the else :

    Code:
    while (xox[ai] == 'X' || xox[ai] == 'O')
    
          ai = rand() % 9;
    i don't know if it's will work, but i will not be surprised if it's.
    if it's don't, try that, it's better than nothing...

    Code:
    while (1)
                  if(xox[ai] == 'X' || xox[ai] == 'O')
                                   ai = rand() % 9;
                    else
                                    break;
    you won't need those { } between all the code inside the "while", because there is only one commands asked to do in.

    ( don't know if you have understood, my english is not perfect ^_^)

  15. #15
    *this
    Join Date
    Mar 2005
    Posts
    498
    Quote Originally Posted by quzah
    No they're not static. Why would they be? That would mean that all class instances share that same member. That's obviously incorrect.
    No its not obviously incorrect, i was thinking about the class itself. Each individual class object has its own static members thats what i meant to say. so no its not obviously incorrect.
    Last edited by JoshR; 08-23-2005 at 12:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding a word in a string.
    By esbo in forum C Programming
    Replies: 15
    Last Post: 08-28-2006, 07:48 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Line Counting
    By 00Sven in forum C Programming
    Replies: 26
    Last Post: 04-02-2006, 08:59 PM
  4. tic tac toe
    By holden in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-09-2004, 09:59 AM