From a8b14bb2aa18ef06d6b5873f1a55997b63dd973d Mon Sep 17 00:00:00 2001 From: RossTheRoss Date: Thu, 18 Apr 2019 14:28:28 -0500 Subject: T O U C H U P S How does one spell ascetic. Asctetic? --- ee1301/wk6/lab5/strap012_lab5_w_1.cpp | 69 +++++++++++++++++++---------------- 1 file 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 #include -#include +#include //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; j1; 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 -- cgit v1.2.3