freecell

裴英才
2023-12-01

  

printf("/t/t/t/t选局/n/t/t/t  从 1 到 1000000: ");
cin>>g_gamenumber;
Shuffle(g_gamenumber);
string sR = ToString();
printf("%s/n", sR.c_str());
{
    char fileOut[128];
    sprintf(fileOut, "%d.txt", g_gamenumber);
    ofstream fOut(fileOut);
    fOut << convertToFile();
} 
 
#define     BLACK           0               // COLOUR(card)
#define     RED             1
#define     ACE             0               // VALUE(card)
#define     DEUCE           1
#define     CLUB            0               // SUIT(card)
#define     DIAMOND         1
#define     HEART           2
#define     SPADE           3
#define     SUIT(card)      ((card) % 4)
#define     VALUE(card)     ((card) / 4)
#define     COLOUR(card)    (SUIT(card) == DIAMOND || SUIT(card) == HEART)
#define     EMPTY          -1
#define     MAXPOS         21
#define     MAXCOL          9       // includes top row as column 0
typedef     int             CARD;
CARD        card[MAXCOL][MAXPOS];   // current layout of cards, CARDs are ints
int         g_gamenumber;
void Shuffle(int gamenumber)
{
    int  i, j;                // generic counters
    int  col, pos;
    int  wLeft = 52;          // cards left to be chosen in shuffle
    CARD deck[52];            // deck of 52 unique cards
    g_gamenumber = gamenumber;
    
    for (col = 0; col < MAXCOL; col++)          // clear the deck
        for (pos = 0; pos < MAXPOS; pos++)
            card[col][pos] = EMPTY;
        
    /* shuffle cards */
    
    for (i = 0; i < 52; i++)      // put unique card in each deck loc.
        deck[i] = i;
    
    srand(gamenumber);            // gamenumber is seed for rand()
    for (i = 0; i < 52; i++)
    {
        j = rand() % wLeft;
        card[(i%8)+1][i/8] = deck[j];
        deck[j] = deck[--wLeft];
    }
}
string convertToFile()
{
    string sR;
    sR.reserve(100);
    char value[] = "A23456789TJQK";
    char sTmp[20];
    
    // 梅花 theCard % 4 = 0
    // 方块 theCard % 4 = 1
    // 红心 theCard % 4 = 2
    // 黑桃 theCard % 4 = 3
    char suit[] = "CDHS"; // 梅花,方块、红心、黑桃
    // display the card
    for (int row = 0; row < MAXPOS; row++)
    {
        for (int col = 1; col < MAXCOL; col++)
        {
            int theCard = card[col][row];
            if (theCard == EMPTY)
                break;
            else
            {
                sprintf(sTmp, "%c%c ", suit[theCard % 4], value[theCard / 4]);
                sR.append(sTmp);
            }
        }
        sR.append(1, '/n');
    }
    
    return sR;
}

相关阅读

相关文章

相关问答