Game Programming




 
game programming
Game Programming :: Programming :: General Programming :: Code Examples :: [C++]Guess the number game V2 - View Topic
Topic Rating: *****
Printable View
Andy
Administrator
*****

Posts: 40
Status: Offline
Gender: Male
Joined:  
pm

Reputation: 0%

[C++]Guess the number game V2 (11th Jan 08 at 1:53am UTC)
So here is a new version of the guess the number i just made as you can see i must have been board to make something this simple so here it is it lets you choose from a range of numbers starting from

0 to 10 and up to 0 to 50

this is a simple little game that makes use of the srand and rand() function witch i have not used for a while

iv also used functions for the guess as to make it more clear for users to read and understand

you may use the source code as you please but if you use the code or any sections of the code it would be nice if you could give credit from were you got it

if you have any question or comments then please post

Code:
 
  1. #include <iostream>
  2. #include <ctime>
  3.  
  4. void start();
  5. void results();
  6.  
  7. int i, j, pLife, MAX;
  8. char c;
  9.  
  10.  
  11.  
  12. void start()
  13. {
  14. i = 0;
  15. j = 0;
  16. MAX = 6;
  17. pLife = 0;
  18.  
  19. std::cout << "================================ \n";
  20. std::cout << "GUESS NUMBER GAME MADE BY Andy \n";
  21. std::cout << "================================ \n";
  22. std::cout << "1. 0 - 10 \n";
  23. std::cout << "2. 0 - 20 \n";
  24. std::cout << "3. 0 - 50 \n";
  25. std::cout << "================================ \n";
  26.  
  27. c = 30;
  28.  
  29. std::cin >> c;
  30.  
  31. std::cout << "\n";
  32.  
  33. switch (c)
  34. {
  35. case '1': MAX = 10;
  36. break;
  37. case '2': MAX = 20;
  38. break;
  39. case '3': MAX = 50;
  40. break;
  41. default: exit(0);
  42. break;
  43. }
  44.  
  45. system("cls");
  46.  
  47. pLife = 5; // number of lives
  48. srand((unsigned)time(NULL));//int rand() function
  49. j = rand() % MAX;
  50.  
  51.  
  52. results();
  53.  
  54.  
  55. }
  56.  
  57.  
  58. void results()
  59. {
  60. if(pLife <= 0)
  61. {
  62. std::cout << "You Lose \n";
  63. system("cls");
  64. start();
  65. }
  66.  
  67. std::cout << "Type a new number:";
  68. std::cin >> i;
  69.  
  70. if((i > MAX) || (1 < 0))
  71. {
  72. std::cout << "Error : Number not between 0 and \n" << MAX;
  73. results();
  74.  
  75. }
  76. if(i == j)
  77. {
  78. std::cout << "WELL DONE YOU WIN \n";
  79. system("cls");
  80. start();
  81.  
  82. }
  83. else if(i > j)
  84. {
  85. std::cout << "Too BIG\n";
  86. pLife = pLife - 1; // -1 to the user's "life"
  87. std::cout << "Number of remaining life: " << pLife << "\n\n";
  88. results();
  89.  
  90. }
  91. else if(i < j)
  92. {
  93.  
  94. std::cout << "Too SMALL\n";
  95. pLife = pLife - 1;
  96. std::cout << "Number of remaining life:\n" << pLife << "\n\n";
  97. results();
  98. }
  99.  
  100. }
  101.  
  102. int main()
  103. {
  104. start();
  105.  
  106. std::cin.ignore();
  107. std::cin.get();
  108.  
  109. return 0;
  110.  
  111. }
  112.  
  113.  
 
 Printable View

All times are GMT+0 :: The current time is 9:14pm
Page generated in 0.2537 seconds
Purchase Ad-Free Credits
This Forum is Powered By vForums (v2.4)
Create a Forum for Free | Find Forums