working, still not stable

This commit is contained in:
Rebecca Schofield 2015-10-27 08:38:25 -05:00
parent 67a2997b8c
commit b03a041719
5 changed files with 156 additions and 29 deletions

124
Board.cpp
View file

@ -81,6 +81,7 @@ bool Board::isGameOver()
void Board::changeTurns()
{
cout << "TURN IS BEING CHANGED\n";
if (turn == 'O') turn = 'X';
else turn = 'O';
}
@ -156,10 +157,9 @@ void Board::move(moves jugada)
cout<<"ERROR: index out of bound in second move()!"<<endl;
}
int temp = boardArray[row][kolumn];
char temp = boardArray[row][kolumn];
if (temp != turn)
{
//ssshhh
//cout<<"you can't move that piece at this turn!"<<endl;
}
@ -255,12 +255,116 @@ void Board::move(moves jugada)
}
//FIX THIS
//take out the prints and turn changes from move
void Board::moveWOPrint(moves jugada)
{
int row = 8 - (jugada.row);
int kolumn = charToIntColumn(jugada.column);
if (row > 8 || row < 0 || kolumn > 8 || kolumn < 0)
{
cout<<"ERROR: index out of bound in second move()!"<<endl;
}
char temp = boardArray[row][kolumn];
if (temp != turn)
{
//cout<<"you can't move that piece at this turn!"<<endl;
}
else if (temp == '_')
{
cout<<"there's no piece in that spot!"<<endl;
}
int reflector = 1;
if (temp == 'X' || temp == 'O')
{
if (temp == 'O')
{
reflector *= -1;
}
if (jugada.moveType == "FWD")
{
if(boardArray[row+reflector][kolumn] != '_')
{
cout<<"you can't move that piece forward"<<endl;
}
else
{
boardArray[row][kolumn] = '_';
boardArray[row+reflector][kolumn] = temp;
changeTurns();
}
}
else if (jugada.moveType == "LEFT")
{
if (kolumn == 0)
{
cout<<"Destination Spot out of range!"<<endl;
}
else if (boardArray[row+reflector][kolumn-1] == temp)
{
cout<<"you hate your own team or something? you can't do that!"<<endl;
}
else
{
boardArray[row][kolumn] = '_';
boardArray[row+reflector][kolumn-1] = temp;
changeTurns();
}
}
else if (jugada.moveType == "RIGHT")
{
if (kolumn == 7)
{
cout<<"Destination Spot out of range!"<<endl;
}
else if (boardArray[row+reflector][kolumn+1] == temp)
{
cout<<"you hate your own team or something? you can't do that!"<<endl;
}
else
{
boardArray[row][kolumn] = '_';
boardArray[row+reflector][kolumn+1] = temp;
changeTurns();
}
}
else
{
cout<<"Unrecognized movetype!"<<endl;
}
}
else
{
cout<<"Invalid piece2!"<<endl;
}
}
bool Board::isThisMovePossible(int r, int c, string moveType)
{
char pieceToMove = boardArray[r][c];
cout << "r: " << r << "\n";
cout << "c: " << c << "\n";
cout << "pieceToMove: " << pieceToMove << "\n";
if (pieceToMove != turn) //trying to move invalid piece
{
@ -421,3 +525,13 @@ void Board::snapshot(vector<Board>& inputVec, Board inputBoard)
inputVec.push_back(inputBoard);
}
void Board::testPrint(){
for (int i = 7; i >= 0; --i){
for (int j = 7; j >= 0; --j){
cout << boardArray[i][j] << " ";
}
cout << "\n";
}
}

View file

@ -34,10 +34,12 @@ public:
char intToCharColumn(int input);
void move(string inputMove);
void move(moves jugada);
void moveWOPrint(moves jugada);
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);
void testPrint();
};

View file

@ -85,41 +85,48 @@ void Engine::easyAI()
void Engine::AI(){
cout << "----------------------BEGIN AI FUNCTION----------------------\n";
vector<moves> listOfMoves = b->viewPossibleMoves();
Board* temp = b;
if (temp->getTurn() != 'X'){
temp->changeTurns();
Board temp = *b;
//probably not needed, check later
if (temp.getTurn() != 'X'){
cout << "a changing of turns is needed. \n";
temp.changeTurns();
}
//temp->changeTurns();
//cout << "after: " << temp->getTurn() << "\n";
//only doing 1 branch right now because testing
/*for (int i = 0; i < listOfMoves.size(); ++i){
minMax(temp, listOfMoves[i]);
}*/
minMax(temp, listOfMoves[0]);
b->move(listOfMoves[0]);
b->moveWOPrint(minMax(temp, listOfMoves[0], 0));
b->displayBoard();
cout << "----------------------END AI FUNCTION----------------------\n";
}
void Engine::minMax(Board* temp, moves m){
if (temp->isGameOver() == true){
moves Engine::minMax(Board temp, moves m, int c){
//testing purposes only
if (c > 1000){
return m;
}
if (temp.isGameOver() == true){
cout << "END OF PATH REACHED\n";
return;
return m;
}
else {
if(temp.isThisMovePossible(8 - m.row, temp.charToIntColumn(m.column), m.moveType)){
cout << "piece has been moved in minMax\n";
temp.moveWOPrint(m);
}
cout << "c: " << c << "\n\n";
cout << "current turn: " << temp.getTurn() << "\n";
vector<moves> listOfMoves = temp.viewPossibleMoves();
cout << "m.row: " << m.row << "\n";
cout << "charToIntColumn(m.column): " << temp->charToIntColumn(m.column) << "\n";
cout << "m.moveType: " << m.moveType << "\n";
cout << "temp->isThisMovePossible(stuff): " << temp->isThisMovePossible(m.row, temp->charToIntColumn(m.column), m.moveType) << "\n\n\n";
for (int i = 0; i < listOfMoves.size(); ++i){
cout << "listOfMoves[i]: (" << listOfMoves[i].row << ", " << listOfMoves[i].column << ")\n";
//minMax(temp, listOfMoves[i]);
}
vector<moves> listOfMoves = temp->viewPossibleMoves();
/*for (int i = 0; i < listOfMoves.size(); ++i){
minMax(temp, listOfMoves[i]);
}*/
temp->displayBoard();
return minMax(temp, listOfMoves[0], ++c);
}
}

View file

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

View file

@ -4,6 +4,10 @@ using namespace std;
int main()
{
//board testing
Board b;
//engine testing
Engine e;
e.startGame();
}