All mechanics errors fixed. Invalid moves no longer tolerated.

This commit is contained in:
Alexander Huddleston 2015-10-27 17:30:33 -05:00
parent 9fe535b10c
commit b94ebe26f6
5 changed files with 184 additions and 101 deletions

167
Board.cpp
View file

@ -6,6 +6,7 @@ using namespace std;
Board::Board() { Board::Board() {
Piece* temp; Piece* temp;
bool valid = false;
for (int i = 0; i < 2; ++i) { for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 8; ++j) { for (int j = 0; j < 8; ++j) {
temp = new Piece(i, j, 'X'); temp = new Piece(i, j, 'X');
@ -27,6 +28,7 @@ Board::Board(const Board& b) {
vector<Piece*> xp = b.getXPieces(); vector<Piece*> xp = b.getXPieces();
vector<Piece*> op = b.getOPieces(); vector<Piece*> op = b.getOPieces();
Piece* temp; Piece* temp;
bool valid = false;
for (int i = 0; i < xp.size(); ++i) { for (int i = 0; i < xp.size(); ++i) {
temp = new Piece(xp[i]->getX(), xp[i]->getY(), 'X'); temp = new Piece(xp[i]->getX(), xp[i]->getY(), 'X');
@ -41,6 +43,18 @@ Board::Board(const Board& b) {
} }
} }
void Board::setValidFalse() {
valid = false;
}
void Board::setValidTrue() {
valid = true;
}
bool Board::isValid() {
return valid;
}
//make this efficient! //make this efficient!
bool Board::isPiece(int r, int c){ bool Board::isPiece(int r, int c){
for (int i = 0; i < pieces.size(); ++i){ for (int i = 0; i < pieces.size(); ++i){
@ -52,6 +66,31 @@ bool Board::isPiece(int r, int c){
return false; return false;
} }
void Board::isTaken(int r, int c) {
for (int i = 0; i < pieces.size(); ++i){
if (pieces[i]->getX() == r && pieces[i]->getY() == c){
if(pieces[i]->getType() == 'O') {
for(int x = 0; x < opieces.size(); ++x) {
if (opieces[x]->getX() == r && opieces[x]->getY() == c) {
opieces.erase(opieces.begin() + x);
//break;
}
}
}
else {
for(int x = 0; x < xpieces.size(); ++x) {
if (xpieces[x]->getX() == r && xpieces[x]->getY() == c) {
xpieces.erase(xpieces.begin() + x);
//break;
}
}
}
pieces.erase(pieces.begin() + i);
break;
}
}
}
//make this efficient! //make this efficient!
Piece* Board::getPiece(int r, int c){ Piece* Board::getPiece(int r, int c){
for (int i = 0; i < pieces.size(); ++i){ for (int i = 0; i < pieces.size(); ++i){
@ -67,10 +106,10 @@ moves Board::parse(string input){
input = myToUpper(input); input = myToUpper(input);
int temp1; int temp1;
char temp2; int temp2;
string temp3; string temp3;
temp2 = input[0]; temp2 = input[0] - 'A';
temp1 = input[1] - '0'; temp1 = input[1] - '0';
if (input[3] == 'L') if (input[3] == 'L')
@ -212,14 +251,15 @@ char Board::intToCharColumn(int input){
void Board::move(string inputMove){ void Board::move(string inputMove){
moves m = parse(inputMove); moves m = parse(inputMove);
cout << "MOVE: " << m.row << " " << m.column << " " << m.moveType << "\n\n";
move(m); move(m);
} }
void Board::move(moves m){ void Board::move(moves m){
int row = 8 - (m.row); int row = 8 - (m.row);
int column = charToIntColumn(m.column); int column = m.column;
//cout << row << " " << column << "\n\n"; cout << "INSIDE MOVE: " << row << " " << column << "\n\n";
if (row > 8 || row < 0 || column > 8 || column < 0) { if (row > 8 || row < 0 || column > 8 || column < 0) {
cout<<"ERROR: index out of bound."<<endl; cout<<"ERROR: index out of bound."<<endl;
@ -241,44 +281,26 @@ void Board::move(moves m){
} }
else { else {
if (jugada.moveType == "FWD") { if(isThisMovePossible(row, column, m.moveType))
if(piece->getType() == 'O') { {
row--;
}
else {
row++;
}
if(isPiece(row, column)) {
cout << "Invalid move, there is a piece there.\n";
return;
}
else {
piece->moveFwd();
}
/*
=======
if (!(isThisMovePossible(row, column, m.moveType))){
cout << "Unable to move: impossible move.\n";
//add a try again
return;
}
if (m.moveType == "FWD") { if (m.moveType == "FWD") {
piece->moveFwd(); piece->moveFwd();
>>>>>>> beccadev
*/
} }
else if (m.moveType == "LEFT") { else if (m.moveType == "LEFT") {
//add error checking //add error checking
if(isPiece(row--, column--)) { if(piece->getType() == 'O') {
if(getPiece(row--, column--)->getType() != piece->getType()) { row--;
getPiece(row--, column--)->isTaken(); column--;
piece->moveLeft();
} }
else { else {
cout << "Invalid move, there is a piece there.\n"; row++;
return; column--;
}
if(isPiece(row, column)) {
if(getPiece(row, column)->getType() != piece->getType()) {
isTaken(row, column);
piece->moveLeft();
} }
} }
else { else {
@ -288,25 +310,40 @@ void Board::move(moves m){
else if (m.moveType == "RIGHT") { else if (m.moveType == "RIGHT") {
//add error checking //add error checking
if(isPiece(row--, column++)) { //cout << "TESTING??\n\n";
if(getPiece(row--, column++)->getType() != piece->getType()) { if(piece->getType() == 'O') {
getPiece(row--, column++)->isTaken(); row--;
piece->moveRight(); column++;
} }
else { else {
cout << "Invalid move, there is a piece there.\n"; row++;
return; column++;
}
if(isPiece(row, column)) {
if(getPiece(row, column)->getType() != piece->getType()) {
isTaken(row, column);
piece->moveRight();
} }
} }
else { else {
cout << piece->getX() << " " << piece->getY() << "\n\n";
piece->moveRight(); piece->moveRight();
cout << piece->getX() << " " << piece->getY() << "\n\n";
} }
} }
setValidTrue();
}
else
{
cout << "Invalid move.\n\n";
setValidFalse();
}
} }
} }
bool Board::isThisMovePossible(int r, int c, string moveType){ bool Board::isThisMovePossible(int r, int c, string moveType){
Piece* piece; Piece* piece;
Piece* temp;
if (isPiece(r, c)) if (isPiece(r, c))
piece = getPiece(r, c); piece = getPiece(r, c);
else else
@ -321,7 +358,7 @@ bool Board::isThisMovePossible(int r, int c, string moveType){
int reflector = 1; int reflector = 1;
if (piece->getType() == 'O') if (piece->getType() == 'O')
reflector *= -1; reflector = -1;
if (moveType == "FWD"){ if (moveType == "FWD"){
if (!isPiece(r + reflector, c)) if (!isPiece(r + reflector, c))
@ -331,18 +368,48 @@ bool Board::isThisMovePossible(int r, int c, string moveType){
} }
else if (moveType == "RIGHT"){ else if (moveType == "RIGHT"){
if (!isPiece(r+reflector, c+1) && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 <= 7)) 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; return true;
else }
else if(temp->getType() != piece->getType()) {
char a = temp->getType();
char b = piece->getType();
cout << a << " " << b << "\n\n";
return true;
}
else {
return false; return false;
} }
}
else {
return false;
}
}
else if (moveType == "LEFT"){ else if (moveType == "LEFT"){
if (!isPiece(r+reflector, c+1) && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 >= 0)) 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; return true;
else }
else if(temp->getType() != piece->getType()) {
char a = temp->getType();
char b = piece->getType();
cout << a << " " << b << "\n\n";
return true;
}
else {
return false; return false;
} }
}
else {
return false;
}
}
else return false; else return false;
} }
@ -358,25 +425,25 @@ vector<moves> Board::viewPossibleMoves(){
c = xpieces[i]->getY(); c = xpieces[i]->getY();
if (isThisMovePossible(r, c, "FWD")) if (isThisMovePossible(r, c, "FWD"))
{ {
moves temp(8-r,intToCharColumn(c+1), "FWD"); moves temp(8-r,c, "FWD");
output.push_back(temp); output.push_back(temp);
} }
if (isThisMovePossible(r,c,"LEFT")) if (isThisMovePossible(r,c,"LEFT"))
{ {
moves temp(8-r,intToCharColumn(c+1), "LEFT"); moves temp(8-r,c, "LEFT");
output.push_back(temp); output.push_back(temp);
} }
if (isThisMovePossible(r,c,"RIGHT")) if (isThisMovePossible(r,c,"RIGHT"))
{ {
moves temp(8-r,intToCharColumn(c+1), "RIGHT"); moves temp(8-r,c, "RIGHT");
output.push_back(temp); output.push_back(temp);
} }
} }
} }
else if (turn == 'X') { else if (turn == '0') {
for (int i = 0; i < opieces.size(); ++i){ for (int i = 0; i < opieces.size(); ++i){
r = opieces[i]->getX(); r = opieces[i]->getX();
c = opieces[i]->getY(); c = opieces[i]->getY();
@ -447,6 +514,7 @@ void Board::undo(Board& tablero){
void Board::interpret(string input, Board& tablero){ void Board::interpret(string input, Board& tablero){
vector<Board> record; vector<Board> record;
input = myToUpper(input); input = myToUpper(input);
//cout << "MOVE: " << input << "\n\n";
if (input == "UNDO") if (input == "UNDO")
{ {
@ -463,7 +531,6 @@ void Board::interpret(string input, Board& tablero){
} }
cout<<"---------------------------------------------------END DISPLAY RECORD------------------------"<<endl; cout<<"---------------------------------------------------END DISPLAY RECORD------------------------"<<endl;
} }
else tablero.move(input); else tablero.move(input);
} }

View file

@ -8,7 +8,7 @@ using namespace std;
struct moves { struct moves {
int row; int row;
char column; int column;
string moveType; string moveType;
moves(int linea, int columna, string m) { moves(int linea, int columna, string m) {
@ -23,11 +23,16 @@ class Board {
vector<Piece*> opieces; vector<Piece*> opieces;
vector<Piece*> pieces; vector<Piece*> pieces;
char turn = 'O'; char turn = 'O';
bool valid = false;
public: public:
Board(); Board();
Board(const Board& b); Board(const Board& b);
void setValidFalse();
void setValidTrue();
bool isValid();
bool isPiece(int r, int c); bool isPiece(int r, int c);
void isTaken(int r, int c);
Piece* getPiece(int r, int c); Piece* getPiece(int r, int c);
vector<Piece*> getXPieces() const { return xpieces; } vector<Piece*> getXPieces() const { return xpieces; }
vector<Piece*> getOPieces() const { return opieces; } vector<Piece*> getOPieces() const { return opieces; }

View file

@ -31,24 +31,28 @@ void Engine::startGame(){
{ {
gameOver = b->isGameOver(); gameOver = b->isGameOver();
while(b->getTurn() == 'O' ) while(b->getTurn() == 'O' && !b->isValid())
{ {
b->displayBoard(); b->displayBoard();
cout<<"\nEnter command: "; cout<<"\nEnter command: ";
cin>>move; cin>>move;
cout << "\n"; cout << "\n";
b->interpret(move, *b); b->interpret(move, *b);
if(b->isValid()) {
b->changeTurns(); b->changeTurns();
b->setValidFalse();
}
} }
cout << "TEST\n\n"; if(b->isValid()) cout << b->getTurn();
while(b->getTurn() == 'X' ) while(b->getTurn() == 'X' )
{ {
AI(); easyAI();
} }
gameOver = b->isGameOver(); gameOver = b->isGameOver();
b->setValidFalse();
b->snapshot(record, *b); b->snapshot(record, *b);
} }
@ -57,7 +61,11 @@ void Engine::startGame(){
void Engine::easyAI() void Engine::easyAI()
{ {
vector<moves> listOfMoves = b->viewPossibleMoves(); vector<moves> listOfMoves = b->viewPossibleMoves();
/*
for(int x = 0; x < listOfMoves.size(); ++x) {
cout << listOfMoves[x].row << " " << listOfMoves[x].column << " " << listOfMoves[x].moveType << "\n\n";
}
*/
srand(time(NULL)); srand(time(NULL));
int randomChoice = rand() % (listOfMoves.size()-1) - 0; int randomChoice = rand() % (listOfMoves.size()-1) - 0;
@ -70,61 +78,64 @@ void Engine::easyAI()
void Engine::AI(){ void Engine::AI(){
cout << "----------------------BEGIN AI FUNCTION----------------------\n"; cout << "----------------------BEGIN AI FUNCTION----------------------\n";
vector<moves> listOfMoves = b->viewPossibleMoves(); vector<moves> listOfMoves = b->viewPossibleMoves();
Board* temp = new Board(*b); //Board* b = new Board(*b);
//probably not needed, check later //probably not needed, check later
if (temp->getTurn() != 'X'){ /*
if (b->getTurn() != 'X'){
cout << "a changing of turns is needed. \n"; cout << "a changing of turns is needed. \n";
temp->changeTurns(); b->changeTurns();
} }
*/
//only doing 1 branch right now because testing //only doing 1 branch right now because testing
/*for (int i = 0; i < listOfMoves.size(); ++i){ /*for (int i = 0; i < listOfMoves.size(); ++i){
minMax(temp, listOfMoves[i]); minMax(b, listOfMoves[i]);
}*/ }*/
b->move(minMax(temp, listOfMoves[0], 0)); b->move(minMax(listOfMoves[0], 0));
b->changeTurns(); b->changeTurns();
//verification of correct turn //verification of correct turn
/*
if (b->getTurn() != 'O'){ if (b->getTurn() != 'O'){
cout << "ERROR in Engine::AI: b is on the wrong turn. \n"; cout << "ERROR in Engine::AI: b is on the wrong turn. \n";
} }
*/
b->displayBoard(); b->displayBoard();
cout << "----------------------END AI FUNCTION----------------------\n"; cout << "----------------------END AI FUNCTION----------------------\n";
} }
moves Engine::minMax(Board* temp, moves m, int c){ moves Engine::minMax(moves m, int c){
//testing purposes only, c = finite depth //testing purposes only, c = finite depth
/*
if (c > 5){ if (c > 5){
return m; return m;
} }
if (temp->isGameOver() == true){ if (b->isGameOver() == true){
cout << "END OF PATH REACHED\n"; cout << "END OF PATH REACHED\n";
return m; return m;
} }
else { else {
if(temp->isThisMovePossible(8 - m.row, temp->charToIntColumn(m.column), m.moveType)){ if(b->isThisMovePossible(8 - m.row, b->charToIntColumn(m.column), m.moveType)){
cout << "piece has been moved in minMax\n"; cout << "piece has been moved in minMax\n";
temp->move(m); b->move(m);
temp->changeTurns(); b->changeTurns();
} }
cout << "c: " << c << "\n\n"; cout << "c: " << c << "\n\n";
cout << "current turn: " << temp->getTurn() << "\n"; cout << "current turn: " << b->getTurn() << "\n";
vector<moves> listOfMoves = temp->viewPossibleMoves(); vector<moves> listOfMoves = b->viewPossibleMoves();
for (int i = 0; i < listOfMoves.size(); ++i){ for (int i = 0; i < listOfMoves.size(); ++i){
//return minMax(temp, listOfMoves[i]); //return minMax(b, listOfMoves[i]);
} }
temp->displayBoard(); b->displayBoard();
//limited recursion //limited recursion
return minMax(temp, listOfMoves[0], ++c); return minMax(b, listOfMoves[0], ++c);
//testing //testing
return m; return m;
} }*/
} }

View file

@ -12,5 +12,5 @@ public:
void startGame(); void startGame();
void easyAI(); void easyAI();
void AI(); void AI();
moves minMax(Board* temp, moves m, int c); moves minMax(moves m, int c);
}; };

View file

@ -42,7 +42,7 @@ void Piece::moveLeft(){
else if (type == 'O'){ else if (type == 'O'){
x--; x--;
y++; y--;
} }
else else
@ -57,7 +57,7 @@ void Piece::moveRight(){
else if (type == 'O'){ else if (type == 'O'){
x--; x--;
y--; y++;
} }
else else