aboutsummaryrefslogtreecommitdiffstats
path: root/ee1301/wk6/lab5/strap012_lab5_w_1.cpp
diff options
context:
space:
mode:
authorMatt Strapp <matt@mattstrapp.net>2022-05-24 11:18:46 -0500
committerMatt Strapp <matt@mattstrapp.net>2022-05-24 11:19:55 -0500
commit7a73162607544204032aa66cce755daf21edebda (patch)
tree58578e01f15f34a855d99c32898db9d7a1603e67 /ee1301/wk6/lab5/strap012_lab5_w_1.cpp
parentdo some stuff (diff)
downloadhomework-7a73162607544204032aa66cce755daf21edebda.tar
homework-7a73162607544204032aa66cce755daf21edebda.tar.gz
homework-7a73162607544204032aa66cce755daf21edebda.tar.bz2
homework-7a73162607544204032aa66cce755daf21edebda.tar.lz
homework-7a73162607544204032aa66cce755daf21edebda.tar.xz
homework-7a73162607544204032aa66cce755daf21edebda.tar.zst
homework-7a73162607544204032aa66cce755daf21edebda.zip
Graduate
Signed-off-by: Matt Strapp <matt@mattstrapp.net>
Diffstat (limited to 'ee1301/wk6/lab5/strap012_lab5_w_1.cpp')
-rw-r--r--ee1301/wk6/lab5/strap012_lab5_w_1.cpp75
1 files changed, 75 insertions, 0 deletions
diff --git a/ee1301/wk6/lab5/strap012_lab5_w_1.cpp b/ee1301/wk6/lab5/strap012_lab5_w_1.cpp
new file mode 100644
index 0000000..9f5e285
--- /dev/null
+++ b/ee1301/wk6/lab5/strap012_lab5_w_1.cpp
@@ -0,0 +1,75 @@
+#include <iostream>
+//#include <time.h> //Needed if using MinGW
+
+class DeckOfCards {
+private:
+ int index=0, deck[52];
+public:
+ DeckOfCards();
+ void shuffle();
+ int dealCard();
+};
+
+void showHand(int hand[], const int size);
+int main() {
+ srand(time(NULL));
+ const int size=4; //Size can be changed for larger hands
+ DeckOfCards deck;
+ int hand[size];
+ for (int i=0; i<13; i++) {
+ for (int j=0; j<size; j++) {
+ hand[j]=deck.dealCard();
+ }
+ showHand(hand, size);
+ }
+}
+
+//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;
+ case 10: std::cout << 'J';
+ break;
+ case 11: std::cout << 'Q';
+ break;
+ case 12: std::cout << 'K';
+ break;
+ default: std::cout << hand[i]%13+1;
+ }
+ std::cout << " ";
+ }
+ std::cout << std::endl;
+}
+
+//Deck initialized with 1-52 and shuffled
+DeckOfCards::DeckOfCards() {
+ for(int i=0; i<51; 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.
+//Returns the card drawn from the deck
+int DeckOfCards::dealCard() {
+ index++;
+ if (index>=52) {
+ index=0;
+ shuffle();
+ }
+ return deck[index];
+} \ No newline at end of file