Tic Tac Toe program
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Tic Tac Toe program

  1. #1
    Join Date
    Oct 2008
    Posts
    59

    Question Tic Tac Toe program

    Hi,

    I've created a program for the Tic Tac Toe game. I'm working on an exercise posted on the cplusplus.com website. Here is the exercise:

    Code:
    Requires:
    variables, data types, and numerical operators
    basic input/output
    logic (if statements, switch statements)
    loops (for, while, do-while)
    arrays
    
    Make a two player tic tac toe game.
    
    ★ Modify the program so that it will announce when a player has won the game (and which player won, x or o)
    
    ★★ Modify the program so that it is a one player game against the computer (with the computer making its moves randomly)
    
    ★★★★ Modify the program so that anytime the player is about to win (aka, they have 2 of 3 x's in a row, the computer will block w/ an o)
    I have so far reached the one star ★ part of the exercise and noticed that the program requires arrays, which I have not used. I'm not exactly sure how to use arrays to do the exercise. Could someone point me in the right direction ans well as critiquing my existing code? Here's my code so far:

    Code:
    #include <iostream>
    
    using namespace std;
    
    //void printBoard()
    //{
    //	cout << cell1 <<"|"<< cell2 << "|" << cell3 << endl;
    //	cout <<"-+-+-"<< endl;
    //	cout << cell4 <<"|"<< cell5 << "|" << cell6 << endl;
    //	cout <<"-+-+-"<< endl;
    //	cout << cell7 <<"|"<< cell8 << "|" << cell9 << endl;
    //	cout <<"-+-+-"<< endl;
    //}
    
    
    int main()
    {
    	char cell1 = '1';
    	char cell2 = '2';
    	char cell3 = '3';
    	char cell4 = '4';
    	char cell5 = '5';
    	char cell6 = '6';
    	char cell7 = '7';
    	char cell8 = '8';
    	char cell9 = '9';
    	char player = '1';
    	char mark;
    	bool gameover = false;
    
    	do
    	{
    		cout << cell1 <<"|"<< cell2 << "|" << cell3 << endl;
    		cout <<"-+-+-"<< endl;
    		cout << cell4 <<"|"<< cell5 << "|" << cell6 << endl;
    		cout <<"-+-+-"<< endl;
    		cout << cell7 <<"|"<< cell8 << "|" << cell9 << endl;
    		cout <<"-+-+-"<< endl;
    
    		//printBoard();
    	
    		//Handle player marker
    		if (player == '1')
    		{
    			mark = 'X';
    		}
    		else
    		{
    			mark = 'O';
    		}
    
    		bool invalid; 
    		do
    		{
    			cout << "Player "<<player<< ": \n";
    			char move;
    			cin >> move;
    			invalid = false;
    			
    
    			//Validate move
    
    			if (move == '1' && cell1 == '1')
    			{
    				cell1 = mark;
    			}
    
    			else if (move == '2' && cell2 == '2')
    			{
    				cell2 = mark;
    			}
    
    			else if (move == '3' && cell3 == '3')
    			{
    				cell3 = mark;
    			}
    
    			else if (move == '4' && cell4 == '4')
    			{
    				cell4 = mark;
    			}
    	
    			else if (move == '5' && cell5 == '5')
    			{
    				cell5 = mark;
    			}
    
    			else if (move == '6' && cell6 == '6')
    			{
    				cell6 = mark;
    			}
    
    			else if (move == '7' && cell7 == '7')
    			{
    				cell7 = mark;
    			}
    
    			else if (move == '8' && cell8 == '8')
    			{
    				cell8 = mark;
    			}
    
    			else if (move == '9' && cell9 == '9')
    			{
    				cell9 = mark;
    			}
    
    			else
    			{
    				cout <<" Invalid Move"<<endl;;
    				invalid = true;
    			}
    		}
    		while (invalid);                             
    
    		//Check for winning & draw conditions
    		
    		bool draw = false;
    		if (cell1 != '1')
    		{
    			if (cell1 == cell2 && cell1 == cell3)
    			{
    				gameover = true;
    			}
    
    			else if (cell1 == cell4 && cell1 == cell7)
    			{
    				gameover = true;
    			}
    		}
    
    		if (cell9 != '9')
    		{
    			if (cell9 == cell3 && cell9 == cell6)
    			{
    				gameover = true;
    			}
    
    			else if (cell9 == cell8 && cell9 == cell7)
    			{
    				gameover = true;
    			}
    		}
    
    		if (cell5 != '5')
    		{
    			if (cell5 == cell2 && cell5 == cell8)
    			{
    				gameover = true;
    			}
    
    			else if (cell5 == cell4 && cell5 == cell6)
    			{
    				gameover = true;
    			}
    
    			else if (cell5 == cell1 && cell5 == cell9)
    			{
    				gameover = true;
    			}
    
    			else if (cell5 == cell3 && cell5 == cell7)
    			{
    				gameover = true;
    			}
    
    		}
    
    		// Check for draw conditions
    		if ( cell1 != '1' && cell2 != '2' && cell3 != '3' && 
    			cell4 != '4' && cell5 != '5' && cell6 != '6' &&
    			cell7 != '7' && cell8 != '8' && cell9 != '9')
    		{
    			cout <<" Its a draw"<<endl;
    			gameover = true;
    			draw = true; 
    		}
    
    		if (gameover)
    		{
    			if (draw == false)
    			{
    				cout << "Player "<< player << " wins!"<<endl;
    				cout <<endl;
    			}
    
    			//printBoard();
    			cout << cell1 <<"|"<< cell2 << "|" << cell3 << endl;
    			cout <<"-+-+-"<< endl;
    			cout << cell4 <<"|"<< cell5 << "|" << cell6 << endl;
    			cout <<"-+-+-"<< endl;
    			cout << cell7 <<"|"<< cell8 << "|" << cell9 << endl;
    			cout <<"-+-+-"<< endl;
    			cout <<endl;
    
    			cout<< "Play again? (y/n)"<< endl;
    			char playAgain;
    			cin >> playAgain;
    
    			//Reset board if user wants to play again
    			if (playAgain == 'y')
    			{
    				gameover = false;
    				cell1 = '1';
    				cell2 = '2';
    				cell3 = '3';
    				cell4 = '4';
    				cell5 = '5';
    				cell6 = '6';
    				cell7 = '7';
    				cell8 = '8';
    				cell9 = '9';
    
    				player = '1';
    			}
    		}
    		
    		else
    		{
    			
    			//Alternate players
    			if (player == '1')
    			{
    				player = '2';
    			}
    			else
    			{
    				player = '1';
    			}
    		}
    		
    
    	}
    	while (gameover == false);                          
    	return 0;
    }

    Thanks in advance

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: Tic Tac Toe program

    I don't see a requirement to use arrays.....however, arrays will make this project much simpler.

    Rather than specifying 9 different cell variables, you can just specify the board using a single 2D array:
    Code:
    char cells[3][3];
    cells[0][0] = '1';
    cells[0][1] = '2';
    // etc
    Try to rewrite your printBoard() function using loops. You will of course need to pass in the board array as a parameter.

  3. #3
    Join Date
    Oct 2008
    Posts
    59

    Question Re: Tic Tac Toe program

    Quote Originally Posted by Lindley View Post
    I don't see a requirement to use arrays.....however, arrays will make this project much simpler.

    Rather than specifying 9 different cell variables, you can just specify the board using a single 2D array:
    Code:
    char cells[3][3];
    cells[0][0] = '1';
    cells[0][1] = '2';
    // etc
    Try to rewrite your printBoard() function using loops. You will of course need to pass in the board array as a parameter.
    It says that arrays are required in the 'Requires' part of the question. I cant see how arrays will make this simpler. From the sample code that you have posted, it would look almost similar to the original code without arrays. Could you elaborate on this?

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,316

    Re: Tic Tac Toe program

    It would be easier to loop over an array than to loop over a collection of separate variables.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Tic Tac Toe program

    Quote Originally Posted by fsdama View Post
    It says that arrays are required in the 'Requires' part of the question. I cant see how arrays will make this simpler.
    Look at this repeated code:
    Code:
    			if (move == '1' && cell1 == '1')
    			{
    				cell1 = mark;
    			}
    
    			else if (move == '2' && cell2 == '2')
    			{
    				cell2 = mark;
    			}
    
    			else if (move == '3' && cell3 == '3')
    			{
    				cell3 = mark;
    			}
    
    			else if (move == '4' && cell4 == '4')
    			{
    				cell4 = mark;
    			}
    	
    			else if (move == '5' && cell5 == '5')
    			{
    				cell5 = mark;
    			}
    
    			else if (move == '6' && cell6 == '6')
    			{
    				cell6 = mark;
    			}
    
    			else if (move == '7' && cell7 == '7')
    			{
    				cell7 = mark;
    			}
    
    			else if (move == '8' && cell8 == '8')
    			{
    				cell8 = mark;
    			}
    
    			else if (move == '9' && cell9 == '9')
    			{
    				cell9 = mark;
    			}
    What is the difference in all of those if-else statements? They are all the same thing except the number. What if it this were "super" tic-tac-toe board, say a 36x36 board? Would you write 36 if() statements to see which cell gets the mark? (I use 36 if you allow 0-9, A-Z for valid input moves).

    Here is an obvious place where an array and looping makes a difference.

    Example:
    Code:
    char cell[9];
    //...
    const char* allMoves = "123456789";
    //...
    cin >> move;
    bool cellMarked = false;
    for (int i = 0; i < 9; ++i )
    {
          if ( move == allMoves[i]  && cell[i] == allMoves[i] )
          {
               cell[i] = mark;
               cellMarked = true;
               break;
           }
    }
    
    if ( !cellMarked )
    {
       // invalid
    }
    That one piece of code above where cell is a 1D array is equivalent to that entire repeated code you wrote. Note the usage of a string that has "123456789", and all I'm doing is looping through the string, seeing if any of the items is equal to one of the characters.

    As a matter of fact, here is an even shorter version, without having to write a loop:
    Code:
    #include <string.h>
    //...
    cin >> move;
    const char* allMoves = "123456789";
    const char* pStr = strchr(allMoves, (int)move);
    if ( pStr )  // move has been found
       cell[pStr - allMoves] = mark;  // this marks the correct cell with the mark
    else
    {
       // invalid move
    }
    Anytime you see code that has a repeated pattern, it can be rewritten using loops, arrays, calling a function that does the looping internally, containers, or if its a difference in type, then a template class or function can be written.

    I didn't do the 2d version as Lindley suggested (which I would have suggested also) -- I took the code you did write to show how even a simple change to a 1d array makes the code much shorter.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; October 28th, 2011 at 12:23 AM.

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,591

    Talking Re: Tic Tac Toe program

    Quote Originally Posted by Paul McKenzie View Post
    What if it this were "super" tic-tac-toe board, say a 36x36 board? Would you write 36 if() statements to see which cell gets the mark?
    It even would be 1296 ifs...

    Aside from that, great post - of course.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  7. #7
    Join Date
    Aug 2009
    Posts
    439

    Re: Tic Tac Toe program

    Quote Originally Posted by Eri523 View Post
    It even would be 1296 ifs...

    Aside from that, great post - of course.
    Could we call that a "Super-If Block"? Officially?

  8. #8
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Tic Tac Toe program

    Quote Originally Posted by Eri523 View Post
    It even would be 1296 ifs...
    Yes, it's worse than what I posted.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center