Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Snake_N_Ladders/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
9
62 5
33 6
49 9
88 16
41 20
56 53
98 64
93 73
95 75
8
2 37
27 46
10 32
51 68
61 79
65 84
71 91
81 100
2
Gaurav
Sagar
189 changes: 189 additions & 0 deletions Snake_N_Ladders/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
#include<bits/stdc++.h>
using namespace std;

class Board {
private:
int cells, snakesCount, laddersCount;

unordered_map<int, int> snakes;
unordered_map<int, int> ladders;
void setSnakesValues (vector<pair<int, int>> &snakes) {
this->snakesCount = snakes.size();
for (auto each : snakes)
this->snakes[each.first] = each.second;
}

void setLaddersValues (vector<pair<int, int>> &ladders) {
this->laddersCount = ladders.size();
for (auto each : ladders)
this->ladders[each.first] = each.second;
}

public:
Board (vector<pair<int, int>> &snakes, vector<pair<int, int>> &ladders) {
setSnakesValues(snakes);
setLaddersValues(ladders);
this->cells = 100;
}

Board (vector<pair<int, int>> &snakes, vector<pair<int, int>> &ladders, int cells) {
setSnakesValues(snakes);
setLaddersValues(ladders);
this->cells = cells;
}

int isSnakeSting (int cellNumber) {
if (snakes[cellNumber]) {
return snakes[cellNumber];
}
else {
return -1;
}
}

int isLadderRewarded (int cellNumber) {
if (ladders[cellNumber]) {
return ladders[cellNumber];
}
else {
return -1;
}
}
};


class DiceRoller {
private:
int diceCount;

public:
DiceRoller () {
this->diceCount = 1;
}

DiceRoller (int n) {
this->diceCount = n;
}

int RollTheDice () {
mt19937 generator(std::chrono::system_clock::now().time_since_epoch().count());
uniform_int_distribution<int> distribution(1, 6);
int sum = distribution(generator);
if (sum == 6) {
int secondChance = distribution(generator);
if (sum + secondChance == 12) {
int thirdChance = distribution(generator);
if (sum + secondChance + thirdChance == 18) {
return 0;
} else {
return sum + secondChance + thirdChance;
}
} else {
return sum + secondChance;
}
} else {
return sum;
}
}
};

class Player {
private:
string name;
int cellPosition;

public:
Player (string name) {
this->name = name;
cellPosition = 0;
}

string getName() {
return this->name;
}

int getCellNumber () {
return this->cellPosition;
}

void updateCellPosition (int newCellPosition) {
this->cellPosition = newCellPosition;
}
};

class Game {
public:
Game () {

}
void play () {
int numberOfSnakes;
cin>>numberOfSnakes;
int from , to;
vector<pair<int, int> > snakes(numberOfSnakes);
for (int i =0 ; i < numberOfSnakes; ++i) {
cin>>from>>to;
snakes[i] = { from, to };
}
int numberOfLadders;
cin>>numberOfLadders;
vector<pair<int, int> > ladders(numberOfLadders);
for (int i =0; i < numberOfLadders; ++i) {
cin>>from>>to;
ladders[i] = { from, to };
}
Board * board = new Board(snakes, ladders);
int numberOfPlayers;
cin>>numberOfPlayers;
string name;
vector<Player *> players(numberOfPlayers);
for (int i =0; i < numberOfPlayers; ++i) {
cin>>name;
players[i] = new Player(name);
}

DiceRoller * dice = new DiceRoller();
int playerReached = -1;
while(true) {
for (int i = 0; i < numberOfPlayers; ++i) {
int num = dice->RollTheDice();
int initialPosition = players[i]->getCellNumber();
int newPosition = num + initialPosition;
int snakePosition = board->isSnakeSting(newPosition);
int ladderPosition = board->isLadderRewarded(newPosition);
if (snakePosition != -1) {
newPosition = snakePosition;
} else if (ladderPosition != -1) {
newPosition = ladderPosition;
}
if (newPosition > 100) {
newPosition = initialPosition;
cout<<players[i]->getName()<<" rolled a "<<num<<" which is not valid from "<<initialPosition<<endl;
} else {
players[i]->updateCellPosition(newPosition);
cout<<players[i]->getName()<<" rolled a "<<num<<" and moved from "<<initialPosition<<" to "<<newPosition<<endl;

}
if (newPosition == 100) {
playerReached = i;
break;
}
}
if (playerReached != -1) {
cout<<players[playerReached]->getName()<<" wins the game";
break;
}
}
}
};

int main () {

#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
Game * game = new Game();
game->play();
return 0;
}
42 changes: 42 additions & 0 deletions Snake_N_Ladders/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Gaurav rolled a 4 and moved from 0 to 4
Sagar rolled a 4 and moved from 0 to 4
Gaurav rolled a 2 and moved from 4 to 6
Sagar rolled a 2 and moved from 4 to 6
Gaurav rolled a 2 and moved from 6 to 8
Sagar rolled a 2 and moved from 6 to 8
Gaurav rolled a 2 and moved from 8 to 32
Sagar rolled a 2 and moved from 8 to 32
Gaurav rolled a 2 and moved from 32 to 34
Sagar rolled a 2 and moved from 32 to 34
Gaurav rolled a 2 and moved from 34 to 36
Sagar rolled a 2 and moved from 34 to 36
Gaurav rolled a 2 and moved from 36 to 38
Sagar rolled a 2 and moved from 36 to 38
Gaurav rolled a 2 and moved from 38 to 40
Sagar rolled a 2 and moved from 38 to 40
Gaurav rolled a 2 and moved from 40 to 42
Sagar rolled a 2 and moved from 40 to 42
Gaurav rolled a 2 and moved from 42 to 44
Sagar rolled a 2 and moved from 42 to 44
Gaurav rolled a 2 and moved from 44 to 46
Sagar rolled a 2 and moved from 44 to 46
Gaurav rolled a 2 and moved from 46 to 48
Sagar rolled a 2 and moved from 46 to 48
Gaurav rolled a 2 and moved from 48 to 50
Sagar rolled a 2 and moved from 48 to 50
Gaurav rolled a 2 and moved from 50 to 52
Sagar rolled a 2 and moved from 50 to 52
Gaurav rolled a 2 and moved from 52 to 54
Sagar rolled a 2 and moved from 52 to 54
Gaurav rolled a 2 and moved from 54 to 53
Sagar rolled a 2 and moved from 54 to 53
Gaurav rolled a 2 and moved from 53 to 55
Sagar rolled a 2 and moved from 53 to 55
Gaurav rolled a 2 and moved from 55 to 57
Sagar rolled a 2 and moved from 55 to 57
Gaurav rolled a 2 and moved from 57 to 59
Sagar rolled a 2 and moved from 57 to 59
Gaurav rolled a 2 and moved from 59 to 79
Sagar rolled a 2 and moved from 59 to 79
Gaurav rolled a 2 and moved from 79 to 100
Gaurav wins the game
10 changes: 10 additions & 0 deletions Splitwise/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
SHOW
SHOW u1
EXPENSE u1 1000 4 u1 u2 u3 u4 EQUAL
SHOW u4
SHOW u1
EXPENSE u1 1250 2 u2 u3 EXACT 370 880
SHOW
EXPENSE u4 1200 4 u1 u2 u3 u4 PERCENT 40 20 20 20
SHOW u1
SHOW
Loading