aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--ee1301/wk6/lab5/strap012_lab5_w_1.cpp69
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