石头剪刀布

Posted BBJ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了石头剪刀布相关的知识,希望对你有一定的参考价值。

#pragma once
#pragma once
#include <string>
using namespace std;

class gamers
{
public:
    gamers();
    gamers(char c);
    ~gamers();
    bool operator == (const gamers& g)
    {
        return type == g.type;
    }
    string name;
    int type;//出拳类型
};

gamers::gamers()
{

}

gamers::gamers(char c)
{
    name = c;
    type = -1;
}
gamers::~gamers()
{
}
.h
// game3.cpp : 定义控制台应用程序的入口点。
//


#include <iostream>
#include <vector>
#include<string>
#include<time.h>
#include"gamers.h"
using namespace std;

string decode(int type)
{/*
 分别用0,1,2来代表石头、剪刀、布。
 该函数用来输出时候解码
 */
    switch (type)
    {
    case 0:
        return "石头";
        break;
    case 1:
        return "剪刀";
        break;
    case 2:
        return "";
        break;
    default:
        cout << "输入有误" << endl;
        break;
    }

}
int whichIsZero(int a, int b, int c)
{/*每一轮结束之后,统计出拳的种类过程中缺少的那一种
 */
    if (a == 0)
        return 0;
    else if (b == 0)
        return 1;
    else if (c == 0)
        return 2;
    else
        cout << "统计有误" << endl;
}

void game(vector<gamers>& LeftPeople, int NumOfPerson)
{
    srand((int)time(NULL));
    for (int i = 0; i != NumOfPerson; i++)
    {//得到上一轮获胜者出拳类型
        int temp = rand() % 3;
        LeftPeople[i].type = temp;
        cout << "选手" << LeftPeople[i].name << "出:" << decode(temp) << endl;
    }
    int arr[3] = { 0,0,0 };
    for (int i = 0; i < (int)LeftPeople.size(); i++)
    {
        arr[LeftPeople[i].type]++;
    }
    int NumOfRock = arr[0];
    int NumOfScissors = arr[1];
    int NumOfPaper = arr[2];

    int NumOfType = 0;
    if (NumOfRock != 0)NumOfType++;
    if (NumOfScissors != 0)NumOfType++;
    if (NumOfPaper != 0)NumOfType++;

    vector<gamers> next_round_people;
    if (NumOfType == 2)
    {
        int zero = whichIsZero(NumOfRock, NumOfScissors, NumOfPaper);
        switch (zero)
        {
        case 0:
            next_round_people.clear();
            for (int i = 0; i < NumOfPerson; i++)
            {
                if (LeftPeople[i].type == 1)
                    next_round_people.push_back(LeftPeople[i]);
            }
            NumOfPerson -= NumOfPaper;
            if (NumOfPerson == 1)
            {//此处处理特殊情况:“剪刀,布,布”时,NumOfPerson变为1,输赢可定,不用调用game(1);下同
                cout << "最后获胜者" << LeftPeople[0].name << "出:剪刀" << endl;
                return;
            }
            else
            {
                cout << "这一轮没有石头,剪刀的获胜,由" << NumOfPerson << "个胜利者接着进行游戏......" << endl;
                game(next_round_people, NumOfPerson);
            }
            break;
        case 1:
            next_round_people.clear();
            for (int i = 0; i < NumOfPerson; i++)
            {
                if (LeftPeople[i].type == 2)
                    next_round_people.push_back(LeftPeople[i]);
            }
            NumOfPerson -= NumOfRock;
            if (NumOfPerson == 1)
            {
                cout << "最后获胜者" << next_round_people[0].name << "出:布" << endl;
                return;
            }
            else
            {
                cout << "这一轮没有剪刀,布的获胜,由" << NumOfPerson << "个胜利者接着进行游戏......" << endl;
                game(next_round_people, NumOfPerson);
            }
            break;
        case 2:
            next_round_people.clear();
            for (int i = 0; i < NumOfPerson; i++)
            {
                if (LeftPeople[i].type == 0)
                    next_round_people.push_back(LeftPeople[i]);
            }
            
            NumOfPerson -= NumOfScissors;
            if (NumOfPerson == 1)
            {
                cout << "最后获胜者" << next_round_people[0].name << "出:石头" << endl;
                return;
            }
            else
            {
                cout << "这一轮没有布,石头的获胜,由" << NumOfPerson << "个胜利者接着进行游戏......" << endl;
                game(next_round_people, NumOfPerson);
            }
            break;
        default:
            break;
        }
    }
    else
    {
        cout << "这一轮没有获胜方,继续下一轮......" << endl;
        game(LeftPeople, NumOfPerson);
    }
}

int main()
{
    /*
    0 石头
    1 剪刀
    2 布

    思路:
    递归实现,每次出拳种类为2的时候判定输赢,由赢的人参加下一轮比赛,
    直到确定最后一人的输赢。出拳种类为3或者为1的时候继续下一轮比赛;

    每次根据上一轮获胜人数产生0到2的随机数(分别代表石头,剪刀,布),
    保存到vector中,利用大小为3的数组arr来统计每轮出拳情况。
    */

    int NumOfPerson = 5;
    if (NumOfPerson == 1)
    {
        cout << "游戏需要1个以上的参与者!" << endl;
        return 0;
    }
    vector<gamers> LeftPeople;
    for (int i = 0; i < NumOfPerson; i++)
    {
        LeftPeople.push_back(gamers(\'A\' + i));
    }
    game(LeftPeople, NumOfPerson);
    system("pause");
    return 0;
}
.cpp

 

以上是关于石头剪刀布的主要内容,如果未能解决你的问题,请参考以下文章

编写石头、纸、剪刀游戏

oj115

《石头、剪刀、布》幼儿园教案

剪刀石头布游戏

百练 石头剪刀布

[WC2007] 剪刀石头布