Thread: C++ game?
View Single Post
Join Date: Mar 2004
Posts: 28
Reputation: Bleek is an unknown quantity at this point 
Solved Threads: 0
Bleek Bleek is offline Offline
Light Poster

Re: C++ game?

 
0
  #10
Apr 13th, 2004
i wrote it a while ago but anyways...
Dice Game

  1. /*
  2.   Name: Dice Game
  3.   Author: Bleek
  4.   Description: Game
  5.   Date: N/A
  6.   Copyright: N/A
  7. */
  8.  
  9. #include <iostream>
  10. #include <time.h>
  11. #include <cstdlib>
  12. #include <windows.h>
  13. using namespace std;
  14. void one();
  15. void two();
  16. void three();
  17. void four();
  18. void five();
  19. void six();
  20. //Declare Functions used
  21. int main()
  22. {
  23. short unsigned int score = 0;
  24. short unsigned int compScore = 0;
  25. short unsigned int num = 0;
  26. short unsigned int num2 = 0;
  27. short unsigned int compNum = 0;
  28. short unsigned int compNum2 = 0;
  29. short unsigned int sum = 0;
  30. short unsigned int compSum = 0;
  31. char letter;
  32. //Declare Variables
  33. srand(time(NULL));
  34. //Initialize random number generator
  35. system("title Joe's Dice Game");
  36. while (letter != 'q')
  37. {
  38. cout << "Your Score: " << score << endl;
  39. cout << "computer's Score: " << compScore << endl << endl;
  40. cout << "Press r to roll or q to quit: ";
  41. cin >> letter;
  42. num = 1 + rand() % (6 - 1 + 1);
  43. num2 = 1 + rand() % (6 - 1 + 1);
  44. compNum = 1 + rand() % (6 - 1 + 1);
  45. compNum2 = 1 + rand() % (6 - 1 + 1);
  46.  
  47. //Random numbers
  48.  
  49. sum = num + num2;
  50. compSum = compNum + compNum2;
  51.  
  52. //Calculate Sums
  53.  
  54. if (letter == 'q')
  55. break;
  56. if (letter != 'r')
  57. {
  58. system("cls");
  59. continue;
  60. }
  61.  
  62. switch (num)
  63. {
  64. case 1:
  65. one();
  66. break;
  67. case 2:
  68. two();
  69. break;
  70. case 3:
  71. three();
  72. break;
  73. case 4:
  74. four();
  75. break;
  76. case 5:
  77. five();
  78. break;
  79. case 6:
  80. six();
  81. break;
  82. default:
  83. cout << "Error...";
  84. break;
  85. } //end switch
  86.  
  87. switch (num2)
  88. {
  89. case 1:
  90. one();
  91. break;
  92. case 2:
  93. two();
  94. break;
  95. case 3:
  96. three();
  97. break;
  98. case 4:
  99. four();
  100. break;
  101. case 5:
  102. five();
  103. break;
  104. case 6:
  105. six();
  106. break;
  107. default:
  108. cout << "Error...";
  109. break;
  110. } //end switch
  111.  
  112. cout << endl << "Yours: " << num << ", " << num2 << endl;
  113. cout << "Computer's: " << compNum << ", " << compNum2 << "\n\n";
  114.  
  115. //Display dice and numbers
  116.  
  117. if (sum > compSum)
  118. {
  119. cout << "You won!!" << endl << endl;
  120. score++;
  121. }
  122. else
  123. {
  124. compScore++;
  125. cout << "you lost..." << endl << endl;
  126. }
  127.  
  128. //Calculate score
  129.  
  130. system("pause");
  131. system("cls");
  132.  
  133. if (score == 12)
  134. {
  135. MessageBox(0, "You Won!!!", "Results:", MB_ICONEXCLAMATION);
  136. break;
  137. }
  138. if (compScore == 12)
  139. {
  140. MessageBox(0, "You lost...", "Results:", MB_ICONEXCLAMATION);
  141. break;
  142. }
  143. }
  144. return 0;
  145. }
  146.  
  147. void one()
  148. {
  149. cout << " -----" << endl;
  150. cout << "| |" << endl;
  151. cout << "| O |" << endl;
  152. cout << "| |" << endl;
  153. cout << " -----" << endl;
  154. }
  155. void two()
  156. {
  157. cout << " -----" << endl;
  158. cout << "| O|" << endl;
  159. cout << "| |" << endl;
  160. cout << "|O |" << endl;
  161. cout << " -----" << endl;
  162. }
  163. void three()
  164. {
  165. cout << " -----" << endl;
  166. cout << "| O|" << endl;
  167. cout << "| O |" << endl;
  168. cout << "|O |" << endl;
  169. cout << " -----" << endl;
  170. }
  171. void four()
  172. {
  173. cout << " -----" << endl;
  174. cout << "|O O|" << endl;
  175. cout << "| |" << endl;
  176. cout << "|O O|" << endl;
  177. cout << " -----" << endl;
  178. }
  179. void five()
  180. {
  181. cout << " -----" << endl;
  182. cout << "|O O|" << endl;
  183. cout << "| O |" << endl;
  184. cout << "|O O|" << endl;
  185. cout << " -----" << endl;
  186. }
  187. void six()
  188. {
  189. cout << " -----" << endl;
  190. cout << "|O O|" << endl;
  191. cout << "|O O|" << endl;
  192. cout << "|O O|" << endl;
  193. cout << " -----" << endl;
  194. }
<< moderator edit: added [code][/code] tags >>
Reply With Quote