weixin_42343634
饭来
采纳率66.7%
2019-03-28 14:30 阅读 286

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

40

首先是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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    caozhy 从今以后生命中的每一秒都属于我爱的人 2019-03-29 09:50
    // 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 . . .
    
    点赞 评论 复制链接分享

相关推荐