Thread: need help with source

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    73

    Question need help with source

    Ok im having trouble with my Tic-Tac-Toe program. I am currently working on the part where the computer reads what move the user did and then moves according to the rules. Now I havn't wrote all of the computers rules yet because I dont know if this is how to do it. When I try to run the program it messes up. Here is the source. I hope Yall can find the problem.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    
    using namespace std;
    
    int x=1;
    int o=2;
    void startgame();
    void xturn();
    void oturn();
    
    int moves[9] = {1,2,3,4,5,6,7,8,9};
    
    
    	int main()
    	{
    
    		int xx;
    
    		cout << "Would you like to play a game of Tic-Tac-Toe?" << endl;
    		cout << "1 for yes, and 2 for no" << endl;
    
    		cin >> xx;
    
    		 if (xx == 1)
    		     startgame();
    		 else
    			 if ( xx == 2)
    			  cout << "Looseer!!!!" << endl;
    
    			 return 0;
    
    	}
    
    
    	void startgame()
    	{
    
    		int win = 0;
    		int count = 0;
    		int victory = 0;
    		int mover;
    
    		cout << "Lets play." <<  endl;
    
    		cout << "You are X. Press 1-9 to make a move." << endl;
    
    
    		for ( count=1; count<=9; count++)
    		{
    			moves[9] = count;
    
    			// This part of the code is used to deturmine whos move it is.
    			if (moves[1] != false)
    				xturn();
    			if (moves[2] != false)
    				oturn();
    			if (moves[3] != false)
    				xturn();
    			if (moves[4] != false)
    				oturn();
    			if (moves[5] != false)
    				xturn();
    			if (moves[6] != false)
    				oturn();
    			if (moves[7] != false)
    				xturn();
    			if (moves[8] != false)
    				oturn();
    			if (moves[9] != false)
    				xturn();
    
    			if (count==9)
    			{
    				if (!victory)
    					cout << "DRAW!!" << endl;
    				else
    					if (victory != false)
    						cout << mover << " WINS!!" << endl;
    			}
    		}
    
    	}
    
    	void xturn()
    	{
    		cin >> x;
    
    		cout << "You moved to square " << x;
    		cout << endl;
    	}
    
    	void oturn()
    	{
    		
    		if (x==1)
    		{
    			o==5;
    		}
    		else
    			if (x==5)
    				o==1;
    
    		cout << "The computer moved to square " << o;
    		cout << endl;
    	}

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Remember that arrays start indexing at 0, not 1.

    Code:
    int moves[9] = {1,2,3,4,5,6,7,8,9}; // given this
    moves[9] = count; // this 'undefined' behavior.. read, BAD
    The first move is at 0, the last is at 8.

    This

    Code:
    for ( count=1; count<=9; count++)
    // should be
    for (count = 0; count < 9; count++)
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    Yes i know that arrays start at 0 but when I plug in 0 then the for loop goes 10 times and there is only a maximum of 9 moves in tic tac toe. I guess I can change that 9 to an 8. But do u think the code i used to check to see whos turn it is will work? If not any suggestions?

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    I'd do it like this, personally.

    Code:
    int main() {
    // init
         bool xturn = true;
         bool gameOver=false;
         while (!gameOver) {
             if (xturn)  x_turn();
             else        o_turn();
             xturn = !xturn; // flip xturn
             // check for gameOver
        }
    // finish 
    }
    The turn is
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    Well dud there is only one problem with that code. The while loop will always continue. I would fix it to where gameover checks to see if any one won or if there was a draw. But I do no know how to do this. Can I do it like this?

    Code:
    bool gameOver=false;
    
    //then later on down
    
    bool gameOver
    {
       for (count=0; count <=8; count++)
       {
         if (moves[x] == 1 && moves[x] == 2 && moves[x] == 3)
            win;
        }
    }
    Will something like that work? If not how can I do this?

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    Ok I figured out that last question on my own. But now I have anouther. Ok I wrote some code that will check if the square is taken; like so

    Code:
    void square(int player)
    	{
    
    		if (player==1)
    			cout << "Cannot move there Square taken!" << endl;
    		else
    		if (player==2)
    			cout << "Cannot move there Square taken!" << endl;
    		else
    		if (player==3)
    			cout << "Cannot move there Square taken!" << endl;
    		else
    		if (player==4)
    			cout << "Cannot move there Square taken!" << endl;
    		else
    		if (player==5)
    			cout << "Cannot move there Square taken!" << endl;
    		else
    		if (player==6)
    			cout << "Cannot move there Square taken!" << endl;
    		else
    		if (player==7)
    			cout << "Cannot move there Square taken!" << endl;
    		else
    		if (player==8)
    			cout << "Cannot move there Square taken!" << endl;
    		else
    		if (player==9)
    			cout << "Cannot move there Square taken!" << endl;
    	}
    Basically it checks every square to see if the player took it or not. But I am unable to install it. Where do yall think should I put it? My source is above. Thanx

  7. #7
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    Code:
    if (xx == 1)
    startgame();
    else
    if ( xx == 2)
     cout << "Looseer!!!!" << endl;
    return 0;
    I dont think this will work as intended. Use braces when you have more than one line to execute in "if" statements. It increases readability, and reduces errors
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    well fry u see that part of the game doesnt crash. What im trying to do is figure out how to make the gameOver variable work in such a way that it detects who won and detects if the moves have exceded 9. Iv got the game to end after 9 moves. That was easy. I know how to check to see if there is a winner but how can i use it in a while(!gameOver) loop? Can I juz plug it in there or what? Here is my code.

    Code:
    while (!gameOver)
    			{
    
    				count++;
    
    				  if(count==9)
    				  {
    					gameOver=true;
    				  }
    
    				 if (xturn)  
    					 x_turn();
    				 else        
    					 o_turn();
    				 xturn = !xturn; // flip xturn
    				 // check for gameOver
    			}
    	}

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    O and do you guys think that this code will work (used in the right place) to detect weather someone has moved there?

    Code:
    void square(int player)
    	{
    
    		if (player==1)
    			cout << "Cannot move there Square taken!" << endl;
    		else
    		if (player==2)
    			cout << "Cannot move there Square taken!" << endl;
    		else
    		if (player==3)
    			cout << "Cannot move there Square taken!" << endl;
    		else
    		if (player==4)
    			cout << "Cannot move there Square taken!" << endl;
    		else
    		if (player==5)
    			cout << "Cannot move there Square taken!" << endl;
    		else
    		if (player==6)
    			cout << "Cannot move there Square taken!" << endl;
    		else
    		if (player==7)
    			cout << "Cannot move there Square taken!" << endl;
    		else
    		if (player==8)
    			cout << "Cannot move there Square taken!" << endl;
    		else
    		if (player==9)
    			cout << "Cannot move there Square taken!" << endl;
    	}
    I think it should work. If not any suggestions?

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    Hey guys. Im trying somthing new. Im trying to take an array called board and use that as the tic tac toe board. And then im using an array called moves for the x's and o's. Now when I compile this code and run it. All I get back from the computer is 0. So one of my questions is did I set up the board right? Moves[x]==board[x] does that tell the board that the square inputted by x is taken? If not how can I do it?

    If my first question is answered then I can probley figure out my second one.

    Code:
    void x_turn()
    	{
    
    		moves[0,2,4,6,8]=x;
    
    		cin >> x;
    
    		moves[x]==board[x];
    
    		cout << "You moved to square " << x;
    		cout << endl;
    
    		if (moves[x] > 9)
    		{
    			cout << "You may not move there"; 
    		    gameOver=true;
    		    cout << endl;
    			cout << "GAME OVER!" << endl;
    		}
    	}
    
    	void o_turn()
    	{
    
    		moves[1,3,5,7]=o;
    
    		int count=1;
    
    				if (board[x]==5)
    					moves[o]=1;
    				else
    					if (board[x]==1)
    						moves[o]=5;	
    
    		//if (x == corner)
    		//	o=2;
    
    
    		cout << o;
    		cout << endl;
    	}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug into Source
    By lehe in forum C++ Programming
    Replies: 4
    Last Post: 02-18-2009, 10:45 AM
  2. Open Source and Security
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 06-17-2008, 01:23 AM
  3. Open Source Licenses
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 10-10-2006, 08:53 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM