fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. /*
  5. Requirements
  6. ============
  7. 1. It should be a multiplayer game.
  8. 2. Customizable Board in terms of snake and ladder placement & board size.
  9. 3. Customizable in erms of number of dice used
  10. 4. When Game Board loads it should:
  11. a. Load the configured Board size.
  12. b. Load the configured Snake and Ladder positions.
  13. c. Load the configured Number of dice
  14. d. Load the number of players who will play the game and all of them start at 0.
  15.  
  16.  
  17. Classes To Think Of
  18. ===================
  19. Configuration, Snake, Ladder, Board, Player.
  20.  
  21. */
  22.  
  23.  
  24. class Player {
  25. private:
  26. string playerName;
  27. int id;
  28.  
  29. public:
  30. Player(string playerName, int id){
  31. this->playerName = playerName;
  32. this->id = id;
  33. }
  34.  
  35. string getPlayerName(){
  36. return playerName;
  37. }
  38. };
  39.  
  40.  
  41. class Dice {
  42. private:
  43. int numberOfDice;
  44.  
  45. public:
  46.  
  47. Dice () {}
  48. Dice(int numberOfDice){
  49. this->numberOfDice = numberOfDice;
  50. }
  51.  
  52. int rollDice() {
  53. return (random()%((numberOfDice*6 - numberOfDice*1)+1)) + numberOfDice;
  54. }
  55. };
  56.  
  57.  
  58. class Jumper {
  59. private:
  60. int startPoint;
  61. int endPoint;
  62. public:
  63. Jumper(int startPoint, int endPoint){
  64. this->startPoint = startPoint;
  65. this->endPoint = endPoint;
  66. }
  67.  
  68. int getStartPoint(){
  69. return startPoint;
  70. }
  71.  
  72. int getEndPoint(){
  73. return endPoint;
  74. }
  75. };
  76.  
  77.  
  78. class GameBoard {
  79. private:
  80. Dice* dice;
  81. queue<Player*> nextTurn;
  82. vector<Jumper*> snakes;
  83. vector<Jumper*> ladder;
  84. unordered_map<string, int> playersCurrentPosition;
  85. int boardSize;
  86. public:
  87. GameBoard(Dice* dice, queue<Player*> nextTurn, vector<Jumper*> snakes, vector<Jumper*> ladder, unordered_map<string, int> playersCurrentPosition, int boardSize){
  88. this->dice = dice;
  89. this->nextTurn = nextTurn;
  90. this->snakes = snakes;
  91. this->snakes = snakes;
  92. this->ladder = ladder;
  93. this->playersCurrentPosition = playersCurrentPosition;
  94. this->boardSize = boardSize;
  95. }
  96.  
  97. // This function can be refactored into several other functions
  98. void StartGame(){
  99. while(nextTurn.size() > 1){
  100. Player* player = nextTurn.front();
  101. nextTurn.pop();
  102.  
  103. int currentPosition = playersCurrentPosition[player->getPlayerName()];
  104. int diceValue = dice->rollDice();
  105. cout<<player->getPlayerName()<<" rolled the dice and got "<<diceValue<<endl;
  106. int nextCell = currentPosition + diceValue;
  107. if(nextCell > boardSize)
  108. nextTurn.push(player);
  109. else if (nextCell == boardSize)
  110. cout<<player->getPlayerName()<<" has won the game!"<<endl;
  111. else {
  112. bool change = false;
  113. for(auto &s: snakes){
  114. if(s->getStartPoint() == nextCell){
  115. nextCell = s->getEndPoint();
  116. change = true;
  117. cout<<player->getPlayerName()<<" got bitten by snake!"<<endl;
  118. }
  119. }
  120.  
  121. for(auto &s: ladder){
  122. if(s->getStartPoint() == nextCell){
  123. nextCell = s->getEndPoint();
  124. change = true;
  125. cout<<player->getPlayerName()<<" got a ladder!"<<endl;
  126. }
  127. }
  128.  
  129. if(nextCell == boardSize)
  130. cout<<player->getPlayerName()<<" has won the game!"<<endl;
  131. else {
  132. playersCurrentPosition[player->getPlayerName()] = nextCell;
  133. cout<<player->getPlayerName()<<" is at position "<<nextCell<<endl;
  134. nextTurn.push(player);
  135. }
  136. }
  137. }
  138. }
  139. };
  140.  
  141.  
  142. int main(){
  143.  
  144. Dice* dice = new Dice(1);
  145. Player* p1 = new Player("Harshit", 1);
  146. Player* p2 = new Player("Vijaya", 2);
  147. queue<Player*> allPlayers;
  148. allPlayers.push(p1);
  149. allPlayers.push(p2);
  150. Jumper* snake1 = new Jumper(10, 2);
  151. Jumper* snake2 = new Jumper(99, 12);
  152. vector<Jumper*> snakes = {snake1, snake2};
  153. Jumper* ladder1 = new Jumper(2, 25);
  154. Jumper* ladder2 = new Jumper(40, 89);
  155. vector<Jumper*> ladders = {ladder1, ladder2};
  156. unordered_map<string, int> playersCurrentPosition;
  157. playersCurrentPosition[p1->getPlayerName()] = 0;
  158. playersCurrentPosition[p2->getPlayerName()] = 0;
  159. GameBoard* gb = new GameBoard(dice, allPlayers, snakes, ladders, playersCurrentPosition, 100);
  160. gb->StartGame();
  161. return 0;
  162. }
Success #stdin #stdout 0s 5348KB
stdin
Standard input is empty
stdout
Harshit rolled the dice and got 2
Harshit got a ladder!
Harshit is at position 25
Vijaya rolled the dice and got 5
Vijaya is at position 5
Harshit rolled the dice and got 4
Harshit is at position 29
Vijaya rolled the dice and got 2
Vijaya is at position 7
Harshit rolled the dice and got 6
Harshit is at position 35
Vijaya rolled the dice and got 2
Vijaya is at position 9
Harshit rolled the dice and got 5
Harshit got a ladder!
Harshit is at position 89
Vijaya rolled the dice and got 1
Vijaya got bitten by snake!
Vijaya got a ladder!
Vijaya is at position 25
Harshit rolled the dice and got 4
Harshit is at position 93
Vijaya rolled the dice and got 2
Vijaya is at position 27
Harshit rolled the dice and got 3
Harshit is at position 96
Vijaya rolled the dice and got 2
Vijaya is at position 29
Harshit rolled the dice and got 3
Harshit got bitten by snake!
Harshit is at position 12
Vijaya rolled the dice and got 2
Vijaya is at position 31
Harshit rolled the dice and got 6
Harshit is at position 18
Vijaya rolled the dice and got 5
Vijaya is at position 36
Harshit rolled the dice and got 1
Harshit is at position 19
Vijaya rolled the dice and got 1
Vijaya is at position 37
Harshit rolled the dice and got 5
Harshit is at position 24
Vijaya rolled the dice and got 5
Vijaya is at position 42
Harshit rolled the dice and got 6
Harshit is at position 30
Vijaya rolled the dice and got 3
Vijaya is at position 45
Harshit rolled the dice and got 4
Harshit is at position 34
Vijaya rolled the dice and got 4
Vijaya is at position 49
Harshit rolled the dice and got 3
Harshit is at position 37
Vijaya rolled the dice and got 3
Vijaya is at position 52
Harshit rolled the dice and got 3
Harshit got a ladder!
Harshit is at position 89
Vijaya rolled the dice and got 2
Vijaya is at position 54
Harshit rolled the dice and got 2
Harshit is at position 91
Vijaya rolled the dice and got 2
Vijaya is at position 56
Harshit rolled the dice and got 6
Harshit is at position 97
Vijaya rolled the dice and got 1
Vijaya is at position 57
Harshit rolled the dice and got 1
Harshit is at position 98
Vijaya rolled the dice and got 1
Vijaya is at position 58
Harshit rolled the dice and got 6
Vijaya rolled the dice and got 4
Vijaya is at position 62
Harshit rolled the dice and got 2
Harshit has won the game!