forked from Arsollo/WarZone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deck.cpp
115 lines (90 loc) · 2.74 KB
/
Deck.cpp
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
//============================================================================
// Name : Deck.cpp
// Author : Arsany Fahmy
// Version :
// Copyright : Your copyright notice
// Description : Comp 345 Ass 1
//============================================================================
#include <iostream>
#include <random>
#include <stdexcept>
#include "Deck.h"
using namespace std;
Deck::Deck(): remainingCards(), cardTypes()
{
//Storing all possible card types
cardTypes.push_back("Bomb");
cardTypes.push_back("Reinforcement");
cardTypes.push_back("Blockade");
cardTypes.push_back("Airlift");
cardTypes.push_back("Diplomacy");
totalNumCards = 0;
numRemainingCards = 0;
create();
}
Deck::Deck(int numCards): remainingCards(), cardTypes()
{
//Storing all possible card types
cardTypes.push_back("Bomb");
cardTypes.push_back("Reinforcement");
cardTypes.push_back("Blockade");
cardTypes.push_back("Airlift");
cardTypes.push_back("Diplomacy");
totalNumCards = numCards;
numRemainingCards = numCards;
create();
}
Card Deck::draw()
{
//Printing an error message if no cards available to draw
if(numRemainingCards < 1)
{
throw std::runtime_error(std::string("No cards left to draw from the deck :("));
}
else
{
//Generating random number between 0 and sizeOfArray of remaining cards - 1
//in order to draw a random card from the pile of remaining cards
std::random_device rd; // obtain a random number from hardware
std::mt19937 gen(rd()); // seed the generator
std::uniform_int_distribution<> distr(0, numRemainingCards-1); // define the range
int randomIndex = distr(gen) ; // generate number
//Card to return
Card card("asdasdas");
//Copying content of randomly selected card
card.copy(remainingCards[0]);
//Updating the deck by removing the card that was picked
remainingCards.erase(remainingCards.begin() + randomIndex);
//Updating number of remaining cards
numRemainingCards--;
return card;
}
}
void Deck::create()
{
for (int i = 0; i < totalNumCards; i++)
{
//Generating a random index to use to create a random card type
std::random_device rd; // obtain a random number from hardware
std::mt19937 gen(rd()); // seed the generator
std::uniform_int_distribution<> distr(0, 4); // since 5 possible types
int randomIndex = distr(gen) ; // generate number
//Getting random type
string type = cardTypes[randomIndex];
//Creating a new card to be added to deck
Card card(type);
//Adding card to deck
remainingCards.push_back(card);
}
}
void Deck::display()
{
cout << "\nRemaining cards: " << endl;
//Iterate over all cards
for(int i = 0; i < numRemainingCards; i++)
{
cout << "Card #" << (i+1) << " ->";
cout << remainingCards[i].getAssignedType() << ",";
cout << endl;
}
}