Docsity

C++ Program for Snake and Ladders Game, Lab Reports of Data Structures and Algorithms

Toggle sidebar
bg1
string player1,player2;
cout<<"enter the name of player 1"<<endl;
cin>>player1;
cout<<"enter the name of the player 2 "<<endl;
cin>>player2;
cout<<"the position of player1 and player2 is 0 initially"<<endl;
while(p1<=100 && p2<=100) // loop until anyone one player reach the goal
{
cout<<player1<<" It is your turn press any key to play "<<endl;
cin.get();
randomfuncton(p1); // calling random function to generate number i-e rolling of dice
cout<<"your score is "<<p1<<endl;
cout<<player2<<" it is your turn press any key to play"<<endl;
cin.get();
randomfuncton(p2);
cout<<"your score is "<<p2<<endl;
}
if(p1>p2) // win loose conditions
{
cout<<player1<<" is the winner"<<endl;
}
if(p2>p1)
{
cout<<player2<<" is the winner"<<endl;
}
pf3
pf4
pf5

Partial preview of the text

Download C++ Program for Snake and Ladders Game and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

string player1,player2; cout<<"enter the name of player 1"<<endl; cin>>player1; cout<<"enter the name of the player 2 "<<endl; cin>>player2; cout<<"the position of player1 and player2 is 0 initially"<<endl; while(p1<=100 && p2<=100) // loop until anyone one player reach the goal { cout<<player1<<" It is your turn press any key to play "<<endl; cin.get(); randomfuncton(p1); // calling random function to generate number i-e rolling of dice cout<<"your score is "<<p1<<endl; cout<<player2<<" it is your turn press any key to play"<<endl; cin.get(); randomfuncton(p2); cout<<"your score is "<<p2<<endl; } if(p1>p2) // win loose conditions { cout<<player1<<" is the winner"<<endl; } if(p2>p1) { cout<<player2<<" is the winner"<<endl; }

if(p1==p2) { cout<<"match is draw"<<endl; } } int randomfuncton(int &score) //random function { int random; random=rand()%MAX_NUM; // generating random numbers less then six as MAX_NUM= cout<<"your number is "<<random<<endl; score=random+score; //updating existing score by adding new one switch(score) // switch statements for snake and ladders { case 98 :score=28; cout<<"you ran into a snake!"<<endl; break; case 95 :score=24; cout<<"you ran into a snake!"<<endl; break; case 92 :score=51; cout<<"you ran into a snake!"<<endl; break; case 83 :score=19; cout<<"you ran into a snake!"<<endl; break;

cout<<"you ran into a snake!"<<endl; break; case 44 :score=22; cout<<"you ran into a snake!"<<endl; break; case 8 :score=26; cout<<"luckyyy boy u got ladder"<<endl; break; case 21 :score=82; cout<<"luckyyy boy u got ladder"<<endl; break; case 43 :score=77; cout<<"luckyyy boy u got ladder"<<endl; break; case 50 :score=91; cout<<"luckyyy boy u got ladder"<<endl; break; case 54 :score=93; cout<<"luckyyy boy u got ladder"<<endl; break; case 62 :score=96; cout<<"luckyyy boy u got ladder"<<endl;

break; case 66 :score=87; cout<<"luckyyy boy u got ladder"<<endl; break; case 80 :score=100; cout<<"luckyyy boy u got ladder"<<endl; } return score; }