This repository has been archived on 2025-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
breakthroughpine64backup/Parser.cpp
2015-11-03 23:24:42 -06:00

101 lines
No EOL
2.2 KiB
C++
Executable file

/* 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;
}
void parseAndMove(vector<string> 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<string> tokens, Board& board) {
if(tokens[0] == "UNDO") {
board = *(board.undo());
}
else if(tokens[0] == "DISPLAYRECORD") {
cout << "record size: " << board.getRecord().size();
vector<Board> 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<string> tokens;
string temp;
while(ss >> temp) {
tokens.push_back(temp);
}
parseCmd(tokens, board);
}