-
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++.
-
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;
}
/***************************/
-
> 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
-
Thanks, the problem was the rand() % 2. I was thinking that that meant 0, 1, or 2.