Repository navigation
Expand file tree
/
Copy pathGrid.cpp
More file actions
96 lines (80 loc) · 2.37 KB
/
Copy pathGrid.cpp
File metadata and controls
96 lines (80 loc) · 2.37 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
#include "Grid.h"
#include <windows.h>
#include <iostream>
#include <cstdlib>
using namespace std;
Grid::Grid(int w, int h) : width(w), height(h) {
cells.resize(height, vector<char>(width, ' '));
initializeGrid();
}
void Grid::initializeGrid() {
// Create walls around the perimeter
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (i == 0 || i == height - 1 || j == 0 || j == width - 1) {
cells[i][j] = '#';
}
}
}
// Add some internal walls
for (int i = 5; i < 15; i++) {
if (width/2 < width) cells[i][width/2] = '#';
}
for (int j = 5; j < width/2 && j < width; j++) {
if (7 < height) cells[7][j] = '#';
}
// Create exits
exits.push_back(Position(1, 0));
exits.push_back(Position(width-2, 0));
exits.push_back(Position(width-1, height/2));
for (const auto& exit : exits) {
if (isValid(exit)) {
cells[exit.y][exit.x] = 'E';
}
}
}
bool Grid::isValid(const Position& pos) const {
return pos.x >= 0 && pos.x < width && pos.y >= 0 && pos.y < height;
}
char Grid::getCell(const Position& pos) const {
if (!isValid(pos)) return '#';
return cells[pos.y][pos.x];
}
void Grid::setCell(const Position& pos, char value) {
if (isValid(pos)) {
cells[pos.y][pos.x] = value;
}
}
bool Grid::isWalkable(const Position& pos) const {
if (!isValid(pos)) return false;
char cell = getCell(pos);
return cell == ' ' || cell == 'E';
}
vector<Position> Grid::getNeighbors(const Position& pos) const {
vector<Position> neighbors;
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
for (int i = 0; i < 4; i++) {
Position neighbor(pos.x + dx[i], pos.y + dy[i]);
if (isValid(neighbor)) {
neighbors.push_back(neighbor);
}
}
return neighbors;
}
const vector<Position>& Grid::getExits() const {
return exits;
}
void Grid::clearScreen() {
system("cls");
}
void Grid::display() const {
COORD coord = {0, 0};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
cout << cells[i][j];
}
cout << endl;
}
}