-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.h
96 lines (82 loc) · 2.31 KB
/
Board.h
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
#ifndef BOARD_H_
#define BOARD_H_
#include "GameConstants.h"
#include <iostream>
#include <fstream>
#include <string>
class Board
{
public:
enum GridEntry {
empty, player, blue_coin_square, red_coin_square, up_dir_square,
down_dir_square, left_dir_square, right_dir_square, event_square,
bank_square, star_square, bowser, boo
};
enum LoadResult {
load_success, load_fail_file_not_found, load_fail_bad_format
};
Board()
{
for (int gy = 0; gy < BOARD_HEIGHT; gy++)
for (int gx = 0; gx < BOARD_WIDTH; gx++)
m_grid[gy][gx] = empty;
}
LoadResult loadBoard(const std::string &filename)
{
std::ifstream boardFile(filename);
if (!boardFile)
return load_fail_file_not_found;
// get the grid
std::string line;
int numPlayerLocations = 0;
for (int gy = BOARD_HEIGHT-1; std::getline(boardFile, line); gy--)
{
if (gy < 0) // too many grid lines?
{
if (line.find_first_not_of(" \t\r") != std::string::npos)
return load_fail_bad_format; // non-blank line
char dummy;
if (boardFile >> dummy) // non-blank rest of file
return load_fail_bad_format;
break;
}
if (line.size() < BOARD_WIDTH ||
line.find_first_not_of(" \t\r", BOARD_WIDTH) != std::string::npos)
return load_fail_bad_format;
for (int gx = 0; gx < BOARD_WIDTH; gx++)
{
GridEntry ge;
switch (line[gx])
{
default: return load_fail_bad_format;
case ' ': ge = empty; break;
case '@': ge = player; numPlayerLocations++; break;
case '+': ge = blue_coin_square; break;
case '-': ge = red_coin_square; break;
case '<': ge = left_dir_square; break;
case '>': ge = right_dir_square; break;
case '^': ge = up_dir_square; break;
case 'v': ge = down_dir_square; break;
case '!': ge = event_square; break;
case '$': ge = bank_square; break;
case '*': ge = star_square; break;
case 'B': ge = bowser; break;
case 'b': ge = boo; break;
}
m_grid[gy][gx] = ge;
}
}
if (numPlayerLocations != 1)
return load_fail_bad_format;
return load_success;
}
GridEntry getContentsOf(int gx, int gy)
{
if (gx < 0 || gx >= BOARD_WIDTH || gy < 0 || gy >= BOARD_HEIGHT)
return empty;
return m_grid[gy][gx];
}
private:
GridEntry m_grid[BOARD_HEIGHT][BOARD_WIDTH]; // indexed by [gy][gx]
};
#endif // #ifndef BOARD_H_