-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomBox.c
47 lines (39 loc) · 1.41 KB
/
randomBox.c
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
/*
* PURPOSE : Implement of random generate box location inside the map
* DATE : 14/4/23
* DATE MOD : 17/4/23
*/
#include <stdlib.h>
#include <time.h>
#include "macro.h"
#include "randomBox.h"
/*call this function just ONCE at the beginning of your program before using the random number generator */
void initRandom()
{
srand(time(NULL));
}
/* Will return random integer between *low* and *high* inclusive.
If the low is larger than the high, it will return -1 instead.
In theory, it still works with negative *low* and *high* (as long as low is not larger than high), but for this assignment you will most likely not need the negative number.
Please ensure you call initRandom function once before starting using this function. */
int randomGenerator(int low, int high)
{
int number = -1;
if(low <= high)
{
number = (rand() % (high-low+1)) + low;
}
return number;
}
/* Function that generate box row and box column */
void boxLocation(int *mRow, int *mCol, int *pRow, int *pCol, int *gRow, int *gCol, int *bRow, int *bCol )
{
initRandom();
/* do-while loop is used to generate right box row and column which not the same as player and goal position */
do
{
*bRow = randomGenerator(3, *mRow - 3);
*bCol = randomGenerator(3, *mCol - 3);
}
while ( ISEQUAL(*bRow, *bCol, *pRow, *pCol) == TRUE || ISEQUAL(*bRow, *bCol, *gRow, *gCol) == TRUE );
}