I have my whole program and its works fine in human v. human. however i am having trouble with human v. computer. I looked it up online but i can figure out how to do this, as the game goes on the player and the computer(picking a random space every turn) start putting pieces on the board. So basically say spots 3,6,8,9 are all taken- if i am using rand() to determine of comptuters placement of pieces how can i take out those 4 numbers(out of the rand()) so they put in a correct space? If only my own turn if i put in one of them it would day invalid entry- but how can i do it so the computer doesnt pick a space already occupied?
there is no way to stop rand() from returning some specific value. There are several ways I can think of.
1. the most simple one: repeat randomization until a free spot is found.
2. a little smarter one: generate a number from 0 to 'number of free spots', and then find the one you chose.
1 2 3 4 5 6 7 8 9 10 11 12 13
staticint num_spots = 9;//assuming computer starts first..
int spot = rand()%num_spots;
for(int i = 0, j = 0; i < 9; i++){
if(j == spot){
board[i] = 'X';
break;
}
if(board[i] == ' ') j++;
}
num_spots-=2;//it would be better not to use a static variable,
//but to recalculate num_spots every time. I'm just lazy.