Completely separated all parser code into Parser.cpp and Parser.h.
This commit is contained in:
parent
68f3b9514a
commit
c807d75e14
9 changed files with 191 additions and 119 deletions
108
Board.cpp
108
Board.cpp
|
@ -124,36 +124,6 @@ vector<Piece*> Board::getTypePieces(char type) const{
|
|||
}
|
||||
}
|
||||
|
||||
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){
|
||||
if (xpieces[i]->getX() == 7){
|
||||
|
@ -248,52 +218,11 @@ 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."<<endl;
|
||||
|
@ -461,19 +390,19 @@ vector<moves> 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<moves> 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<Board> record;
|
||||
input = myToUpper(input);
|
||||
|
||||
if (input == "UNDO"){
|
||||
cout << "TEST";
|
||||
undo(tablero);
|
||||
}
|
||||
|
||||
else if (input == "DISPLAYRECORD"){
|
||||
cout<<"record: "<<endl;
|
||||
cout<<record.size();
|
||||
|
||||
for (int i = 0; i < record.size(); ++i){
|
||||
record[i].displayBoard();
|
||||
}
|
||||
}
|
||||
|
||||
else tablero.move(input);
|
||||
}
|
||||
|
||||
void Board::snapshot(vector<Board>& inputVec, Board inputBoard){
|
||||
if (inputVec.size() == 10){
|
||||
inputVec.erase(inputVec.begin());
|
||||
|
|
5
Board.h
5
Board.h
|
@ -44,22 +44,17 @@ public:
|
|||
vector<Piece*> getPieces() const { return pieces; }
|
||||
vector<Piece*> 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<moves> viewPossibleMoves();
|
||||
string myToUpper(string input);
|
||||
void undo(Board& tablero);
|
||||
void interpret(string input, Board& tablero);
|
||||
void snapshot(vector<Board>& inputVec, Board inputBoard);
|
||||
int evaluate(char max, char min);
|
||||
};
|
||||
|
|
40
Client.java
40
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 = "";
|
||||
}
|
||||
|
|
11
Engine.cpp
11
Engine.cpp
|
@ -45,6 +45,8 @@ void Engine::userGame(int difficulty){
|
|||
bool gameOver = false;
|
||||
vector<Board> record;
|
||||
b->snapshot(record, *b);
|
||||
cin.ignore();
|
||||
cin.clear();
|
||||
|
||||
while (gameOver != true){
|
||||
gameOver = b->isGameOver();
|
||||
|
@ -52,9 +54,10 @@ void Engine::userGame(int difficulty){
|
|||
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){
|
||||
|
|
1
Engine.h
1
Engine.h
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include "Board.h"
|
||||
#include "MNode.h"
|
||||
#include "Parser.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
|
116
Parser.cpp
Executable file
116
Parser.cpp
Executable file
|
@ -0,0 +1,116 @@
|
|||
/* 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 parseMove(vector<string> 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<string> 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<string> 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);
|
||||
}
|
11
Parser.h
Executable file
11
Parser.h
Executable file
|
@ -0,0 +1,11 @@
|
|||
#pragma once
|
||||
|
||||
#include "Engine.h"
|
||||
#include "Board.h"
|
||||
|
||||
//using namespace std;
|
||||
|
||||
string myToUpper(string input);
|
||||
void parseMove(vector<string> tokens, Board& board);
|
||||
void parseCmd(vector<string> tokens, Board& board);
|
||||
void parse(string input, Board& board);
|
|
@ -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();
|
||||
|
|
10
makefile
10
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
|
||||
|
|
Reference in a new issue