Basic C++ Coding Help please! (Tic-Tac-Toe game)

Discussion in 'Computing, Science, and Technology' started by mclaurin10, Sep 7, 2009.

  1. mclaurin10

    mclaurin10 New Member

    Joined:
    Mar 18, 2008
    Messages:
    554
    Likes Received:
    11
    Device:
    iPhone 3G (Black)
    Hey everyone,
    I have a big project I have to finish by wednesday, its a tic tac toe game designed in C++. Ive written a ton of the code, but its not working, it compiles but I get no end of problems after that.

    I realize no one has to help me, but I dont know what to do and I really dont want to fail, so please look it over and see if you can tell me what is going on


    What happens is that player 1 goes, then player two goes and the game resets.

    Oh yea, and instead of X and O i get smiley faces for both players (wtf?!)


    THANK YOU SO MUCH

    Code:
    
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	char board[3][3] = { {' ', ' ', ' '},
    						{' ', ' ', ' '},
    						{' ', ' ', ' '} };
    	int choose;
    	int choose2;
    	char player1;
    	char player2;
    	char win;
    	int any1;
    	int any2;
    	char again;
    	int playerturn;
    	char playermark;
    
    	playerturn = 1;
    
    
    
    	again = 'y';
    
    	any1 = 1;
    	any2 = 2;
    
    	int X;
    
    	X = 1;
    
    	if (playerturn == 1)
    	{	
    		playermark = 'X';
    	}
    
    	if (playermark == 2)
    	{
    		playermark = 'y';
    	}
    
    
    	win = 'n';
    
    	player1 = 'X';
    	player2 = 'y';
    
    
    
    
    	do 
    	{
    		cout << "This is the tic tac toe board as of now.\n\n";
    
    		cout << "  1  |  2  |  3   \n";
    		cout << "  -------------    \n";
    		cout << "  4  |  5  |  6     \n";
    		cout << "  -------------    \n";
    		cout << "  7  |  8  |  9    \n\n";
    
    		cout << "After the game starts the letter l will mark an untaken space.\n\n";
    
    		cout << "Now you need to decide who goes first, once you do that the person goig first should enter the number on the board aove that corresponds to the portion of teh board they want to place their marker in.\n\n";
    
    		cout << "Now please player 1 you will be X and player 2 will be o.\n\n"; 
    
    		playermark = 1;
    
    
    	if (win != 'w')
    	{
    
    
    	cout << "Player 1, please enter your choice.";
    	cin >> choose;
    
    	cout << endl << endl;
    
    	
    	
    		if (choose == 1)
    		{			
    			board[0][0] = playermark;
    		}
    
    		if (choose == 2)
    		{
    			board[0][1] = playermark;	
    		}
    
    		if (choose == 3)
    		{
    			board[0][2] = playermark;	
    		}
    
    		if (choose == 4)
    		{
    			board[1][0] = playermark;
    		}
    
    		if (choose == 5)
    		{
    			board[1][1] = playermark;	
    		}
    
    		if (choose == 6)
    		{
    			board[1][2] = playermark;
    		}
    
    			if (choose == 7)
    		{
    			board[2][0] = playermark;
    		}
    
    		if (choose == 8)
    		{
    			board[2][1] = playermark;
    		}
    
    		if (choose == 9)
    		{
    			board[2][2] = playermark;
    		}
    
    		
    			cout << "   " <<  board[0][0] << "  |  " << board[0][1] << "  |  " << board[0][2] << endl;
    			cout << "  --------------   \n";
    			cout << "   " <<  board[1][0] << "  |  " << board[1][1] << "  |  " << board[1][2] << endl;
    			cout << "  --------------   \n";
    			cout << "   " << board[2][0] << "  |  " << board[2][1] << "  |  " << board[2][2] << endl;
    		
    	
    
    		
    	} 
    
    
    	if (board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X' || board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X' || board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X' || board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X' || board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X' || board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X' || board[1][0] == 'X' && board[1][1] == 'X' && board [1][2] == 'X' || board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')
    	{
    		cout << "Congradulations player 1, you have won!\n\n";;
    		win = 'w';
    	}
    
    	if (board[0][0] == 'y' && board[1][0] == 'y' && board[2][0] == 'y' || board[0][1] == 'y' && board[1][1] == 'y' && board[2][1] == 'y' || board[0][2] == 'y' && board[1][2] == 'y' && board[2][2] == 'y' || board[0][0] == 'y' && board[1][1] == 'y' && board[2][2] == 'y' || board[0][2] == 'y' && board[1][1] == 'y' && board[2][0] == 'y' || board[0][0] == 'y' && board[0][1] == 'y' && board[0][2] == 'y' || board[1][0] == 'y' && board[1][1] == 'y' && board [1][2] != 'y' || board[2][0] == 'y' && board[2][1] == 'y' && board[2][2] == 'y')
    	{
    		cout << "Congradulations player 1, you have won!\n\n";
    		win = 'w';;
    	}
    
    	playerturn = 2;
    
    	if (win != 'w')
    	{
    
    
    		cout << "Ok, now, player two, its your turn.\n";
    		cin >> choose2;
    		cout << endl << endl;
    
    
    			if (choose2 == 1)
    			{
    				board[0][0] = playermark;
    			}
    
    			if (choose2 == 2)
    			{
    				board[0][1] = playermark;
    			}
    
    			if (choose2 == 3)
    			{
    				board[0][2] = playermark;
    			}
    
    			if (choose2 == 4)
    			{
    				board[1][0] = playermark;
    			}
    
    			if (choose2 == 5)
    			{
    				board[1][1] = playermark;	
    			}
    
    			if (choose2 == 6)
    			{
    				board[1][2] = playermark;	
    			}
    
    			if (choose2 == 7)
    			{
    				board[2][0] = playermark;		
    			}
    
    			if (choose2 == 8)
    			{
    				board[2][1] = playermark;
    			}
    
    
    	
    		
    		if (board[0][0] == 'y' && board[1][0] == 'y' && board[2][0] == 'y' || board[0][1] == 'y' && board[1][1] == 'y' && board[2][1] == 'y' || board[0][2] == 'y' && board[1][2] == 'y' && board[2][2] == 'y' || board[0][0] == 'y' && board[1][1] == 'y' && board[2][2] == 'y' || board[0][2] == 'y' && board[1][1] == 'y' && board[2][0] == 'y' || board[0][0] == 'y' && board[0][1] == 'y' && board[0][2] == 'y' || board[1][0] == 'y' && board[1][1] == 'y' && board [1][2] != 'y' || board[2][0] == 'y' && board[2][1] == 'y' && board[2][2] == 'y')
    		{
    			win = 'w';
    		}
    
    		if (board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X' || board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X' || board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X' || board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X' || board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X' || board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X' || board[1][0] == 'X' && board[1][1] == 'X' && board [1][2] == 'X' || board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')
    		{
    			win = 'w';
    		}
    
    
    
    
    
    		 	cout << "   " <<  board[0][0] << "  |  " << board[0][1] << "  |  " << board[0][2] << endl;
    			cout << "  --------------   \n";
    			cout << "   " <<  board[1][0] << "  |  " << board[1][1] << "  |  " << board[1][2] << endl;
    			cout << "  --------------   \n";
    			cout << "   " << board[2][0] << "  |  " << board[2][1] << "  |  " << board[2][2] << endl;
    
    	playermark = 2;
    
    	
     
    	} // player kiio
    
    
    		
    		 
    	if (board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X' || board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X' || board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X' || board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X' || board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X' || board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X' || board[1][0] == 'X' && board[1][1] == 'X' && board [1][2] == 'X' || board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')
    	{
    		cout << "Congradulations player 1, you have won!\n\n";
    
    				cout << "Do you want to play again (y/n)\n\n";
    		cin >> again;
    
    		if (again =='y')
    		{
    			win = 'n';
    
    			board[0][0] = ' ';
    			board[0][1] = ' ';
    			board[0][2] = ' ';
    			board[1][0] = ' ';
    			board[1][1] = ' ';
    			board[1][2] = ' ';
    			board[2][0] = ' ';
    			board[2][1] = ' ';
    			board[2][2] = ' ';
    
    		}
    
    	}
    
    	if (board[0][0] == 'y' && board[1][0] == 'y' && board[2][0] == 'y' || board[0][1] == 'y' && board[1][1] == 'y' && board[2][1] == 'y' || board[0][2] == 'y' && board[1][2] == 'y' && board[2][2] == 'y' || board[0][0] == 'y' && board[1][1] == 'y' && board[2][2] == 'y' || board[0][2] == 'y' && board[1][1] == 'y' && board[2][0] == 'y' || board[0][0] == 'y' && board[0][1] == 'y' && board[0][2] == 'y' || board[1][0] == 'y' && board[1][1] == 'y' && board [1][2] != 'y' || board[2][0] == 'y' && board[2][1] == 'y' && board[2][2] == 'y')
    	{
    		cout << "Congradulations player 2, you have won!\n\n";
    		
    			cout << "Do you want to play again (y/n)\n\n";
    		cin >> again;
    
    		if (again =='y')
    		{
    			win = 'n';
    
    			board[0][0] = ' ';
    			board[0][1] = ' ';
    			board[0][2] = ' ';
    			board[1][0] = ' ';
    			board[1][1] = ' ';
    			board[1][2] = ' ';
    			board[2][0] = ' ';
    			board[2][1] = ' ';
    			board[2][2] = ' ';
    
    		}
    
    	}
    
    
    
    	
    
    
    	}while (win != 'y');
    		
    	
    
    
    	return 0;
    }
    
    
    
    ------------------double post merged------------------
    shameless self bump, pleasseeee someone help me!

Share This Page