-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.c
More file actions
61 lines (52 loc) · 1.35 KB
/
setup.c
File metadata and controls
61 lines (52 loc) · 1.35 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
#include <stdio.h>
#include <stdlib.h>
#include "setup.h"
#include "box.h"
#include "printer.h"
#include "mapStruct.h"
#include "carStruct.h"
#include "playerStruct.h"
#include "movement.h"
/* NAME: setup
* PURPOSE: setups the map and calls other methods then free after finished
* IMPORTS: fileName
* EXPORTS: NONE
* ASSERTIONS: validator function returned 0
*/
void setup(char fileName[])
{
int i;
Car* car =(Car*) malloc(sizeof(Car));
Player* player = (Player*) malloc(sizeof(Player));
Map* myMap = (Map*) malloc (sizeof (Map));
FILE * f = fopen(fileName,"r");
if(f == NULL)
{
perror("Error: could not open file\n");
}
else
{
if(fscanf(f, "%d %d",&myMap->row,&myMap->col)==2){
myMap->map = (int**) malloc (( myMap->row + 2 ) * sizeof (int *) );
for(i = 0; i < ( myMap->row + 2); i++){
myMap->map[i]=malloc((myMap->col + 2) * sizeof(int));
}
}
}
/*call methods */
box(myMap,f,car,player); /*init creation of playspace*/
printer(myMap,car,player); /*init print to screen*/
movement(myMap,car,player);
/* game finished now
* free everything
*/
for(i = 0; i < myMap->row + 2; i++)
{
free(myMap->map[i]);
}
free(myMap->map);
free(myMap);
free(car);
free(player);
fclose(f);
}