-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.cpp
100 lines (85 loc) · 1.97 KB
/
board.cpp
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
#include "board.h"
#include <cstring>
#include <fstream>
#include <iostream>
#include <exception>
Board::Board(const char *fileName)
{
boardVector = fileToBoard(fileName);
boardRows = boardVector.size();
boardCols = boardVector[0].size();
}
Board::Board(Board &other)
{
boardRows = other.boardRows;
boardCols = other.boardCols;
boardVector = other.boardVector;
}
//convert the file to a vector
std::vector<std::string> Board::fileToBoard(const char *fileName)
{
std::string line = fileName;
std::ifstream file(line, std::ios::in);
//check if the file is open
if (!file.is_open())
{
throw std::runtime_error("Unable to open file!");
}
std::vector<std::string> lines;
while (std::getline(file, line))
{
lines.push_back(line);
}
file.close();
return lines;
}
char Board::getBoardChar(int row, int col)
{
return boardVector[row][col];
}
int Board::getBoardRows()
{
return boardRows;
}
int Board::getBoardCols()
{
return boardCols;
}
void Board::setBoard(int row, int col, char value)
{
boardVector[row][col] = value;
}
void Board::positionToBoard(Position pos, char value)
{
setBoard(pos.first, pos.second, value);
}
//update the board with the new position of an entity
void Board::updateOldPosition(Position &oldPos, Position &newPos, char value)
{
positionToBoard(oldPos, '.');
positionToBoard(newPos, value);
}
bool Board::posIsChar(int row, int col, char c)
{
return (boardVector[row][col] == c);
}
bool Board::posIsChar(Position p, char c)
{
return posIsChar(p.first, p.second, c);
}
bool Board::isValid(int row, int col)
{
return (row >= 0 && row < boardRows && col >= 0 && col < boardCols);
}
bool Board::isWall(int row, int col)
{
return (isValid(row,col) && posIsChar(row, col, '*'));
}
bool Board::isPath(int row, int col)
{
return (isValid(row,col) && posIsChar(row, col, '.'));
}
bool Board::isPath(Position p)
{
return isPath(p.first, p.second);
}