-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
112 lines (97 loc) · 1.96 KB
/
Player.cpp
File metadata and controls
112 lines (97 loc) · 1.96 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
#include "Player.h"
#include <algorithm>
#include <vector>
#include "Game.h"
#include "NetworkHandler.h"
Player::Player(int id)
{
this->id = id;
}
void Player::addCard(Card* card, bool netCall)
{
hand.push_back(card);
durak->handUi->refresh();
if (!netCall) sendPacket(ADDCARD, std::format("{};{}", std::to_string(id), card->id));
}
void Player::removeCard(Card* card, bool netCall)
{
auto it = std::find_if(hand.begin(), hand.end(),
[&](const Card* k) { return k == card; });
hand.erase(it);
durak->handUi->refresh();
if (!netCall) sendPacket(REMOVECARD, std::format("{};{}", std::to_string(id), card->id));
}
bool Player::invalidHand()
{
//Count the amount of cards for each type
int amountSpades = 0, amountHearts = 0, amountDiamonds = 0, amountClubs = 0;
for (Card* card : hand)
{
switch (card->type)
{
case SPADES:
amountSpades++;
break;
case CLUBS:
amountClubs++;
break;
case DIAMONDS:
amountDiamonds++;
break;
case HEART:
amountHearts++;
break;
}
}
//If player has 5 or more cards of one type, it's "Neumischung"
return (amountSpades > 4 || amountHearts > 4 || amountDiamonds > 4 || amountClubs > 4);
}
void Player::clearHand()
{
while (hand.size() > 0)
{
removeCard(hand.front(), false);
}
}
void Player::updateStatus()
{
if(status == FINISHED) return;
if (hand.size() == 0)
{
status = FINISHED;
finalPos = game->getFinishedPlayers();
}
else if (this == game->currentAttack->attacker1)
{
status = ATTACKER1;
}
else if (this == game->currentAttack->attacker2)
{
status = ATTACKER2;
}
else if (this == game->currentAttack->defender)
{
status = DEFENDER;
}
else
{
status = NONE;
}
if(this == game->player) durak->setPlayerStatus(status);
}
std::string strFromStatus(PlayerStatus status)
{
switch (status)
{
case ATTACKER1:
return "Attacker 1";
case ATTACKER2:
return "Attacker 2";
case DEFENDER:
return "Defender";
case FINISHED:
return "Finished";
case NONE:
return "None";
}
}