Stumped with IF

Hi Guys, I'm new here and was wondering if anyone can help me on this one problem I'm having, or explain whats wrong so I can at least 'try' and sort it out.

I've made an attempt at a tic tac toe game, everything's going well with it apart from this one part (as far as I can see) it's to do with the computer picking a random number. It generates a number but it overwrites whats previously there when it shouldn't.

void AI(){
	int randomnumber;
	srand (time(NULL));
		randomnumber = rand() % 8;
		if((BoardNumbers[randomnumber] != 'X') || (BoardNumbers[randomnumber] != 'O' )){
			BoardNumbers[randomnumber] = 'X';
			Player = true;

If needed I can post the whole code. Many Thanks,

Your code will overwrite if one of the following two things is TRUE:

What's there already is not a X
What's there already is not a O

Can you think of ANY value for which both of those conditions will be false? No. At least one of them be true, no matter what value is there already.
Thanks for the reply Moschops, going from your:
What's there already is not a X
What's there already is not a O

There's already a character there going from 1-9, its in a char array which is set globally. I've tried taking the AI out and just playing as a player 1 vs player 2 thing and it works fine, granted it was using a switch statements though.

Last edited on
What Moschops was saying is that the || needs to be a &&. || means or so the code is executed if (BoardNumbers[randomnumber] != 'X') OR (BoardNumbers[randomnumber] != 'O' ) If there is already a value there, like X, then it's not equal to O so the code will execute and vice versa for if the value there is O. By changing it to and(&&) instead of or, the code will only execute when both (not either like with or) of the conditions are true. This way, if there's an X there then one of the conditions is false and the code won't execute. Same goes for if there's an O there
After putting that in the code works, sat back and pondered and realized what Moschops meant. Sorry for that, think I'll do a quick skim through operators again.

Topic archived. No new replies allowed.