diff --git a/SnakesAndLadder/Board.cpp b/SnakesAndLadder/Board.cpp new file mode 100644 index 00000000..015c7257 --- /dev/null +++ b/SnakesAndLadder/Board.cpp @@ -0,0 +1,31 @@ +#include "Board.h" + +Board::Board(int size, vector> snakes, vector> 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; +} \ No newline at end of file diff --git a/SnakesAndLadder/Board.h b/SnakesAndLadder/Board.h new file mode 100644 index 00000000..d3c9501c --- /dev/null +++ b/SnakesAndLadder/Board.h @@ -0,0 +1,19 @@ +#pragma once +#include +#include + +using namespace std; + +class Board +{ +private: + int winningPosition; + unordered_map snakes; + unordered_map ladders; +public: + Board(int size, vector> snakes, vector> ladders); + int makeMove(int diceRoll, int position); + bool isWinningPosition(int position); + +}; + diff --git a/SnakesAndLadder/Game.cpp b/SnakesAndLadder/Game.cpp new file mode 100644 index 00000000..91de46d5 --- /dev/null +++ b/SnakesAndLadder/Game.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#include "Game.h" + +using namespace std; + +Game::Game(int boardSize, vector> snakes, vector> ladders) + : board(boardSize, snakes, ladders) {} + +void Game::AddPlayers(vector playerNames) { + for (int i = 0; i < playerNames.size(); i++) { + this->players[i] = make_shared(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; + } + } +} diff --git a/SnakesAndLadder/Game.h b/SnakesAndLadder/Game.h new file mode 100644 index 00000000..b0d93403 --- /dev/null +++ b/SnakesAndLadder/Game.h @@ -0,0 +1,21 @@ +#pragma once +#include "Player.h" +#include "Board.h" + +using namespace std; + +class Game +{ +private: + unordered_map> players; + Board board; +public: + Game(int size, vector> snakes, vector> ladders); + + int RollDice(); + bool MakeMoveForPlayer(int playerId); + void AddPlayers(vector playerNames); + void StartGame(); + void DisplayWinner(int playerId); + +}; \ No newline at end of file diff --git a/SnakesAndLadder/Player.cpp b/SnakesAndLadder/Player.cpp new file mode 100644 index 00000000..75b9a0b8 --- /dev/null +++ b/SnakesAndLadder/Player.cpp @@ -0,0 +1,20 @@ +#include "Player.h" +#include + +using namespace std; + +Player::Player(string name, int id) : name(name), id(id) { + position = 0; +} + +int Player::getPosition() { + return position; +} + +void Player::updatePosition(int position) { + this->position = position; +} + +string Player::getName() { + return name; +} \ No newline at end of file diff --git a/SnakesAndLadder/Player.h b/SnakesAndLadder/Player.h new file mode 100644 index 00000000..32444d53 --- /dev/null +++ b/SnakesAndLadder/Player.h @@ -0,0 +1,18 @@ +#pragma once +#include + +using namespace std; + +class Player +{ +private: + int id; + string name; + int position; +public: + Player(string name, int id); + int getPosition(); + void updatePosition(int position); + string getName(); +}; + diff --git a/SnakesAndLadder/SnakesAndLadder.cpp b/SnakesAndLadder/SnakesAndLadder.cpp new file mode 100644 index 00000000..bcbf149b --- /dev/null +++ b/SnakesAndLadder/SnakesAndLadder.cpp @@ -0,0 +1,23 @@ +// SnakesAndLadder.cpp : This file contains the 'main' function. Program execution begins and ends there. +// + +#include +#include "Game.h" + +int main() +{ + Game snakesAndLadders(100, { { 99, 1 },{ 98, 2 },{ 97, 3 } }, { { 1, 38 },{ 4, 14 },{ 9, 31 } }); + snakesAndLadders.AddPlayers({ "Gaurav", "Saurabh", "Ravi" }); + snakesAndLadders.StartGame(); +} + +// Run program: Ctrl + F5 or Debug > Start Without Debugging menu +// Debug program: F5 or Debug > Start Debugging menu + +// Tips for Getting Started: +// 1. Use the Solution Explorer window to add/manage files +// 2. Use the Team Explorer window to connect to source control +// 3. Use the Output window to see build output and other messages +// 4. Use the Error List window to view errors +// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project +// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file diff --git a/SnakesAndLadder/SnakesAndLadder.sln b/SnakesAndLadder/SnakesAndLadder.sln new file mode 100644 index 00000000..2b41cc58 --- /dev/null +++ b/SnakesAndLadder/SnakesAndLadder.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.12.35728.132 d17.12 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SnakesAndLadder", "SnakesAndLadder.vcxproj", "{4C1D133E-52F8-4CCF-B455-405AEB793934}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4C1D133E-52F8-4CCF-B455-405AEB793934}.Debug|x64.ActiveCfg = Debug|x64 + {4C1D133E-52F8-4CCF-B455-405AEB793934}.Debug|x64.Build.0 = Debug|x64 + {4C1D133E-52F8-4CCF-B455-405AEB793934}.Debug|x86.ActiveCfg = Debug|Win32 + {4C1D133E-52F8-4CCF-B455-405AEB793934}.Debug|x86.Build.0 = Debug|Win32 + {4C1D133E-52F8-4CCF-B455-405AEB793934}.Release|x64.ActiveCfg = Release|x64 + {4C1D133E-52F8-4CCF-B455-405AEB793934}.Release|x64.Build.0 = Release|x64 + {4C1D133E-52F8-4CCF-B455-405AEB793934}.Release|x86.ActiveCfg = Release|Win32 + {4C1D133E-52F8-4CCF-B455-405AEB793934}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/SnakesAndLadder/SnakesAndLadder.vcxproj b/SnakesAndLadder/SnakesAndLadder.vcxproj new file mode 100644 index 00000000..7cecb4e2 --- /dev/null +++ b/SnakesAndLadder/SnakesAndLadder.vcxproj @@ -0,0 +1,143 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {4c1d133e-52f8-4ccf-b455-405aeb793934} + SnakesAndLadder + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/SnakesAndLadder/SnakesAndLadder.vcxproj.filters b/SnakesAndLadder/SnakesAndLadder.vcxproj.filters new file mode 100644 index 00000000..afe3aa9c --- /dev/null +++ b/SnakesAndLadder/SnakesAndLadder.vcxproj.filters @@ -0,0 +1,42 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + + + \ No newline at end of file diff --git a/SnakesAndLadder/SnakesAndLadder.vcxproj.user b/SnakesAndLadder/SnakesAndLadder.vcxproj.user new file mode 100644 index 00000000..88a55094 --- /dev/null +++ b/SnakesAndLadder/SnakesAndLadder.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/SnakesAndLadder/SnakesAndLadder/x64/Debug/Board.obj b/SnakesAndLadder/SnakesAndLadder/x64/Debug/Board.obj new file mode 100644 index 00000000..a85c0832 Binary files /dev/null and b/SnakesAndLadder/SnakesAndLadder/x64/Debug/Board.obj differ diff --git a/SnakesAndLadder/SnakesAndLadder/x64/Debug/Game.obj b/SnakesAndLadder/SnakesAndLadder/x64/Debug/Game.obj new file mode 100644 index 00000000..1598681e Binary files /dev/null and b/SnakesAndLadder/SnakesAndLadder/x64/Debug/Game.obj differ diff --git a/SnakesAndLadder/SnakesAndLadder/x64/Debug/Player.obj b/SnakesAndLadder/SnakesAndLadder/x64/Debug/Player.obj new file mode 100644 index 00000000..44bb14a1 Binary files /dev/null and b/SnakesAndLadder/SnakesAndLadder/x64/Debug/Player.obj differ diff --git a/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.exe.recipe b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.exe.recipe new file mode 100644 index 00000000..e9fc1a5a --- /dev/null +++ b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.exe.recipe @@ -0,0 +1,11 @@ + + + + + D:\LLD\SnakesAndLadder\x64\Debug\SnakesAndLadder.exe + + + + + + \ No newline at end of file diff --git a/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.ilk b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.ilk new file mode 100644 index 00000000..7f197bcd Binary files /dev/null and b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.ilk differ diff --git a/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.log b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.log new file mode 100644 index 00000000..26012087 --- /dev/null +++ b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.log @@ -0,0 +1,2 @@ + Game.cpp + SnakesAndLadder.vcxproj -> D:\LLD\SnakesAndLadder\x64\Debug\SnakesAndLadder.exe diff --git a/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.obj b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.obj new file mode 100644 index 00000000..c8219eb2 Binary files /dev/null and b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.obj differ diff --git a/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/CL.command.1.tlog b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/CL.command.1.tlog new file mode 100644 index 00000000..63b852fc Binary files /dev/null and b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/CL.command.1.tlog differ diff --git a/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/CL.read.1.tlog b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/CL.read.1.tlog new file mode 100644 index 00000000..84a10d72 Binary files /dev/null and b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/CL.read.1.tlog differ diff --git a/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/CL.write.1.tlog b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/CL.write.1.tlog new file mode 100644 index 00000000..74626189 Binary files /dev/null and b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/CL.write.1.tlog differ diff --git a/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/Cl.items.tlog b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/Cl.items.tlog new file mode 100644 index 00000000..d4013abe --- /dev/null +++ b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/Cl.items.tlog @@ -0,0 +1,4 @@ +D:\LLD\SnakesAndLadder\Board.cpp;D:\LLD\SnakesAndLadder\SnakesAndLadder\x64\Debug\Board.obj +D:\LLD\SnakesAndLadder\Game.cpp;D:\LLD\SnakesAndLadder\SnakesAndLadder\x64\Debug\Game.obj +D:\LLD\SnakesAndLadder\Player.cpp;D:\LLD\SnakesAndLadder\SnakesAndLadder\x64\Debug\Player.obj +D:\LLD\SnakesAndLadder\SnakesAndLadder.cpp;D:\LLD\SnakesAndLadder\SnakesAndLadder\x64\Debug\SnakesAndLadder.obj diff --git a/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/SnakesAndLadder.lastbuildstate b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/SnakesAndLadder.lastbuildstate new file mode 100644 index 00000000..504ea9c1 --- /dev/null +++ b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/SnakesAndLadder.lastbuildstate @@ -0,0 +1,2 @@ +PlatformToolSet=v143:VCToolArchitecture=Native64Bit:VCToolsVersion=14.42.34433:TargetPlatformVersion=10.0.22621.0: +Debug|x64|D:\LLD\SnakesAndLadder\| diff --git a/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/link.command.1.tlog b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/link.command.1.tlog new file mode 100644 index 00000000..9906ff0c Binary files /dev/null and b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/link.command.1.tlog differ diff --git a/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/link.read.1.tlog b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/link.read.1.tlog new file mode 100644 index 00000000..faf9f005 Binary files /dev/null and b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/link.read.1.tlog differ diff --git a/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/link.secondary.1.tlog b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/link.secondary.1.tlog new file mode 100644 index 00000000..c1e0aa66 --- /dev/null +++ b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/link.secondary.1.tlog @@ -0,0 +1,2 @@ +^D:\LLD\SNAKESANDLADDER\SNAKESANDLADDER\X64\DEBUG\BOARD.OBJ|D:\LLD\SNAKESANDLADDER\SNAKESANDLADDER\X64\DEBUG\GAME.OBJ|D:\LLD\SNAKESANDLADDER\SNAKESANDLADDER\X64\DEBUG\PLAYER.OBJ|D:\LLD\SNAKESANDLADDER\SNAKESANDLADDER\X64\DEBUG\SNAKESANDLADDER.OBJ +D:\LLD\SnakesAndLadder\SnakesAndLadder\x64\Debug\SnakesAndLadder.ilk diff --git a/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/link.write.1.tlog b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/link.write.1.tlog new file mode 100644 index 00000000..e96c4106 Binary files /dev/null and b/SnakesAndLadder/SnakesAndLadder/x64/Debug/SnakesAndLadder.tlog/link.write.1.tlog differ diff --git a/SnakesAndLadder/SnakesAndLadder/x64/Debug/vc143.idb b/SnakesAndLadder/SnakesAndLadder/x64/Debug/vc143.idb new file mode 100644 index 00000000..32181c5d Binary files /dev/null and b/SnakesAndLadder/SnakesAndLadder/x64/Debug/vc143.idb differ diff --git a/SnakesAndLadder/SnakesAndLadder/x64/Debug/vc143.pdb b/SnakesAndLadder/SnakesAndLadder/x64/Debug/vc143.pdb new file mode 100644 index 00000000..d775a81f Binary files /dev/null and b/SnakesAndLadder/SnakesAndLadder/x64/Debug/vc143.pdb differ diff --git a/SnakesAndLadder/x64/Debug/SnakesAndLadder.exe b/SnakesAndLadder/x64/Debug/SnakesAndLadder.exe new file mode 100644 index 00000000..bff62981 Binary files /dev/null and b/SnakesAndLadder/x64/Debug/SnakesAndLadder.exe differ diff --git a/SnakesAndLadder/x64/Debug/SnakesAndLadder.pdb b/SnakesAndLadder/x64/Debug/SnakesAndLadder.pdb new file mode 100644 index 00000000..661a0d8d Binary files /dev/null and b/SnakesAndLadder/x64/Debug/SnakesAndLadder.pdb differ