rp-scrabble
Simple, terminal-based Scrabble game
utils.cc
Go to the documentation of this file.
1 
4 #include <vector>
5 #include <fstream>
6 #include <sstream>
7 #include "tile.h"
8 #include "square.h"
9 #include "play.h"
10 #include "utils.h"
11 
12 using namespace std;
13 
24 bool squarePresent(std::vector<Square*> s, Square* target)
25 {
26  auto it = s.end();
27  if(target) {
28  it = std::find(s.begin(), s.end(), target);
29  }
30  return(it == s.end() ? false : true);
31 }
32 
43 bool tilePresent(std::vector<Tile*> t, Tile* target)
44 {
45  auto it = t.end();
46  if(target) {
47  it = std::find(t.begin(), t.end(), target);
48  }
49  return(it == t.end() ? false : true);
50 }
51 
62 bool charPresent(string str, char ch)
63 {
64  auto it = str.end();
65  it = std::find(str.begin(), str.end(), ch);
66  return(it == str.end() ? false : true);
67 }
68 
78 std::vector<std::string> parsePlay(std::string in)
79 {
80  vector<string> parse;
81 
82  parse.push_back("");
83 
84  for(char ch : in) {
85  if(ch == '-') {
86  parse.push_back("");
87  }
88  else {
89  parse.back().append(1u, ch);
90  }
91  }
92 
93  return parse;
94 }
95 
104 void log(string logFilePath, string str)
105 {
106  ofstream logFile(logFilePath, ios::app);
107 
108  if(logFile.is_open()) {
109  logFile << str + "\n";
110  }
111  else {
112  throw(string("Failed to open file at " + logFilePath + "\n"));
113  }
114 
115  logFile.close();
116 }
117 
127 string RawTimeToString(const time_t& t)
128 {
129  ostringstream oss;
130  oss << t;
131  return oss.str();
132 }
Definition: square.h:30
Definition: tile.h:22
string RawTimeToString(const time_t &t)
Definition: utils.cc:127
void log(string logFilePath, string str)
Definition: utils.cc:104
bool squarePresent(std::vector< Square * > s, Square *target)
Definition: utils.cc:24
bool tilePresent(std::vector< Tile * > t, Tile *target)
Definition: utils.cc:43
std::vector< std::string > parsePlay(std::string in)
Definition: utils.cc:78
bool charPresent(string str, char ch)
Definition: utils.cc:62