2015-11-03 15:12:29 -06:00
|
|
|
/* A more robust parser for the game. */
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <vector>
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2015-11-03 20:37:25 -06:00
|
|
|
void parseAndMove(vector<string> tokens, Board& board) {
|
2015-11-03 15:12:29 -06:00
|
|
|
int col;
|
|
|
|
int row;
|
|
|
|
|
|
|
|
if(tokens[0].size() == 2) {
|
|
|
|
if(tokens[1].size() >= 3 && tokens[1].size() <= 5) {
|
2015-11-03 23:24:42 -06:00
|
|
|
cout << "Inside parser " << tokens[0] << "\n\n";
|
|
|
|
if(tokens[0][0] - 'A' < 0 || tokens[0][0] - 'A' > 8) {
|
2015-11-03 23:00:12 -06:00
|
|
|
cout << "Error. Invalid move location. (1st coord.)\n\n";
|
2015-11-03 15:12:29 -06:00
|
|
|
cout << tokens[0][0] << " " << (tokens[0][0] - 'A') << "\n\n";
|
|
|
|
return;
|
|
|
|
}
|
2015-11-03 23:24:42 -06:00
|
|
|
else if(tokens[0][1] - '0' < 0 || tokens[0][1] - '0' > 8) {
|
|
|
|
cout << "Error. Invalid move location. (2nd coord.)\n\n";
|
2015-11-03 15:12:29 -06:00
|
|
|
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 {
|
2015-11-03 23:00:12 -06:00
|
|
|
cout << "Error. Invalid move location. (size)\n\n";
|
2015-11-03 15:12:29 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void parseCmd(vector<string> tokens, Board& board) {
|
|
|
|
if(tokens[0] == "UNDO") {
|
2015-11-03 23:00:12 -06:00
|
|
|
board = *(board.undo());
|
2015-11-03 15:12:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
else if(tokens[0] == "DISPLAYRECORD") {
|
2015-11-03 23:00:12 -06:00
|
|
|
cout << "record size: " << board.getRecord().size();
|
|
|
|
vector<Board> record = board.getRecord();
|
2015-11-03 15:12:29 -06:00
|
|
|
|
|
|
|
for(int i = 0; i < record.size(); ++i) {
|
|
|
|
record[i].displayBoard();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
2015-11-03 20:37:25 -06:00
|
|
|
parseAndMove(tokens, board);
|
2015-11-03 15:12:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void parse(string in, Board& board) {
|
|
|
|
string input = myToUpper(in);
|
|
|
|
stringstream ss(input);
|
|
|
|
vector<string> tokens;
|
|
|
|
string temp;
|
|
|
|
while(ss >> temp) {
|
|
|
|
tokens.push_back(temp);
|
|
|
|
}
|
2015-11-03 20:37:25 -06:00
|
|
|
|
2015-11-03 15:12:29 -06:00
|
|
|
parseCmd(tokens, board);
|
|
|
|
}
|