-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.cpp
More file actions
81 lines (70 loc) · 2.12 KB
/
player.cpp
File metadata and controls
81 lines (70 loc) · 2.12 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
/* File: player.cpp
* Course: CS 215-012
* Project: Project 2
* Purpose: The implementation of the player class.
* Author: Brennen Adams
*/
#include <iostream>
#include <list>
#include <vector>
#include "player.h"
#include "card.h"
#include <stdexcept> // for out_of_range exception
using namespace std;
// default constructor
Player::Player() {
numCards = 0;
}
// alternative constructor
Player::Player(vector<Card> ini_cards) {
numCards = ini_cards.size();
for (int i = 0; i < numCards; i++) {
cards.push_back(ini_cards[i]);
}
}
// return how many cards player holds currently
int Player::getNumCards() const {
return numCards;
}
// player plays one card from the front of cards at hand
Card Player::play_a_card() {
Card playedCard = cards.front(); // Get the first card
cards.pop_front(); // Remove the first card from the list
numCards--; // Decrease the number of cards
return playedCard; // Return the played card
if (cards.empty()) {
throw out_of_range("Not enough cards to drop! \n");
}
}
// when the player wins the round, this function will be called
// player adds winning cards to the end of the cards at hand
void Player::addCards(vector<Card> winningCards) {
for (Card card : winningCards) {
cards.push_back(card); // Add each winning card to the end of the list
}
numCards += winningCards.size(); // Update the number of cards
}
vector<Card> Player::dropCards() {
vector<Card> droppedCards;
for (int i = 0; i < 3 && !cards.empty(); i++) {
droppedCards.push_back(cards.front()); // Get the first card
cards.pop_front(); // Remove the first card from the list
numCards--; // Decrease the number of cards
}
if (cards.empty()) {
droppedCards.clear(); // Clear the dropped cards if there are no cards left
cout <<"\nNot enough cards to drop! \n";
}
return droppedCards; // Return the dropped cards
}
// display cards at player's hand
void Player::print() const {
if (cards.empty()) {
cout << "No cards in hand." << endl;
return;
}
for (const Card& card : cards) {
card.print(); // Print each card
}
cout << endl; // New line after printing all cards
}