Thread: Help with random numbers&regenerating them

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    31

    Help with random numbers&regenerating them

    Here's my code for a tic-tac-toe game. It's incomplete, but almost done.
    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    char playagain='y';
    char number;
    char array[10];
    char i='1';
    char compnumber;
    int main(void)
    {
    	array[1]='1';
    	array[2]='2';
    	array[3]='3';
    	array[4]='4';
    	array[5]='5';
    	array[6]='6';
    	array[7]='7';
    	array[8]='8';
    	array[9]='9';
    	srand(time(NULL));
    	compnumber=rand()%10;
    	while(playagain=='y')
    	{
    	i=1;
    	cout << array[1];
    	cout << "|";
    	cout << array[2];
    	cout << "|";
    	cout << array[3];
    	cout << "\n-----\n";
    	cout << array[4];
    	cout << "|";
    	cout << array[5];
    	cout << "|";
    	cout << array[6];
    	cout << "\n-----\n";
    	cout << array[7];
    	cout << "|";
    	cout << array[8];
    	cout << "|";
    	cout << array[9];
    	cout << "\n";
    	cout << "Please enter the number of the space you would like to place an X in: ";
    	cin >> number;
    	for(i=i-'0';i<10;i++) // Player number calculation
    	{
    		if(number==i+'0' && array[i]!=i+'0') //Space already taken
    		{
    			cout << "That space is taken!\n";
    		}
    		if(number==i+'0' && array[i]==i+'0') //Assigns X
    		{
    			array[i]='X';
    		}
    	}
    	if((array[1]=='X' && array[2]=='X') || (array[1]=='O' && array[2]=='O')) //Computer AI. Stops player win or finishes computer win.
    	{
    		compnumber='3';
    	}
    	if((array[1]=='X' && array[5]=='X') || (array[1]=='O' && array[5]=='O'))
    	{
    		compnumber='9';
    	}
    	if((array[1]=='X' && array[4]=='X') || (array[1]=='O' && array[4]=='O'))
    	{
    		compnumber='7';
    	}
    	if((array[2]=='X' && array[5]=='X') || (array[2]=='O' && array[5]=='O'))
    	{
    		compnumber='8';
    	}
    	if((array[3]=='X' && array[2]=='X') || (array[3]=='O' && array[2]=='O'))
    	{
    		compnumber='1';
    	}
    	if((array[3]=='X' && array[5]=='X') || (array[3]=='O' && array[5]=='O'))
    	{
    		compnumber='7';
    	}
    	if((array[3]=='X' && array[6]=='X') || (array[3]=='O' && array[6]=='O'))
    	{
    		compnumber='9';
    	}
    	if((array[4]=='X' && array[5]=='X') || (array[4]=='O' && array[5]=='O'))
    	{
    		compnumber='3';
    	}
    	else;
    	{
    	for(i=i-'0';i<10;i++) //Computer random number
    	{
    		while(compnumber==i+'0' && array[i]!=i+'0') //Space taken
    		{
    				srand(time(NULL));
    				compnumber=rand()%10;
    
    		}
    		if(compnumber==i+'0' && array[i]==i+'0') //Assigns O
    		{
    			array[i]='O';
    		}
    	}
    	}
    }
    return 0;
    }
    The computer doesn't seem to be taking a turn, unless the player has array[1]+array[2]=='X'. Can someone help me?

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    You never seed the random generator. Look up the srand() function in your documentation. Send a value like a system timer for a seed, like clock().

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    It worked before, exactly like that...
    Can you explain this a little more? I'm very new to C++.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    problem is that you don't have all the possibilites covered....for one, all your if statements require that 2 spots are filled, therefore, it'll only move after one of the combinations are found.....

    2nd, again, you dont have conditions for every possibility.....try 1 and 3, wont go....you dont have a condition for that....so first off, fix it so that it'll move when you have made only 1 human move, and then move onto the rest of the logic.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    31

    Re: Help with random numbers&regenerating them

    Originally posted by Kespoosh
    Here's my code for a tic-tac-toe game. It's incomplete, but almost done.
    I said it's incomplete....It checks to see if it can win or stop the player from winning first (not completed yet), and if it can't it uses a random number to determine which spot to put an O in, and if that spot's taken it uses a new random number until it finds a suitable spot. I know the "AI" isn't completed yet but it's the random number I'm having problems with.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    gotcha....i notice you have the srand inside of the while loop...bad idea....take that out, and declare the srand in the global area, or in the top of the main when your doing your local variables, and it should be fine..

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    Alright, I'll do that, thanks. Can you explain how srand(time(NULL)) works? It would help greatly in the future if I understood that.

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    ok well srand sets a seed in which the rand function uses....if you dont set a seed, you will get the same set of random numbers over and over again...i.e., run the progmra the first time, you'll get 3, 4, 5, 6 bla bla bla.....run it again, you'll get the same 3, 4, 5, 6 bla bla bla bla again...what srand(time(NULL)) does, is uses the current time of the machine, and sets that as the srand...obviously, everytime you run the program, it wont be the same time, so its a diff set of numbers each time

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    char playagain='y';
    char number;
    char array[10];
    char i='1';
    char compnumber;
    int main(void)
    {
    	array[1]='1';
    	array[2]='2';
    	array[3]='3';
    	array[4]='4';
    	array[5]='5';
    	array[6]='6';
    	array[7]='7';
    	array[8]='8';
    	array[9]='9';
    	srand(time(NULL));
    	compnumber=rand()%10;
    	while(playagain=='y')
    	{
    	i=1;
    	cout << array[1];
    	cout << "|";
    	cout << array[2];
    	cout << "|";
    	cout << array[3];
    	cout << "\n-----\n";
    	cout << array[4];
    	cout << "|";
    	cout << array[5];
    	cout << "|";
    	cout << array[6];
    	cout << "\n-----\n";
    	cout << array[7];
    	cout << "|";
    	cout << array[8];
    	cout << "|";
    	cout << array[9];
    	cout << "\n";
    	cout << "Please enter the number of the space you would like to place an X in: ";
    	cin >> number;
    	for(i=i-'0';i<10;i++) // Player number calculation
    	{
    		if(number==i+'0' && array[i]!=i+'0') //Space already taken
    		{
    			cout << "That space is taken!\n";
    		}
    		if(number==i+'0' && array[i]==i+'0') //Assigns X
    		{
    			array[i]='X';
    		}
    	}
    	if((array[1]=='X' && array[2]=='X') || (array[1]=='O' && array[2]=='O')) //Computer AI. Stops player win or finishes computer win.
    	{
    		compnumber='3';
    	}
    	if((array[1]=='X' && array[5]=='X') || (array[1]=='O' && array[5]=='O'))
    	{
    		compnumber='9';
    	}
    	if((array[1]=='X' && array[4]=='X') || (array[1]=='O' && array[4]=='O'))
    	{
    		compnumber='7';
    	}
    	if((array[2]=='X' && array[5]=='X') || (array[2]=='O' && array[5]=='O'))
    	{
    		compnumber='8';
    	}
    	if((array[3]=='X' && array[2]=='X') || (array[3]=='O' && array[2]=='O'))
    	{
    		compnumber='1';
    	}
    	if((array[3]=='X' && array[5]=='X') || (array[3]=='O' && array[5]=='O'))
    	{
    		compnumber='7';
    	}
    	if((array[3]=='X' && array[6]=='X') || (array[3]=='O' && array[6]=='O'))
    	{
    		compnumber='9';
    	}
    	if((array[4]=='X' && array[5]=='X') || (array[4]=='O' && array[5]=='O'))
    	{
    		compnumber='3';
    	}
    	else;
    	{
    	for(i=i-'0';i<10;i++) //Computer random number
    	{
    		while(compnumber==i+'0' && array[i]!=i+'0') //Space taken
    		{
    				compnumber=rand()%10;
    
    		}
    		if(compnumber==i+'0' && array[i]==i+'0') //Assigns O
    		{
    			array[i]='O';
    		}
    	}
    	}
    }
    return 0;
    }
    I did what you said, and it's still not working. I don't think it's getting past while(compnumber==i+'0' && array[i]!=i+'0'), because the spaces never become Os. Ah, I think I'm starting to understand the srand thing. What's "seed"?

  10. #10
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    I think I know what's wrong. For(i=i+'0';i<10;i++)...i++ tries to add (int)1 to (char)i, doesn't it?

  11. #11
    Prometheus1
    Guest

    Arrow

    The problem may be that stray semicolen.
    Works fine now.

    {
    compnumber='3';
    }
    else; <<< right here <<<
    {
    for(i=i-'0';i<10;i++) //Computer random number
    {

  12. #12
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    Uh...what?

  13. #13
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    there is a stray semi colon after that else, but for some reason, that provided no parse error, nor an error in logic.....

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Jamsan
    there is a stray semi colon after that else, but for some reason, that provided no parse error, nor an error in logic.....
    It wouldn't. It's valid code. It just doesn't do what you want.

    else;

    The above means to do nothing at all on the 'else' case. Following that, it will always execute the following code block, even if the original if check works.

    A ; by itself on a line is perfectly valid. It just does nothing. Since it was by itself and not enclosed in braces first, it just took that one single statement, and ended the else at that point.

    Since the else was ended, it then executed the { } block because it was the next thing in the flow of the program for it to execute.

    It is perfectly valid to have { } pairs in your code whenever you wish to create a new scope block. For example:
    Code:
    int main( void )
    {
        {
            {
                {
                }
            }
        }
        return 0;
    }
    This program does nothing, and is perfectly legal code. You could sprinkle random amounts of ; in there also, which would have no effect either.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM