diff --git a/Board.cpp b/Board.cpp index cdde4cc..d407bd7 100755 --- a/Board.cpp +++ b/Board.cpp @@ -1,604 +1,524 @@ -#include -#include -#include "Board.h" - -using namespace std; - -Board::Board() { - for (int i = 0; i < 2; ++i) { - for (int j = 0; j < 8; ++j) { - boardArray[i][j] = 'X'; - } - } - - - for (int i = 2; i < 6; ++i) { - for (int j = 0; j < 8; ++j) { - boardArray[i][j] = '_'; - } - } - - - for (int i = 6; i <= 7; ++i) { - for (int j = 0; j < 8; ++j) { - boardArray[i][j] = 'O'; - } - } -} - -moves Board::parse(string input) -{ - - input = myToUpper(input); - - cout< 8 || row < 0 || kolumn > 8 || kolumn < 0) - { - cout<<"ERROR: index out of bound!"< 8 || row < 0 || kolumn > 8 || kolumn < 0) - { - cout<<"ERROR: index out of bound!"<= 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; - else return false; - } - - else return false; - } -} - -vector Board::viewPossibleMoves() -{ - vector output; - - for (int r = 0; r < 8; ++r) - { - for (int c = 0; c < 8; ++c) - { - if (boardArray[r][c] == turn) - { - if (isThisMovePossible(r,c,"FWD")) - { - moves temp(8-r,intToCharColumn(c+1),"FWD"); - output.push_back(temp); - } - - if (isThisMovePossible(r,c,"LEFT")) - { - moves temp(8-r,intToCharColumn(c+1),"LEFT"); - output.push_back(temp); - } - - if (isThisMovePossible(r,c,"RIGHT")) - { - moves temp(8-r,intToCharColumn(c+1),"RIGHT"); - output.push_back(temp); - } - - } - } - } - - return output; -} - -string Board::myToUpper(string input) -{ - string output; - - for (int i = 0 ; i < input.size(); ++i) - { - int numeric; - - if ((input[i] - 0 >= 97) && (input[i] - 0 <= 122)) - { - numeric = input[i] - 32; - output.push_back((char)numeric);// = 'Q';//(char) numeric; - } - else output.push_back(input[i]); - } - - for (int i = 0; i < output.size(); ++i) - { - cout< input) -{ - cout<<"\n\nList of possible Moves:"< record; - input = myToUpper(input); - - if (input[0] == 'U') - { - undo(); - } - - else if (input == "DISPLAYRECORD") //for debugging purposes - { - cout<<"record: "< 10) - { - cout<<"QUEUE OVERFLOW!"< listOfMoves = viewPossibleMoves(); - - //2) pick a movement - - srand(time(NULL)); - int randomChoice = rand() % (listOfMoves.size()-1) - 0; // choose a move betwen listOfMoves[0] to last element - - //3) execute movement - - int temp = randomChoice; - - move(listOfMoves[randomChoice]); - - //cout<<"\n\nMove executed by AI: "< +#include +#include "Board.h" + +using namespace std; + +Board::Board() { + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 8; ++j) { + boardArray[i][j] = 'X'; + } + } + + + for (int i = 2; i < 6; ++i) { + for (int j = 0; j < 8; ++j) { + boardArray[i][j] = '_'; + } + } + + + for (int i = 6; i <= 7; ++i) { + for (int j = 0; j < 8; ++j) { + boardArray[i][j] = 'O'; + } + } +} + +moves Board::parse(string input) +{ + input = myToUpper(input); + + cout< 8 || row < 0 || kolumn > 8 || kolumn < 0) + { + cout<<"ERROR: index out of bound in move()!"< 8 || row < 0 || kolumn > 8 || kolumn < 0) + { + cout<<"ERROR: index out of bound in second move()!"<= 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; + else return false; + } + + else return false; + } +} + +vector Board::viewPossibleMoves() +{ + vector output; + + for (int r = 0; r < 8; ++r) + { + for (int c = 0; c < 8; ++c) + { + if (boardArray[r][c] == turn) + { + if (isThisMovePossible(r,c,"FWD")) + { + moves temp(8-r,intToCharColumn(c+1),"FWD"); + output.push_back(temp); + } + + if (isThisMovePossible(r,c,"LEFT")) + { + moves temp(8-r,intToCharColumn(c+1),"LEFT"); + output.push_back(temp); + } + + if (isThisMovePossible(r,c,"RIGHT")) + { + moves temp(8-r,intToCharColumn(c+1),"RIGHT"); + output.push_back(temp); + } + + } + } + } + + return output; +} + +string Board::myToUpper(string input) +{ + string output; + + for (int i = 0 ; i < input.size(); ++i) + { + int numeric; + + if ((input[i] - 0 >= 97) && (input[i] - 0 <= 122)) + { + numeric = input[i] - 32; + output.push_back((char)numeric);// = 'Q';//(char) numeric; + } + else output.push_back(input[i]); + } + + for (int i = 0; i < output.size(); ++i) + { + cout< record; + + if (record.size() < 2) + { + cout<<"nothing to undo"< record; + input = myToUpper(input); + + if (input == "UNDO") + { + undo(tablero); + } + + else if (input == "DISPLAYRECORD") //for debugging purposes + { + cout<<"record: "<& inputVec, Board inputBoard) +{ + if (inputVec.size() == 10) + { + inputVec.erase(inputVec.begin()); + } + + else if (inputVec.size() > 10) + { + cout<<"QUEUE OVERFLOW!"< record; public: Board(); @@ -67,11 +37,7 @@ public: bool isThisMovePossible(int r, int c, string moveType); vector viewPossibleMoves(); string myToUpper(string input); - void displayPossibleMoves(vector input); - void undo(); + void undo(Board& tablero); void interpret(string input, Board& tablero); - void snapshot(); - void easyAI(); - string boardToString(); - void displayRecord(); -}; + void snapshot(vector& inputVec, Board inputBoard); +}; \ No newline at end of file diff --git a/Engine.cpp b/Engine.cpp new file mode 100755 index 0000000..c5a1f4b --- /dev/null +++ b/Engine.cpp @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include "Engine.h" + + +Engine::Engine(){ + Board* brd = new Board(); + b = brd; +} + +void Engine::startGame(){ + cout<<"WELCOME\n"; + + cout<<"1. Play against AI?\n"; + cout<<"2. Play against a human?\n"; + //cout<<"CHANGE THIS TO PARSE THINGS\n"; + cout<<"Enter choice: \n"; + + int choice = -1; + cin >> choice; + cout << "OK" << endl; + + string move; + + bool gameOver = false; + vector record; + b->snapshot(record, *b); + + while (gameOver != true) + { + gameOver = b->isGameOver(); + + while(b->getTurn() == 'O' ) + { + b->displayBoard(); + cout<<"\nEnter command: "; + cin>>move; + b->interpret(move, *b); + } + + while(b->getTurn() == 'X' ) + { + AI(); + } + + gameOver = b->isGameOver(); + + b->snapshot(record, *b); + } + + //for debugging purposes + cout<<"Record:"< listOfMoves = b->viewPossibleMoves(); + + //obvious moves + if (false){ + b->changeTurns(); + } + + //random + else { + srand(time(NULL)); + int randomChoice = rand() % (listOfMoves.size()-1) - 0; // choose a move betwen listOfMoves[0] to last element + + //3) execute movement + int temp = randomChoice; + b->move(listOfMoves[randomChoice]); + } +} + +void Engine::AI(){ + vector listOfMoves = b->viewPossibleMoves(); + // +} + +void Engine::minMax(){ + //do more things here +} \ No newline at end of file diff --git a/Engine.h b/Engine.h new file mode 100755 index 0000000..8baa665 --- /dev/null +++ b/Engine.h @@ -0,0 +1,16 @@ +#pragma once + +#include "Board.h" + +using namespace std; + +class Engine { + Board* b; + +public: + Engine(); + void startGame(); + void easyAI(); + void AI(); + void minMax(); +}; \ No newline at end of file diff --git a/a.out b/a.out deleted file mode 100755 index 8a730d5..0000000 Binary files a/a.out and /dev/null differ diff --git a/test.cpp b/test.cpp index 7dd63ca..8b1eb77 100755 --- a/test.cpp +++ b/test.cpp @@ -1,76 +1,9 @@ -#include -#include -#include -#include -#include "Board.h" +#include "Engine.h" using namespace std; int main() { - cout<<"WELCOME\n"; - - cout<<"1. Play against AI?\n"; - cout<<"2. Play against a human?\n"; - //cout<<"CHANGE THIS TO PARSE THINGS\n"; - cout<<"Enter choice: \n"; - - int choice; - cin >> choice; - cout << "OK" << endl; - - Board b; - string move; - - bool gameOver = false; - vector record; - b.snapshot(record,b); - - while (gameOver != true) - { - gameOver = b.isGameOver(); - - while(b.getTurn() == 'O' ) - { - b.displayBoard(); - cout<<"\nEnter command: "; - cin>>move; - b.interpret(move,b); - } - - - vector possibleMoves = b.viewPossibleMoves(); - - if (choice == 1) - { - b.easyAI(); - } - - else - { - while(b.getTurn() == 'X' ) - { - b.displayBoard(); - cout<<"\nEnter command: "; - cout<<"OK\n"; - cin>>move; - b.interpret(move,b); - } - } - - //b.snapshot(); - gameOver = b.isGameOver(); - - b.snapshot(record,b); - } - - //for debugging purposes - cout<<"Record:"<