/* 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 parseMove(vector tokens, Board& board) { //Debugging cout << "Piece at: " << tokens[0] << " Move: " << tokens[1] << "\n\n"; int col; int row; if(tokens[0].size() == 2) { if(tokens[1].size() >= 3 && tokens[1].size() <= 5) { if(tokens[0][0] - 'A' < 0 || tokens[0][0] - 'A' > 7) { 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' > 7) { cout << "Error. Invalid move location. (1st coord.)\n\n"; return; } else { col = tokens[0][0] - 'A'; row = 8 - (tokens[0][1] - '0'); cout << "row: " << row << "col: " << col << "\n\n"; } 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") { //Debugging cout << "Testing UNDO.\n\n"; //Change when you fix undo. board.undo(board); } else if(tokens[0] == "DISPLAYRECORD") { //Debugging cout << "Testing DISPLAYRECORD.\n\n"; /*Fix record, uncomment this. cout << "recordsize: " << board->getRecord.size(); for(int i = 0; i < record.size(); ++i) { record[i].displayBoard(); } */ } else { parseMove(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); cout << temp << "\n"; } //Debugging // for(int i = 0; i < tokens.size(); ++i) { cout << "tokens[i]: " << tokens[i] << "\n"; } // parseCmd(tokens, board); }