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 |
#include < iostream.h > // for standed input output #include < conio.h > // for console input output #include < stdlib.h > // for rand function #include < time.h > // for time.(to get the seed of rand) #include < string.h > // for string manupulation #include < iomanip.h > // for data manupulator(formatter) class SaanpSidhi { public: int Position; private: int ConditionNegaOrPlus(int current, int newrand); // condition for negation or plus public: void ptar(int Pos1, char User1[], int Pos2, char User2[]); // print the place of player and whole grid int RandomNum(int current); // adds random number }; int SaanpSidhi::ConditionNegaOrPlus(int current, int newrand) { // logic of condition for negation or plus int newval; newval = current + newrand; cout << " You have got " << newrand << endl; switch (newval) { case 5: cout << "You have gone up to 35. "; return 35; break; case 23: cout << "You have gone up to 44. "; return 44; break; case 41: cout << "You have gone up to 63. "; return 63; break; case 66: cout << "You have gone up to 85. "; return 85; break; case 72: cout << "You have gone up to 91. "; return 91; break; case 24: cout << "You have gone down to 7. "; // start of nega cases return 7; break; case 50: cout << "You have gone down to 10. "; return 10; break; case 88: cout << "You have gone down to 39. "; return 39; break; case 96: cout << "You have gone down to 14. "; return 14; break; case 98: cout << "You have gone down to 79. "; return 79; break; default: return newval; } } int SaanpSidhi::RandomNum(int current) { // logic of random num and call private ConditionNegaOrPlus long int seconds; time( & seconds); srand(seconds); return ConditionNegaOrPlus(current, rand() % 6 + 1); // rand argument modules with 6 to get remainder less than 6, and adding 1 to get more than 0 and equal to 6 } void SaanpSidhi::ptar(int Pos1, char User1[], int Pos2, char User2[]) { // logic of ptar(print array)args=Position of p1 and p2 - const int width = 8; // and Players name const char seprator = ' '; int i, j, k = 1; for (i = 1; i <= 10; i++) { for (j = 1; j <= 10; j++) { if (k == Pos1) { cout << setw(width) << setfill(seprator) << User1; // use of formatter setw,setfill defined in iomanip.h } else if (k == Pos2) { cout << setw(width) << setfill(seprator) << User2; } else { cout << setw(width) << setfill(seprator) << k; } k++; } cout << endl; } // both loop ended } void main() { // start of main int max = 100; // upto which a player should go to win char player1[20], player2[20], r; // array for name of players and r for rolling the dice clrscr(); cout << "Enter the name of player 1: "; // Player 1 name cin >> player1; cout << "Enter the name of player 2: "; // Player 2 name cin >> player2; SaanpSidhi p1, p2; // creation of two objects for two players p1 and p2 p1.Position = 0; // set position to 0 for both players p2.Position = 0; p1.ptar(p1.Position, player1, p2.Position, player2); // print initial grid do { cout << "*************** " << player1 << " Turn ********************" << endl; //start of do while until one player reach 100 cout << player1 << " Press r to roll" << endl; // get the response from player 1 for rolling the dice cin >> r; if (r == 'r' || r == 'R') { // condition to check if player pressed r or R p1.Position = p1.RandomNum(p1.Position); // call the function RandomNum with args = Player 1 Position p1.ptar(p1.Position, player1, p2.Position, player2); // Printing the grid after player 1 has rolled the dice if (p1.Position >= 100) { // check if player reaches 100 or more if yes break the loop and declare him winner cout << player1 << " is winner"; break; } } cout << endl << player1 << " is now on " << p1.Position << endl; // printing the Player 1 Position // end of player 1 play cout << "---------------- " << player2 << " Turn ---------------" << endl; // Now starting player 2 code same as above cout << player2 << " Press r to roll" << endl; cin >> r; if (r == 'r' || r == 'R') { p2.Position = p2.RandomNum(p2.Position); p1.ptar(p1.Position, player1, p2.Position, player2); if (p2.Position >= 100) { cout << player2 << " is winner"; break; } } cout << endl << player2 << " is now on " << p2.Position << endl; //end of Player 2 play cout << "*************** Score ********************" << endl; // Print the both player position after each have rolled equally cout << player1 << " : " << p1.Position << endl; cout << player2 << " : " << p2.Position << endl; cout << "****************************************************" << endl; } while (p1.Position <= max || p2.Position <= max); // while statement to check if any one of them is below 100 getch(); } |
Java
Java Applet Of Moving Plane
“applet” class [crayon-5a2bbaa6d666f133307387/] HTML File [crayon-5a2bbaa6d667a415225892/] How to Run After compilation you can use “appletviewer” to run your applet. appletviewer xyz.html