-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunoGame.cpp
More file actions
executable file
·155 lines (132 loc) · 4.04 KB
/
Copy pathunoGame.cpp
File metadata and controls
executable file
·155 lines (132 loc) · 4.04 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "unoGame.h"
#include <algorithm>
#include <iterator>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
unoGame::unoGame(ifstream & inCard, string names[], int numPlayers)
{
short cNumber;
string caColor;
if(inCard)
{
while(inCard)
{
inCard >> cNumber >> caColor;
gameDeck.push_back(unoCard(cNumber, caColor));
}
}
else
{
cout << "File failed to open, now exiting" << endl;
exit(1);
}
for(int i = 0; i < 10; ++i)
shuffleDeck(gameDeck);
addPlayers(names, numPlayers);
cout << "Size: " << gameDeck.size() << endl;
}
//This function simply shuffles the deck by using the STL algorithm
//random_shuffle. It is called
void unoGame::shuffleDeck(vector<unoCard>& gameDeck)
{
vector<unoCard>::iterator beginning, end;
beginning = gameDeck.begin();
end = gameDeck.end();
random_shuffle(beginning, end);
}
void unoGame::createCard(short cardNumber, string cardColor, unoCard &playedCard)
{
playedCard.setCardColor(cardColor);
playedCard.setCardNumber(cardNumber);
}
void unoGame::displayDeck()
{
vector<unoCard>::iterator position = gameDeck.begin();
while(position != gameDeck.end())
{
cout << position->getCardColor() << " " << position->getCardNumber() << endl;
position++;
}
}
//the default value of 5 is used here to disseminate between a function call
//from the main program to add a card to the discard from a players hand, and
//adding a card to the discard when the game object is initialized. this initial
//card will act as the first "upcard".
bool unoGame::addToDiscard(unoCard discard, short playerNum = 5)
{
if(playerNum != 5)
{
if(players[playerNum].removeCard(discard))
{
discardPile.push_back(discard);
return true;
}
else
return false;
}
else
{
discardPile.push_back(gameDeck.pop_back());
}
}
bool unoGame::isThereAWinner(short playerNumber)
{
return players[playerNumber].getHandSize() == 0;
}
bool unoGame::wasReverseCardUsed(unoCard playedCard)
{
return playedCard.getCardNumber() == REVERSE;
}
const bool unoGame::shouldPlayerBeSkipped(short playerNum)
{
return players[playerNum].getStatus();
}
void unoGame::displayPlayerHand(short playerNumber)
{
players[playerNumber].displayHand();
}
void unoGame::removeCardFromDeckAddToPlayer(short playerNum)
{
if(!gameDeck.empty())
{
vector<unoCard>::iterator removeCard = gameDeck.begin();
unoCard addCard = gameDeck.front();
gameDeck.erase(removeCard);
players[playerNum].addCard(addCard);
}
else
{
if(discardPile.empty())
{
cerr << "No more cards left to play, game over\n";
exit(1);
}
else
{
gameDeck = discardPile;
discardPile.clear();
for(int i = 0; i < 10; ++i)
shuffleDeck(gameDeck);
}
}
}
void unoGame::addPlayers(string names[], int numPlayers)
{
const short NUM_START_CARDS = 7;
for(int i = 0; i < numPlayers; ++i)
{
players.push_back(playerClass(names[i]));
}
for(int j = 0; j < NUM_START_CARDS; ++j)
{
for(int i = 0; i < numPlayers; ++i)
{
unoCard temp(gameDeck.front());
vector<unoCard>::iterator removeCard = gameDeck.begin();
gameDeck.erase(removeCard);
players[i].addCard(temp);
}
}
}