DEV Community

Discussion on: Daily Challenge #207 - Snakes and Ladders

Collapse
 
vidit1999 profile image
Vidit Sarkar

It was fun

#include <bits/stdc++.h>
using namespace std;

// returns the sum of numbers in die1 and die2 and also whether they are same or not
// Example : die1 = 4, die2 = 5  => return <9, false>
// Example : die1 = 3, die2 = 3  => return <6, true>
pair<int, bool> play(){
    int die1 = rand()%6 + 1;
    int die2 = rand()%6 + 1;

    return make_pair(die1 + die2, die1==die2);
}

// main snake and ladder game
void SnakesLadders(){
    srand(time(0));

    // differnt positions of snake and ladders
    // first one is starting position and second one is ending position
    unordered_map<int, int> snakeLadderPos = {
        {99,80},
        {95,75},
        {92,88},
        {89,68},
        {74,53},
        {64,60},
        {62,19},
        {49,11},
        {46,25},
        {16,6},
        {2,38},
        {7,14},
        {8,31},
        {15,26},
        {21,42},
        {28,84},
        {36,44},
        {51,67},
        {71,91},
        {78,98},
        {87,94}
    };

    int playerPos[2] = {0,0}; // positions of player1 and player2 respectively
    int turn = 0; // 0 if it is player1's turn, 1 if player2's turn

    while(true){
        pair<int, bool> move = play(); // roll the dies
        playerPos[turn] += move.first; // make the move

        // prints the move
        cout << "Player " << turn+1 << "'s turn : " << move.first;
        if(move.second)
            cout << " and double chance";
        cout << "\n";

        // bounce off if move takes player to position greater than 100
        if(playerPos[turn] > 100){
            cout << "Player " << turn+1 << " bounces off from " << playerPos[turn] << " to ";
            playerPos[turn] = 200 - playerPos[turn];
            cout << playerPos[turn] << ".\n";
        }

        // if snake or ladder then go the position indicated
        if(snakeLadderPos.find(playerPos[turn]) != snakeLadderPos.end()){
            if(playerPos[turn] > snakeLadderPos[playerPos[turn]])
                cout << "Player " << turn+1 << " is snaked from " << playerPos[turn] << " to ";
            else
                cout << "Player " << turn+1 << " is laddered from " << playerPos[turn] << " to ";
            playerPos[turn] = snakeLadderPos[playerPos[turn]];
            cout  << playerPos[turn] << "\n";
        }


        // this part handles the case if the player reaches the goal or not
        // if goal is reached then prints the winner and break the loop
        if(playerPos[turn] == 100){
            cout << "Player " << turn+1 << " wins!\nGame over for Player " << 2-turn << ".\n";
            break;
        }

        // if player does not have a second move
        // then its turn for other player
        if(!move.second)
            turn = 1 - turn;
        cout << "Player 1 is on square "<< playerPos[0] << ".\n";
        cout << "Player 2 is on square "<< playerPos[1] << ".\n\n";
        this_thread::sleep_for(chrono::milliseconds(500)); // prints after 0.5sec gap
    }
}

// main function
int main(){
    SnakesLadders();
    return 0;
}