srand is normally only called once in a program, unless you want to reset it to a known state to get the same set of values again. Its not the problem, just not really right either. Also consider c++ <random> instead.
Is input being recursive intentional? This could be a problem to debug and could crash if you enter bad data enough times, but as coded I think its ok.
opponent's recursion is more dangerous. It could blow the call stack easily if the board were nearly full and rand were uncooperative. Do you see a pattern where it crashes on the AI's turn when the board is very nearly full?
finally, please use code tags <> in the editor on the side bar for large code blocks.
I can include <vector> and still do int* ip = new int[1000] too. The header is irrelevant.
You did not use <random> tools, you used C tools. Look at the fine examples in this site's reference area on random. But this is just so you can learn something, the C code still works so fix this issue last once it all works.
The method is flawed. Do not use recursion for this.
use a loop.
something like
do
get input
check it
if it is not used, update board.
while(input is a used square)
would be better. Recursion is a tricky subject and while what you have is technically correct it can get into trouble. Recursion calls the function *again*, creating a new copy of local variables and more junk on the stack, and if you roll, its used, push stack, roll, its used, push stack, roll, its used, push stack.... after a while that stack runs dry and it blows up. You only want to use 'controlled recursion' where the code is known to be turned to a loop by the compiler OR you control the iterations so it does not blow out.
there is a work-around. you can put a loop around your random number getter and fix it:
bool tbl[9] = {false};
do
loc = rand()%9;
while tbl[loc] == false
tbl[loc] = true;
...
this keeps you from calling the recursion every time -- the most it can be called now is 8 times, which should be safe.
you need to be careful with that idea too, as tbl needs to be passed into the function, or it will just make new ones every iteration :)
The workaround is just that -- an ugly band-aid. I recommend not doing this but it can be made to work. In general, in the wild.. just about anything can be made to work, even total train wrecks, but you should avoid this unless no other option.
This is actually a very interesting situation you have created. You can learn a lot from it, if you follow what I tried to explain.
You have a lot of code that is nearly identical. That's always a good sign that it can be simplified. For example, is a version of input() that is functionally the same as yours:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void
input()
{
cout << "Please select where you want to go " << endl;
int choice;
cin >> choice;
int r = (choice-1) / 3; // row
int c = (choice-1) % 3; // column
char ch = choice + '0'; // '1', '2', '3', etc.
if (grid[r][c] == ch) {
grid[r][c] = player;
} else {
cout << "Grid is already in use" << endl;
input();
}
}