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
31 changes: 31 additions & 0 deletions SnakesAndLadder/Board.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
19 changes: 19 additions & 0 deletions SnakesAndLadder/Board.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once
#include <vector>
#include <unordered_map>

using namespace std;

class Board
{
private:
int winningPosition;
unordered_map<int, int> snakes;
unordered_map<int, int> ladders;
public:
Board(int size, vector<pair<int,int>> snakes, vector<pair<int,int>> ladders);
int makeMove(int diceRoll, int position);
bool isWinningPosition(int position);

};

50 changes: 50 additions & 0 deletions SnakesAndLadder/Game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
}
}
21 changes: 21 additions & 0 deletions SnakesAndLadder/Game.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once
#include "Player.h"
#include "Board.h"

using namespace std;

class Game
{
private:
unordered_map<int, shared_ptr<Player>> players;
Board board;
public:
Game(int size, vector<pair<int,int>> snakes, vector<pair<int, int>> ladders);

int RollDice();
bool MakeMoveForPlayer(int playerId);
void AddPlayers(vector<string> playerNames);
void StartGame();
void DisplayWinner(int playerId);

};
20 changes: 20 additions & 0 deletions SnakesAndLadder/Player.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "Player.h"
#include <iostream>

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;
}
18 changes: 18 additions & 0 deletions SnakesAndLadder/Player.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once
#include<iostream>

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();
};

23 changes: 23 additions & 0 deletions SnakesAndLadder/SnakesAndLadder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SnakesAndLadder.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#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
28 changes: 28 additions & 0 deletions SnakesAndLadder/SnakesAndLadder.sln
Original file line number Diff line number Diff line change
@@ -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
143 changes: 143 additions & 0 deletions SnakesAndLadder/SnakesAndLadder.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{4c1d133e-52f8-4ccf-b455-405aeb793934}</ProjectGuid>
<RootNamespace>SnakesAndLadder</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Board.cpp" />
<ClCompile Include="Game.cpp" />
<ClCompile Include="Player.cpp" />
<ClCompile Include="SnakesAndLadder.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Board.h" />
<ClInclude Include="Game.h" />
<ClInclude Include="Player.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
Loading