当前位置:   article > 正文

Dev c++自带小游戏_devc++游戏代码

devc++游戏代码

先点击左上方的“文件”,再点击“新建”,接着点击“项目”,就会有下面的样子:

再选Console,就会出现下面的样子:

 接着选Jackpot,再点确定,C++就会打开一个mian,这时你运行就是了。
游戏意思:输入一个数字,杰克会告诉你太大了或太小了,直到猜中或次数用完。
懒的人可以复制下面的代码:

  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. using namespace std;
  5. void Start();
  6. void GetResults();
  7. int i, j, life, maxrand;
  8. char c;
  9. void Start() {
  10. i = 0;
  11. j = 0;
  12. life = 0;
  13. maxrand = 6;
  14. cout << "Select difficulty mode:\n"; // the user has to select a difficutly level
  15. cout << "1 : Easy (0-15)\n";
  16. cout << "2 : Medium (0-30)\n";
  17. cout << "3 : Difficult (0-50)\n";
  18. cout << "or type another key to quit\n";
  19. c = 30;
  20. cin >> c; // read the user's choice
  21. cout << "\n";
  22. switch (c) {
  23. case '1':
  24. maxrand = 15; // the random number will be between 0 and maxrand
  25. break;
  26. case '2':
  27. maxrand = 30;
  28. break;
  29. case '3':
  30. maxrand = 50;
  31. break;
  32. default:
  33. exit(0);
  34. break;
  35. }
  36. life = 5; // number of lifes of the player
  37. srand((unsigned)time(NULL)); // init Rand() function
  38. j = rand() % maxrand; // j get a random value between 0 and maxrand
  39. GetResults();
  40. }
  41. void GetResults() {
  42. if (life <= 0) { // if player has no more life then he loses
  43. cout << "You lose !\n\n";
  44. Start();
  45. }
  46. cout << "Type a number: \n";
  47. cin >> i;
  48. if((i>maxrand) || (i<0)) { // if the user number isn't correct, restart
  49. cout << "Error: number not between 0 and \n" << maxrand;
  50. GetResults();
  51. }
  52. if(i == j) {
  53. cout << "YOU WIN!\n\n"; // the user found the secret number
  54. Start();
  55. } else if(i>j) {
  56. cout << "Too BIG\n";
  57. life = life - 1;
  58. cout << "Lives remaining: " << life << "\n\n";
  59. GetResults();
  60. } else if(i<j) {
  61. cout << "Too SMALL\n";
  62. life = life - 1;
  63. cout << "Lives remaining: " << life << "\n\n";
  64. GetResults();
  65. }
  66. }
  67. int main() {
  68. cout << "** Jackpot game **\n";
  69. cout << "The goal of this game is to guess a number.\n";
  70. cout << "Jackpot will tell you if the number is too big or too\n";
  71. cout << "small compared to the secret number to find.\n\n";
  72. Start();
  73. return 0;
  74. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/638377
推荐阅读
相关标签
  

闽ICP备14008679号