-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject_2.cpp
More file actions
211 lines (179 loc) · 7.13 KB
/
Project_2.cpp
File metadata and controls
211 lines (179 loc) · 7.13 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* File: Project_2.cpp
* Course: CS215-012
* Project: Project 2
* Purpose: Using card, deck, and player classes, simulate a War card game.
* Author: Brennen Adams
*/
#include <iostream>
#include "card.h"
#include "deck.h"
#include "player.h"
#include <vector>
#include <string>
using namespace std;
int main()
{
const int HANDS = 26;
Deck deck;
deck.createDeck();
deck.shuffleDeck();
vector<Card> cards_for_Player1;
vector<Card> cards_for_Player2;
for (int i = 0; i < HANDS; i++)
{
cards_for_Player1.push_back(deck.deal_a_card());
cards_for_Player2.push_back(deck.deal_a_card());
}
Player player1(cards_for_Player1);
Player player2(cards_for_Player2);
vector<Card> pile;
while (true)
{
// Check for game-over conditions
if (player1.getNumCards() == 0 && player2.getNumCards() == 0)
{
cout << "Game is over!" << endl;
cout << "It is a tie game!" << endl;
cout << "Player1 has " << player1.getNumCards() << " cards in hand!" << endl;
cout << "Player2 has " << player2.getNumCards() << " cards in hand!" << endl;
break;
}
else if (player1.getNumCards() == 0)
{
cout << "Game is over!" << endl;
cout << "Player 2 wins the game!" << endl;
break;
}
else if (player2.getNumCards() == 0)
{
cout << "Game is over!" << endl;
cout << "Player 1 wins the game!" << endl;
break;
}
// Each player plays one card
Card card1 = player1.play_a_card();
Card card2 = player2.play_a_card();
pile.push_back(card1);
pile.push_back(card2);
cout << "\nPlayer 1 plays: ";
card1.print();
cout << "\nPlayer 2 plays: ";
card2.print();
cout << "\n----------------------------------------------" << endl;
cout << "\nThere are " << pile.size() << " cards on the pile!" << endl;
cout << "\n----------------------------------------------" << endl;
// Tie condition: WAR
while (card1.getPoint() == card2.getPoint())
{
cout << "It is a tie...for this round!" << endl;
cout << "Each player drops three cards (face down) on the pile, then" << endl;
cout << "play one more card (face up)" << endl;
// Drop 3 cards face down
vector<Card> drop1 = player1.dropCards();
vector<Card> drop2 = player2.dropCards();
pile.insert(pile.end(), drop1.begin(), drop1.end());
pile.insert(pile.end(), drop2.begin(), drop2.end());
cout << "----------------------------------------------" << endl;
cout << "\nThere are " << pile.size() << " cards on the pile!" << endl;
cout << "\n----------------------------------------------" << endl;
cout << "Player1 has " << player1.getNumCards() << " cards in hand!" << endl;
cout << "Player2 has " << player2.getNumCards() << " cards in hand!" << endl;
// If either can't drop 3 cards, game ends
if (drop1.size() < 3 && drop2.size() < 3)
{
cout << "Game is over!" << endl;
cout << "It is a tie game!" << endl;
cout << "Player1 has " << player1.getNumCards() << " cards in hand!" << endl;
cout << "Player2 has " << player2.getNumCards() << " cards in hand!" << endl;
return 0;
}
else if (drop1.size() < 3)
{
cout << "Game is over!" << endl;
cout << "Player 2 wins the game!" << endl;
return 0;
}
else if (drop2.size() < 3)
{
cout << "Game is over!" << endl;
cout << "Player 1 wins the game!" << endl;
return 0;
}
// Prompt user
cout << "\nDo you want to continue...for the next round? (N or n to quit) ";
string input;
getline(cin, input);
if (input == "N" || input == "n")
{
cout << "You choose to quit the game!" << endl;
cout << "Player1 has " << player1.getNumCards() << " cards left!" << endl;
cout << "Player2 has " << player2.getNumCards() << " cards left!" << endl;
return 0;
}
else
{
card1 = player1.play_a_card();
card2 = player2.play_a_card();
pile.push_back(card1);
pile.push_back(card2);
cout << "Player 1 plays: ";
card1.print();
cout << "\nPlayer 2 plays: ";
card2.print();
cout << "\n----------------------------------------------" << endl;
cout << "\nThere are " << pile.size() << " cards on the pile!" << endl;
cout << "\n----------------------------------------------" << endl;
}
// Play one more card
if (player1.getNumCards() == 1 || player2.getNumCards() == 1)
{
cout << "It is a tie...for this round!" << endl;
cout << "Each player drops three cards (face down) on the pile, then" << endl;
cout << "play one more card (face up)" << endl;
cout << "Game is over!" << endl;
if (player1.getNumCards() == 1 && player2.getNumCards() == 1)
{
cout << "It is a tie game!" << endl;
}
else if (player1.getNumCards() == 0)
{
cout << "Player 2 wins the game!" << endl;
}
else
{
cout << "Player 1 wins the game!" << endl;
}
cout << "Player1 has " << player1.getNumCards() << " cards in hand!" << endl;
cout << "Player2 has " << player2.getNumCards() << " cards in hand!" << endl;
return 0;
}
}
// One player wins this round
if (card1.getPoint() > card2.getPoint())
{
cout << "Player 1 wins...get all cards from the pile!" << endl;
player1.addCards(pile);
}
else
{
cout << "Player 2 wins...get all cards from the pile!" << endl;
player2.addCards(pile);
}
pile.clear();
cout << "Player1 has " << player1.getNumCards() << " cards in hand!" << endl;
cout << "Player2 has " << player2.getNumCards() << " cards in hand!" << endl;
cout << endl;
// Prompt user
cout << "\nDo you want to continue...for the next round? (N or n to quit) ";
string input;
getline(cin, input);
if (input == "N" || input == "n")
{
cout << "You choose to quit the game!" << endl;
cout << "Player1 has " << player1.getNumCards() << " cards left!" << endl;
cout << "Player2 has " << player2.getNumCards() << " cards left!" << endl;
break;
}
}
return 0;
}