#include "vec.h"
#include "list.h"
#include "score.h"
#include <time.h>
#include <stdio.h>


std::string PU[54] = {
    "3D", "3C", "3B", "3A",
    "4D", "4C", "4B", "4A",
    "5D", "5C", "5B", "5A",
    "6D", "6C", "6B", "6A",
    "7D", "7C", "7B", "7A",
    "8D", "8C", "8B", "8A",
    "9D", "9C", "9B", "9A",
    "XD", "XC", "XB", "XA",
    "JD", "JC", "JB", "JA",
    "QD", "QC", "QB", "QA",
    "KD", "KC", "KB", "KA",
    "1D", "1C", "1B", "1A",
    "2D", "2C", "2B", "2A",
    "BJ", "RJ"};

int main()
{
    Score s(3);
    int xipai_flag = 0;
    printf("生成随机扑克序列\n");
    srand(time(NULL));
    Vec v(54, 2);

    List l;

    printf("开始建立链表\n");

    for (int i = 0; i <= 53; i++)
    {
        l.firstInsert(v.get(i));
        std::cout << PU[l[0]] << "  ";
        if (i % 2 != 0)
            printf("\n");
    }

Begin:
    printf("\n是否重新洗牌?(0/1)");
    scanf("%d", &xipai_flag);

    if (xipai_flag == 1)
    {
        v.reorder();

        l.clear();

        printf("===============开始建立链表===============\n");

        for (int i = 0; i <= 53; i++)
        {
            l.firstInsert(v.get(i));
            std::cout << PU[l[0]] << "  ";
            if (i % 2 != 0)
                printf("\n");
        }
        xipai_flag == 0;
        goto Begin;
    }
    printf("================开始发牌================\n");
    List p1, p2, p3;
    for (int i = 0; i <= 53; i++)
    {
        switch (i % 3)
        {
        case 0:
            p1.firstInsert(l[i]);
            break;
        case 1:
            p2.firstInsert(l[i]);
            break;
        case 2:
            p3.firstInsert(l[i]);
            break;
        }
    }
    printf("你手中的牌:\n");
    for (int i = 0; i < p1.getSize(); i++)
    {
        std::cout << i << ": " << PU[(p1)[i]] << "\n";
    }
    l.clear();
    p1.sort();
    p2.sort();
    p3.sort();
    l.firstInsert(-1);
    int opt = 0;
    int pass_num = 0;
    int round = 1;
    int finish_num = 0;
    printf("理牌完成,选择你的操作:\n");
    while (finish_num < 2 && round < 54)
    {

        if (pass_num > 3 - finish_num)
            pass_num = 3 - finish_num;
        printf("+++++===========================+++++\n              轮次%d \n+++++===========================+++++\n", round);

        if (p1.getSize())
        {

            printf("你手中的牌:\n");
            for (int i = 0; i < p1.getSize(); i++)
            {
                std::cout << i << ": " << PU[(p1)[i]] << "\n";
            }
            printf("剩余%d张牌\n", p1.getSize());

            // if (l[0] > p1[p1.getSize() - 1])
            // {
            //     if (pass_num == 2 - finish_num)
            //     {
            //         pass_num = 0;
            //         goto A;
            //     }
            //     printf("无法出牌，已自动跳过\n");
            // }
        A:
            printf("0出牌1查看牌堆2跳过");
            scanf("%d", &opt);
            switch (opt)
            {
            case 0:
                printf("选择:");
                scanf("%d", &opt);
                if (opt >= p1.getSize() || (p1[opt] < l[0]&&pass_num!=2))
                {
                    printf("错误输入\n");
                    goto A;
                }
                std::cout << "打出" << PU[l.firstInsert((p1).remove(opt))];
                if (p1.getSize() == 0)
                {
                    s.finish(0);
                    printf(",玩家A出完所有牌\n");
                    finish_num++;
                    pass_num = 0;
                }
                else
                    printf(",剩余%d张牌\n", p1.getSize());
                    pass_num=0;

                break;
            case 1:
                for (int i = 0; i < l.getSize() - 1; i++)
                {
                    std::cout << i << ": " << PU[l[i]];
                    printf("\n");
                }
                goto A;

            case 2:
                pass_num++;
                goto B;

            default:
                printf("错误输入\n");
                goto A;
            }
        }
        else
            printf("玩家A轮空\n");
    B:
        if (p2.getSize())
        {
            printf("--------------------\n");
            printf("B玩家");

            if (pass_num == 2 - finish_num)
            {
                std::cout << "打出" << PU[l.firstInsert((p2).remove(0))];
                printf(",剩余%d张牌\n", p2.getSize());
                pass_num = 0;
            }
            else if (l[0] > p2[p2.getSize() - 1])
            {
                printf("无牌可出，跳过\n");
                pass_num++;
            }
            else
                for (int i = 0; i < p2.getSize(); i++)
                {

                    if (l[0] < p2[i])
                    {
                        std::cout << "打出" << PU[l.firstInsert((p2).remove(i))];

                        printf(",剩余%d张牌\n", p2.getSize());
                        pass_num = 0;
                        break;
                    }
                }
            if (p2.getSize() == 0)
            {
                s.finish(1);
                printf("玩家B出完所有牌\n");
                finish_num++;
                pass_num = 0;
            }
        }
        else
            printf("玩家B轮空\n");
    C:
        if (p3.getSize())
        {
            printf("--------------------\n");
            printf("C玩家");

            if (pass_num == 2 - finish_num)
            {
                std::cout << "打出" << PU[l.firstInsert((p3).remove(0))];
                printf(",剩余%d张牌\n", p3.getSize());
                pass_num = 0;
            }
            else if (l[0] > p3[p3.getSize() - 1])
            {
                printf("无牌可出，跳过\n");
                pass_num++;
            }
            else
                for (int i = 0; i < p3.getSize(); i++)
                {

                    if (l[0] < p3[i])
                    {
                        std::cout << "打出" << PU[l.firstInsert((p3).remove(i))];

                        printf(",剩余%d张牌\n", p3.getSize());
                        pass_num = 0;
                        break;
                    }
                }
            if (p3.getSize() == 0)
            {
                s.finish(2);
                printf("玩家C出完所有牌\n");
                finish_num++;
                pass_num = 0;
            }
        }
        else
            printf("玩家C轮空\n");
        round++;
    }
    printf("=====================================\n");
    printf("分数结算:\n");
    printf("玩家A:%d分\n", s.score(0));
    printf("玩家B:%d分\n", s.score(1));
    printf("玩家C:%d分\n", s.score(2));

    system("pause");

    return 0;
}