#include #include "Fields.h" bool Fields::hasState(const Vector& v, State state) const { return fields[v.x][v.y] == state; } Fields::State Fields::getState(const Vector& v) const { return fields[v.x][v.y]; } void Fields::setState(const Vector& v, State state) { fields[v.x][v.y] = state; } void Fields::print(const Vector& active) { String s; toString(s, active); std::cerr << s; } char Fields::getChar(int x, int y, const Vector& active) const { if(active.compare(x, y)) { return '*'; } static const char map[] = {'#', 'O', '.'}; return map[fields[x][y]]; } void Fields::toString(String& s, const Vector& active) const { s.append(" 0 1 2 3 4 5 6 7 8\n"); lineToString(s, 0, active); s.append(" |\\|/|\\|/|\\|/|\\|/|\n"); lineToString(s, 1, active); s.append(" |/|\\|/|\\|/|\\|/|\\|\n"); lineToString(s, 2, active); s.append(" |\\|/|\\|/|\\|/|\\|/|\n"); lineToString(s, 3, active); s.append(" |/|\\|/|\\|/|\\|/|\\|\n"); lineToString(s, 4, active); s.append("\n"); } void Fields::lineToString(String& s, int y, const Vector& active) const { s.append(y + '0').append(' '); for(int x = 0; x < 8; x++) { s.append(getChar(x, y, active)).append('-'); } s.append(getChar(8, y, active)).append("\n\r"); } void Fields::consumeLine() const { // skip until newline while(std::cin.get() != '\n'); // skip any trailing line carriage while(true) { char c = std::cin.peek(); if(c != '\r') { break; } std::cin.get(); } } void Fields::readLine(uint y) { // skip line heading std::cin.get(); std::cin.get(); std::cin.get(); for(uint x = 0; x < 9; x++) { // read field state char c = std::cin.get(); if(c == '2') { fields[x][y] = BLACK; } else if(c == '0') { fields[x][y] = EMPTY; } else if(c == '1') { fields[x][y] = WHITE; } else { std::cerr << "game field parsing error\n"; } // skip " - " until the next field (or the next line for x = 8) std::cin.get(); std::cin.get(); std::cin.get(); } } void Fields::read() { for(uint i = 0; i < 5; i++) { consumeLine(); readLine(i); } }