#include #include #include #define CARD 52 #define SUITS 4 #define RANKS 13 using namespace std; void print(int deck[SUITS][RANKS]) { for (int i = 0; i < SUITS; i++) { for (int j = 0; j < RANKS; j++) { cout << deck[i][j] << "\t"; } cout << endl; } } int convertToPoint(const char ch[]) { if (ch == "Ace") return 1; if (ch == "Two") return 2; if (ch == "Three") return 3; if (ch == "Four") return 4; if (ch == "Five") return 5; if (ch == "Six") return 6; if (ch == "Seven") return 7; if (ch == "Eight") return 8; if (ch == "Nine") return 9; if (ch == "Ten") return 10; if (ch == "Jack") return 11; if (ch == "Queen") return 12; if (ch == "King") return 13; return 0; } void shuffleCards(int deck[][RANKS]){ srand(time(0)); for (int card = 1; card <= CARD; card++) { int row; int col; do { row = rand() % SUITS; col = rand() % RANKS; } while (deck[row][col] != NULL); deck[row][col] = card; } } void printCardsShuffling(int deck[][RANKS], const char* suits[], const char* ranks[]) { for (int card = 1; card <= CARD; card++) { for (int i = 0; i < SUITS; i++) { for (int j = 0; j < RANKS; j++) { if (deck[i][j] == card) { cout << "(" << suits[i] << "," << ranks[j] << ")" << endl; } } } } } int** dealingForHand(int deck[SUITS][RANKS]) { int** result = new int*[5]; for (int i = 0; i < 5; i++) { result[i] = new int[3]; } for (int card = 1; card <= 5; card++) { int row = card - 1; for (int i = 0; i < SUITS; i++) { for (int j = 0; j < RANKS; j++) { if (deck[i][j] == card) { // The 1 co dia chi i va j result[row][0] = i; result[row][1] = j; result[row][2] = 0; } } } } return result; } void printHand(int** hand, const char* suits[], const char* ranks[]) { for (int i = 0; i < 5; i++) hand[i][2] = convertToPoint(ranks[hand[i][1]]); cout << endl; for (int i = 0; i < 5; i++) { cout << "(" << suits[hand[i][0]] << "," << ranks[hand[i][1]] << ")" << endl; } } int isFourOfAKind(int** hand) { for (int i = 0; i < 5; i++) { int temp = 0; for (int j = 0; j < 5; j++) { if (hand[i][2] == hand[j][2]) { temp++; cout << temp << endl; } if (temp == 4) return 1; } } return 0; } int main() { int deck[SUITS][RANKS] = { 0 }; shuffleCards(deck); print(deck); cout << endl; const char* suits[SUITS] = { "Hearts", "Diamonds", "Clubs", "Spades" }; const char* ranks[RANKS] = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; printCardsShuffling(deck, suits, ranks); int** hand = dealingForHand(deck); printHand(hand, suits, ranks); // The number of card of one player is 5 => 5 rows // Column J is column writing the position and value (value cof card in random 52 cards.) for (int i = 0; i < 5; i++) { for (int j = 0; j < 3; j++) { cout << hand[i][j] << "\t"; } cout << endl; } //Check Four of a kind if (isFourOfAKind(hand) == 1) cout << "Four of a kind"; else cout << "Not four of a kind"; return 0; }