undo command works
This commit is contained in:
parent
a2f5023881
commit
78c5605d4c
1 changed files with 154 additions and 12 deletions
160
main.cpp
160
main.cpp
|
@ -4,6 +4,7 @@
|
|||
#include <stdio.h>
|
||||
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
// class piece
|
||||
|
@ -31,7 +32,6 @@ using namespace std;
|
|||
// }
|
||||
// };
|
||||
|
||||
|
||||
string myToUpper(string input)
|
||||
{
|
||||
string output;
|
||||
|
@ -72,8 +72,14 @@ struct moves
|
|||
column = columna;
|
||||
moveType = m;
|
||||
}
|
||||
|
||||
void display()
|
||||
{
|
||||
cout<<column<<row<<" "<<moveType<<endl;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class Board
|
||||
{
|
||||
char boardArray [8][8];
|
||||
|
@ -113,6 +119,16 @@ class Board
|
|||
|
||||
}
|
||||
|
||||
char elementAt(int r, int k)
|
||||
{
|
||||
return boardArray[r][k];
|
||||
}
|
||||
|
||||
void modifyAt(int r, int k, char input)
|
||||
{
|
||||
boardArray[r][k] = input;
|
||||
}
|
||||
|
||||
moves parse(string input)
|
||||
{
|
||||
|
||||
|
@ -491,20 +507,30 @@ class Board
|
|||
if (moveType == "FWD")
|
||||
{
|
||||
|
||||
if (boardArray[r+reflector][c] == '_') return true;
|
||||
if (boardArray[r+reflector][c] == '_')
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
else return false;
|
||||
|
||||
}
|
||||
|
||||
else if (moveType == "RIGHT")
|
||||
{
|
||||
if (boardArray[r+reflector][c+1] != pieceToMove && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 <= 7) ) return true;
|
||||
if (boardArray[r+reflector][c+1] != pieceToMove && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 <= 7) )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else return false;
|
||||
}
|
||||
|
||||
else if (moveType == "LEFT")
|
||||
{
|
||||
if (boardArray[r+reflector][c-1] != pieceToMove && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 >= 0) ) return true;
|
||||
if (boardArray[r+reflector][c-1] != pieceToMove && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 >= 0) )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else return false;
|
||||
}
|
||||
|
||||
|
@ -525,6 +551,7 @@ class Board
|
|||
{
|
||||
if (boardArray[r][c] == turn)
|
||||
{
|
||||
|
||||
if (isThisMovePossible(r,c,"FWD"))
|
||||
{
|
||||
moves temp(8-r,intToCharColumn(c+1),"FWD");
|
||||
|
@ -547,6 +574,11 @@ class Board
|
|||
}
|
||||
}
|
||||
|
||||
// cout<<"\nnumber of possible choices: "<<output.size()<<endl; //for debugging purposes
|
||||
// for (int i = 0; i < output.size(); ++i)
|
||||
// {
|
||||
// output[i].display();
|
||||
// }
|
||||
return output;
|
||||
}
|
||||
|
||||
|
@ -573,7 +605,56 @@ class Board
|
|||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}; //end Board class
|
||||
|
||||
vector<Board> record;
|
||||
|
||||
void undo(Board& tablero)
|
||||
{
|
||||
if (record.size() < 2)
|
||||
{
|
||||
cout<<"nothing to undo"<<endl;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
for (int r = 0; r < 8; ++r)
|
||||
{
|
||||
for (int k = 0; k < 8; ++k)
|
||||
{
|
||||
tablero.modifyAt(r,k,(record[record.size()-2]).elementAt(r,k));
|
||||
}
|
||||
}
|
||||
record.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void interpret(string input, Board& tablero) //determines what kind of command its input is
|
||||
{
|
||||
input = myToUpper(input);
|
||||
|
||||
if (input == "UNDO")
|
||||
{
|
||||
undo(tablero);
|
||||
}
|
||||
|
||||
else if (input == "DISPLAYRECORD") //for debugging purposes
|
||||
{
|
||||
cout<<"record: "<<endl;
|
||||
cout<<record.size();
|
||||
for (int i = 0; i < record.size(); ++i)
|
||||
{
|
||||
record[i].displayBoard();
|
||||
}
|
||||
cout<<"---------------------------------------------------END DISPLAY RECORD------------------------"<<endl;
|
||||
}
|
||||
|
||||
else tablero.move(input);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void displayPossibleMoves(vector<moves> input)
|
||||
|
@ -585,13 +666,43 @@ void displayPossibleMoves(vector<moves> input)
|
|||
}
|
||||
}
|
||||
|
||||
void snapshot(vector<Board>& inputVec, Board inputBoard)
|
||||
{
|
||||
if (inputVec.size() == 10)
|
||||
{
|
||||
inputVec.erase(inputVec.begin());
|
||||
}
|
||||
|
||||
else if (inputVec.size() > 10)
|
||||
{
|
||||
cout<<"QUEUE OVERFLOW!"<<endl;
|
||||
}
|
||||
|
||||
|
||||
inputVec.push_back(inputBoard);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
cout<<"\n----------------------------"<<endl;
|
||||
cout<<"Welcome to Breakthrough\n"<<endl;
|
||||
cout<<"playing with AI..."<<endl;
|
||||
cout<<"-----------------------------\n\n"<<endl;
|
||||
|
||||
cout<<"1. Play agains AI?"<<endl;
|
||||
cout<<"2. Play against a human?"<<endl;
|
||||
cout<<"Enter choice: "<<endl;
|
||||
|
||||
int choice;
|
||||
cin>>choice;
|
||||
|
||||
if (choice == 1) cout<<"playing with AI..."<<endl;
|
||||
else cout<<"playing with human..."<<endl;
|
||||
|
||||
Board b;
|
||||
|
||||
|
@ -599,6 +710,8 @@ int main()
|
|||
|
||||
bool gameOver = false;
|
||||
|
||||
snapshot(record,b);
|
||||
|
||||
while (gameOver != true)
|
||||
{
|
||||
gameOver = b.isGameOver();
|
||||
|
@ -606,19 +719,48 @@ int main()
|
|||
while(b.getTurn() == 'O' )
|
||||
{
|
||||
b.displayBoard();
|
||||
cout<<"\nEnter move: ";
|
||||
cout<<"\nEnter command: ";
|
||||
cin>>move;
|
||||
b.move(move);
|
||||
interpret(move,b);
|
||||
}
|
||||
|
||||
|
||||
vector<moves> possibleMoves = b.viewPossibleMoves();
|
||||
//displayPossibleMoves(possibleMoves); for debugging purposes - AI
|
||||
|
||||
|
||||
if (choice == 1)
|
||||
{
|
||||
b.easyAI();
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
while(b.getTurn() == 'X' )
|
||||
{
|
||||
b.displayBoard();
|
||||
cout<<"\nEnter command: ";
|
||||
cin>>move;
|
||||
interpret(move,b);
|
||||
}
|
||||
}
|
||||
|
||||
//b.snapshot();
|
||||
gameOver = b.isGameOver();
|
||||
|
||||
snapshot(record,b);
|
||||
}
|
||||
|
||||
//for debugging purposes
|
||||
cout<<"Record:"<<endl;
|
||||
|
||||
|
||||
cout<<record.size();
|
||||
for (int i = 0; i < record.size(); ++i)
|
||||
{
|
||||
record[i].displayBoard();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Reference in a new issue