diff options
author | RossTheRoss <msattr@gmail.com> | 2019-04-18 14:28:28 -0500 |
---|---|---|
committer | RossTheRoss <msattr@gmail.com> | 2019-04-18 14:28:28 -0500 |
commit | a8b14bb2aa18ef06d6b5873f1a55997b63dd973d (patch) | |
tree | a5a23bf86d27ba6e9d329e7931c36a326f5a8af0 /ee1301/wk6/lab5 | |
parent | Fix oopsie-woopsies (diff) | |
download | homework-a8b14bb2aa18ef06d6b5873f1a55997b63dd973d.tar homework-a8b14bb2aa18ef06d6b5873f1a55997b63dd973d.tar.gz homework-a8b14bb2aa18ef06d6b5873f1a55997b63dd973d.tar.bz2 homework-a8b14bb2aa18ef06d6b5873f1a55997b63dd973d.tar.lz homework-a8b14bb2aa18ef06d6b5873f1a55997b63dd973d.tar.xz homework-a8b14bb2aa18ef06d6b5873f1a55997b63dd973d.tar.zst homework-a8b14bb2aa18ef06d6b5873f1a55997b63dd973d.zip |
T O U C H U P S
How does one spell ascetic. Asctetic?
Diffstat (limited to 'ee1301/wk6/lab5')
-rw-r--r-- | ee1301/wk6/lab5/strap012_lab5_w_1.cpp | 69 |
1 files changed, 38 insertions, 31 deletions
diff --git a/ee1301/wk6/lab5/strap012_lab5_w_1.cpp b/ee1301/wk6/lab5/strap012_lab5_w_1.cpp index 63d0a79..d3684fb 100644 --- a/ee1301/wk6/lab5/strap012_lab5_w_1.cpp +++ b/ee1301/wk6/lab5/strap012_lab5_w_1.cpp @@ -1,46 +1,23 @@ #include <iostream> #include <string> -#include <time.h> +#include <time.h> //MinGW does not compile without this included when time is involved. class DeckOfCards { private: - int deck[53]; - int index; + int index=0, deck[53]; //deck is size 53 as a hack designed to prevent segfaults. public: - DeckOfCards() { - for(int i=0; i<52; i++) { - deck[i]=i+1; - } - } - int dealCard() { - index++; - if (index>=52) { - index=0; - shuffle(); - } - return deck[index]; - } - void shuffle() { - int j=0; - int temp=0; - for (int i=51; i>1; i--) { - j = rand() % 51 + 1; - if (0 < j && j < i) { - temp=deck[i]; deck[i]=deck[j]; deck[j]=temp; - } - } - } + DeckOfCards(); + void shuffle(); + int dealCard(); }; void showHand(int hand[], const int size); -char findCard(int card); int main() { - const int size=4; srand(time(NULL)); + const int size=4; //Size can be changed for larger hands DeckOfCards deck; int hand[size]; - deck.shuffle(); - for (int i=0; i<13; i++) { + for (int i=0; i<26; i++) { for (int j=0; j<size; j++) { hand[j]=deck.dealCard(); } @@ -48,9 +25,10 @@ int main() { } } +//Prints out hand shuffled from before void showHand(int hand[], const int size) { - for (int i=0; i<size; i++) { + //Switch case needed for showing face cards and aces switch(hand[i]%13) { case 0: std::cout << 'A'; break; @@ -66,3 +44,32 @@ void showHand(int hand[], const int size) { } std::cout << std::endl; } + +//Deck initialized with 1-52 and shuffled +DeckOfCards::DeckOfCards() { + for(int i=0; i<52; i++) { + deck[i]=i+1; + } + shuffle(); +} + +//Implementation of Knuth Shuffle +void DeckOfCards::shuffle() { + int j=0, temp=0; + for (int i=50; i>1; i--) { + j = rand() % 50 + 1; + if (j < i) { + temp=deck[i]; deck[i]=deck[j]; deck[j]=temp; + } + } +} + +//Function to deal the card when asked by grabbing from the deck and shuffling if such card does not exist. +int DeckOfCards::dealCard() { + index++; + if (index>=52) { + index=0; + shuffle(); + } + return deck[index]; +}
\ No newline at end of file |