Dev-C++自带小游戏Jackpot改进

1.项目简介

这是Dev-C++自带的小游戏Jackpot,本质上是一个靠运气和一点点逻辑推理的猜字游戏

2.源代码

打开Dev-C++,点击左上方的“文件”,再点击“新建”,接着点击“项目”,出现如下页面:

 

然后点击“Console”,选择“Jackpot”,点击“确定”,就会出现一个main程序

详细代码如下:

#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

void Start();
void GetResults();

int i, j, life, maxrand;
char c;

void Start() {
    i = 0;
    j = 0;
    life = 0;
    maxrand = 6;
    
    cout << "Select difficulty mode:\n"; //选择难度
    cout << "1 : Easy (0-15)\n";
    cout << "2 : Medium (0-30)\n";
    cout << "3 : Difficult (0-50)\n";
    cout << "or type another key to quit\n";
    c = 30;

    cin >> c;                   // 判断游戏者的报数
    cout << "\n";

    switch (c) {
        case '1':
            maxrand = 15;  // 数值结果在报数和最大值中间
            break;
        case '2':
            maxrand = 30;
            break;
        case '3':
            maxrand = 50;
            break;
        default:
            exit(0);
        break;
    }

    life = 5;         // 游戏的生命数
    srand((unsigned)time(NULL)); // 初始化函数
    j = rand() % maxrand;  //得到数值结果在报数和最大值中间
    
    GetResults();
}

void GetResults() {
    if (life <= 0) { // 如果生命数为0,则失败
        cout << "You lose !\n\n";
        Start();
    }

    cout << "Type a number: \n";
    cin >> i;
    
    if((i>maxrand) || (i<0)) { // 如果报数不对,则继续
        cout << "Error: number not between 0 and \n" << maxrand;
        GetResults();
    }

    if(i == j) {
        cout << "YOU WIN!\n\n"; // 找到结果
        Start();
    } else if(i>j) {
        cout << "Too BIG\n";
        life = life - 1;
        cout << "Lives remaining: " << life << "\n\n";
        GetResults();
    } else if(i<j) {
        cout << "Too SMALL\n";
        life = life - 1;
        cout << "Lives remaining: " << life << "\n\n";
        GetResults();
    }
}

int main() {
    cout << "** Jackpot game **\n";
    cout << "The goal of this game is to guess a number.\n";
    cout << "Jackpot will tell you if the number is too big or too\n";
    cout << "small compared to the secret number to find.\n\n";
    Start();
    return 0;
}

3.运行体验

 

 

 经过运行测试,发现此游戏难度较为简单,故思量增加一组数值范围,稍增挑战性

4.二次开发

添加代码如下:

 

仅添加了一组数值范围,以增加挑战性

5.运行测试

 

 增添数值组合后,由于范围的增大,游戏挑战性显著增加,趣味性增强

6.感受总结

此次项目改进,让我感受到了二次开发的不易,首先需要对源代码进行充分的了解分析,知晓每句代码的作用,然后经过运行体验发现其中的不足之处,再去进行增删改查,反复测试,最终呈现出二次开发的成果,虽只是增添几句简单的代码,但也需要反复地推敲思考,实来之不易。

 

posted @ 2023-03-07 11:37  苏烟台  阅读(855)  评论(0)    收藏  举报