Marcus Aseth was hinting at this with his first post, but your problem is your naming conventions of x, y, i, j getting you confused.
The easiest way to fix your problem would be to make your (wrong) notation be consistent, by changing it to
1 2 3 4
|
if(y == fruitX && x == fruitY){
fruitX = (rand() % HEIGHT-1)+1;
fruitY = (rand() % WIDTH-1)+1;
}
|
...but I would suggest you fix the bad notation of mixing up x and y, which is the source of the problem in the first place.
Also: notice
rand() % WIDTH-1 (and similar). The % has higher precedence than the minus operation. This is
might be giving you wrong result every once in a while, although you cancel the -1 with a +1, but it looks like it could be a bug. I believe your Fruit can appear out of bounds.
Aside: I reckon more people would be comfortable with using the arrow keys or WASD, not "WAZS"(?).
___________________________________
Edit: Furthermore, note that
1 2 3 4 5 6
|
fruitX = rand() % WIDTH-1;
fruitY = rand() % HEIGHT-1;
if(x == fruitX && y == fruitY){
fruitX = (rand() % WIDTH-1)+1;
fruitY = (rand() % HEIGHT-1)+1;
}
|
Does not guarentee that the fruit and the player do not start on the same tile, it just makes the chance 1/(WIDTH*WIDTH*HEIGHT*HEIGHT).