fixed undo
This commit is contained in:
parent
39c2da093a
commit
9dfa13b85d
6 changed files with 27 additions and 31 deletions
29
Board.cpp
29
Board.cpp
|
@ -8,7 +8,7 @@
|
|||
using namespace std;
|
||||
|
||||
Board::Board() {
|
||||
Piece* temp;
|
||||
Piece* temp;
|
||||
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
for (int j = 0; j < 8; ++j) {
|
||||
|
@ -219,8 +219,8 @@ string Board::boardToString(){
|
|||
Board Board::move(moves m){
|
||||
int row = m.row;
|
||||
int column = m.column;
|
||||
|
||||
Piece* piece;
|
||||
|
||||
if (isPiece(row, column))
|
||||
piece = getPiece(row, column);
|
||||
|
||||
|
@ -231,11 +231,13 @@ Board Board::move(moves m){
|
|||
|
||||
if (piece->getType() != turn) {
|
||||
cout<<"ERROR: attempting to move the wrong side's piece.\n";
|
||||
return *this;
|
||||
}
|
||||
|
||||
else {
|
||||
if(isThisMovePossible(row, column, m.moveType))
|
||||
{
|
||||
record.push_back(*this);
|
||||
if (m.moveType == "FWD") {
|
||||
piece->moveFwd();
|
||||
}
|
||||
|
@ -282,6 +284,7 @@ Board Board::move(moves m){
|
|||
}
|
||||
setValidTrue();
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
cout << "Invalid move.\n\n";
|
||||
|
@ -435,22 +438,18 @@ string Board::myToUpper(string input){
|
|||
return output;
|
||||
}
|
||||
|
||||
void Board::undo(Board& tablero){
|
||||
vector<Board> record;
|
||||
|
||||
if (record.size() < 2){
|
||||
cout<<"nothing to undo"<<endl;
|
||||
Board* Board::undo(){
|
||||
//assuming this will only be called by a player, against an AI
|
||||
if (record.size() == 0){
|
||||
cout<< "ERROR: there is nothing to undo";
|
||||
return this;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
Board* temp = new Board(record[record.size()-2]);
|
||||
return temp;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Board::snapshot(vector<Board>& inputVec, Board inputBoard){
|
||||
|
|
6
Board.h
6
Board.h
|
@ -28,12 +28,14 @@ class Board {
|
|||
vector<Piece*> xpieces;
|
||||
vector<Piece*> opieces;
|
||||
vector<Piece*> pieces;
|
||||
vector<Board> record;
|
||||
bool valid, xtaken, otaken;
|
||||
char turn = 'O';
|
||||
char turn = 'O';
|
||||
|
||||
public:
|
||||
Board();
|
||||
Board(const Board& b);
|
||||
vector<Board> getRecord() const{ return record; }
|
||||
void setValidFalse(){ valid = false; }
|
||||
void setValidTrue(){ valid = true; }
|
||||
bool isValid(){ return valid; }
|
||||
|
@ -54,7 +56,7 @@ public:
|
|||
bool isThisMovePossible(int r, int c, string moveType);
|
||||
vector<moves> viewPossibleMoves();
|
||||
string myToUpper(string input);
|
||||
void undo(Board& tablero);
|
||||
Board* undo();
|
||||
void snapshot(vector<Board>& inputVec, Board inputBoard);
|
||||
int evaluate(char max, char min);
|
||||
};
|
||||
|
|
|
@ -13,9 +13,11 @@ Engine::Engine(){
|
|||
b = brd;
|
||||
alpha = INT_MIN;
|
||||
beta = INT_MAX;
|
||||
undo_cnt = 0;
|
||||
}
|
||||
|
||||
void Engine::startGame(){
|
||||
undo_cnt = 0;
|
||||
cout<<"WELCOME\n";
|
||||
|
||||
cout<<"1. Play against AI?\n";
|
||||
|
|
2
Engine.h
2
Engine.h
|
@ -8,7 +8,7 @@ using namespace std;
|
|||
|
||||
class Engine {
|
||||
Board* b;
|
||||
int alpha, beta;
|
||||
int alpha, beta, undo_cnt;
|
||||
|
||||
public:
|
||||
Engine();
|
||||
|
|
19
Parser.cpp
19
Parser.cpp
|
@ -29,19 +29,18 @@ string myToUpper(string input) {
|
|||
}
|
||||
|
||||
void parseAndMove(vector<string> tokens, Board& board) {
|
||||
//Debugging
|
||||
int col;
|
||||
int row;
|
||||
|
||||
if(tokens[0].size() == 2) {
|
||||
if(tokens[1].size() >= 3 && tokens[1].size() <= 5) {
|
||||
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";
|
||||
return;
|
||||
}
|
||||
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;
|
||||
}
|
||||
else {
|
||||
|
@ -64,29 +63,23 @@ void parseAndMove(vector<string> tokens, Board& board) {
|
|||
}
|
||||
}
|
||||
else {
|
||||
//cout << "Error. Invalid move location. (size)\n\n";
|
||||
cout << "Error. Invalid move location. (size)\n\n";
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void parseCmd(vector<string> tokens, Board& board) {
|
||||
if(tokens[0] == "UNDO") {
|
||||
//Debugging
|
||||
//cout << "Testing UNDO.\n\n";
|
||||
//Change when you fix undo.
|
||||
board.undo(board);
|
||||
board = *(board.undo());
|
||||
}
|
||||
|
||||
else if(tokens[0] == "DISPLAYRECORD") {
|
||||
//Debugging
|
||||
//cout << "Testing DISPLAYRECORD.\n\n";
|
||||
/*Fix record, uncomment this.
|
||||
cout << "recordsize: " << board->getRecord.size();
|
||||
cout << "record size: " << board.getRecord().size();
|
||||
vector<Board> record = board.getRecord();
|
||||
|
||||
for(int i = 0; i < record.size(); ++i) {
|
||||
record[i].displayBoard();
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
else {
|
||||
|
|
BIN
a.out
Executable file
BIN
a.out
Executable file
Binary file not shown.
Reference in a new issue