-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.cpp
90 lines (78 loc) · 2.37 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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
/**
* A simple class representing a coordinate on a 3D plane
*/
class Position{
public:
double x;
double y;
double z;
Position() {} //Default constructor
Position(double x, double y, double z){
this->x = x;
this->y = y;
this->z = z;
}
};
/**
* A class representing a 2D board (in our case, the y is fixed at 0)
*
* A board object is istantiated by passing the level name to the constructor
*
* @field startingPos: A {@link:Position} that represents where the controllable block starts on this board
* @field winningPos: A {@link:Position} that represents the winning tile on the board. Standing upright on this tile, wins the levels
* @field dimension: The dimension of the board. the bord is dimension x dimension large
* @field board: A vector of strings, where a single character represents one tile
* The types of the tiles are as follows:
* * F -> Filled Tile
* * X -> Empty Tile
* * W -> Win Tile
* * T -> Trap
* @field moves: The number of moves the player has taken so far
* @field optimal: The lowest number of moves this level can be solved in
*
* A level file is of (txt) extension, in the folder "levels/" in the same directory as main.cpp. It is in this form:
* * n
* * n lines of n long strings
* * x1 z1
* * x2 z2
* * d
* Where n is the dimension of the board, (x1, z1) are the coords of the start position,
* (x2, z2) are the coords of the winning position, and d is the number of optimal moves to win the game
*/
class Board{
public:
Position startingPos;
Position winningPos;
int dimension;
vector<string> board;
int optimal;
int moves;
Board(){} //Default Constructor
Board(int level){
ifstream in("levels/" + to_string((long double)level) + ".txt");
string s;
getline(in, s);
this->dimension = stoi(s);
for(int i = 0; i < this->dimension; i++){
getline(in, s);
board.push_back(s);
}
int x, z;
x = (int) (in.get() - '0'); in.get();
z = (int) (in.get() - '0'); in.get();
this->startingPos = Position(x, 0, z);
x = (int) (in.get() - '0'); in.get();
z = (int) (in.get() - '0'); in.get();
this->winningPos = Position(x, 0, z);
optimal = (int)(in.get() - '0') * 100;
optimal += (int)(in.get() - '0') * 10;
optimal += (int)(in.get() - '0'); in.get();
moves = 0;
in.close();
}
};