簡體   English   中英

比較卡片(等級和花色) - C 編程

[英]Comparing Cards (Rank & Suit) - C programming

需要幫助比較卡片(等級)。 代碼按應有的方式編譯和運行,需要 function -> compareCards() 紙牌游戲的幫助,玩家在該游戲中比較卡片,看看誰的卡片排名較高(比較排名)。 比較兩張牌:“Ace”擊敗“King”,后者又擊敗“Queen”,后者又擊敗“Jack”。 編號較大的牌擊敗編號較小的牌。 即:10拍4等等

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#define HAND 5
#define NUM_SUITS 4
#define NUM_RANKS 13
#define SHOE_SIZE 1
#define DECK_SIZE (NUM_RANKS * NUM_SUITS * SHOE_SIZE)
#define PLAYERS_PER_TABLE 6

const char RANKS[] = "23456789TJQKA";
const char SUITS[] = "SDHC";

typedef struct newCard
{
    char rank;
    char suit;
} Card;

typedef struct newPlayer
{
    char name[30];
    Card hand[HAND];
} Player;

void MakeNewDeck(Card newDeck[NUM_RANKS * NUM_SUITS * SHOE_SIZE])
{
    int deckNum = 0;
    for (; deckNum < SHOE_SIZE; deckNum++)
    {
        int suit = 0;
        for (; suit < NUM_SUITS; suit++)
        {
            int rank = 0;
            for (; rank < NUM_RANKS; rank++)
            {
                Card temp;
                temp.rank = RANKS[rank];
                temp.suit = SUITS[suit];
                newDeck[(suit * NUM_RANKS) + rank] = temp;
            }
        }
    }
}
void ShuffleDeck(Card shuffledDeck[NUM_RANKS * NUM_SUITS * SHOE_SIZE])
{
    Card newDeck[NUM_RANKS * NUM_SUITS * SHOE_SIZE];
    MakeNewDeck(newDeck);
    int limit = DECK_SIZE - 1;
    int cardIndex = 0;
    for (; cardIndex < DECK_SIZE; cardIndex++)
    {
        int nextIndex = 0;
        if (limit != 0)
        {
            nextIndex = rand() % limit;
        }
        shuffledDeck[cardIndex] = newDeck[nextIndex];
        newDeck[nextIndex] = newDeck[limit--];
    }
    return;
}

void DealCards(Card deck[NUM_RANKS * NUM_SUITS * SHOE_SIZE], Player players[], int numPlayers)
{
    if (numPlayers * HAND > DECK_SIZE)
    {
        printf("%s", "Too many players");
        return;
    }
    int cardNum = 0;
    for (; cardNum < HAND; cardNum++)
    {
        int playerNum = 0;
        int rank = 0;
        for (; playerNum < numPlayers; playerNum++)
        {
            players[playerNum].hand[cardNum] = deck[(cardNum * numPlayers) + playerNum];
        }
    }
}

int compareCards(Card comapreCards[NUM_RANKS * NUM_SUITS * SHOE_SIZE])
{
    // DO WORK
}

int main()
{
    srand(time(NULL));
    Card deck[NUM_RANKS * NUM_SUITS * SHOE_SIZE];
    ShuffleDeck(deck);
    Player table[PLAYERS_PER_TABLE];
    DealCards(deck, table, PLAYERS_PER_TABLE);
    int player = 0;

    int numOfUsers;
    printf("Enter number of players (0-8): ");
    scanf("%d", &numOfUsers);
    for (; player < numOfUsers; player++)
    {
        printf("%i - ", player);
        int card = 0;
        for (; card < HAND - 1; card++)
        {
            printf("%c%c,", table[player].hand[card].rank, table[player].hand[card].suit);
        }
        printf("%c%c\n", table[player].hand[card].rank, table[player].hand[card].suit);
    }
    return 0;
}

我會使用一個char來表示卡片組,將它的低兩位分配給花色,並將等級分配給上面移動兩位 通過這種方式,您可以輕松比較卡片,只需比較向右移動兩個位置的值。

#define SUIT(a) ((a) & 3)          /* from 0 to 3 */
#define RANK(a) ((a) >> 2)         /* from 0/2 to 12/Ace */
#define COMP(a, _op, b) (RANK(a) _op RANK(b)) /* compare only ranks */

並打印它們,我將創建一個const static字符串表,其中包含完整的值集(排序以匹配上面的分配):

const * const deck_table[] = {
 /* hears, spades, clubs,  diamonds */
    "2H",  "2S",   "2C",   "2D", /* first rank */
    "3H",  "3S",   "3C",   "3D", 

    /* ... the rest of card names here */

    "KH",  "KS",   "KC",   "KD",
    "AH",  "AS",   "AC",   "AD", /* last rank */
};

因此您可以通過在deck_table[]中選擇適當的元素輕松打印它們:

    if (COMP(a, ==, b)) { 
        printf("draw on %s == %s\n",
                deck_table[a], deck_table[b]);
    } else if (COMP(a, >, b)) {
        printf("%s wins to %s\n",
                deck_table[a], deck_table[b]);
    } else {
        printf("%s losses to %s\n",
                deck_table[a], deck_table[b]);
    }

這樣,洗牌后的牌組是數字 0 到 52 的排列,可以在簡單的char []數組中處理,手牌是char hand[5] ,您可以僅使用每張牌的一個字節來排列所有牌組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM