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