Symphony 1.0
Loading...
Searching...
No Matches
simple_maze.h
Go to the documentation of this file.
1//
2// Created by velocitatem on 12/16/24.
3//
4
5#ifndef SIMPLE_MAZE_H
6#define SIMPLE_MAZE_H
7
8#include <iostream>
9#include <vector>
10#include "symphony.h"
11
12
13class MazeState : public State {
14// use a 2D array to represent the maze
15public:
17 maze = {
18 {0, 0, 0, 0, 0},
19 {0, 1, 1, 1, 0},
20 {0, 1, -1, 0, 0},
21 {0, 1, 1, 1, 0},
22 {0, 0, 0, 0, 0}
23 };
24 x = 1;
25 y = 1;
26 }
27 MazeState(std::vector<std::vector<int>> maze, int x, int y) : maze(maze), x(x), y(y) {}
28 std::vector<std::vector<int>> maze;
29 int x;
30 int y;
31 void print() override {
32 int row_index = 0;
33 for (auto &row : maze) {
34 int col_index = 0;
35 for (auto &cell : row) {
36 if (x == row_index && y == col_index) {
37 std::cout << "V";
38 col_index++;
39 continue;
40 }
41 if (cell == 0) {
42 std::cout << " ";
43 } else if (cell == 1) {
44 std::cout << "#";
45 } else if (cell == -1) {
46 std::cout << "X";
47 }
48 col_index++;
49 }
50 row_index++;
51 std::cout << std::endl;
52 }
53 }
54};
55
56
57class MazeProblem : public Problem {
58public:
61 }
63 }
64 bool goal_test(State *state) override {
65 auto *maze_state = dynamic_cast<MazeState *>(state);
66 return maze_state->maze[maze_state->x][maze_state->y] == -1;
67 }
68
69 std::vector<std::shared_ptr<Action>> actions(std::shared_ptr<State> state) override {
70 auto maze_state = std::dynamic_pointer_cast<MazeState>(state);
71 std::vector<std::shared_ptr<Action>> actions;
72
73 auto create_state = [&](int x, int y) {
74 return std::make_shared<MazeState>(maze_state->maze, x, y);
75 };
76
77 // Add possible actions with smart pointers
78 if (maze_state->x > 0 && maze_state->maze[maze_state->x - 1][maze_state->y] != 1) {
79 actions.push_back(std::make_shared<Action>("Up", 1, state, create_state(maze_state->x - 1, maze_state->y)));
80 }
81 if (maze_state->x < maze_state->maze.size() - 1 && maze_state->maze[maze_state->x + 1][maze_state->y] != 1) {
82 actions.push_back(std::make_shared<Action>("Down", 1, state, create_state(maze_state->x + 1, maze_state->y)));
83 }
84 if (maze_state->y > 0 && maze_state->maze[maze_state->x][maze_state->y - 1] != 1) {
85 actions.push_back(std::make_shared<Action>("Left", 1, state, create_state(maze_state->x, maze_state->y - 1)));
86 }
87 if (maze_state->y < maze_state->maze[0].size() - 1 && maze_state->maze[maze_state->x][maze_state->y + 1] != 1) {
88 actions.push_back(std::make_shared<Action>("Right", 1, state, create_state(maze_state->x, maze_state->y + 1)));
89 }
90
91 return actions;
92 }
93
94
95
96 double heuristic(State *state) override {
97 auto *maze_state = dynamic_cast<MazeState *>(state);
98 return abs(maze_state->x - 3) + abs(maze_state->y - 3);
99 }
100};
101
102
103#endif //SIMPLE_MAZE_H
double heuristic(State *state) override
Computes a heuristic estimate for a given state.
Definition simple_maze.h:96
bool goal_test(State *state) override
Tests if a given state satisfies the goal condition.
Definition simple_maze.h:64
std::vector< std::shared_ptr< Action > > actions(std::shared_ptr< State > state) override
Retrieves the set of actions applicable to a given state.
Definition simple_maze.h:69
MazeState(std::vector< std::vector< int > > maze, int x, int y)
Definition simple_maze.h:27
std::vector< std::vector< int > > maze
Definition simple_maze.h:28
void print() override
Prints the state details.
Definition simple_maze.h:31
Represents an abstract problem that needs to be solved.
Definition definitions.h:63
State * initial_state_
Pointer to the initial state of the problem.
Represents an abstract state in a problem.
Definition definitions.h:18