就基于之前的石头剪刀布的游戏,多增加一些元素

首先是Jack的 class 这个人只出 paper
Tom的 class 这个人 按照顺序出 paper,rock,scissors
Roy 的class 这个人随机出
new computer player 的class 随机选上面的三种player

然后是tournament class 就是创建8个player,依次为player1-8进行 两两比赛,每次比5场,赢家进入下一轮,
要是结果是平局的话,就按照创建的顺序,序数小的晋级,例如 player1 和player4平局,则player1晋级。
每晋级一轮,则清空记忆,(例子:Tom 进入下一轮的时候又会从paper开始)

sample input:
Jack Tom Roy Tom Roy Jack Tom Tom
output:
Tom

c++

1个回答

// Q753922.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include "stdio.h"
#include <time.h>
#include <stdlib.h>

char * Jackname = "Jack";
char * Tomname = "Tom";
char * Royname = "Roy";

class Behavior
{
public:
    virtual char Play() = 0;
    virtual void Reset() = 0;
    virtual char * Name() = 0;
};

class Jack : public Behavior
{
public:
    char Play()
    {
        return 'P';
    }
    void Reset() { }
    char * Name() { return Jackname; }
};

class Tom : public Behavior
{
private:
    int state;
public:
    Tom()
    {
        state = 0;
    }
    char Play()
    {
        if (state == 3) state = 0;
        if (state++ == 0)
            return 'P';
        if (state == 2)
            return 'R';
        if (state == 3)
            return 'S';
    }
    void Reset() { state = 0; }
    char * Name() { return Tomname; }
};

class Roy : public Behavior
{
public:
    char Play()
    {
        srand(time(NULL));
        int state = rand() % 3;
        if (state == 0)
            return 'P';
        if (state == 1)
            return 'R';
        if (state == 2)
            return 'S';
    }
    void Reset() { }
    char * Name() { return Royname; }
};

class Player
{
private:
    Behavior *b;
public:
    Player()
    {
        int sel = rand() % 3;
        switch (sel)
        {
        case 0: b = new Jack(); break;
        case 1: b = new Tom(); break;
        case 2: b = new Roy(); break;
        }
    }
    char Play()
    {
        return b->Play();
    }
    void Reset() { b->Reset(); }
    void PrnName()
    {
        printf("%s", b->Name());
    }
};



class Referee
{
public:
    char Judge(char m, char p)
    {
        if (m == p) return 'T';
        if ((m == 'S' && p == 'P') ||
            (m == 'P' && p == 'R') ||
            (m == 'R' && p == 'S'))
            return 'W';
        else
            return 'L';
    }
};

class Tournament
{
private:
    Player comp(Player p1, Player p2)
    {
        int sco = 0;
        Referee rf;
        for (int i = 0; i < 5 && sco > -3 && sco < 3; i++)
        {
            int r = rf.Judge(p1.Play(), p2.Play());
            if (r == 'W' || r == 'T') sco++; else sco--;
        }
        if (sco == 3) return p1; else return p2;
    }
public:
    Tournament()
    {
        srand(time(NULL));
        Player players[8];
        for (int i =0; i < 8; i++)
        {
            players[i].PrnName();
            printf(" ");
        }
        printf("\n");
        Player players1[4];
        for (int i = 0; i < 4; i++)
        {
            players1[i] = comp(players[i * 2], players[i * 2 + 1]);
        }
        Player players2[2];
        for (int i = 0; i < 2; i++)
        {
            players1[i * 2].Reset();
            players1[i * 2 + 1].Reset();
            players2[i] = comp(players1[i * 2], players1[i * 2 + 1]);
        }
        players2[0].Reset();
        players2[1].Reset();
        Player winner = comp(players2[0], players2[1]);
        winner.PrnName();
        printf("\n");
    }
};

int main()
{
    Tournament t;
    return 0;
}

Tom Jack Tom Roy Jack Jack Jack Roy
Jack
Press any key to continue . . .


Roy Jack Tom Jack Jack Tom Roy Roy
Roy
Press any key to continue . . .
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复饭来: 因为 state++。所以原来1的变成2.2变成3了
一年多之前 回复
weixin_42343634
饭来 谢谢,Tom 那个class为啥没有state==1的情况呀
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐