-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathBoard.original.hpp
More file actions
75 lines (61 loc) · 2.23 KB
/
Board.original.hpp
File metadata and controls
75 lines (61 loc) · 2.23 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
/**
* Header file for the board of the war game.
*
* You can copy this file to a new file called Board.hpp, and extend it as you like.
*
* @author Oz Levi
* @author Erel Segal-Halevi
* @since 2020-05
*/
#include <string>
#include <vector>
#include <stdexcept>
#include "Soldier.hpp"
namespace WarGame {
struct SoldierProxy {
Soldier*& soldier;
SoldierProxy(Soldier*& the_soldier): soldier(the_soldier) {}
operator=(Soldier* new_soldier) {
if (soldier==nullptr) {
soldier = new_soldier;
} else {
throw _exception("Cannot override a non-null soldier");
}
}
operator Soldier*() {
return soldier;
}
};
class Board {
private:
std::vector<std::vector<Soldier*>> board;
public:
enum MoveDIR { Up, Down, Right, Left };
Board(uint numRows, uint numCols) :
board(numRows, std::vector<Soldier*>(numCols, nullptr)) {}
// operator for putting soldiers on the game-board during initialization.
SoldierProxy operator[](std::pair<int,int> location) {
return board[location.first][location.second];
}
// operator for reading which soldiers are on the game-board.
Soldier* operator[](std::pair<int,int> location) const;
// The function "move" tries to move the soldier of player "player"
// from the "source" location to the "target" location,
// and activates the special ability of the soldier.
// If the move is illegal, it throws "std::invalid_argument".
// Illegal moves include:
// * There is no soldier in the source location (i.e., the soldier pointer is null);
// * The soldier in the source location belongs to the other player.
// * There is already another soldier (of this or the other player) in the target location.
// IMPLEMENTATION HINT: Do not write "if" conditions that depend on the type of soldier!
// Your code should be generic. All handling of different types of soldiers
// must be handled by polymorphism.
void move(uint player_number, std::pair<int,int> source, MoveDIR direction);
// returns true iff the board contains one or more soldiers of the given player.
bool has_soldiers(uint player_number) const;
//~Board()
//{
// board.clear();
//}
};
}