1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- #include <iostream>
- #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);
- }
- }
|