working, still not stable
This commit is contained in:
parent
67a2997b8c
commit
b03a041719
5 changed files with 156 additions and 29 deletions
124
Board.cpp
124
Board.cpp
|
@ -81,6 +81,7 @@ bool Board::isGameOver()
|
||||||
|
|
||||||
void Board::changeTurns()
|
void Board::changeTurns()
|
||||||
{
|
{
|
||||||
|
cout << "TURN IS BEING CHANGED\n";
|
||||||
if (turn == 'O') turn = 'X';
|
if (turn == 'O') turn = 'X';
|
||||||
else turn = 'O';
|
else turn = 'O';
|
||||||
}
|
}
|
||||||
|
@ -156,10 +157,9 @@ void Board::move(moves jugada)
|
||||||
cout<<"ERROR: index out of bound in second move()!"<<endl;
|
cout<<"ERROR: index out of bound in second move()!"<<endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
int temp = boardArray[row][kolumn];
|
char temp = boardArray[row][kolumn];
|
||||||
if (temp != turn)
|
if (temp != turn)
|
||||||
{
|
{
|
||||||
//ssshhh
|
|
||||||
//cout<<"you can't move that piece at this turn!"<<endl;
|
//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)
|
bool Board::isThisMovePossible(int r, int c, string moveType)
|
||||||
{
|
{
|
||||||
char pieceToMove = boardArray[r][c];
|
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
|
if (pieceToMove != turn) //trying to move invalid piece
|
||||||
{
|
{
|
||||||
|
@ -421,3 +525,13 @@ void Board::snapshot(vector<Board>& inputVec, Board inputBoard)
|
||||||
|
|
||||||
inputVec.push_back(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";
|
||||||
|
}
|
||||||
|
}
|
2
Board.h
2
Board.h
|
@ -34,10 +34,12 @@ public:
|
||||||
char intToCharColumn(int input);
|
char intToCharColumn(int input);
|
||||||
void move(string inputMove);
|
void move(string inputMove);
|
||||||
void move(moves jugada);
|
void move(moves jugada);
|
||||||
|
void moveWOPrint(moves jugada);
|
||||||
bool isThisMovePossible(int r, int c, string moveType);
|
bool isThisMovePossible(int r, int c, string moveType);
|
||||||
vector<moves> viewPossibleMoves();
|
vector<moves> viewPossibleMoves();
|
||||||
string myToUpper(string input);
|
string myToUpper(string input);
|
||||||
void undo(Board& tablero);
|
void undo(Board& tablero);
|
||||||
void interpret(string input, Board& tablero);
|
void interpret(string input, Board& tablero);
|
||||||
void snapshot(vector<Board>& inputVec, Board inputBoard);
|
void snapshot(vector<Board>& inputVec, Board inputBoard);
|
||||||
|
void testPrint();
|
||||||
};
|
};
|
49
Engine.cpp
49
Engine.cpp
|
@ -85,41 +85,48 @@ 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 = b;
|
Board temp = *b;
|
||||||
if (temp->getTurn() != 'X'){
|
|
||||||
temp->changeTurns();
|
//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
|
//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(temp, listOfMoves[i]);
|
||||||
}*/
|
}*/
|
||||||
minMax(temp, listOfMoves[0]);
|
b->moveWOPrint(minMax(temp, listOfMoves[0], 0));
|
||||||
|
b->displayBoard();
|
||||||
b->move(listOfMoves[0]);
|
|
||||||
cout << "----------------------END AI FUNCTION----------------------\n";
|
cout << "----------------------END AI FUNCTION----------------------\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void Engine::minMax(Board* temp, moves m){
|
moves Engine::minMax(Board temp, moves m, int c){
|
||||||
if (temp->isGameOver() == true){
|
//testing purposes only
|
||||||
|
if (c > 1000){
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (temp.isGameOver() == true){
|
||||||
cout << "END OF PATH REACHED\n";
|
cout << "END OF PATH REACHED\n";
|
||||||
return;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
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";
|
for (int i = 0; i < listOfMoves.size(); ++i){
|
||||||
cout << "charToIntColumn(m.column): " << temp->charToIntColumn(m.column) << "\n";
|
cout << "listOfMoves[i]: (" << listOfMoves[i].row << ", " << listOfMoves[i].column << ")\n";
|
||||||
cout << "m.moveType: " << m.moveType << "\n";
|
//minMax(temp, listOfMoves[i]);
|
||||||
cout << "temp->isThisMovePossible(stuff): " << temp->isThisMovePossible(m.row, temp->charToIntColumn(m.column), m.moveType) << "\n\n\n";
|
}
|
||||||
|
|
||||||
vector<moves> listOfMoves = temp->viewPossibleMoves();
|
return minMax(temp, listOfMoves[0], ++c);
|
||||||
/*for (int i = 0; i < listOfMoves.size(); ++i){
|
|
||||||
minMax(temp, listOfMoves[i]);
|
|
||||||
}*/
|
|
||||||
|
|
||||||
temp->displayBoard();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
2
Engine.h
2
Engine.h
|
@ -12,5 +12,5 @@ public:
|
||||||
void startGame();
|
void startGame();
|
||||||
void easyAI();
|
void easyAI();
|
||||||
void AI();
|
void AI();
|
||||||
void minMax(Board* temp, moves m);
|
moves minMax(Board temp, moves m, int c);
|
||||||
};
|
};
|
4
test.cpp
4
test.cpp
|
@ -4,6 +4,10 @@ using namespace std;
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
//board testing
|
||||||
|
Board b;
|
||||||
|
|
||||||
|
//engine testing
|
||||||
Engine e;
|
Engine e;
|
||||||
e.startGame();
|
e.startGame();
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue