#include <bits/stdc++.h>
usingnamespacestd;// 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(){intdie1=rand()%6+1;intdie2=rand()%6+1;returnmake_pair(die1+die2,die1==die2);}// main snake and ladder gamevoidSnakesLadders(){srand(time(0));// differnt positions of snake and ladders// first one is starting position and second one is ending positionunordered_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}};intplayerPos[2]={0,0};// positions of player1 and player2 respectivelyintturn=0;// 0 if it is player1's turn, 1 if player2's turnwhile(true){pair<int,bool>move=play();// roll the diesplayerPos[turn]+=move.first;// make the move// prints the movecout<<"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 100if(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 indicatedif(snakeLadderPos.find(playerPos[turn])!=snakeLadderPos.end()){if(playerPos[turn]>snakeLadderPos[playerPos[turn]])cout<<"Player "<<turn+1<<" is snaked from "<<playerPos[turn]<<" to ";elsecout<<"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 loopif(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 playerif(!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 functionintmain(){SnakesLadders();return0;}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
It was fun