removing comments
This commit is contained in:
parent
b517b06bc6
commit
9ce871ef8e
5 changed files with 35 additions and 66 deletions
24
Board.cpp
24
Board.cpp
|
@ -80,7 +80,6 @@ void Board::isTaken(int r, int c) {
|
|||
for(int x = 0; x < opieces.size(); ++x) {
|
||||
if (opieces[x]->getX() == r && opieces[x]->getY() == c) {
|
||||
opieces.erase(opieces.begin() + x);
|
||||
//cout << "otaken set to TRUE\n";
|
||||
otaken = true;
|
||||
}
|
||||
}
|
||||
|
@ -89,7 +88,6 @@ void Board::isTaken(int r, int c) {
|
|||
for(int x = 0; x < xpieces.size(); ++x) {
|
||||
if (xpieces[x]->getX() == r && xpieces[x]->getY() == c) {
|
||||
xpieces.erase(xpieces.begin() + x);
|
||||
//cout << "xtaken set to TRUE\n";
|
||||
xtaken = true;
|
||||
}
|
||||
}
|
||||
|
@ -222,15 +220,7 @@ Board Board::move(moves m){
|
|||
int row = m.row;
|
||||
int column = m.column;
|
||||
|
||||
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;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Piece* piece;
|
||||
//cout << "TEST" << row << " " << column << "\n\n";
|
||||
if (isPiece(row, column))
|
||||
piece = getPiece(row, column);
|
||||
|
||||
|
@ -238,7 +228,7 @@ Board Board::move(moves m){
|
|||
cout<<"ERROR: attempting to move an invalid piece.\n";
|
||||
return *this;
|
||||
}
|
||||
//cout << piece->getX() << piece->getY() << "\n\n";
|
||||
|
||||
if (piece->getType() != turn) {
|
||||
cout<<"ERROR: attempting to move the wrong side's piece.\n";
|
||||
}
|
||||
|
@ -272,8 +262,6 @@ Board Board::move(moves m){
|
|||
}
|
||||
|
||||
else if (m.moveType == "RIGHT") {
|
||||
//add error checking
|
||||
//cout << "TESTING??\n\n";
|
||||
if(piece->getType() == 'O') {
|
||||
row--;
|
||||
column++;
|
||||
|
@ -289,9 +277,7 @@ Board Board::move(moves m){
|
|||
}
|
||||
}
|
||||
else {
|
||||
//cout << piece->getX() << " " << piece->getY() << "\n\n";
|
||||
piece->moveRight();
|
||||
//cout << piece->getX() << " " << piece->getY() << "\n\n";
|
||||
}
|
||||
}
|
||||
setValidTrue();
|
||||
|
@ -336,13 +322,11 @@ bool Board::isThisMovePossible(int r, int c, string moveType){
|
|||
temp = getPiece(r + reflector, c+1);
|
||||
if(c < 7) {
|
||||
if (!isPiece(r+reflector, c+1) && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 <= 7)) {
|
||||
//cout << "What.\n\n";
|
||||
return true;
|
||||
}
|
||||
else if(temp->getType() != piece->getType()) {
|
||||
char a = temp->getType();
|
||||
char b = piece->getType();
|
||||
//cout << a << " " << b << "\n\n";
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
|
@ -358,13 +342,11 @@ bool Board::isThisMovePossible(int r, int c, string moveType){
|
|||
temp = getPiece(r + reflector, c-1);
|
||||
if(c > 0) {
|
||||
if (!isPiece(r+reflector, c-1) && (r+reflector >= 0) && (r+reflector <= 7)) {
|
||||
//cout << "What.\n\n";
|
||||
return true;
|
||||
}
|
||||
else if(temp->getType() != piece->getType()) {
|
||||
char a = temp->getType();
|
||||
char b = piece->getType();
|
||||
//cout << a << " " << b << "\n\n";
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
|
@ -491,16 +473,12 @@ int Board::evaluate(char max, char min){
|
|||
|
||||
val += 2 * (maxPieces.size() - minPieces.size());
|
||||
|
||||
//cout << (checkTaken(max)) << "\n";
|
||||
//cout << (checkTaken(min)) << "\n";
|
||||
//check for taken conditions
|
||||
if (checkTaken(min)){
|
||||
//cout << "adding 10 to val\n";
|
||||
val += 10;
|
||||
}
|
||||
|
||||
if (checkTaken(max)){
|
||||
//cout << "subtracting 10 from val\n";
|
||||
val -= 10;
|
||||
}
|
||||
|
||||
|
|
48
Engine.cpp
48
Engine.cpp
|
@ -6,6 +6,7 @@
|
|||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include "Engine.h"
|
||||
#include "Parser.h"
|
||||
|
||||
Engine::Engine(){
|
||||
Board* brd = new Board();
|
||||
|
@ -68,14 +69,14 @@ void Engine::userGame(int difficulty){
|
|||
|
||||
while(b->getTurn() == 'X' ){
|
||||
AI(difficulty);
|
||||
}
|
||||
|
||||
gameOver = b->isGameOver();
|
||||
b->setValidFalse();
|
||||
b->snapshot(record, *b);
|
||||
gameOver = b->isGameOver();
|
||||
b->setValidFalse();
|
||||
b->snapshot(record, *b);
|
||||
b->displayBoard();
|
||||
}
|
||||
}
|
||||
|
||||
b->displayBoard();
|
||||
string s = "";
|
||||
s += b->getTurn();
|
||||
cout << "Game over. " + s + " wins!\n\n";
|
||||
|
@ -98,6 +99,8 @@ void Engine::AIGame(int difficulty){
|
|||
AI(difficulty);
|
||||
}
|
||||
|
||||
b->displayBoard();
|
||||
sleep(1);
|
||||
gameOver = b->isGameOver();
|
||||
}
|
||||
|
||||
|
@ -113,8 +116,6 @@ void Engine::AI(int depth){
|
|||
createMMTree(root, depth, 1);
|
||||
m = evaluateMMTree(root);
|
||||
|
||||
//printTree(0, root);
|
||||
|
||||
b->move(m);
|
||||
b->changeTurns();
|
||||
b->resetTaken();
|
||||
|
@ -152,24 +153,18 @@ void Engine::createMMTree(MNode* node, int depth, int alt){
|
|||
current.changeTurns();
|
||||
min = current.getTurn();
|
||||
|
||||
/*
|
||||
if (current.checkTaken('X'))
|
||||
cout << "xtaken true\n";
|
||||
if (current.checkTaken('O'))
|
||||
cout << "otaken true\n";
|
||||
*/
|
||||
|
||||
temp = new MNode(current, listOfMoves[i], current.evaluate(max, min));
|
||||
|
||||
if (alt == 1)
|
||||
temp->setType("max");
|
||||
else if (alt == 1)
|
||||
else if (alt == -1)
|
||||
temp->setType("min");
|
||||
|
||||
current.resetTaken();
|
||||
node->addChild(temp);
|
||||
|
||||
createMMTree(temp, --depth, alt * -1);
|
||||
alt = alt * -1;
|
||||
createMMTree(temp, --depth, alt);
|
||||
|
||||
current.changeTurns();
|
||||
}
|
||||
|
@ -191,40 +186,45 @@ void Engine::createMMTree(MNode* node, int depth, int alt){
|
|||
}
|
||||
}
|
||||
|
||||
//cout << "alpha: " << alpha << "\n";
|
||||
//cout << "beta: " << beta << "\n";
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
moves Engine::evaluateMMTree(MNode* node){
|
||||
//returns the move from children that maximizes evaluate()
|
||||
MNode* temp;
|
||||
moves m;
|
||||
int max = INT_MIN;
|
||||
int min = INT_MAX;
|
||||
int val;
|
||||
vector<MNode*> children = node->getChildren();
|
||||
|
||||
for (auto &c : children){
|
||||
val = evaluateMMBranch(c, INT_MIN);
|
||||
val = evaluateMMBranch(c, INT_MIN, INT_MAX);
|
||||
if(val > max){
|
||||
temp = c;
|
||||
max = val;
|
||||
}
|
||||
|
||||
if(val < min){
|
||||
temp = c;
|
||||
min = val;
|
||||
}
|
||||
}
|
||||
|
||||
return temp->getMove();
|
||||
}
|
||||
|
||||
int Engine::evaluateMMBranch(MNode* node, int max){
|
||||
int Engine::evaluateMMBranch(MNode* node, int max, int min){
|
||||
vector<MNode*> children = node->getChildren();
|
||||
|
||||
for (auto &c : children){
|
||||
evaluateMMBranch(c, max);
|
||||
evaluateMMBranch(c, max, min);
|
||||
}
|
||||
|
||||
if (node->getMMVal() > max)
|
||||
max = node->getMMVal();
|
||||
|
||||
if (node->getMMVal() < min)
|
||||
min = node->getMMVal();
|
||||
|
||||
return max;
|
||||
}
|
||||
}
|
2
Engine.h
2
Engine.h
|
@ -19,5 +19,5 @@ public:
|
|||
void AI(int depth);
|
||||
void createMMTree(MNode* node, int depth, int alt);
|
||||
moves evaluateMMTree(MNode* node);
|
||||
int evaluateMMBranch(MNode* node, int sum);
|
||||
int evaluateMMBranch(MNode* node, int max, int min);
|
||||
};
|
||||
|
|
25
Parser.cpp
25
Parser.cpp
|
@ -28,28 +28,25 @@ string myToUpper(string input) {
|
|||
return output;
|
||||
}
|
||||
|
||||
void parseMove(vector<string> tokens, Board& board) {
|
||||
void parseAndMove(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 << "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";
|
||||
//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") {
|
||||
|
@ -67,7 +64,7 @@ void parseMove(vector<string> tokens, Board& board) {
|
|||
}
|
||||
}
|
||||
else {
|
||||
cout << "Error. Invalid move location. (size)\n\n";
|
||||
//cout << "Error. Invalid move location. (size)\n\n";
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -75,14 +72,14 @@ void parseMove(vector<string> tokens, Board& board) {
|
|||
void parseCmd(vector<string> tokens, Board& board) {
|
||||
if(tokens[0] == "UNDO") {
|
||||
//Debugging
|
||||
cout << "Testing UNDO.\n\n";
|
||||
//cout << "Testing UNDO.\n\n";
|
||||
//Change when you fix undo.
|
||||
board.undo(board);
|
||||
}
|
||||
|
||||
else if(tokens[0] == "DISPLAYRECORD") {
|
||||
//Debugging
|
||||
cout << "Testing DISPLAYRECORD.\n\n";
|
||||
//cout << "Testing DISPLAYRECORD.\n\n";
|
||||
/*Fix record, uncomment this.
|
||||
cout << "recordsize: " << board->getRecord.size();
|
||||
|
||||
|
@ -93,7 +90,7 @@ void parseCmd(vector<string> tokens, Board& board) {
|
|||
}
|
||||
|
||||
else {
|
||||
parseMove(tokens, board);
|
||||
parseAndMove(tokens, board);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -104,13 +101,7 @@ void parse(string in, Board& board) {
|
|||
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);
|
||||
}
|
2
Parser.h
2
Parser.h
|
@ -6,6 +6,6 @@
|
|||
//using namespace std;
|
||||
|
||||
string myToUpper(string input);
|
||||
void parseMove(vector<string> tokens, Board& board);
|
||||
void parseAndMove(vector<string> tokens, Board& board);
|
||||
void parseCmd(vector<string> tokens, Board& board);
|
||||
void parse(string input, Board& board);
|
Reference in a new issue