CCF 201609-3 炉石传说

思路:
1. 两个玩家,每个玩家用一个向量 vector 表示,如果随从死了,删除对应随从,vector 自动更新;如果用数组,判断更新会比较麻烦。
2. 依次判断每行第一个字符串,按游戏规则作相应处理即可。注意英雄提前挂掉的情况。
3. 遇到字符串”end” 切换玩家,0表示第一玩家,1表示第二玩家,则 player = (++player)%2 即可切换玩家。文中多处用取模方法来切换玩家,比 if 判断方便得多。当前玩家是 nowplayer,则对方就是 (nowplayer + 1)%2。
代码如下:

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <sstream>

using namespace std;

struct sommen
{
    int position;
    int attack;
    int health;
    sommen(){position = 0; attack = 0; health = 0;}
    sommen(int p, int a, int h):position(p), attack(a), health(h){}
};

int main()
{
    //freopen("game.txt", "r", stdin);

    vector<sommen> game[2];  //game[0]表示玩家一,game[1]玩家二 
    int n;
    int nowplayer = 0;
    game[0].push_back(sommen(0, 0, 30));
    game[1].push_back(sommen(0, 0, 30));
    int winner = 0;
    cin >> n;
    cin.get();
    for(int numline = 0; numline < n; numline++)  //依次读入指令,按规则处理即可 
    {       
        string line, action;
        int arg1(0), arg2(0), arg3(0);
        getline(cin, line);
        stringstream s(line);
        s >> action;

        if(action == "summon")
        {
            s >> arg1 >> arg2 >> arg3;
            game[nowplayer].insert(game[nowplayer].begin() + arg1, sommen(arg1, arg2, arg3));

            for(int i = 1; i < game[0].size(); i++)
                game[0][i].position = i;
            for(int i = 1; i < game[1].size(); i++)
                game[1][i].position = i;
        }

        if(action == "attack")
        {
            s >> arg1 >> arg2;

            game[nowplayer][arg1].health -= game[(nowplayer+1)%2][arg2].attack;
            game[(nowplayer+1)%2][arg2].health -= game[nowplayer][arg1].attack;

            if(game[nowplayer][arg1].health <= 0)
            {
                if(arg1 == 0) //自己的英雄挂了 
                {
                    winner = nowplayer==0?-1:1;
                    break;
                }
                else  //挂掉的是随从 
                    game[nowplayer].erase(game[nowplayer].begin() + arg1);  
            }

            if(game[(nowplayer+1)%2][arg2].health <= 0)
            {
                if(arg2 == 0) //对方的英雄挂了 
                {
                    winner = nowplayer==0?1:-1;
                    break;
                }
                else 
                    game[(nowplayer+1)%2].erase(game[(nowplayer+1)%2].begin() + arg2);
            }

            //更新各个随从game[s][i]的位置 
            for(int i = 1; i < game[0].size(); i++)
                game[0][i].position = i;
            for(int i = 1; i < game[1].size(); i++)
                game[1][i].position = i;

        }
        if(action == "end")  //切换当前玩家 
            nowplayer = (++nowplayer)%2;
    }

    //以下为处理输出 
    cout << winner << endl;
    cout << game[0][0].health << endl;
    int i;
    for(i = 0; i < game[0].size(); i++);
        cout << i-1 << " ";
    for(i = 1; i < game[0].size(); i++)
        cout << game[0][i].health << " ";
    cout << endl;

    cout << game[1][0].health << endl;
    for(i = 0; i < game[1].size(); i++);
        cout << i-1 << " ";
    for(i = 1; i < game[1].size(); i++)
        cout << game[1][i].health << " ";

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值