answersLogoWhite

0


Best Answer

#include<iostream>

#include<array>

#include<string>

#include<random>

#include<ctime>

using player_t = std::array<unsigned, 2>;

using pair_t = std::pair<unsigned, unsigned>;

using snake_t = std::array<pair_t, 10>;

using ladder_t = std::array<std::pair<unsigned, unsigned>, 9>;

const std::string player (const bool human)

{

return std::string {human ? "You" : "I"};

}

int main()

{

std::default_random_engine generator ((unsigned) time (0));

std::uniform_int_distribution<unsigned> distribution (1, 6);

player_t players = {0,0};

const snake_t snakes = {pair_t {98,78}, {95,75}, {93,73}, {87,24}, {64,60}, {62,19}, {56,53}, {49,11}, {47,26}, {16,6}};

const ladder_t ladders = {pair_t {1,38}, {4,14}, {9,31}, {21,42}, {28,84}, {36,44}, {51,67}, {71,91}, {80,100}};

std::cout << "Snakes and Ladders\n";

std::cout << "==================\n\n";

std::cout << "First to land exactly on square 100 wins.\n";

bool human = (distribution (generator) % 2)==0 ? true : false;

std::cout << player (human) << " will go first.\n\n";

for (;;human=!human)

{

std::cout << (human ? "Your" : "My") << " turn:\n";

unsigned dice = distribution (generator);

std::cout << '\t' << player (human) << " rolled a " << dice << ".\n";

unsigned& pos = players [human?0:1];

if (pos+dice>100)

{

std::cout << '\t' << player (human) << " cannot move";

goto next_player;

}

pos+=dice;

std::cout << '\t' << player (human) << " landed on square " << pos;

for (auto snake : snakes)

{

if (snake.first==pos)

{

pos = snake.second;

std::cout << " with a snake; return to square " << pos;

goto next_player;

}

}

for (auto ladder : ladders)

{

if (ladder.first==pos)

{

pos = ladder.second;

std::cout << " with a ladder; climb to square " << pos;

goto next_player;

}

}

next_player:

std::cout << ".\n\n";

if (pos==100)

{

std::cout << player (human) << " won!\n";

break;

}

}

}

User Avatar

Wiki User

8y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the program for snake and ladder game in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Is there an answer book for the A plus program?

The A Plus Program is an initiative, not a test. So no, there is no answer book.


What is the only function all C plus plus programs must contain?

Every C plus plus program that is a main program must have the function 'main'.


A program c plus plus on automorphic numbers or not?

how to write a program that counts automorphic number from 1 to 999


Lint is a compiler b a interactive debugger c a cinterpreter d a tool for analysing c plus plus program?

d a tool for analysing c plus plus program


What is the basic requirements to develop a c plus plus program?

Text editor.

Related questions

What is a C plus plus game project program?

A C++ game project program could either be a game written in C++ or a C++ library that helps you develop a game.


Is c plus plus can be used in game programming?

yes! why not! this c program is used in ATM servicesand also in games.


What is the maximum size of else-if ladder in c plus plus?

There is no preset limit on the size of an else-if ladder in C++. It depends on compiler design and available memory.


How to restart c plus plus program?

Exit the program and relaunch it.


Can you program games with c plus plus?

Yes, you can program games with C++.


Is there an answer book for the A plus program?

The A Plus Program is an initiative, not a test. So no, there is no answer book.


How do you make lizard in Doodle God?

worm and snake.


Why does sometimes when you run a program it will execute the previous program instead the current open program in C plus plus?

Because you aren't careful enough.


What is the only function all C plus plus programs must contain?

Every C plus plus program that is a main program must have the function 'main'.


A program c plus plus on automorphic numbers or not?

how to write a program that counts automorphic number from 1 to 999


Lint is a compiler b a interactive debugger c a cinterpreter d a tool for analysing c plus plus program?

d a tool for analysing c plus plus program


What did the snake tell eve about the knowledge of good plus evil?

in the gghgouk i