Fishtank
string-screen.cpp
Go to the documentation of this file.
1 #include "string-screen.h"
2 
3 StringScreen::StringScreen(int width, int height) : Screen(width, height) {
4  buffer_.resize(height, std::vector<char>(width, ' '));
5 }
6 
7 char StringScreen::charAt(int col, int row) const {
8  checkCoordinates(col, row);
9  return buffer_[row][col];
10 }
11 
12 void StringScreen::charAtPut(int col, int row, char value) {
13  checkCoordinates(col, row);
14  buffer_[row][col] = value;
15 }
16 
18  for (size_t row = 0; row < buffer_.size(); row++) {
19  for (size_t col = 0; col < buffer_[row].size(); col++) {
20  buffer_[row][col] = ' ';
21  }
22  }
23 }
24 
25 std::string StringScreen::toString() const {
26  std::string result;
27  result += "+";
28  for (int colidx = 0; colidx < width_; colidx++) {
29  result += "-";
30  }
31  result += "+\n";
32  for (int rowidx = 0; rowidx < height_; rowidx++) {
33  result += "|";
34  for (int colidx = 0; colidx < width_; colidx++) {
35  result += buffer_[rowidx][colidx];
36  }
37  result += "|\n";
38  }
39  result += "+";
40  for (int colidx = 0; colidx < width_; colidx++) {
41  result += "-";
42  }
43  result += "+";
44  return result;
45 }
Represents the abstract notion of a character-addressable writable screen.
Definition: screen.h:4
int width_
Definition: screen.h:28
int width() const
Definition: screen.cpp:31
void checkCoordinates(int col, int row) const
Definition: screen.cpp:16
int height_
Definition: screen.h:29
int height() const
Definition: screen.cpp:33
void charAtPut(int col, int row, char value)
std::string toString() const
StringScreen(int width, int height)
char charAt(int col, int row) const
Fetch the character at the given coordinate pair.