I have some code...
Code:
void opponent( int arg[ ][ 3 ], int &row, int &col )
{
srand( time( NULL ) );
short int new_row;
short int new_col;
//get new numbers. They start off unitialised basically.
new_row = rand( ) % 2;
new_col = rand( ) % 2;
while( ( ( new_row == row ) && ( new_col == col ) ) || arg[new_row][new_col] != 9 )
{
//Then we haven't actually got a new column have we. >.> baka.
new_row = rand( ) % 2;
new_col = rand( ) % 2;
}
//On the other hand they should now be different.
//At this point it is different so... now we need to check the board if the position has been filled or not.
row = new_row;
col = new_col;
//Carry on while the value at the selected coordinates is 0 or 1.
}
Tada!
Now. This is for a Tic Tac Toe game... all I want it to do is pick some random numbers from 0 to 2, they're coordinates on a board. Now this DOES work fine for the first...mmm four to six turns. There's a win condition that's easy enough to achieve after three turns...but it starts getting flustered if you purposely avoid winning.
I don't really want to subtract positions away from the array because that'd be a bit pointless. Instead I've took to putting in the number 9 as a blank space or a 1 or 0 as a taken space. A space that is none of these would screw up the computer. So it has to be a 9, 1 or 0.
My array is a multidimensional array, 3x3 (tic tac toe who da thunk it). [0-2], [0-2]. So it loops through the array and picks numbers. Now it seems to be able to go for both extremes just fine and according to the documentation that is how rand is supposed to work... but maybe the documentation is...slightly ambiguous? That said... it should loop until it finds a block that's empty.
Well it does loop. FOREVER. Maybe my computer is just dumb ¬_¬ *evils* so I need to give it some assistance. Can anyone think up a way to jog it's memory that other squares are in fact available and to get it to shift?
Would asking for the srand() again be of any assistance do you think? Thank you for your time!
edit
As a note, this 'used' to be a function that called itself...but my computer really doesn't like segfaults >.>