Thread: wierd random problem

  1. #1
    wierd guy bart's Avatar
    Join Date
    Aug 2001
    Posts
    87

    Question wierd random problem

    Im busy writing my tic tac toe program, and this is what i have:

    Code:
    #include <stdlib.h>//rand
    #include <iostream.h>// cout
    #include <time.h>//time
    #include <stdio.h>//getchar
    /*
    player is O, AI is X
    status [row or x-coordinate][collum or y-coordinate]
    */
    char status[3][3]={' ',' ',' ',' ',' ',' ',' ',' ',' '};  // the stat is decided with this, and can be O,X, or ' '
    int printer();// show the status
    int AI(); //the AI
    int main()
    {
     AI();  //run the AI
     AI();
     AI();
     AI();
     return 0;
    }
    /***********************/
    int AI() // get random numbers for the AI
    {
     srand(time(NULL));
     int row, collum;
     row = rand() % 2;
     srand(time(NULL));
     collum = rand() % 2;
     if (status[row][collum]==' ')
     {
      status[row][collum]='X'; // set new stat
      printer();
     }
     else
     {
      AI();
     }
    }
    /************************/
    int printer()//i dont think there's a problem here
    {
     cout<<"\n+------+\n|"<<status[0][0]<<"|"<<status[0][1]
     <<"|"<<status[0][2]<<"|\n+------+\n|"<<status[1][0]
     <<"|"<<status[1][1]<<"|"<<status[1][2]<<"|\n+------+\n|"<<status[2][0]
     <<"|"<<status[2][1]<<"|"<<status[2][2]<<"|\n+------+\n";
     getchar();
    }
    /***************************/
    the problem is that it always fills in this
    Code:
    +------+
    |X|X| |
    +------+
    |X|X| |
    +------+
    | | | |
    +------+
    could someone help me? I use Dev-C++.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    17

    Talking

    I have made a couple of changes to your code, as seen below. I can't seem to produce the problem you are having, but give this code a go and see if it now works.

    What have I done to your code?

    1. removed the char status[][] from a global var. to a local var. inside of main, (a lot safer this way),
    2. you could use const. int values for the size of the grid, makes it easy to change later (you can change the way it init. and prints the status char array to use loops, I have left that one for you to play with),
    3. You only need to use srand() once, as this is the seed for the rand() function (may have caused your probs??)
    4. Change your functions AI() and print() to void type, as int requires a int. value to be returned.
    5. Placed in a return; in the the print() & AI() functions (as all functions, even void should have a return), and
    6. I pass the char status array to the print function (using const to protect the original data).

    Best of luck.


    Code:
    #include <stdlib.h>//rand
    #include <iostream.h>// cout
    #include <time.h>//time
    #include <stdio.h>//getchar
    /*
    player is O, AI is X
    status [row or x-coordinate][collum or y-coordinate]
    */
    
    const int No_Rows = 3;
    const int No_Cols = 3;
    
    void printer(const char[][No_Rows]);// show the status
    void AI(); //the AI
    
    int main()
    {
    	AI();  //run the AI
    	AI();
    	AI();
    	AI();
    	return 0;
    }
    /***********************/
    void AI() // get random numbers for the AI
    {
    	char status[No_Rows][No_Cols]={' ',' ',' ',' ',' ',' ',' ',' ',' '};  // the stat is decided with this, and can be O,X, or ' '
    	int row, collum;
    
    	srand(time(NULL));
    	row = rand() % 2;
    	collum = rand() % 2;
    
    	if (status[row][collum]==' ')
    	{
    		status[row][collum]='X'; // set new stat
    		printer(status);
    	}
    	else
    	{
    		AI();
    	}
    	return;
    }
    /************************/
    void printer(const char status[][No_Rows])//i dont think there's a problem here
    {
    	cout<<"\n+------+\n|"<<status[0][0]<<"|"<<status[0][1]
    	<<"|"<<status[0][2]<<"|\n+------+\n|"<<status[1][0]
    	<<"|"<<status[1][1]<<"|"<<status[1][2]<<"|\n+------+\n|"<<status[2][0]
    	<<"|"<<status[2][1]<<"|"<<status[2][2]<<"|\n+------+\n";
    	getchar();
    	return;
    }
    /***************************/
    -----------------------------------------------
    everready

    To code, or not to code, that is the question.

    Well the answer is 'TO CODE' of cause

  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
    > srand(time(NULL));
    You should only call this ONCE at the start of main.
    If you call it before each call to rand(), all you do is reset the seed, and hence you will get the same value back from rand(). Remember, time() is a constant in a short lived program.

    > row = rand() % 2;
    Should be row = rand() % 3; if you want all possible rows.

    > AI();
    Unchecked recursive calls are a bad idea

  4. #4
    wierd guy bart's Avatar
    Join Date
    Aug 2001
    Posts
    87
    Thanks, the problem was the rand() % 2. I was thinking that that meant 0, 1, or 2.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wierd Malloc Problem
    By mohankarthik in forum C Programming
    Replies: 11
    Last Post: 09-17-2008, 02:14 PM
  2. problem with boost random func (again)
    By l2u in forum C++ Programming
    Replies: 5
    Last Post: 11-06-2006, 03:22 AM
  3. Random Numbers...Problem With FAQ Answer
    By sitestem in forum C++ Programming
    Replies: 12
    Last Post: 04-14-2004, 09:22 AM
  4. Random things are fine, just one problem with them.
    By DarkSFK in forum C++ Programming
    Replies: 14
    Last Post: 08-19-2002, 08:40 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM