1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
#include <iostream.h> #include <conio.h> #include <stdlib.h> #include <iomanip.h> class SaanpSidhi { private: char Player[20]; int Position; void Check_Nega_Plus(); // Checks if current position have Snake or ladder public: SaanpSidhi() { // initializes the Player position Position=0; } void roll(); // to roll the dice void Get_Player() { // to get the player name cout << "Enter Your Name : "; cin >> Player; } void Player_Name() { // display player name cout << Player; } void Display_Position() { // display player position cout<< Position; } int Return_Position() { // returns the current position return Position; } void ptar(SaanpSidhi,SaanpSidhi); // prints current position on grid }; void SaanpSidhi::Check_Nega_Plus() { switch (Position) { case 5: cout <<"You have gone up to 35.\n "; Position=35; break; case 23: cout<<"You have gone up to 44.\n "; Position=44; break; case 41: cout <<"You have gone up to 63.\n "; Position=63; break; case 66: cout <<"You have gone up to 85.\n "; Position=85; break; case 72: cout <<"You have gone up to 91.\n "; Position=91; break; case 24: cout <<"You have gone down to 7.\n "; // start of nega cases Position=7; break; case 50: cout <<"You have gone down to 10.\n "; Position=10; break; case 88: cout <<"You have gone down to 39.\n "; Position=39; break; case 96: cout <<"You have gone down to 14.\n "; Position=14; break; case 98: cout <<"You have gone down to 79.\n "; Position=79; break; default: ""; } } void SaanpSidhi::roll() { int num; char r; do { // now two do while to roll dice cout << Player; cout << " Press r to roll the dice: "; cin >> r; } while(r!='r'); r='x'; do { num=random(6)+1; cout << "You have Got "<<num<<"\n"; if(num==6) cout<< "You have Got 6 So Rolled Again\n"; Position=Position+num; } while(num==6); Check_Nega_Plus(); } void SaanpSidhi::ptar(SaanpSidhi p1,SaanpSidhi p2){ const int width= 6; const char seprator=' '; int i,j,k=1; for (i=1;i<=10;i++) { for (j=1;j<=10;j++) { if(k==p1.Return_Position()) { cout << setw(width)<<setfill(seprator);p1.Player_Name(); } else if(k==p2.Return_Position()) { cout <<setw(width)<<setfill(seprator); p2.Player_Name() ; } else { cout <<setw(width)<<setfill(seprator)<< k; } k++; } cout << endl; } // both loop ended } void main() { char r; clrscr(); SaanpSidhi p1,p2,p3; p1.Get_Player(); p2.Get_Player(); p3.ptar(p1,p2); do { // loop till anyone reaches 100 p1.roll(); p3.ptar(p1,p2); p1.Player_Name(); p1.Display_Position(); cout <<"\n"; // player 2 code starts p2.roll(); cout <<"\n"; p3.ptar(p1,p2); p2.Player_Name(); cout <<"\n"; p2.Display_Position(); cout <<"\n"; } while(p1.Return_Position()<100 && p2.Return_Position()<100); //first do while ends when any player reaches 100 if(p1.Return_Position()>=100) { p1.Player_Name(); cout << " is winner \n"; } else { p2.Player_Name(); cout << " is winner \n"; } getch(); } |
Java
Java Applet Of Moving Plane
“applet” class [crayon-5a2bbaa5de9a9535348389/] HTML File [crayon-5a2bbaa5de9b4599830686/] How to Run After compilation you can use “appletviewer” to run your applet. appletviewer xyz.html