-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathComputerPlayer.cpp
124 lines (98 loc) · 3.56 KB
/
ComputerPlayer.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
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
#include "ComputerPlayer.h"
#include "misc.h"
Card *ComputerPlayer::makeDecision(std::vector<Card *> available_cards) {
this->available_cards = available_cards;
cardsOnTable = _game->getCurrentDeal()->getCurrentTrick()->getCards();
vector<Card *>::iterator random_it = select_randomly(available_cards.begin(), available_cards.end());
Card *random = *random_it;
if (!cardsOnTable.empty()) {
if (hasColor(_game->getCurrentDeal()->getCurrentTrick()->getStartingColor())) {
if (hasGreater())
return theGreatestAvailable();
else if (hasContractColor())
return theLowestAvailable(contractColor());
else
return theLowestAvailable(available_cards);
} else if (hasContractColor())
return theLowestAvailable(contractColor());
else
return theLowestAvailable(available_cards);
} else
return random;
}
bool ComputerPlayer::hasContractColor() {
vector<Card *>::iterator iter = available_cards.begin();
Card *card = *iter;
//does the Player have the contract color?
for (++iter; iter != available_cards.end(); ++iter) {
if (card->getColor() == _game->getCurrentDeal()->getContract()->getColor()) {
return true;
}
card = *iter;
}
return false;
}
vector<Card *> ComputerPlayer::contractColor() {
vector<Card *>::iterator iter = available_cards.begin();
vector<Card *> contractColor;
Card *card = *iter;
//create vector of only available contract colors
for (++iter; iter != available_cards.end(); ++iter) {
if (card->getColor() == _game->getCurrentDeal()->getContract()->getColor()) {
contractColor.push_back(card);
}
card = *iter;
}
return contractColor;
}
bool ComputerPlayer::hasGreater() {
Card *greatestAvail = theGreatestAvailable();
Card *greatestTable = greatestOnTable();
return greatestAvail->isBigger(greatestTable, _game->getCurrentDeal()->getContract());
}
Card *ComputerPlayer::greatestOnTable() {
vector<Card *>::iterator iter = cardsOnTable.begin();
Card *greatest = *iter;
//greatest Card on the table
Card *tmp = *iter;
for (++iter; iter != cardsOnTable.end(); ++iter) {
tmp = *iter;
if (tmp->isBigger(greatest, _game->getCurrentDeal()->getContract()))
greatest = tmp;
//std::cout << ' ' << greatest->getValue() << '\n';
}
return greatest;
}
Card *ComputerPlayer::theGreatestAvailable() {
vector<Card *>::iterator it = available_cards.begin();
Card *greatest = *it;
//greatest Card in available cards
Card *tmp = *it;
for (++it; it != available_cards.end(); ++it) {
tmp = *it;
if (tmp->isBigger(greatest, _game->getCurrentDeal()->getContract()))
greatest = tmp;
//std::cout << ' ' << greatest->getValue() << '\n';
}
return greatest;
}
Card *ComputerPlayer::theLowestAvailable(vector<Card *> available_cards) {
vector<Card *>::iterator it = available_cards.begin();
Card *lowest = *it;
//lowest Card in available cards
Card *tmp = *it;
for (++it; it != available_cards.end(); ++it) {
tmp = *it;
if (lowest->isBigger(tmp, _game->getCurrentDeal()->getContract()))
lowest = tmp;
}
return lowest;
}
Contract *ComputerPlayer::proposeContract(Contract *current_max) {
if (current_max == nullptr) {
Contract *contract = new Contract(1, Color::CLUB);
return contract;
} else {
return Contract::Pass();
}
}