-
Notifications
You must be signed in to change notification settings - Fork 513
Expand file tree
/
Copy pathGame.cpp
More file actions
50 lines (39 loc) · 1.15 KB
/
Copy pathGame.cpp
File metadata and controls
50 lines (39 loc) · 1.15 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
#include <iostream>
#include <vector>
#include <unordered_map>
#include "Game.h"
using namespace std;
Game::Game(int boardSize, vector<pair<int, int>> snakes, vector<pair<int, int>> ladders)
: board(boardSize, snakes, ladders) {}
void Game::AddPlayers(vector<string> playerNames) {
for (int i = 0; i < playerNames.size(); i++) {
this->players[i] = make_shared<Player>(playerNames[i], i);
}
}
int Game::RollDice() {
return rand() % 6 + 1;
}
bool Game::MakeMoveForPlayer(int playerId) {
int DiceRoll = RollDice();
int currPos = players[playerId]->getPosition();
int newPos = board.makeMove(DiceRoll, currPos);
players[playerId]->updatePosition(newPos);
bool haveWon = false;
cout << players[playerId]->getName() << " rolled a " << DiceRoll << " and moved from " << currPos << " to " << newPos << endl;
if (board.isWinningPosition(newPos)) {
DisplayWinner(playerId);
haveWon = true;
}
return haveWon;
}
void Game::DisplayWinner(int playerId) {
cout << players[playerId]->getName() << " wins the game!" << endl;
}
void Game::StartGame() {
while (true) {
for (auto player : players) {
if (MakeMoveForPlayer(player.first))
return;
}
}
}