-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·150 lines (132 loc) · 6.15 KB
/
main.cpp
File metadata and controls
executable file
·150 lines (132 loc) · 6.15 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
#include <iostream>
#include <deque>
#include "cmake-build-debug/Deck.h"
void readAllCards(std::deque<Card>& cards) {
std::cout << "Your current cards: " << std::endl;
for (int i = 0; i < cards.size(); ++i) {
std::cout << i+1 << " | " << cards.at(i) << std::endl;
}
}
void readGameStatus(std::deque<Card>& cards, std::deque<Card>& enemyCards) {
std::cout << "Your opponent has " << enemyCards.size() << " cards" << std::endl;
std::cout << "You currently have " << cards.size() << " cards." << std::endl;
readAllCards(cards);
}
bool isDeckPlayable(std::deque<Card> hand, Card topCard) {
bool playable = false;
for (int i = 0; i < hand.size(); ++i) {
if (hand.at(i).getFace() == topCard.getFace() || hand.at(i).getSuit() == topCard.getSuit() || hand.at(i).getFace() == EIGHT) {
playable = true;
}
}
return playable;
}
int main() {
bool gameDone = false;
Deck dealingDeck = Deck();
dealingDeck.shuffle();
//deal 5 cards in an unordered set
std::deque<Card> userDeck ={dealingDeck.dealCard(), dealingDeck.dealCard(), dealingDeck.dealCard(), dealingDeck.dealCard(), dealingDeck.dealCard()};
std::deque<Card> aiDeck ={dealingDeck.dealCard(), dealingDeck.dealCard(),dealingDeck.dealCard(),dealingDeck.dealCard(),dealingDeck.dealCard()};
std::cout << "Beginning game of Crazy Eights against AI opponent" << std::endl;
Card topCard = dealingDeck.dealCard();
int playingIndex = 0;
bool validated = false;
bool isEight = false;
bool cardDrawn = false;
while (!userDeck.empty() && !aiDeck.empty()) {
readGameStatus(userDeck, aiDeck);
std::cout << "Current card at the top of the deck: " << topCard << std::endl;
while (!validated) {
std::cout << "What card would you like to play? (state the number or enter 0 to draw card)" << std::endl;
std::cin >> playingIndex;
if (playingIndex == 0) {
validated = true;
//draw a card
userDeck.push_front(dealingDeck.dealCard());
cardDrawn = true;
}
else if (playingIndex>0 && playingIndex<=userDeck.size()) {
//check if the card that user is trying to play has either the same face, same suit, or is an eight.
validated = (topCard.getFace() == userDeck.at(playingIndex-1).getFace() || topCard.getSuit() == userDeck.at(playingIndex-1).getSuit());
if (userDeck.at(playingIndex-1).getFace() == EIGHT) {
validated = true;
isEight = true;
std::cout << "Pick a suit: " << "\n" << "1: Clubs \n 2: Diamonds \n 3: Hearts \n 4: Spades "<< std::endl;
int num = -1;
while (num>4 || num<1) {
std::cin >> num;
}
switch (num) {
case 1: topCard.setSuit(CLUBS); break;
case 2: topCard.setSuit(DIAMONDS); break;
case 3: topCard.setSuit(HEARTS); break;
case 4: topCard.setSuit(SPADES); break;
default: topCard.setSuit(CLUBS);
}
topCard.setFace(EIGHT);
userDeck.erase(userDeck.begin()+(playingIndex-1));
}
}
}
if (!isEight && !cardDrawn) {
//add card to top of pile then remove card from user deck
std::cout << "You have played " << userDeck.at(playingIndex-1) << std::endl;
topCard.setFace(userDeck.at(playingIndex-1).getFace());
topCard.setSuit(userDeck.at(playingIndex-1).getSuit());
userDeck.erase(userDeck.begin() + (playingIndex - 1));
}
//reset vars
isEight = false;
validated = false;
cardDrawn = false;
//then AI Plays
if (!userDeck.empty()) {
int playable = false;
int index = -1;
for (int i = 0; i < aiDeck.size(); ++i) {
if (aiDeck.at(i).getFace() == topCard.getFace() || aiDeck.at(i).getSuit() == topCard.getSuit() || aiDeck.at(i).getFace() == EIGHT) {
playable = true;
index = i;
}
}
if (!playable) {
std::cout << "AI Draws card" << std::endl;
aiDeck.push_back(dealingDeck.dealCard());
}
else {
std::cout << "AI Plays card " << aiDeck.at(index) << std::endl;
if (aiDeck.at(index).getFace() == EIGHT) {
//special logic, always go for suit of first card, or second if eight is index 0
if (index!=0) {
//if its not in index 0
std::cout << "AI sets suit to " << ToString(aiDeck.at(0).getSuit()) << std::endl;
topCard.setSuit(aiDeck.at(0).getSuit());
}
else if ((int)aiDeck.size()>1) {
//if its in index 0
std::cout << "AI sets suit to " << aiDeck.at(1).getSuit() << std::endl;
topCard.setSuit(aiDeck.at(1).getSuit());
}
else {
//if its the last card
std::cout << "AI sets suit to " << aiDeck.at(index).getSuit() << std::endl;
topCard.setSuit(aiDeck.at(index).getSuit());
}
topCard.setFace(EIGHT);
}
else {
topCard = aiDeck.at(index);
}
aiDeck.erase(aiDeck.begin() + (index));
}
playable = false;
} }
if (aiDeck.empty()) {
std::cout << "AI Wins!";
}
else {
std::cout << "You win!";
}
return 0;
}