Guest User

Untitled

a guest
Jul 15th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. //Darts Exercise 1, a simple game of 301
  2.  
  3. #include <iostream>
  4. #include <time.h>
  5. #include <ctime>
  6. #include <conio.h>
  7. #include <windows.h>
  8. #include <time.h>
  9. #include <ctype.h>
  10. using namespace std;
  11.  
  12. int sidscore=301;
  13. int joescore=301;
  14. int joeselfishmeter=0;
  15. int sidselfishmeter=0;
  16. int turn_number=1;
  17.  
  18. //Define States & Inputs
  19.  
  20. enum current_state {sidthrew, joethrew, neutral};
  21. enum current_input {sidthrow, joethrow, No_input};
  22. current_state state = neutral;
  23.  
  24. //Initialise Display
  25. void Init_Graphics(void);
  26. inline void Set_Color(int fcolor, int bcolor);
  27. inline void Draw_String(int x,int y, char *string);
  28. void run_fsm();
  29. void display_welcome_screen();
  30. void display_neutral();
  31. void display_sidthrew();
  32. void display_joethrew();
  33.  
  34. void main(){
  35. run_fsm();
  36. }
  37.  
  38. // Local declarations
  39. void run_fsm(){
  40. state = neutral;
  41. current_input input = No_input;
  42.  
  43. display_welcome_screen();
  44.  
  45. while (1){
  46.  
  47.  
  48. switch (state){
  49. case neutral: display_neutral(); break;
  50. case sidthrew: display_sidthrew(); break;
  51. case joethrew: display_joethrew(); break;
  52. }
  53.  
  54. Sleep(800);
  55.  
  56. if(joeselfishmeter < 1){
  57. input = joethrow;
  58. }
  59. if(sidselfishmeter < 1){
  60. input = sidthrow;
  61. }
  62. if(input == sidthrow || input == joethrow){
  63.  
  64. switch (state) {
  65. case neutral: if(input == sidthrow){
  66. state = sidthrew;
  67. }
  68. if(input == joethrow){
  69. state = joethrew;
  70. }
  71. break;
  72. } // end switch
  73. }// end if
  74. } // end while
  75. } // end run_fsm()
  76.  
  77.  
  78. void display_welcome_screen(){
  79. cout << "Joe vs Sid, Task 1" << endl << endl;
  80. Sleep(1200);
  81. cout << "301 Darts Simulation" << endl << endl;
  82. Sleep(1500);
  83. cout << "Press return to start the simulation" << endl << endl;
  84. cin.get();
  85. }
  86.  
  87. void display_neutral(){
  88. if ((sidscore * joescore)>0){
  89. }
  90. if (sidscore == 0){
  91. cout << " Congratulations, Sid has won!" << endl;
  92. joeselfishmeter=1;
  93. sidselfishmeter=1;
  94. }
  95. if (joescore ==0){
  96. cout << " Congratulations, Joe has won!" << endl;
  97. joeselfishmeter=1;
  98. sidselfishmeter=1;
  99. }
  100.  
  101. };
  102.  
  103. void display_sidthrew(){
  104. srand ( time(NULL) );
  105. if(sidselfishmeter<1){
  106. if(sidscore >51){
  107. if((rand()%100) < 81){
  108. cout << "Sid Aims For The Darts Board" << endl;
  109. sidscore=sidscore-20;
  110. cout << "Turn Number: " << turn_number << " Direct Hit, Nice One! Sid's Score is now: " << sidscore << " Now It's Joe's Turn" << endl;
  111. sidselfishmeter=1;
  112. joeselfishmeter=0;
  113. }
  114. if((rand()%100) > 80){
  115. cout << "Sid Aims For The Darts Board" << endl;
  116. sidscore=sidscore-(rand()%20);
  117. cout << "Turn Number: " << turn_number << " Missed the target, but you still got a nice hit. Sid's score is now: " << sidscore << " Now It's Joe's Turn" << endl;
  118. sidselfishmeter=1;
  119. joeselfishmeter=0;
  120. }
  121. }
  122. if(sidscore <51){
  123. if((rand()%100) < 51){
  124. cout << "Turn Number: " << turn_number << " Sid Aims For The Bullseye!" << endl;
  125. cout << "Congratulations, Sid has won!" << endl;
  126. joeselfishmeter=1;
  127. sidselfishmeter=1;
  128. }
  129. }
  130. }
  131. state = neutral;
  132. display_neutral();
  133. turn_number = turn_number+1;
  134. };
  135. void display_joethrew(){
  136. srand ( time(NULL) );
  137. if(joeselfishmeter<1){
  138. if(joescore >51){
  139. if((rand()%100) < 81){
  140. cout << "Joe Aims For The Darts Board" << endl;
  141. joescore=joescore-20;
  142. cout << "Turn Number: " << turn_number << " Direct Hit, Nice One! Joe's Score is now: " << joescore << " Now It's Sid's Turn" << endl;
  143. sidselfishmeter=0;
  144. joeselfishmeter=1;
  145. }
  146. if((rand()%100) > 80){
  147. cout << "Joe Aims For The Darts Board" << endl;
  148. joescore=joescore-(rand()%20);
  149. cout << "Turn Number: " << turn_number << " Missed the Target, but still a nice hit. Joe's score is now " << joescore << " Now It's Sid's Turn" << endl;
  150. sidselfishmeter=0;
  151. joeselfishmeter=1;
  152. }
  153. }
  154. if(joescore <51){
  155. if((rand()%100) < 51){
  156. cout << "Joe Aims For The Bullseye!" << endl;
  157. cout << "Turn Number: " << turn_number << " Congratulations, Joe has won!" << endl;
  158. joeselfishmeter=1;
  159. sidselfishmeter=1;
  160. }
  161. }
  162. }
  163. state = neutral;
  164. display_neutral();
  165. };
Add Comment
Please, Sign In to add comment