-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
96 lines (82 loc) · 1.91 KB
/
Player.cpp
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
#include <string>
#include "Player.h"
#include <iostream>
#include <stdexcept>
#include <exception>
using std::string;
Player::Player(string s, Pile* buildPiles[4], Pile& drawPile, int stockSize)
: name(s), isActive(false), isHuman(false)
{
for(int i = 0; i < 4; ++i)
builds[i] = buildPiles[i];
for(int j = 0; j < stockSize; ++j)
stock += drawPile.draw();
}
void Player::discardCard(int handPos, int pileNum)
{
discard[pileNum] += hand.drawAt(handPos);
isActive = false;
}
void Player::buildTo(int pile, Card c)
{
Pile& b = *builds[pile];
if(b.getSize() != 0)
c.transform(builds[pile]->getTop());
else
c.transform(0);
*builds[pile] += c;
}
Card Player::getTopStock()
{
return stock.getTop();
}
//"draw" means remove AND return.
Card Player::drawTopStock()
{
return stock.draw();
}
//get the i-th discard pile's top card
Card Player::getTopDisc(int i)
{
return discard[i].getTop();
}
Card Player::drawTopDisc(int i)
{
return discard[i].draw();
}
void Player::buildFromHand(int nPile)
{
//non-skip-bos take priority on building
for(int i = 0; i < hand.getSize(); ++i)
if(!hand[i].getSkipBo() && isBuildable(hand[i], *builds[nPile]))
{
buildTo(nPile, hand.drawAt(i));
return;
}
//if no non-skip-bos are found, build with a skip-bo
for(int i = 0; i < hand.getSize(); ++i)
if(isBuildable(hand[i], *builds[nPile]))
{
buildTo(nPile, hand.drawAt(i));
return;
}
//if none, throw an exception
throw std::invalid_argument(ERR_CANT_BUILD_FROM_HAND);
}
void Player::buildFromDiscard(int to, int from)
{
if(isBuildable(discard[from].getTop(), *builds[to]))
{
buildTo(to, discard[from].draw());
return;
}
//if not buildable, throw an exception
throw std::invalid_argument(ERR_CANT_BUILD_FROM_HAND);
}
bool Player::isBuildable(Card c, Pile p)
{
return ((p.getSize() == 0 && c.getData() == 1)
|| c.getSkipBo() == true
|| (p.getSize() != 0 &&
p.getTop().getData() - c.getData() == -1));
}