test

· · 个人记录

//selectstat.h
#include <string>
#include <set>

char getAChar(std :: string charSet); // returns if get a char in charSet

class Card
{
    private:
        int id;
    protected:

    public:
        Card(int x);

        void setID(int x);
        void showCard(bool light);

        //int getID(); 

        bool operator < (const Card & b) const;
};

class Select
{
    private:
        std :: set <Card> handCard;
        std :: set <Card> toPlay;

        int hCnum;
        int tPnum;

        //void sortCard();
    protected:

    public:
        Select();

        void printHandCard(int pos);
        void printToPlay(int pos);
        void switchCard(int pos, bool b); // b == true - pos in hC; b == false - pos in tP.
        void select();
        void printAll(int pos, bool b);
        void move(char op, int & p, bool & h);

        int countHandCard();
        int countToPlay();
};
//selectimpl.h
#include "selectstat.h"
#include <algorithm>
#include <windows.h>
#include <conio.h>
#include <cstdio>

//#define setTextWHITE SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
//#define setTextRED SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY)
//#define setTextBLUE SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY)

#define setColor(Foreground, Background)  SetConsoleTextAttribute(hConsole, Foreground | (Background << 4 ) )

#define BLACK 0
#define BLUE 1
#define GREEN 2
#define RED 4
#define WHITE 7

const HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

/*---------------- getAChar ----------------*/

char getAChar(std :: string charSet)
{
    char ch = '\0';
    bool charS[128 + 5] = {};
    int len = charSet . size();
    for(int i = 0; i < len; i++)
    {
        charS[(int)charSet[i]] = true;
    }
    do
    {
        ch = _getch();
    }
    while(!charS[(int)ch]);
    return ch;
}

/*---------------- Card ----------------*/

Card :: Card(int x)
{
    setID(x);
    return;
}

void Card :: showCard(bool light)
{
    if(light)
    {
        setColor(BLACK, WHITE);
    }
    printf("%d", id);
    setColor(WHITE, BLACK);
    printf(" ");
    if(id < 10)
    {
        printf(" ");
    }
    return;
}

void Card :: setID(int x)
{
    id = x;
    return;
}

//int Card :: getID()
//{
//  return id;
//}

bool Card :: operator < (const Card& b) const
{
    return id < b . id;
}

/*---------------- Select ----------------*/

//void Select :: sortCard()
//{
//  sort(handCard . begin(), handCard . end());
//  sort(toPlay . begin(), toPlay . end());
//  return;
//}

Select :: Select()
{
    Card tmpCard(0);
    for(int i = 1; i <= 17; i++)
    {
        tmpCard . setID(i);
        handCard . insert(tmpCard);
        hCnum++;
    }
    return;
}

void Select :: printHandCard(int pos)
{
    int cnt = 0;
    for(auto i : handCard)
    {
        cnt++;
        if(cnt == pos)
        {
            i . showCard(true);
        }
        else
        {
            i . showCard(false);
        }
    }
    printf("\n");
    return;
}

void Select :: printToPlay(int pos)
{
    int cnt = 0;
    for(auto i : toPlay)
    {
        cnt++;
        if(cnt == pos)
        {
            i . showCard(true);
        }
        else
        {
            i . showCard(false);
        }
    }
    printf("\n");
    return;
}

void Select :: switchCard(int pos, bool b) // b == true - pos in hC; b == false - pos in tP.
{
    if(b)
    {
        //auto tmp = handCard . erase(handCard . begin() + pos - 1);
        auto tmpIter = handCard . begin();
        for(int i = 1; i < pos; i++)
        {
            tmpIter++;
        }
        toPlay . insert(* tmpIter);
        handCard . erase(tmpIter);
        hCnum--;
        tPnum++;
    }
    else
    {
        auto tmpIter = toPlay . begin();
        for(int i = 1; i < pos; i++)
        {
            tmpIter++;
        }
        handCard . insert(* tmpIter);
        toPlay . erase(tmpIter);
        hCnum++;
        tPnum--;
    }
    return;
}

int Select :: countHandCard()
{
    return hCnum;
}

int Select :: countToPlay()
{
    return tPnum;
}

void Select :: printAll(int pos, bool b)
{
    int tmpP = b ? pos : -1;
    printHandCard(tmpP);
    for(int i = 1; i <= hCnum; i++)
    {
        if(b && i == pos)
        {
            printf("^  ");
        }
        else
        {
            printf("   ");
        }
    }
    printf("\n");
    tmpP = b ? -1 : pos;
    printToPlay(tmpP);
    for(int i = 1; i <= tPnum; i++)
    {
        if(!b && i == pos)
        {
            printf("^   ");
        }
        else
        {
            printf("   ");
        }
    }
    printf("\n");
    return;
}

void Select :: move(char op, int & p, bool & h)
{
    if(op == 's' || op == 'w')
    {
        if(hCnum && tPnum)
        {
            h ^= 1;
        }
    }
    else if(op == 'a')
    {
        if(p > 1)
        {
            p--;
        }
    }
    else if(op == 'd')
    {
        if(h)
        {
            if(p < hCnum)
            {
                p++;
            }
        }
        else
        {
            if(p < tPnum)
            {
                p++;
            }
        }
    }
    return;
}

void Select :: select()
{
    int p = 1; // p - position in row
    bool h = true; // h - is in the handCard? default = true, else in the toPlay.
    printAll(p, h);
    while(true)
    {
        char op = getAChar("wasdq ");//wasd - move; q - quit
        if(op != ' ' && op != 'q')
        {
            move(op, p, h);     
        }
        else if(op == ' ')
        {
            switchCard(p, h);
            p = 1;
            h ^= 1;
        }
        else
        {
            break;
        }
        system("cls");
        printAll(p, h);
    }
    printf("Over.\n");
    return;
}
//main.cpp
#include "selectimpl.h"

using namespace std;

Select sel;

int main()
{
    sel.select();
    return 0;
}