Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example program
- #include <iostream>
- #include <string>
- #include <ctime>
- #include <cstdlib>
- using namespace std;
- void shuffleDeck(int *&deck);
- int dealHand(int *deck);
- void checkHand(int *hand);
- void orderHand(int *hand);
- int *newDeck = new int[52];
- string rankStrings[13] = {"one","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king"};
- string suitStrings[4] = {"spades","diamonds","clubs","hearts"};
- int main()
- {
- //int *ranks = new int[52];
- //int *suits = new int[52];
- for(int i = 0; i < 52; i++) {
- newDeck[i] = i;
- //cout << "Ranks: " << ranks[i] << endl;
- //cout << "Suits: " << suits[i] << endl;
- //cout << "New Deck: " << newDeck[i] << endl;
- }
- shuffleDeck(newDeck);
- for (int i = 0; i < 52; i++) {
- //cout << newDeck[i] << endl;
- }
- int *myHand = new int[5];
- for (int j = 0; j < 50; j+=5) {
- for (int i = j; i < j+5; i++ ) {
- myHand[i%5] = newDeck[i];
- cout << rankStrings[myHand[i%5]/4] << " of " << suitStrings[myHand[i%5]%4] << endl;
- }
- checkHand(myHand);
- cout << "------------------------" << endl;
- }
- }
- void shuffleDeck(int *&deck) {
- srand(time(NULL));
- //do the swap here
- for (int i = 0; i < 52; i++) {
- int temp = deck[i];
- int swap = rand() % (i+1);
- deck[i] = deck[swap];
- deck[swap] = temp;
- }
- }
- void orderHand(int *hand) {
- for (int j = 0; j < 5; j++)
- {
- for (int l = 0; l < 5; l++){
- if (hand[l] <= hand[l+1])
- {
- int tempCard = *&hand[l+1];
- hand[l+1] = *&hand[l];
- hand[l] = *&tempCard;
- }
- }
- }
- }
- void checkHand(int *hand) {
- int *cards = new int[13];
- int *counts = new int[5];
- int *cloneHand = new int[5];
- for (int i = 0; i < 5; i++)
- {
- cloneHand[i] = hand[i]/4;
- }
- for (int j = 0; j < 5; j++){
- int card = cloneHand[j];
- cards[card]++;
- }
- for (int k = 0; k < 13; k++) {
- if (cards[k] == 0) {
- counts[0]++;
- } else if (cards[k] == 1) {
- counts[1]++;
- }
- else if (cards[k] == 2) {
- counts[2]++;
- } else if (cards[k] == 3) {
- counts[3]++;
- } else if (counts[4]) {
- counts[4]++;
- }
- }
- if (counts[4] == 1) {
- cout << "Four of a kind!" << endl;
- } else if (counts[3] == 1) {
- cout << "Three of a kind!" << endl;
- } else if (counts[2] == 2) {
- cout << "Two pairs!" << endl;
- } else if (counts[2] == 1) {
- cout << "Pair!" << endl;
- } else {
- cout << "No win!" << endl;
- }
- //delete[] cards;
- //delete[] counts;
- //delete[] cloneHand;
- }
- /*int dealHand(int *deck) {
- int *myHand = new int[5];
- for (int i = 0; i < 5; i++) {
- myHand[i] = deck[i];
- }
- return *myHand;
- }*/
Advertisement
Add Comment
Please, Sign In to add comment