#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

// 运算牌
struct Card
{
    // 运算符 op 0:- 1:+ 2:*
    // 数字 num
    int op, num;
    void print()
    {
        if (op == 0)
        {
            cout << "-";
        }
        if (op == 1)
        {
            cout << "+";
        }
        if (op == 2)
        {
            cout << "*";
        }
        cout << num;
    }
};

// 玩家
struct Player
{
    // win 胜场 aid 数字牌序号 bid 运算牌序号 res 结果
    int win, aid, bid, res;
    int a[15]; // 数字牌
    Card b[15]; // 运算牌
};
Player pi, me;
int cnt = 7; // 手牌数

// 数字牌 按照数字从大到小排序
bool cmp_a(int x, int y)
{
    return x > y;
}

// 运算牌 减牌按照数字从大到小排序,加乘牌按照数字从小到大排序
bool cmp_b(Card x, Card y)
{
    if (x.op != y.op)
    {
        return x.op < y.op;
    }
    if (x.op == 0)
    {
        return x.num > y.num;
    }
    return x.num < y.num;
}

// 计算算式结果
int cal(int a, Card b)
{
    if (b.op == 0)
    {
        return a - b.num;
    }
    if (b.op == 1)
    {
        return a + b.num;
    }
    return a * b.num;
}

// 游戏介绍
void intro()
{
    cout << "卡牌加减乘" << endl;
    cout << "游戏规则:" << endl;
    cout << "你方和对方各抽取6张随机数字卡和6张运算卡," << endl;
    cout << "每轮每方挑选1张数字卡贴在自己的数字区,1张运算卡贴在对方的运算区," << endl;
    cout << "你方的运算结果如大于对方4局则挑战成功。" << endl;
}

// 初始化手牌
void init()
{
    // 数字0~9 运算符0~2
    srand(time(0));
    for (int i = 1; i <= 7; i++)
    {
        pi.a[i] = rand() % 10;
        pi.b[i].num = rand() % 10;
        pi.b[i].op = rand() % 3;
        
        me.a[i] = rand() % 10;
        me.b[i].num = rand() % 10;
        me.b[i].op = rand() % 3;
    }
    sort(me.a + 1, me.a + 8, cmp_a);
    sort(me.b + 1, me.b + 8, cmp_b);
    sort(pi.a + 1, pi.a + 8, cmp_a);
    sort(pi.b + 1, pi.b + 8, cmp_b);
    pi.aid = 1;
    pi.bid = 1;
}

// 展示手牌
void showhand()
{
    cout << "你的手牌:" << endl;
    cout << "序  号: ①\t②\t③\t④\t⑤\t⑥\t⑦\t" << endl;
    cout << "数字牌: ";
    for (int i = 1; i <= cnt; i++)
    {
        cout << me.a[i] << "\t";
    }
    cout << endl;
    cout << "运算牌: ";
    for (int i = 1; i <= cnt; i++)
    {
        me.b[i].print();
        cout << "\t";
    }
    cout << endl;
}

// 确定出牌
void decision()
{
    cout << "请选择你要使用的数字牌序号:" << endl;
    cin >> me.aid;
    while (me.aid > cnt || me.aid < 1)
    {
        cout << "没有该数字牌!请重新输入!" << endl;
        cout << "请选择你要使用的数字牌序号:" << endl;
        cin >> me.aid;
    }
    cout << "请选择你要使用的运算牌序号:" << endl;
    cin >> me.bid;
    while (me.bid > cnt || me.bid < 1)
    {
        cout << "没有该运算牌!请重新输入!" << endl;
        cout << "请选择你要使用的运算牌序号:" << endl;
        cin >> me.bid;
    }
}

// 展示出牌
void showcard()
{
    me.res = cal(me.a[me.aid], pi.b[pi.bid]);
    pi.res = cal(pi.a[pi.aid], me.b[me.bid]);
    cout << "--------------------" << endl;
    cout << "你的结果:" << endl;
    cout << "数字\t运算\t等于" << endl;
    cout << me.a[me.aid] << "\t";
    pi.b[pi.bid].print();
    cout << "\t" << me.res << endl << endl;

    cout << "皮皮的结果:" << endl;
    cout << "数字\t运算\t等于" << endl;
    cout << pi.a[pi.aid] << "\t";
    me.b[me.bid].print();
    cout << "\t" << pi.res << endl;
}

// 比较当局胜负
void pk()
{
    cout << "--------------------" << endl;
    if (me.res > pi.res)
    {
        cout << "你拿下了关键的一局!" << endl;
        me.win++;
    }
    if (me.res == pi.res)
    {
        cout << "缘分啊,平局!" << endl;
    }
    if (me.res < pi.res)
    {
        cout << "很遗憾,错失一局!" << endl;
        pi.win++;
    }
    cout << endl;
}

// 游戏环节
void game()
{
    // 初始化手牌
    init();
    int round = 1;
    while (pi.win < 4 && me.win < 4)
    {
        cout << "----------------------------------------" << endl;
        cout << "第 " << round << " 轮" << endl;
        // 展示你的手牌
        showhand();

        // 确定出牌
        decision();

        // 展示出牌
        showcard();

        // 比较当局胜负
        pk();

        // 更新变量及手牌 
        cnt--;
        round++;
        pi.bid++;
        pi.aid++;
        for (int i = me.aid; i <= cnt; i++)
        {
            me.a[i] = me.a[i + 1];
        }
        for (int i = me.bid; i <= cnt; i++)
        {
            me.b[i] = me.b[i + 1];
        }
    }
}

// 展示游戏胜负结果
void result()
{
    if (me.win == 4)
    {
        cout << "你赢了!你是最棒的!" << endl;
    }
    else
    {
        cout << "你输了!胜败乃兵家常事,大侠请从头来过" << endl;
    }
}

int main()
{
    // 游戏介绍
    intro();

    // 游戏环节
    game();

    // 结果
    result();
    return 0;
}