From 010a85325b3501179eca6610d2f4d9003101269d Mon Sep 17 00:00:00 2001 From: Rebecca Schofield Date: Tue, 27 Oct 2015 11:31:44 -0500 Subject: [PATCH] stable restructure, not finished --- Board.cpp | 251 ++++++++++++++++++++++++++--------------------------- Board.h | 13 +-- Engine.cpp | 37 ++++---- Piece.h | 1 - test.cpp | 5 +- 5 files changed, 151 insertions(+), 156 deletions(-) diff --git a/Board.cpp b/Board.cpp index b423b00..f02ebbb 100755 --- a/Board.cpp +++ b/Board.cpp @@ -8,18 +8,22 @@ Board::Board() { for (int i = 0; i < 2; ++i) { for (int j = 0; j < 8; ++j) { xpieces.push_back(new Piece(i, j, 'X')); + pieces.push_back(new Piece(i, j, 'X')); } } + for (int i = 6; i < 8; ++i) { for (int j = 0; j < 8; ++j) { opieces.push_back(new Piece(i, j, 'O')); + pieces.push_back(new Piece(i, j, 'O')); } } } -bool Board::elemInXs(int r, int c){ - for (int i = 0; i < xpieces.size(); ++i){ - if (xpieces[i]->getX() == r && xpieces[i]->getY() == c){ +//make this efficient! +bool Board::isPiece(int r, int c){ + for (int i = 0; i < pieces.size(); ++i){ + if (pieces[i]->getX() == r && pieces[i]->getY() == c){ return true; } } @@ -27,18 +31,18 @@ bool Board::elemInXs(int r, int c){ return false; } -bool Board::elemInOs(int r, int c){ - for (int i = 0; i < opieces.size(); ++i){ - if (opieces[i]->getX() == r && opieces[i]->getY() == c){ - return true; +//make this efficient! +Piece* Board::getPiece(int r, int c){ + for (int i = 0; i < pieces.size(); ++i){ + if (pieces[i]->getX() == r && pieces[i]->getY() == c){ + return pieces[i]; } } - return false; + return new Piece(); } - -moves Board::parse(string input) -{ + +moves Board::parse(string input){ input = myToUpper(input); int temp1; @@ -66,43 +70,40 @@ moves Board::parse(string input) moves output(temp1, temp2, temp3); return output; - } -bool Board::isGameOver() -{ +bool Board::isGameOver(){ for (int i = 0; i < xpieces.size(); ++i){ - if (xpieces[i]->getX() == 0){ + if (xpieces[i]->getX() == 7){ cout<<"\n\n\nPlayer X wins!\n\n\n"<getX() == 7){ + if (opieces[i]->getX() == 0){ cout<<"\n\n\nPlayer O wins!\n\n\n"<getType() == 'X') + cout << "|" << "X"; + else + cout << "|" << "O"; else cout << "|" << "_"; } @@ -114,8 +115,7 @@ void Board::displayBoard() cout<<"turn: "< 8 || row < 0 || column > 8 || column < 0) { cout<<"ERROR: index out of bound."<getType(); - cout << "temp: " << temp << "\n"; + Piece* piece; + if (isPiece(row, column)) + piece = getPiece(row, column); - if (temp != turn || temp == '_') { - cout<<"ERROR: attempting to move an invalid piece."<getType() != turn) { + cout<<"ERROR: attempting to move the wrong side's piece.\n"; } else { if (jugada.moveType == "FWD") { - boardArray[row][column]->moveFwd(); + piece->moveFwd(); } else if (jugada.moveType == "LEFT") { - if (column == 0) - cout<<"Destination Spot out of range!"<getType() == temp) - cout<<"you hate your own team or something? you can't do that!"<moveLeft(); + //add error checking + piece->moveLeft(); } else if (jugada.moveType == "RIGHT") { - if (column == 7) - cout<<"Destination Spot out of range!"<getType() == temp) - cout<<"you hate your own team or something? you can't do that!"<moveRight(); + //add error checking + piece->moveRight(); } } } -*/ -//FIX THIS -//take out the prints and turn changes from move -void Board::moveWOPrint(moves jugada) -{ - // -} -/* -bool Board::isThisMovePossible(int r, int c, string moveType) -{ - char pieceToMove = boardArray[r][c]->getType(); - if (pieceToMove != turn) //trying to move invalid piece - { +bool Board::isThisMovePossible(int r, int c, string moveType){ + Piece* piece; + if (isPiece(r, c)) + piece = getPiece(r, c); + else + return false; + + if (piece->getType() != turn) { cout << "Error in Board::isThisMovePossible: trying to move a piece outside your turn.\n"; return false; } - int reflector = 1; - - if (pieceToMove == 'O') - { - reflector *= -1; - } - - else - { - if (moveType == "FWD") - { - if (boardArray[r+reflector][c]->getType() == '_') + else{ + int reflector = 1; + + if (piece->getType() == 'O') + reflector *= -1; + + if (moveType == "FWD"){ + if (isPiece(r+reflector, c)) return true; else return false; } - else if (moveType == "RIGHT") - { - if (boardArray[r+reflector][c+1]->getType() != pieceToMove && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 <= 7) ) + else if (moveType == "RIGHT"){ + if (isPiece(r+reflector, c+1) && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 <= 7)) return true; else return false; } - else if (moveType == "LEFT") - { - if (boardArray[r+reflector][c-1]->getType() != pieceToMove && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 >= 0) ) + else if (moveType == "LEFT"){ + if (isPiece(r+reflector, c+1) && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 >= 0)) return true; else return false; @@ -259,43 +237,62 @@ bool Board::isThisMovePossible(int r, int c, string moveType) } } -vector Board::viewPossibleMoves() -{ +vector Board::viewPossibleMoves(){ + int r, c = -1; vector output; - for (int r = 0; r < 8; ++r) - { - for (int c = 0; c < 8; ++c) - { - if (boardArray[r][c]->getType() == turn) + if (turn == 'X'){ + for (int i = 0; i < xpieces.size(); ++i){ + r = xpieces[i]->getX(); + c = xpieces[i]->getY(); + if (isThisMovePossible(r, c, "FWD")) { - if (isThisMovePossible(r,c,"FWD")) - { - moves temp(8-r,intToCharColumn(c+1),"FWD"); - output.push_back(temp); - } - - if (isThisMovePossible(r,c,"LEFT")) - { - moves temp(8-r,intToCharColumn(c+1),"LEFT"); - output.push_back(temp); - } - - if (isThisMovePossible(r,c,"RIGHT")) - { - moves temp(8-r,intToCharColumn(c+1),"RIGHT"); - output.push_back(temp); - } - + moves temp(8-r,intToCharColumn(c+1), "FWD"); + output.push_back(temp); + } + + if (isThisMovePossible(r,c,"LEFT")) + { + moves temp(8-r,intToCharColumn(c+1), "LEFT"); + output.push_back(temp); + } + + if (isThisMovePossible(r,c,"RIGHT")) + { + moves temp(8-r,intToCharColumn(c+1), "RIGHT"); + output.push_back(temp); + } + } + } + + else { + for (int i = 0; i < opieces.size(); ++i){ + r = opieces[i]->getX(); + c = opieces[i]->getY(); + if (isThisMovePossible(r, c, "FWD")) + { + moves temp(8-r,intToCharColumn(c+1), "FWD"); + output.push_back(temp); + } + + if (isThisMovePossible(r,c,"LEFT")) + { + moves temp(8-r,intToCharColumn(c+1), "LEFT"); + output.push_back(temp); + } + + if (isThisMovePossible(r,c,"RIGHT")) + { + moves temp(8-r,intToCharColumn(c+1), "RIGHT"); + output.push_back(temp); } } } return output; } -*/ -string Board::myToUpper(string input) -{ + +string Board::myToUpper(string input){ string output; for (int i = 0 ; i < input.size(); ++i) @@ -314,8 +311,7 @@ string Board::myToUpper(string input) } -void Board::undo(Board& tablero) -{ +void Board::undo(Board& tablero){ vector record; if (record.size() < 2) @@ -336,9 +332,8 @@ void Board::undo(Board& tablero) record.pop_back(); } } -/* -void Board::interpret(string input, Board& tablero) //determines what kind of command its input is -{ + +void Board::interpret(string input, Board& tablero){ vector record; input = myToUpper(input); @@ -360,9 +355,8 @@ void Board::interpret(string input, Board& tablero) //determines what kind of co else tablero.move(input); } -*/ -void Board::snapshot(vector& inputVec, Board inputBoard) -{ + +void Board::snapshot(vector& inputVec, Board inputBoard){ if (inputVec.size() == 10) { inputVec.erase(inputVec.begin()); @@ -390,4 +384,9 @@ int Board::evaluate(char max, char min){ cout << "Unidentified max, must be either X or O.\n"; return 0; } -} \ No newline at end of file +} + + + + + diff --git a/Board.h b/Board.h index 8f3284c..c4bf1f6 100755 --- a/Board.h +++ b/Board.h @@ -22,13 +22,14 @@ class Board { //vector> boardArray; vector xpieces; vector opieces; + vector pieces; //char boardArray [8][8]; char turn = 'O'; public: Board(); - bool elemInXs(int r, int c); - bool elemInOs(int r, int c); + bool isPiece(int r, int c); + Piece* getPiece(int r, int c); moves parse(string input); char getTurn() { return turn; } bool isGameOver(); @@ -36,11 +37,11 @@ public: void displayBoard(); int charToIntColumn(char input); char intToCharColumn(int input); - //void move(string inputMove); - //void move(moves jugada); + void move(string inputMove); + void move(moves jugada); void moveWOPrint(moves jugada); - //bool isThisMovePossible(int r, int c, string moveType); - //vector viewPossibleMoves(); + bool isThisMovePossible(int r, int c, string moveType); + vector viewPossibleMoves(); string myToUpper(string input); void undo(Board& tablero); void interpret(string input, Board& tablero); diff --git a/Engine.cpp b/Engine.cpp index c68e650..3aebf01 100755 --- a/Engine.cpp +++ b/Engine.cpp @@ -38,13 +38,11 @@ void Engine::startGame(){ cin>>move; cout << "\n"; b->interpret(move, *b); + b->changeTurns(); } - b->changeTurns(); - while(b->getTurn() == 'X' ) { - cout << "\n\n\n\nit gets here\n\n\n\n"; easyAI(); } @@ -56,23 +54,22 @@ void Engine::startGame(){ void Engine::easyAI() { - //1) see all possible movements vector listOfMoves = b->viewPossibleMoves(); - //obvious moves - if (false){ - b->changeTurns(); - } - - //random - else { - srand(time(NULL)); - int randomChoice = rand() % (listOfMoves.size()-1) - 0; // choose a move betwen listOfMoves[0] to last element - - //3) execute movement - int temp = randomChoice; - b->move(listOfMoves[randomChoice]); - } + cout << listOfMoves[0].row << "\n"; + cout << listOfMoves[0].column << "\n"; + cout << listOfMoves[0].moveType << "\n\n"; + b->move(listOfMoves[0]); + b->changeTurns(); + /* + srand(time(NULL)); + int randomChoice = rand() % (listOfMoves.size()-1) - 0; // choose a move betwen listOfMoves[0] to last element + + //3) execute movement + int temp = randomChoice; + b->move(listOfMoves[randomChoice]); + b->changeTurns(); + */ } void Engine::AI(){ @@ -91,7 +88,7 @@ void Engine::AI(){ minMax(temp, listOfMoves[i]); }*/ - b->moveWOPrint(minMax(temp, listOfMoves[0], 0)); + //b->moveWOPrint(minMax(temp, listOfMoves[0], 0)); //verification of correct turn if (b->getTurn() != 'O'){ @@ -116,7 +113,7 @@ moves Engine::minMax(Board temp, moves m, int c){ else { if(temp.isThisMovePossible(8 - m.row, temp.charToIntColumn(m.column), m.moveType)){ cout << "piece has been moved in minMax\n"; - temp.moveWOPrint(m); + //temp.moveWOPrint(m); } cout << "c: " << c << "\n\n"; cout << "current turn: " << temp.getTurn() << "\n"; diff --git a/Piece.h b/Piece.h index 60a3a00..cc27d24 100755 --- a/Piece.h +++ b/Piece.h @@ -19,7 +19,6 @@ public: int getY(){ return y; } void setY(int c){ y = c; } char getType(){ return type; } - void setType(char t){ type = t; } void makeEmpty(){ type = '_'; } void isTaken(); }; \ No newline at end of file diff --git a/test.cpp b/test.cpp index fd5452f..ba030f6 100755 --- a/test.cpp +++ b/test.cpp @@ -7,9 +7,8 @@ int main() { //board testing Board b; - b.displayBoard(); //engine testing - //Engine e; - //e.startGame(); + Engine e; + e.startGame(); }