猜数小游戏C++

游戏简述:一共有5组人,每组有6个人吃点心,每组总共有三十个点心,先让玩家判断一共有几组,回答正确则继续,回答错误则继续猜,一共有5次机会。猜不对游戏结束,猜对的话会知晓每组都有谁,每个人分别都吃了多少个,还剩余多少个。(属于自己玩一玩,随便写的,比较仓促,目的是每天练一练,熟悉熟悉感觉,主要是想复习指针数组的使用。)

#include <iostream>
#include <string>

using namespace std;

int sum(int *arr, int Size); //定义的和函数
const int Size = 6;

//5个组每个人所吃的个数
int a1[Size] = {1, 2, 3, 4, 5, 6};
int a2[Size] = {2, 2, 1, 5, 4, 5};
int a3[Size] = {4, 2, 5, 4, 1, 4};
int a4[Size] = {3, 2, 2, 4, 5, 3};
int a5[Size] = {3, 6, 7, 5, 4, 5};

//5个组每组人员的姓名
string bb1[Size] = {"Bai", "Hei", "Shi", "Liu", "Li", "Xiao"};
string bb2[Size] = {"Ba", "He", "Sh", "Li", "Le", "Xia"};
string bb3[Size] = {"B", "H", "S", "L", "Lee", "Xi"};
string bb4[Size] = {"Beai", "Heei", "Shie", "Lieu", "Lei", "X"};
string bb5[Size] = {"Bao", "Heo", "Shio", "Lio", "Lo", "Xie"};

//主函数
int main()
{
    int s1 = 0;
    int s2 = 0;
    int s3 = 0;
    int s4 = 0;
    int s5 = 0;
    int n;     //n为小组个数
    int r = 5; //剩余次数
    cout << "输入小组个数: " << endl;
    for (int m = 0; m < 5; m++)
    {
        r -= 1;
        cin >> n;
        if (n != 5)
        {
            cout << "您输错了,剩余" << r << "次机会" << endl;
            continue;
        }
        else
        {
            s1 = sum(a1, Size);
            s2 = sum(a2, Size);
            s3 = sum(a3, Size);
            s4 = sum(a4, Size);
            s5 = sum(a5, Size);
            int s[5] = {s1, s2, s3, s4, s5};
            for (int i = 0; i < 5; i++)
            {
                if (s[i] < 30)
                {
                    cout << "点心没有被吃光,#" << i + 1 << "组还剩余" << 30 - s[i] << "个" << endl;
                }
                else
                    cout << "#" << i + 1 << "组点心被吃光了,好遗憾!" << endl;
            }
        }
        break;
    }
    cout << "大家分别吃的个数列表如下: " << endl;

    cout << "#1 " << bb1[0] << ":" << a1[0] << ", ";
    for (int j = 1; j < 6; j++)
        cout << bb1[j] << ":" << a1[j] << ", ";
    cout << endl;

    cout << "#2 " << bb2[0] << ":" << a2[0] << ", ";
    for (int j = 1; j < 6; j++)
        cout << bb2[j] << ":" << a2[j] << ", ";
    cout << endl;

    cout << "#3 " << bb3[0] << ":" << a3[0] << ", ";
    for (int j = 1; j < 6; j++)
        cout << bb3[j] << ":" << a3[j] << ", ";
    cout << endl;

    cout << "#4 " << bb4[0] << ":" << a4[0] << ", ";
    for (int j = 1; j < 6; j++)
        cout << bb4[j] << ":" << a4[j] << ", ";
    cout << endl;

    cout << "#5 " << bb5[0] << ":" << a5[0] << ", ";
    for (int j = 1; j < 6; j++)
        cout << bb5[j] << ":" << a5[j] << ", ";
    cout << endl;
}

//各小组吃点心数总和函数原型
int sum(int *arr, int size)
{
    int total = 0;
    for (int i = 0; i < size; i++)
    {
        total += arr[i];
    }
    return total;
}

 部分结果如下:

 

 

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>