-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRack.cpp
More file actions
72 lines (66 loc) · 2.44 KB
/
Rack.cpp
File metadata and controls
72 lines (66 loc) · 2.44 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
// Rack.cpp: implementation of the Rack class.
//
//////////////////////////////////////////////////////////////////////
/*********************************************************************************
** Author : C.Brown (c)COPYRIGHT C.Brown 2001-2026 ALL RIGHTS RESERVED **
** Date : 03/10/2001 **
** File : Rack.cpp **
** Purpose : Holds tiles for player **
** Version : 0.86 beta 28/10/2001 **
*********************************************************************************/
#include "Rack.hpp"
using namespace std;
//=======================================================================
// ADDNEWTILE() Loads the player's rack from tile bag
//=======================================================================
void Rack::addNewTile(TileBag& tileBag)
{
while (playerRack_.size() != RACK_MAX_ITEMS && !tileBag.isEmpty()) {
auto tile = tileBag.randGrab();
if (tile.has_value()) {
playerRack_.push_back(*tile);
}
}
}
//=======================================================================
// getFirstLetter() Gets first letter in rack (for sorting)
//=======================================================================
char Rack::getFirstLetter() const
{
if (!playerRack_.empty()) {
return playerRack_[0].getLetter();
}
return 'Z';
}
//=======================================================================
// DISPLAYRACK() Displays tiles in the player's rack
//=======================================================================
void Rack::displayRack() const
{
for (const auto& tile : playerRack_) {
cout << tile.getLetter() << " ";
}
}
//=======================================================================
// ERASETILE() Deletes tile from rack matching the letter
//=======================================================================
void Rack::eraseTile(char letter)
{
for (size_t pos = 0; pos < playerRack_.size(); ++pos) {
if (letter == playerRack_[pos].getLetter()) {
playerRack_.erase(playerRack_.begin() + pos);
return;
}
}
}
//=======================================================================
// returnScore() Returns total score left in rack (for penalty)
//=======================================================================
int Rack::returnScore() const
{
int total = 0;
for (const auto& tile : playerRack_) {
total += tile.getScore();
}
return total;
}