rp-scrabble
Simple, terminal-based Scrabble game
player.cc
Go to the documentation of this file.
1 
4 #include <iostream>
5 #include "player.h"
6 #include "bag.h"
7 #include "tile.h"
8 #include "rack.h"
9 #include "board.h"
10 #include "utils.h"
11 
12 using namespace std;
13 
21 Player::Player(string n)
22 {
23  score = 0;
24  turn = false;
25  playerName = n;
26  rack = new Rack();
27 }
28 
33 {
34  delete rack;
35 }
36 
41 {
42  showScore();
43  cout << "\n";
44 
45  rack->show();
46  cout << "\n";
47 }
48 
53 {
54  if(turn) {
55  BOLD_BRIGHT_GREEN_FG(" " + playerName + ": " + to_string(score) + " points");
56  }
57  else {
58  BOLD_WHITE_FG(" " + playerName + ": " + to_string(score) + " points");
59  }
60 }
61 
68 {
69  return playerName;
70 }
71 
78 {
79  return score;
80 }
81 
89 void Player::setName(std::string name)
90 {
91  playerName = name;
92 }
93 
101 void Player::updateScore(int points)
102 {
103  score += points;
104 }
105 
114 void Player::draw(int count, Bag* b)
115 {
116  rack->fill(b->draw(count));
117 }
118 
133 bool Player::placeTile(Tile* t, Board* b, int r, int c)
134 {
135  return(b->placeTile(t, r, c));
136 }
137 
144 {
145  turn = !turn;
146 }
147 
160 {
161  return(rack->getTile(index));
162 }
163 
175 vector<Tile*> Player::placeTileStr(string str, Board* b, int r, int c, char dir)
176 {
177  for(char& c : str) {
178  c = toupper(c);
179  }
180 
181  vector<Tile*> tileStrVec = rack->getTileStrVec(str);
182 
183  b->placeTileStr(rack, tileStrVec, r, c, dir);
184 
185  return tileStrVec;
186 }
187 
196 {
197  return rack->isEmpty();
198 }
199 
209 {
210  if(t) {
211  rack->addTile(b->retrieve(t->getSquare()->getRow(), t->getSquare()->getCol()));
212  }
213 }
Definition: bag.h:15
std::vector< Tile * > draw(int count)
Definition: bag.cc:232
Definition: board.h:20
bool placeTile(Tile *t, int r, int c)
Definition: board.cc:213
Tile * retrieve(int r, int c)
Definition: board.cc:301
void placeTileStr(Rack *rack, std::vector< Tile * > tilesInStr, int r, int c, char dir)
Definition: board.cc:246
bool placeTile(Tile *t, Board *b, int r, int c)
Definition: player.cc:133
void showScore()
Definition: player.cc:52
void show()
Definition: player.cc:40
~Player()
Definition: player.cc:32
Tile * tileFromRack(int index)
Definition: player.cc:159
void updateScore(int points)
Definition: player.cc:101
bool rackIsEmpty()
Definition: player.cc:195
int getScore()
Definition: player.cc:77
void returnToRack(Tile *t, Board *b)
Definition: player.cc:208
void draw(int count, Bag *b)
Definition: player.cc:114
std::vector< Tile * > placeTileStr(std::string str, Board *b, int r, int c, char dir)
Definition: player.cc:175
void setName(std::string)
Definition: player.cc:89
void toggleTurn()
Definition: player.cc:143
Player(std::string n)
Definition: player.cc:21
std::string getName()
Definition: player.cc:67
Definition: rack.h:17
int getCol()
Definition: square.cc:111
int getRow()
Definition: square.cc:99
Definition: tile.h:22
Square * getSquare()
Definition: tile.cc:145
void BOLD_BRIGHT_GREEN_FG(std::string x)
Definition: utils.h:42
void BOLD_WHITE_FG(std::string x)
Definition: utils.h:72