Random number generator

Hi guys so I know how to use rand() first you need to use the srand() to seed it then you can call rand() and you should get random numbers based on unix time as far as I know,

but in my program when my snake 'O' hovers over the piece of food 'F' nothing happens the 'F' still appears in the same place and F is not generated in a new random place,and worst yet sometimes very seldom it will generate by itself without the snake going over it

can anyone spot something wrong with my code?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  #include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <time.h>

using namespace std;

enum edir{

   STOP = 0,
   UP,
   DOWN,
   RIGHT,
   LEFT
};

bool gameOver;
const int WIDTH = 20;
const int HEIGHT = 20;
int fruitX,fruitY,x,y;
edir dir;


void setUp(){

  gameOver = false;
  dir = STOP;
  x = WIDTH/2;
  y = HEIGHT/2;
  srand(time(NULL));
  fruitX = rand() % WIDTH-1;
  fruitY = rand() % HEIGHT-1;
  if(x == fruitX && y == fruitY){
    fruitX = (rand() % WIDTH-1)+1;
    fruitY = (rand() % HEIGHT-1)+1;
  }
}

void draw(){

     system("cls");

      for(int i = 0; i < WIDTH+1; i++){

        cout << "#";
      }
      cout << endl;

      for(int i = 0; i < HEIGHT; i++){

        for(int j = 0; j < WIDTH; j++){

            if(j == 0){

                cout << "#";
            }
            if(j == WIDTH-1){
                cout << "#";
            }
            if(i == x && j == y){

                cout << "O";
            }
            else if(i == fruitY && j == fruitX){

                cout << "F";
            }
            else{

                cout << " ";
            }
        }
        cout << endl;
      }
      for(int i = 0; i < WIDTH+1; i++){

        cout << "#";
      }


   }

void input(){

   while(_kbhit()){

    switch(_getch()){

case 'w':
    dir = UP;
    break;
case 'z':
    dir = DOWN;
    break;
case 'a':
    dir = LEFT;
    break;
case 's':
    dir = RIGHT;
    break;

    }
   }
}
void logic(){

 switch(dir){

 case UP:
     x--;
     break;
 case DOWN:
     x++;
     break;
 case LEFT:
    y--;
    break;
 case RIGHT:
    y++;
    break;
 }

 if(x > WIDTH || x < 0 || y > HEIGHT-2 || y < 0) // check for in bounds
    gameOver = true;
  if(x == fruitX && y == fruitY){
        fruitX = (rand() % WIDTH-1)+1;
        fruitY = (rand() % HEIGHT-1)+1;
    }

}

int main()
{
   setUp();
   while(!gameOver){

   draw();
   input();
   logic();
   }
   if(gameOver){
    cout << endl;
    cout << endl;
    cout << "GAME OVER!!!!!!!!!!!!" << endl;
   }
}
Hi guys so I know how to use rand() first you need to use the srand() to seed it then you can call rand() and you should get random numbers based on unix time as far as I know,


If this is about how you should be using it, you shouldn't.

https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful

Question:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 switch(dir){

 case UP:
     x--;
     break;
 case DOWN:
     x++;
     break;
 case LEFT:
    y--;
    break;
 case RIGHT:
    y++;
    break;
}


Why is the x axis associated with up and down and the y axis with left and right?
Last edited on
hey guys

@Marcus yeah good point Marcus the Y axis should be up and down and left right should be the X axis but thats not my question,

my question is how come the piece of fruit does not disappear?
I even put some debug code and the problem doesn't seem to be with the random number it seems to be when 'O' my snake goes over the piece of food it doesn't detect it

1
2
3
4
5
6
7

 if(x == fruitX && y == fruitY){
        fruitX = (rand() % WIDTH-1)+1;
        fruitY = (rand() % HEIGHT-1)+1;
        cout << "HEY";
    }


hey never prints anywhere
I know that was not your question, the reason I point it out anyway is just in the off-chance that by fixing that, suddenly your problem disappears :)
Is worth a shot rather than investing the time upfront in following all the code logic
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).
Last edited on
thanks guys bad naming conventions and mixing the x and y axis did in the end come back to bite me on the behind,

and very true Ganado I also have that problem sometimes the 'F' will not appear on the map at all and at other times will appear just outside of the map

and true arrow keys would be much better but I'm not sure their key value using conio.h as the input controller

thanks
Topic archived. No new replies allowed.