-
Notifications
You must be signed in to change notification settings - Fork 2
/
player.hpp
215 lines (172 loc) · 7.42 KB
/
player.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#ifndef player_h
#define player_h
#include "scrabble_game.hpp"
#include "indv_play.hpp"
#include "scrabble_piece.hpp"
#include <set>
#include <map>
#include <iostream>
#include <string>
#include <vector>
struct UnitWrap;
/**
* This class represents an individual player. Responsibilities of this class
* are maintaining your standard player state (name, score, pieces etc.). This
* class is abstract: how the player actually makes an individual play will
* vary greatly depending on the subclass. Basically all other functionality
* of a player is handled in this base class.
*
* Worth an extra note is this class' duty of maintaining a character->piece
* multimap. This map is essential in ease-of-use for both human and AI players.
* It allows players to make general requests for pieces via the desired
* character. This is nice because we usually don't care exactly which piece
* object we get. Also, this helps manage the state of any wildcard pieces.
*
* You can think of the act of playing a piece as a two-phase process. First,
* the get_piece method is called when the player thinks about making a play
* with a certain letter. Second, when the player submits the play, the pieces
* are removed from the vector, which makes the transaction permanent. Simply
* removing a peice from the map is not permanent and can be undone by calling
* remap.
*/
////////////////////////////////////////////////////////////////////////////////
class Player
////////////////////////////////////////////////////////////////////////////////
{
public:
/**
* Constructor - Initializes basic state. Starts player with an empty 'tray'
* of pieces.
*
* name - The name of this player
* the_game - The game this player is taking part in
*/
Player(const std::string& name, Scrabble_Game* the_game);
/**
* Destructor
*/
virtual ~Player() {}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////// PRIMARY INTERFACE ///////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* initialize - Prepares a player's state as the game begins
*/
virtual void initialize() {}
/**
* game_over - A notification to this player that the game has ended. This will
* cause the player to deduct points based on what remains in their
* tray.
*/
void game_over();
/**
* play - The outside world's way of telling us to make a play. This method is
* actually a template-alg pattern that performs some state clean-up
* before calling the abs-virtual method that actually makes the play
* 'make_play'.
*/
const Indv_Play& play();
/**
* add_piece - Adds a piece to the player's tray.
*/
void add_piece(const Scrabble_Piece* new_piece);
/**
* remove_piece - Removes a piece from the player's tray
*/
void remove_piece(const Scrabble_Piece* piece);
/**
* add_score - Gives this player some additional points
*/
void add_score(unsigned score) { m_score += score; }
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////// QUERIES /////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* get_name - Return name of player
*/
const std::string& get_name() const { return m_name; }
/**
* get_score - Return player's score
*/
unsigned get_score() const { return m_score; }
/**
* operator<< - Produces a nice-looking output of the player's state
*/
std::ostream& operator<<(std::ostream&) const;
/**
* operator>> - Read player state from istream
*/
std::istream& operator>>(std::istream&);
/**
* get_num_pieces - Returns the number of pieces in the player's tray
*/
unsigned get_num_pieces() const;
/**
* observe_piece - Look at piece i in player's hand
*/
const Scrabble_Piece* observe_piece(unsigned i) const;
/**
* is_human - Is this player a human player
*/
virtual bool is_human() const { return false; }
protected: // ================ PROTECTED INTERFACE ============================
//////////////////////////////////////////////////////////////////////////////
////////////////////////// FORBIDDEN METHODS /////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Player(const Player&);
Player& operator=(const Player&);
//////////////////////////////////////////////////////////////////////////////
////////////////////////// INTERNAL METHODS //////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* make_play - The method that actually computes what this player's next move
* will be. This method single-handedly defines what type of player
* you are.
*/
virtual void make_play() = 0;
/**
* get_piece - Returns a piece that has letter matching the argument. This
* removes the piece from the map (but not the tray) since the
* piece cannot be used again in the current play. This method
* can also potentially change the state of wildcards.
*/
const Scrabble_Piece* get_piece(char c) const;
/**
* remap - Recreates the map, essentially undoing any get_piece calls that
* were made.
*/
void remap() const;
/**
* has_piece - Returns true if player has 'exact' piece provided by argument.
* (Addresses must match).
*/
bool has_piece(const Scrabble_Piece* piece) const;
//////////////////////////////////////////////////////////////////////////////
///////////////////////////// DATA MEMBERS ///////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//m_name - The player's name
std::string m_name;
//m_score - The player's current score
unsigned m_score;
//m_the_game - A pointer to the game the player is taking part in
Scrabble_Game* m_the_game;
//m_current_play - A handle to the play-object being created by make_play
Indv_Play m_current_play;
//m_pieces - The player's tray of pieces
std::vector<const Scrabble_Piece*> m_pieces;
//m_char_piece_map - Maps characters to piece objects. This map represent what
// letters are available to be played. Note: this map will
// often have things removed from it as a player puts together
// a play, but this has no affect on m_pieces.
mutable std::multimap<char, const Scrabble_Piece*> m_char_piece_map;
//////////////////////////////////////////////////////////////////////////////
/////////////////////////////// FRIENDS //////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
friend struct UnitWrap;
};
////////////////////////////////////////////////////////////////////////////////
///////////////////////// ASSCOCIATED OPERATIONS ///////////////////////////////
////////////////////////////////////////////////////////////////////////////////
std::ostream& operator<<(std::ostream& out, const Player& player);
std::istream& operator>>(std::istream& in, Player& player);
#endif