diff --git a/Board.cpp b/Board.cpp index 84a7a35..7a7d04b 100644 --- a/Board.cpp +++ b/Board.cpp @@ -123,36 +123,6 @@ vector Board::getTypePieces(char type) const{ return pieces; } } - -moves Board::parse(string input){ - input = myToUpper(input); - - int temp1; - int temp2; - string temp3; - - temp2 = input[0] - 'A'; - temp1 = input[1] - '0'; - - if (input[3] == 'L') - { - temp3 = "LEFT"; - } - - else if (input[3] == 'R') - { - temp3 = "RIGHT"; - } - - else - { - temp3 = "FWD"; - } - - moves output(temp1, temp2, temp3); - - return output; -} bool Board::isGameOver(){ for (int i = 0; i < xpieces.size(); ++i){ @@ -247,53 +217,12 @@ string Board::boardToString(){ return output; } - -int Board::charToIntColumn(char input){ - int kolumn; - - switch (input) - { - case 'A': kolumn = 0; break; - case 'B': kolumn = 1; break; - case 'C': kolumn = 2; break; - case 'D': kolumn = 3; break; - case 'E': kolumn = 4; break; - case 'F': kolumn = 5; break; - case 'G': kolumn = 6; break; - case 'H': kolumn = 7; break; - } - - return kolumn; -} - -char Board::intToCharColumn(int input){ - char kolumn; - - switch (input) - { - case 1: kolumn = 'A'; break; - case 2: kolumn = 'B'; break; - case 3: kolumn = 'C'; break; - case 4: kolumn = 'D'; break; - case 5: kolumn = 'E'; break; - case 6: kolumn = 'F'; break; - case 7: kolumn = 'G'; break; - case 8: kolumn = 'H'; break; - } - - return kolumn; -} - -void Board::move(string inputMove){ - moves m = parse(inputMove); - move(m); -} Board Board::move(moves m){ - int row = 8 - (m.row); + int row = m.row; int column = m.column; - //cout << "INSIDE MOVE: " << row << " " << column << " " << m.moveType << "\n\n"; + cout << "INSIDE MOVE: " << row << " " << column << " " << m.moveType << "\n\n"; if (row > 8 || row < 0 || column > 8 || column < 0) { cout<<"ERROR: index out of bound."< Board::viewPossibleMoves(){ c = xpieces[i]->getY(); if (isThisMovePossible(r, c, "FWD")) { - moves temp(8-r,c, "FWD"); + moves temp(r,c, "FWD"); output.push_back(temp); } if (isThisMovePossible(r,c,"LEFT")) { - moves temp(8-r,c, "LEFT"); + moves temp(r,c, "LEFT"); output.push_back(temp); } if (isThisMovePossible(r,c,"RIGHT")) { - moves temp(8-r,c, "RIGHT"); + moves temp(r,c, "RIGHT"); output.push_back(temp); } } @@ -485,19 +414,19 @@ vector Board::viewPossibleMoves(){ c = opieces[i]->getY(); if (isThisMovePossible(r, c, "FWD")) { - moves temp(8-r,c, "FWD"); + moves temp(r,c, "FWD"); output.push_back(temp); } if (isThisMovePossible(r,c,"LEFT")) { - moves temp(8-r,c, "LEFT"); + moves temp(r,c, "LEFT"); output.push_back(temp); } if (isThisMovePossible(r,c,"RIGHT")) { - moves temp(8-r,c, "RIGHT"); + moves temp(r,c, "RIGHT"); output.push_back(temp); } } @@ -542,27 +471,6 @@ void Board::undo(Board& tablero){ } } -void Board::interpret(string input, Board& tablero){ - vector record; - input = myToUpper(input); - - if (input == "UNDO"){ - cout << "TEST"; - undo(tablero); - } - - else if (input == "DISPLAYRECORD"){ - cout<<"record: "<& inputVec, Board inputBoard){ if (inputVec.size() == 10){ inputVec.erase(inputVec.begin()); diff --git a/Board.h b/Board.h index 13a820c..85798ae 100644 --- a/Board.h +++ b/Board.h @@ -44,22 +44,17 @@ public: vector getPieces() const { return pieces; } vector getTypePieces(char type) const; char getTurn() const { return turn; } - moves parse(string input); bool isGameOver(); char whoWon(); void changeTurns(); void resetTaken(); void displayBoard(); string boardToString(); - int charToIntColumn(char input); - char intToCharColumn(int input); - void move(string inputMove); Board move(moves m); bool isThisMovePossible(int r, int c, string moveType); vector viewPossibleMoves(); string myToUpper(string input); void undo(Board& tablero); - void interpret(string input, Board& tablero); void snapshot(vector& inputVec, Board inputBoard); int evaluate(char max, char min); }; diff --git a/Client.java b/Client.java index eee69c6..f8b3737 100755 --- a/Client.java +++ b/Client.java @@ -246,6 +246,33 @@ public class Client { boardoutput += "" + r + "" + c; } + public static String parseOutput(String move) { + String output = ""; + int tempa = move.charAt(0) - '0'; + int tempb = move.charAt(1) - '0'; + int tempc = move.charAt(2) - '0'; + int tempd = move.charAt(3) - '0'; + + if(tempa == (tempc + 1)) { + //cout << "\nTest\n"; + if(tempb == tempd) { + output = ((char)('A' + tempb)) + "" + (8 - tempa) + " FWD"; + } + else if(tempb == (tempd + 1)) { + output = ((char)('A' + tempb)) + "" + (8 - tempa) + " LEFT"; + } + else if(tempb == (tempd - 1)) { + output = ((char)('A' + tempb)) + "" + (8 - tempa) + " RIGHT"; + } + } + else { + return "Invalid"; + } + //Debugging + System.out.println("Output: " + output); + return output; + } + public static void main (String[] args) throws InterruptedException { Scanner keyboard = new Scanner(System.in); String hostname = args[0]; @@ -299,6 +326,7 @@ public class Client { } } output.println(userInput); + String out = ""; while(!end) { char[] buffer = new char[256]; @@ -318,9 +346,17 @@ public class Client { output.flush(); undoString = ""; } + if(boardoutput.length() == 4) { + out = parseOutput(boardoutput); + if(out == "Invalid") { + out = ""; + boardoutput = ""; + System.out.println("Invalid move."); + } + } } - System.out.println(boardoutput); - output.println(boardoutput); + System.out.println(out); + output.println(out); output.flush(); boardoutput = ""; } diff --git a/Engine.cpp b/Engine.cpp index 0c0a1df..ef1b4a3 100644 --- a/Engine.cpp +++ b/Engine.cpp @@ -45,16 +45,19 @@ void Engine::userGame(int difficulty){ bool gameOver = false; vector record; b->snapshot(record, *b); - + cin.ignore(); + cin.clear(); + while (gameOver != true){ gameOver = b->isGameOver(); while(b->getTurn() == 'O' && !b->isValid()){ b->displayBoard(); cout<<"\nEnter command: "; - cin>>move; + cin.clear(); + getline(cin, move); cout << "\n"; - b->interpret(move, *b); + parse(move, *b); if(b->isValid()){ b->changeTurns(); @@ -138,8 +141,8 @@ void Engine::createMMTree(MNode* node, int depth, int alt){ if (depth >= 0){ for (int i = 0; i < listOfMoves.size(); ++i){ - if(current.getPiece(8 - listOfMoves[i].row, listOfMoves[i].column)->getType() == - current.getTurn() && current.isThisMovePossible(8 - listOfMoves[i].row, + if(current.getPiece(listOfMoves[i].row, listOfMoves[i].column)->getType() == + current.getTurn() && current.isThisMovePossible(listOfMoves[i].row, listOfMoves[i].column, listOfMoves[i].moveType)){ if (cond){ diff --git a/Engine.h b/Engine.h index 225e08d..910f80f 100644 --- a/Engine.h +++ b/Engine.h @@ -2,6 +2,7 @@ #include "Board.h" #include "MNode.h" +#include "Parser.h" using namespace std; diff --git a/Parser.cpp b/Parser.cpp new file mode 100755 index 0000000..7146a52 --- /dev/null +++ b/Parser.cpp @@ -0,0 +1,116 @@ +/* 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); +} \ No newline at end of file diff --git a/Parser.h b/Parser.h new file mode 100755 index 0000000..4ddf7e5 --- /dev/null +++ b/Parser.h @@ -0,0 +1,11 @@ +#pragma once + +#include "Engine.h" +#include "Board.h" + +//using namespace std; + +string myToUpper(string input); +void parseMove(vector tokens, Board& board); +void parseCmd(vector tokens, Board& board); +void parse(string input, Board& board); \ No newline at end of file diff --git a/Server.cpp b/Server.cpp index 6acd853..621aa1f 100644 --- a/Server.cpp +++ b/Server.cpp @@ -19,6 +19,7 @@ void error(const char *msg) perror(msg); exit(1); } +/* string tempParse(string move) { string output = ""; int tempa = move[0] - '0'; @@ -55,6 +56,7 @@ string tempParse(string move) { cout << "Debugging: " << output << "\n\n"; return output; } +*/ int main(int argc, char *argv[]) { @@ -147,8 +149,8 @@ int main(int argc, char *argv[]) move = buffer; bzero(buffer,256); //cout << move << "\n\n"; - move = tempParse(move); - e.getBoard()->interpret(move,(*e.getBoard())); + //move = tempParse(move); + parse(move,(*e.getBoard())); if(e.getBoard()->isValid()) { e.getBoard()->changeTurns(); diff --git a/makefile b/makefile index 7adedd8..1332b52 100755 --- a/makefile +++ b/makefile @@ -3,16 +3,16 @@ all: test server Client.class server: Server.o - g++ -std=c++11 -o server Server.o Engine.o Piece.o Board.o MNode.o + g++ -std=c++11 -o server Server.o Engine.o Piece.o Board.o MNode.o Parser.o -Server.o: Server.cpp Engine.cpp Board.cpp Piece.cpp MNode.cpp - g++ -std=c++11 -c -g Server.cpp Engine.cpp Board.cpp Piece.cpp MNode.cpp +Server.o: Server.cpp Engine.cpp Board.cpp Piece.cpp MNode.cpp Parser.cpp + g++ -std=c++11 -c -g Server.cpp Engine.cpp Board.cpp Piece.cpp MNode.cpp Parser.cpp test: test.o - g++ -std=c++11 -o test test.o Engine.o Piece.o Board.o MNode.o + g++ -std=c++11 -o test test.o Engine.o Piece.o Board.o MNode.o Parser.o test.o: test.cpp Engine.cpp Board.cpp Piece.cpp MNode.cpp - g++ -std=c++11 -c -g test.cpp Engine.cpp Board.cpp Piece.cpp MNode.cpp + g++ -std=c++11 -c -g test.cpp Engine.cpp Board.cpp Piece.cpp MNode.cpp Parser.cpp Client.class: Client.java javac -g Client.java