route-planner-ip2-public/robot/simple-simulator.c
2025-06-14 23:26:42 +02:00

67 lines
1.4 KiB
C

#include <stdio.h>
#define SIMULATION
#include <ctype.h>
#include "../leelib/leelib.h"
#include "../lee-algorithm/lee-algorithm.h"
#include "../cell/cell.h"
extern int unexpanded_matrix[MAZE_SIZE][MAZE_SIZE];
extern const cell neighbours[4];
void addBlock(robot r, direction dir, cell targetCell, stack *s);
void simple_control() { //TODO: OUTDATED, rewrite if needed.
/* read the starting and end station.*/
cell startCell, targetCell;
printf("STARTING AND ENDING STATION: ");
readStation(&startCell.x, &startCell.y);
readStation(&targetCell.x, &targetCell.y);
robot r;
r.dir = getStartDirection(startCell);
r.pos = startCell;
stack s;
stack_init(&s);
lee_run(r, targetCell, &s);
while (s.length > 0) {
move(&r, stack_pop(&s));
printMatrix_with_current_pos(unexpanded_matrix, r);
printf("BLOCKAGE: ");
char input;
scanf("%c", &input);
switch (tolower(input)) {
case 'n':
addBlock(r, NORTH, targetCell, &s);
break;
case 's':
addBlock(r, SOUTH, targetCell, &s);
break;
case 'e':
addBlock(r, EAST, targetCell, &s);
break;
case 'w':
addBlock(r, WEST, targetCell, &s);
break;
default:
break;
}
}
stack_free(&s);
}
void addBlock(robot r, direction dir, cell targetCell, stack *s) {
cell block;
cell_add(&block, r.pos, neighbours[dir]);
unexpanded_matrix[block.x][block.y] = -2; // putting -2 makes it not work???
if (!validatePath(r, *s)) {
stack_reinit(s);
lee_run(r, targetCell, s);
}
}