-
Notifications
You must be signed in to change notification settings - Fork 513
Expand file tree
/
Copy pathBoard.cpp
More file actions
31 lines (28 loc) · 799 Bytes
/
Copy pathBoard.cpp
File metadata and controls
31 lines (28 loc) · 799 Bytes
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
#include "Board.h"
Board::Board(int size, vector<pair<int, int>> snakes, vector<pair<int, int>> ladders) {
this->winningPosition = size;
for (auto snake : snakes) {
this->snakes[snake.first] = snake.second;
}
for (auto ladder : ladders) {
this->ladders[ladder.first] = ladder.second;
}
}
int Board::makeMove(int diceRoll, int position) {
int newPosition = position + diceRoll;
if (newPosition > winningPosition) {
return position;
}
if (snakes.find(newPosition) != snakes.end()) {
newPosition = snakes[newPosition];
makeMove(0, newPosition);
}
else if (ladders.find(newPosition) != ladders.end()) {
newPosition = ladders[newPosition];
makeMove(0, newPosition);
}
return newPosition;
}
bool Board::isWinningPosition(int position) {
return position == winningPosition;
}