-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUno.cpp
More file actions
executable file
·157 lines (133 loc) · 4.04 KB
/
Uno.cpp
File metadata and controls
executable file
·157 lines (133 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
156
157
#include <iostream>
#include <fstream>
#include "unoGame.h"
using namespace std;
void buildCardsFile(ofstream &outData);
int main (int argc, char *argv[])
{
ofstream outData("unoCard.txt");
ifstream inData;
string playerNames[4] = {"","","",""};
const int MAX_PLAYERS = 4;
const int MIN_PLAYERS = 2;
int numberOfPlayers;
short cardNumber;
string cardColor;
bool forward = true; //controls direction of the game. only changes to false if a reverse card is used.
bool winner = false;
bool playedCardFound = true;
bool drawnCard;
short playerTurn = 0;
unoCard playedCard;
char playAnswer;
buildCardsFile(outData);
outData.close();
inData.open("unoCard.txt");
cout << "Welcome to Uno!\n\n";
do
{
cout << "How many players do you wish to play with (max of 4, min of 2)? ";
cin >> numberOfPlayers;
}while(numberOfPlayers > MAX_PLAYERS || numberOfPlayers < MIN_PLAYERS);
for(int index = 0; index < numberOfPlayers; index++)
{
cout << "Enter the name of Player " << index + 1 << ": ";
cin >> playerNames[index];
cout << endl;
}
unoGame gameBoard(inData, playerNames, numberOfPlayers);
do
{
do
{
playedCardFound = true;
gameBoard.displayPlayerHand(playerTurn);
cout << "Press D to draw card or P to play card: ";
cin >> playAnswer;
if(playAnswer == 'P' || playAnswer == 'p')
{
cout << "Select a card to discard (num then color): ";
cin >> cardNumber >> cardColor;
gameBoard.createCard(cardNumber, cardColor, playedCard);
playedCardFound = gameBoard.addToDiscard(playedCard, playerTurn);
if(playedCardFound == false)
{
cout << "Card was not found in your hand, try again\n";
}
else
{
if(gameBoard.shouldPlayerBeSkipped(playerTurn))
playerTurn++;
}
}
else if(playAnswer == 'D' || playAnswer == 'd')
{
gameBoard.removeCardFromDeckAddToPlayer(playerTurn);
drawnCard = true;
}
}while(!playedCardFound || !drawnCard);
forward = gameBoard.wasReverseCardUsed(playedCard);
winner = gameBoard.isThereAWinner(playerTurn);
if(!winner && forward)
{
if(gameBoard.shouldPlayerBeSkipped(playerTurn))
{
if((numberOfPlayers - playerTurn) == 1)
playerTurn = 0;
else
playerTurn = ((playerTurn + 1) % numberOfPlayers) + 1;
}
else
playerTurn = (playerTurn + 1) % numberOfPlayers;
}
else if(!winner && !forward)
{
if(playerTurn == 0)
playerTurn = (numberOfPlayers - 1);
else
playerTurn--;
}
}while(!winner);
gameBoard.displayPlayerHand(0);
//gameBoard.displayDeck();
return(0);
}
/* this function takes in a file descriptor, and creates the list
of cards that are to be used for this game. Cards 0-9 are normal
cards, while cards 10-12 are designated as wild cards.
Cards 13 & 14 are the special wild cards, 'Wild' and 'Draw Four'*/
void buildCardsFile(ofstream &outData)
{
for(int k = 0; k < 2; ++k) //create two copies of each card
{
for(int i = 0; i < 4; ++i) //4 colors
{
string color;
switch (i)
{
case 0: color = "Red";
break;
case 1: color = "Blue";
break;
case 2: color = "Yellow";
break;
case 3: color = "Green";
break;
}
for(int j = 0; j < 13; ++j) //cards 10-12 are wild cards
{
if(k == 0 || (k == 1 && j != 0)) //only one zero allowed per color
{
outData << j << " " << color << " ";
}
}
if(k < 1) //only 4 of each Wild card
{
for(int index = 13; index < 15; index++)
{
outData << index << " Wild ";
}
}
}
}
}