/* A more robust parser for the game. */ #include #include #include #include #include #include #include "Parser.h" using namespace std; string myToUpper(string input) { string output; for (int i = 0 ; i < input.size(); ++i) { int numeric; if ((input[i] - 0 >= 97) && (input[i] - 0 <= 122)) { numeric = input[i] - 32; output.push_back((char)numeric); } else output.push_back(input[i]); } return output; } void parseAndMove(vector tokens, Board& board) { int col; int row; if(tokens[0].size() == 2) { if(tokens[1].size() >= 3 && tokens[1].size() <= 5) { cout << "Inside parser " << tokens[0] << "\n\n"; if(tokens[0][0] - 'A' < 0 || tokens[0][0] - 'A' > 8) { cout << "Error. Invalid move location. (1st coord.)\n\n"; cout << tokens[0][0] << " " << (tokens[0][0] - 'A') << "\n\n"; return; } else if(tokens[0][1] - '0' < 0 || tokens[0][1] - '0' > 8) { cout << "Error. Invalid move location. (2nd coord.)\n\n"; return; } else { col = tokens[0][0] - 'A'; row = 8 - (tokens[0][1] - '0'); } if(tokens[1] == "LEFT" || tokens[1] == "RIGHT" || tokens[1] == "FWD") { moves m(row, col, tokens[1]); board.move(m); } else { cout << "Error. Invalid moveType. (type)\n\n"; return; } } else { cout << "Error. Invalid moveType. (size)\n\n"; return; } } else { cout << "Error. Invalid move location. (size)\n\n"; return; } } void parseCmd(vector tokens, Board& board) { if(tokens[0] == "UNDO") { board = *(board.undo()); } else if(tokens[0] == "DISPLAYRECORD") { cout << "record size: " << board.getRecord().size(); vector record = board.getRecord(); for(int i = 0; i < record.size(); ++i) { record[i].displayBoard(); } } else { parseAndMove(tokens, board); } } void parse(string in, Board& board) { string input = myToUpper(in); stringstream ss(input); vector tokens; string temp; while(ss >> temp) { tokens.push_back(temp); } parseCmd(tokens, board); }