-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeck.cpp
More file actions
136 lines (126 loc) · 3.3 KB
/
deck.cpp
File metadata and controls
136 lines (126 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* File: deck.cpp
* Author: James Fregeau
* Created on November 11th, 2025, 4:01 PM
* Holds the cards of the playable deck
*/
#include "deck.h"
//Constructor
Deck::Deck(){
//Sets the current amount of actual crads that havent been used
currSize = 0;
//actually creates the deck containar
deck = new Card *[FULLDECKSIZE];
//Fills it and shuffles
filldeck();
//Second shuffle for more randomness. Prob not necassary
shuffleDeck();
}
//Deconstructor that deletes the whole deck and its items in it
Deck::~Deck(){
emptyDeck();
}
//Deletes all the cards in the deck
void Deck::emptyDeck(){
//Goes through the deck and deletes the obj
for(int i=0;i<UNIQUECARDS;i++){
delete deck[i];
deck[i]=nullptr;
}
}
void Deck::filldeck(){
//Creates the skelton of the deck
if(deck==nullptr){
deck = new Card *[FULLDECKSIZE];
}
//Enum for readiblity
enum cardNames{
ACE,
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING
};
//ptr of ptrs of all the potential cards
Card **cards = new Card*[UNIQUECARDS];
int cardVal = 0;
for(cardNames i=ACE; i<=KING;i=static_cast<cardNames>(i+1)){
//ace is first so on second run we set it to 2 to match the cards
if(cardVal==11)cardVal=2;
//the max value is 10 unless is ace then 11
else if(cardVal !=10)cardVal+=1;
//vars for cards
string name = to_string(cardVal);
string symbol = to_string(cardVal);
//Updates above vars if cards is a special one
if(i==ACE){
name ="Ace";
symbol ="A";
cardVal = 11;
}else if(i==JACK){
name ="Jack";
symbol ="J";
//Queen
}else if(i==QUEEN){
name ="Queen";
symbol ="Q";
//King
}else if(i == KING){
name ="King";
symbol ="K";
}
//creates the card dynamiclly
cards[i] = new Card(name, symbol, cardVal);
}
//assigns deck var currsize
currSize = 0;
//Fills the deck
for(int i=0; i<numDecks*4;i++){
for(int j=0; j<UNIQUECARDS;j++){
//Stores a new copy of the cards into the deck
deck[currSize++] = new Card(*cards[j]);
}
}
//Intial shuffle of the deck
shuffleDeck();
//Cleans up the intial cards array that is no longer needed
for(int i=0;i<UNIQUECARDS;i++){
delete cards[i];
}
delete [] cards;
}
//Resets the deck and shuffles it
void Deck::resetDeck(){
currSize = FULLDECKSIZE;
shuffleDeck();
}
//Shuffles deck based on seed
void Deck::shuffleDeck(){
int ran;
Card *temp;
//Swaps random index for the full deck size amount
for(int i=0; i<FULLDECKSIZE;i++){
//Generates a random num then %s it to get it in range of the index for the deck
ran = rand() % FULLDECKSIZE;
//Swaps
temp = deck[ran];
deck[ran] = deck[i];
deck[i] = temp;
}
}
//Remove a card from the top of the deck and returns it
Card * Deck::removeCardFromDeck(){
//if deck is empty refill it and shuffle
if(currSize<=0){
resetDeck();
}
return deck[--currSize];
}