This commit is contained in:
Alexander Huddleston 2015-10-27 14:55:48 -05:00
commit 9fe535b10c
5 changed files with 81 additions and 44 deletions

View file

@ -5,21 +5,42 @@
using namespace std;
Board::Board() {
Piece* temp;
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'));
temp = new Piece(i, j, 'X');
xpieces.push_back(temp);
pieces.push_back(temp);
}
}
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'));
temp = new Piece(i, j, 'O');
opieces.push_back(temp);
pieces.push_back(temp);
}
}
}
Board::Board(const Board& b) {
vector<Piece*> xp = b.getXPieces();
vector<Piece*> op = b.getOPieces();
Piece* temp;
for (int i = 0; i < xp.size(); ++i) {
temp = new Piece(xp[i]->getX(), xp[i]->getY(), 'X');
xpieces.push_back(temp);
pieces.push_back(temp);
}
for (int i = 0; i < op.size(); ++i) {
temp = new Piece(op[i]->getX(), op[i]->getY(), 'O');
opieces.push_back(temp);
pieces.push_back(temp);
}
}
//make this efficient!
bool Board::isPiece(int r, int c){
for (int i = 0; i < pieces.size(); ++i){
@ -190,13 +211,13 @@ char Board::intToCharColumn(int input){
}
void Board::move(string inputMove){
moves jugada = parse(inputMove);
move(jugada);
moves m = parse(inputMove);
move(m);
}
void Board::move(moves jugada){
int row = 8 - (jugada.row);
int column = charToIntColumn(jugada.column);
void Board::move(moves m){
int row = 8 - (m.row);
int column = charToIntColumn(m.column);
//cout << row << " " << column << "\n\n";
@ -234,9 +255,21 @@ void Board::move(moves jugada){
else {
piece->moveFwd();
}
/*
=======
if (!(isThisMovePossible(row, column, m.moveType))){
cout << "Unable to move: impossible move.\n";
//add a try again
return;
}
else if (jugada.moveType == "LEFT") {
if (m.moveType == "FWD") {
piece->moveFwd();
>>>>>>> beccadev
*/
}
else if (m.moveType == "LEFT") {
//add error checking
if(isPiece(row--, column--)) {
if(getPiece(row--, column--)->getType() != piece->getType()) {
@ -253,7 +286,7 @@ void Board::move(moves jugada){
}
}
else if (jugada.moveType == "RIGHT") {
else if (m.moveType == "RIGHT") {
//add error checking
if(isPiece(row--, column++)) {
if(getPiece(row--, column++)->getType() != piece->getType()) {
@ -291,24 +324,24 @@ bool Board::isThisMovePossible(int r, int c, string moveType){
reflector *= -1;
if (moveType == "FWD"){
if (isPiece(r+reflector, c))
return false;
else
if (!isPiece(r + reflector, c))
return true;
else
return false;
}
else if (moveType == "RIGHT"){
if (isPiece(r+reflector, c+1) && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 <= 7))
return false;
else
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 (isPiece(r+reflector, c+1) && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 >= 0))
return false;
else
if (!isPiece(r+reflector, c+1) && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 >= 0))
return true;
else
return false;
}
else return false;
@ -343,7 +376,7 @@ vector<moves> Board::viewPossibleMoves(){
}
}
else {
else if (turn == 'X') {
for (int i = 0; i < opieces.size(); ++i){
r = opieces[i]->getX();
c = opieces[i]->getY();

View file

@ -19,17 +19,18 @@ struct moves {
};
class Board {
//vector<vector<char>> boardArray;
vector<Piece*> xpieces;
vector<Piece*> opieces;
vector<Piece*> pieces;
//char boardArray [8][8];
char turn = 'O';
public:
Board();
Board(const Board& b);
bool isPiece(int r, int c);
Piece* getPiece(int r, int c);
vector<Piece*> getXPieces() const { return xpieces; }
vector<Piece*> getOPieces() const { return opieces; }
moves parse(string input);
char getTurn() { return turn; }
bool isGameOver();

View file

@ -45,7 +45,7 @@ void Engine::startGame(){
while(b->getTurn() == 'X' )
{
easyAI();
AI();
}
gameOver = b->isGameOver();
@ -62,6 +62,7 @@ void Engine::easyAI()
int randomChoice = rand() % (listOfMoves.size()-1) - 0;
int temp = randomChoice;
cout << "easy AI move: " << listOfMoves[randomChoice].row << listOfMoves[randomChoice].column << listOfMoves[randomChoice].moveType << "\n";
b->move(listOfMoves[randomChoice]);
b->changeTurns();
}
@ -69,12 +70,12 @@ void Engine::easyAI()
void Engine::AI(){
cout << "----------------------BEGIN AI FUNCTION----------------------\n";
vector<moves> listOfMoves = b->viewPossibleMoves();
Board temp = *b;
Board* temp = new Board(*b);
//probably not needed, check later
if (temp.getTurn() != 'X'){
if (temp->getTurn() != 'X'){
cout << "a changing of turns is needed. \n";
temp.changeTurns();
temp->changeTurns();
}
//only doing 1 branch right now because testing
@ -82,7 +83,8 @@ void Engine::AI(){
minMax(temp, listOfMoves[i]);
}*/
//b->moveWOPrint(minMax(temp, listOfMoves[0], 0));
b->move(minMax(temp, listOfMoves[0], 0));
b->changeTurns();
//verification of correct turn
if (b->getTurn() != 'O'){
@ -93,30 +95,32 @@ void Engine::AI(){
cout << "----------------------END AI FUNCTION----------------------\n";
}
moves Engine::minMax(Board temp, moves m, int c){
//testing purposes only
moves Engine::minMax(Board* temp, moves m, int c){
//testing purposes only, c = finite depth
if (c > 5){
return m;
}
if (temp.isGameOver() == true){
if (temp->isGameOver() == true){
cout << "END OF PATH REACHED\n";
return m;
}
else {
if(temp.isThisMovePossible(8 - m.row, temp.charToIntColumn(m.column), m.moveType)){
if(temp->isThisMovePossible(8 - m.row, temp->charToIntColumn(m.column), m.moveType)){
cout << "piece has been moved in minMax\n";
//temp.moveWOPrint(m);
temp->move(m);
temp->changeTurns();
}
cout << "c: " << c << "\n\n";
cout << "current turn: " << temp.getTurn() << "\n";
vector<moves> listOfMoves = temp.viewPossibleMoves();
cout << "current turn: " << temp->getTurn() << "\n";
vector<moves> listOfMoves = temp->viewPossibleMoves();
for (int i = 0; i < listOfMoves.size(); ++i){
//return minMax(temp, listOfMoves[i]);
}
temp->displayBoard();
//limited recursion
return minMax(temp, listOfMoves[0], ++c);

View file

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

View file

@ -7,7 +7,6 @@ int main()
{
//board testing
Board b;
cout << b.boardToString();
//engine testing
Engine e;