-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
135 lines (116 loc) · 5.09 KB
/
main.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
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
#include "stdio.h"
#include "stdlib.h"
#include "sdl.h"
#include "SDL2_gfxPrimitives.h"
#include "time.h"
#include "formulas.h"
#include "wall.h"
#include "robot.h"
int done = 0;
int main(int argc, char *argv[]) {
SDL_Window *window;
SDL_Renderer *renderer;
if(SDL_Init(SDL_INIT_VIDEO) < 0){
return 1;
}
window = SDL_CreateWindow("Robot Maze", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, OVERALL_WINDOW_WIDTH, OVERALL_WINDOW_HEIGHT, SDL_WINDOW_OPENGL);
window = SDL_CreateWindow("Robot Maze", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, OVERALL_WINDOW_WIDTH, OVERALL_WINDOW_HEIGHT, SDL_WINDOW_OPENGL);
renderer = SDL_CreateRenderer(window, -1, 0);
struct Robot robot;
struct Wall_collection *head = NULL;
int front_left_sensor, front_sensor, front_right_sensor=0;
clock_t start_time, end_time;
int msec;
// SETUP MAZE
// You can create your own maze here. line of code is adding a wall.
// You describe position of top left corner of wall (x, y), then width and height going down/to right
// Relative positions are used (OVERALL_WINDOW_WIDTH and OVERALL_WINDOW_HEIGHT)
// But you can use absolute positions. 10 is used as the width, but you can change this.
int gapWidth = 100;
insertAndSetFirstWall(&head, 2, 220, 400, 10, 80);
insertAndSetFirstWall(&head, 2, 20, 400, 200, 10);
insertAndSetFirstWall(&head, 2, 20, 50, 10, 350);
insertAndSetFirstWall(&head, 2, 20, 50, 280, 10);
insertAndSetFirstWall(&head, 2, 300, 50, 10, 100);
insertAndSetFirstWall(&head, 2, 300, 150, 110, 10);
insertAndSetFirstWall(&head, 2, 400, 50, 10, 100);
insertAndSetFirstWall(&head, 2, 400, 50, 220, 10);
insertAndSetFirstWall(&head, 2, 620, 50, 10, 290);
insertAndSetFirstWall(&head, 2, 620, 340, 20, 10);
insertAndSetFirstWall(&head, 1, 320, 300, 10, 180);
insertAndSetFirstWall(&head, 2, 120, 300, 200, 10);
insertAndSetFirstWall(&head, 2, 120, 150, 10, 150);
insertAndSetFirstWall(&head, 2, 120, 150, 80, 10);
insertAndSetFirstWall(&head, 2, 200, 150, 10, 100);
insertAndSetFirstWall(&head, 2, 200, 250, 310, 10);
insertAndSetFirstWall(&head, 2, 500, 150, 10, 100);
insertAndSetFirstWall(&head, 2, 500, 150, 10, 100);
insertAndSetFirstWall(&head, 2, 500, 150, 20, 10);
insertAndSetFirstWall(&head, 2, 520, 150, 10, 290);
insertAndSetFirstWall(&head, 2, 520, 440, 120, 10);
setup_robot(&robot);
updateAllWalls(head, renderer);
SDL_Event event;
while(!done){
SDL_SetRenderDrawColor(renderer, 200, 200, 200, 255);
SDL_RenderClear(renderer);
//Move robot based on user input commands/auto commands
if (robot.auto_mode == 1)
robotAutoMotorMove(&robot, front_left_sensor, front_right_sensor, front_sensor);
robotMotorMove(&robot);
//Check if robot reaches endpoint. and check sensor values
if (checkRobotReachedEnd(&robot, 640, 340, 10, 100)) { // --------------------------------------------------------------------------------------------------------------------------------
end_time = clock();
msec = (end_time-start_time) * 1000 / CLOCKS_PER_SEC;
robotSuccess(&robot, msec);
}
else if(checkRobotHitWalls(&robot, head))
robotCrash(&robot);
//Otherwise compute sensor information
else {
front_left_sensor = checkRobotSensorFrontLeftAllWalls(&robot, head);
if (front_left_sensor>0)
printf("Getting close on the left. Score = %d\n", front_left_sensor);
front_right_sensor = checkRobotSensorFrontRightAllWalls(&robot, head);
if (front_right_sensor>0)
printf("Getting close on the right. Score = %d\n", front_right_sensor);
front_sensor = checkRobotSensorFrontAllWalls(&robot, head);
if (front_sensor>0)
printf("Getting close on the front. Score = %d\n", front_sensor);
}
robotUpdate(renderer, &robot);
updateAllWalls(head, renderer);
// Check for user input
SDL_RenderPresent(renderer);
while(SDL_PollEvent(&event)){
if(event.type == SDL_QUIT){
done = 1;
}
const Uint8 *state = SDL_GetKeyboardState(NULL);
if(state[SDL_SCANCODE_UP]) {
robot.changeSpeed = 1;
} else if(state[SDL_SCANCODE_DOWN]) {
robot.changeSpeed = -1;
} else {
robot.changeSpeed = 0;
}
if(state[SDL_SCANCODE_LEFT] && robot.direction != RIGHT){
robot.direction = LEFT;
}
if(state[SDL_SCANCODE_RIGHT] && robot.direction != LEFT){
robot.direction = RIGHT;
}
if(state[SDL_SCANCODE_SPACE]){
setup_robot(&robot);
}
if(state[SDL_SCANCODE_RETURN]){
robot.auto_mode = 1;
start_time = clock();
}
}
SDL_Delay(120);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
printf("DEAD :(\n");
}