-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameLib.c
More file actions
107 lines (92 loc) · 2.61 KB
/
gameLib.c
File metadata and controls
107 lines (92 loc) · 2.61 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "gameLib.h"
#define MAX_USERS 8
int randNumb(){
return (rand()%9+16);
}
int calculateMaxNumber(int rows, int cols, int element){
int minNum = min(rows, cols);
return ((element*minNum)/16);
}
void createMap(struct mapObjects* info,int rows,int cols,struct cell **map){
int i;
int r,c,index;
int idWarehouse=1,idObstacle=0;
struct warehouse * magazzino;
struct items *oggetto;
struct obstacles *ostacolo;
char items[3]={'$','@','s'};
printf("Choosing number of warehouses...\n");
info->n_warehouses=rand()%(calculateMaxNumber(rows, cols, 4))+1;
printf("Number of Warehouses: %d\n",info->n_warehouses);
printf("Choosing number of obstacles...\n");
info->n_obstacles= (rand()%(calculateMaxNumber(rows, cols, min(rows,cols))))+8;
printf("Number of Obstacles: %d\n", info->n_obstacles);
printf("Choosing number of items...\n");
info->n_items=rand()%(calculateMaxNumber(rows, cols, 5))+12;
printf("Number of items: %d\n",info->n_items);
printf("Generating map...\n");
i=0;
while(i<info->n_warehouses){
r=rand()%rows;
c=rand()%cols;
if(map[r][c].isWareHouse==0){
magazzino=(struct warehouse *)malloc(sizeof(struct warehouse));
map[r][c].isWareHouse=1;
i++;
map[r][c].object=idWarehouse+'0';//cappadavide
magazzino->id=idWarehouse++;
map[r][c].pointer=(void *)magazzino;
}
}
printf("warehouses done\n");
i=0;
while(i<info->n_obstacles){
r=rand()%rows;
c=rand()%cols;
if(map[r][c].isWareHouse==0&&map[r][c].isObstacle==0){
ostacolo=(struct obstacles *)malloc(sizeof(struct obstacles));
map[r][c].isObstacle=1;
i++;
map[r][c].object = 'x';
ostacolo->id=idObstacle++;
map[r][c].pointer=(void *)ostacolo;
}
}
printf("obstacles done\n");
i=0;
while(i<info->n_items){
r=rand()%rows;
c=rand()%cols;
idWarehouse=rand()%info->n_warehouses+1;
index=rand()%3;
if(map[r][c].isWareHouse==0&&map[r][c].isObstacle==0&&map[r][c].object==' '){
oggetto=(struct items *)malloc(sizeof(struct items));
map[r][c].object=items[index];
i++;
oggetto->warehouse=idWarehouse;
oggetto->object=items[index];
map[r][c].pointer=(void *)oggetto;
}
}
printf("items done\n");
}
void printMatrix(int rows, int cols, struct cell **map){
int i, j;
for(i = 0; i < rows; i++){
for(j = 0; j < cols; j++){
printf("%c ", map[i][j].object);
}
printf("\n");
}
}
int min(int rows,int cols){
if(rows<=cols)
return rows;
else
return cols;
}