-
Notifications
You must be signed in to change notification settings - Fork 2
/
scrabble_piece.hpp
182 lines (145 loc) · 6.23 KB
/
scrabble_piece.hpp
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#ifndef scrabble_piece_h
#define scrabble_piece_h
#include "scrabble_point_map.hpp"
#include "scrabble_common.hpp"
#include "scrabble_exception.hpp"
#include <iostream>
#include <string>
class Human_Player;
class Scrabble_Game;
struct UnitWrap;
/**
* This class represents a single piece that can be played on the board. Pieces
* have a letter and a point-value associated them. Pieces can only be played
* once.
*/
////////////////////////////////////////////////////////////////////////////////
class Scrabble_Piece
////////////////////////////////////////////////////////////////////////////////
{
public:
/**
* Constructor - Initializes state.
*/
Scrabble_Piece(const Scrabble_Game& parent, char letter, const Point_Map& point_map) :
m_parent(parent), m_point_map(point_map)
{
my_assert(is_valid_letter(letter) || letter == '-',
std::string("'") + obj_to_str(letter) + "' is not a valid letter" );
m_letter = letter;
//we use the point map to figure out how many point we are worth
m_point_val = m_point_map.get_point_val(m_letter);
m_played = false;
m_been_output = false;
m_wildcard_choice = letter;
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////// PRIMARY INTERFACE ///////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* played - Tells this piece object that it has been played. We use this method
* to ensure that a piece is never played twice.
*/
void played() const
{
my_assert(!m_played,
std::string("Attempted to play piece '") + m_letter + "' twice");
m_played = true;
}
/**
* set_wildcard_value - For wildcard pieces, this method changes the value it
* is assuming.
*/
void set_wildcard_value(char c) const
{
my_assert(is_valid_letter(c),
std::string("Attempted to set wild-card to invalid char '") + m_letter + "'");
my_assert(is_wildcard(), "Attempted to set wild-card on non-wild-card piece");
m_wildcard_choice = c;
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////// QUERIES /////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* get_letter - Returns the letter of this piece (reflect wildcard changes).
*/
char get_letter() const { return m_wildcard_choice; }
/**
* get_encoded_letter - Returns the letter of this piece, using capitalization to
* encode whether it's a wildcard piece or not.
*/
char get_encoded_letter() const;
/**
* get_point_val - Returns the point value of this piece
*/
unsigned get_point_val() const { return m_point_val; }
/**
* is_played - Returns if this piece has been played or not
*/
bool is_played() const { return m_played; }
/**
* operator<< - Outputs the piece (letter value only).
*/
std::ostream& operator<<(std::ostream& out) const;
/**
* is_wildcard - Returns true is this piece is a wild-card piece
*/
bool is_wildcard() const { return m_letter == '-'; }
/**
* is_valid_letter - A static method that returns true if the argument
* is a valid normal letter.
*/
static bool is_valid_letter(char letter) { return ( (letter >= 'A') && (letter <= 'Z') ); }
private: // ================ PRIVATE INTERFACE ================================
//////////////////////////////////////////////////////////////////////////////
////////////////////////// FORBIDDEN METHODS /////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Scrabble_Piece(const Scrabble_Piece&) = delete;
Scrabble_Piece& operator=(const Scrabble_Piece&) = delete;
//////////////////////////////////////////////////////////////////////////////
////////////////////////// INTERNAL METHODS //////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* force_letter_change - Artificially forces a piece to change value. This
* should only be called by Scrabble_Tester or
* Human_Player.
*/
void force_letter_change(char letter)
{
my_assert(is_valid_letter(letter) || letter == '-',
std::string("'") + obj_to_str(letter) + "' is not a valid letter" );
my_assert(!m_played, "Should not call force_letter_change on played piece");
my_assert(!m_been_output, "Should not have been output already");
m_letter = letter;
m_point_val = m_point_map.get_point_val(m_letter);
m_wildcard_choice = letter;
}
//////////////////////////////////////////////////////////////////////////////
///////////////////////////// DATA MEMBERS ///////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// m_parent - Parent game
const Scrabble_Game& m_parent;
// m_letter - The letter of this piece
char m_letter;
// m_wildcard_choice - The letter the wildcard piece is assuming
mutable char m_wildcard_choice;
// m_point_val - The point-value of this piece
unsigned m_point_val;
// m_been_output - Specifies if this piece has been output before. Pieces that
// are output for the first time appear red.
mutable bool m_been_output;
// m_played - Specifies if this piece is on the board.
mutable bool m_played;
// m_point_map - Reference to point map to use
const Point_Map& m_point_map;
//////////////////////////////////////////////////////////////////////////////
/////////////////////////////// FRIENDS //////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
friend class Human_Player;
friend struct UnitWrap;
};
////////////////////////////////////////////////////////////////////////////////
///////////////////////// ASSCOCIATED OPERATIONS ///////////////////////////////
////////////////////////////////////////////////////////////////////////////////
std::ostream& operator<<(std::ostream& out, const Scrabble_Piece& sp);
#endif