stable AI

This commit is contained in:
Rebecca Schofield 2015-10-28 17:12:44 -05:00
parent 37bbd43b84
commit da2a1aec2b
5 changed files with 77 additions and 131 deletions

127
Board.cpp
View file

@ -45,18 +45,6 @@ Board::Board(const Board& b) {
}
}
void Board::setValidFalse() {
valid = false;
}
void Board::setValidTrue() {
valid = true;
}
bool Board::isValid() {
return valid;
}
//make this efficient!
bool Board::isPiece(int r, int c){
for (int i = 0; i < pieces.size(); ++i){
@ -68,31 +56,6 @@ bool Board::isPiece(int r, int c){
return false;
}
void Board::isTaken(int r, int c) {
for (int i = 0; i < pieces.size(); ++i){
if (pieces[i]->getX() == r && pieces[i]->getY() == c){
if(pieces[i]->getType() == 'O') {
for(int x = 0; x < opieces.size(); ++x) {
if (opieces[x]->getX() == r && opieces[x]->getY() == c) {
opieces.erase(opieces.begin() + x);
//break;
}
}
}
else {
for(int x = 0; x < xpieces.size(); ++x) {
if (xpieces[x]->getX() == r && xpieces[x]->getY() == c) {
xpieces.erase(xpieces.begin() + x);
//break;
}
}
}
pieces.erase(pieces.begin() + i);
break;
}
}
}
//make this efficient!
Piece* Board::getPiece(int r, int c){
for (int i = 0; i < pieces.size(); ++i){
@ -104,6 +67,29 @@ Piece* Board::getPiece(int r, int c){
return new Piece();
}
void Board::isTaken(int r, int c) {
for (int i = 0; i < pieces.size(); ++i){
if (pieces[i]->getX() == r && pieces[i]->getY() == c){
if(pieces[i]->getType() == 'O') {
for(int x = 0; x < opieces.size(); ++x) {
if (opieces[x]->getX() == r && opieces[x]->getY() == c) {
opieces.erase(opieces.begin() + x);
}
}
}
else {
for(int x = 0; x < xpieces.size(); ++x) {
if (xpieces[x]->getX() == r && xpieces[x]->getY() == c) {
xpieces.erase(xpieces.begin() + x);
}
}
}
pieces.erase(pieces.begin() + i);
break;
}
}
}
moves Board::parse(string input){
input = myToUpper(input);
@ -137,17 +123,33 @@ moves Board::parse(string input){
bool Board::isGameOver(){
for (int i = 0; i < xpieces.size(); ++i){
if (xpieces[i]->getX() == 7){
cout<<"\n\n\nPlayer X wins!\n\n\n"<<endl;
return true;
}
}
for (int i = 0; i < opieces.size(); ++i){
if (opieces[i]->getX() == 0){
cout<<"\n\n\nPlayer O wins!\n\n\n"<<endl;
return true;
}
}
return false;
}
string Board::whoWon(){
for (int i = 0; i < xpieces.size(); ++i){
if (xpieces[i]->getX() == 7){
return "Player X wins!";
}
}
for (int i = 0; i < opieces.size(); ++i){
if (opieces[i]->getX() == 0){
return "Player O wins!";
}
}
return "ERROR: function whoWon() called incorrectly.";
}
void Board::changeTurns(){
@ -156,15 +158,6 @@ void Board::changeTurns(){
}
void Board::displayBoard(){
/*
cout << "Debugging:\n";
for (int i = 0; i < pieces.size(); ++i){
if(i%8 == 0)
cout << "\n";
cout << pieces[i]->getX() << " " << pieces[i]->getY() << " " << pieces[i]->getType() << "\t";
}
cout << "Debugging:\n\n";
*/
cout << "; A B C D E F G H"<<endl;
for (int i = 0; i < 8; ++i) {
int label = 8 - i;
@ -253,7 +246,7 @@ char Board::intToCharColumn(int input){
void Board::move(string inputMove){
moves m = parse(inputMove);
cout << "MOVE: " << m.row << " " << m.column << " " << m.moveType << "\n\n";
//cout << "MOVE: " << m.row << " " << m.column << " " << m.moveType << "\n\n";
move(m);
}
@ -446,7 +439,6 @@ vector<moves> Board::viewPossibleMoves(){
}
else if (turn == 'O') {
//cout << "\nTESTING\n\n";
for (int i = 0; i < opieces.size(); ++i){
r = opieces[i]->getX();
c = opieces[i]->getY();
@ -483,7 +475,7 @@ string Board::myToUpper(string input){
if ((input[i] - 0 >= 97) && (input[i] - 0 <= 122))
{
numeric = input[i] - 32;
output.push_back((char)numeric);// = 'Q';//(char) numeric;
output.push_back((char)numeric);
}
else output.push_back(input[i]);
}
@ -495,17 +487,13 @@ string Board::myToUpper(string input){
void Board::undo(Board& tablero){
vector<Board> record;
if (record.size() < 2)
{
if (record.size() < 2){
cout<<"nothing to undo"<<endl;
}
else
{
for (int r = 0; r < 8; ++r)
{
for (int k = 0; k < 8; ++k)
{
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));
}
}
@ -517,34 +505,29 @@ void Board::undo(Board& tablero){
void Board::interpret(string input, Board& tablero){
vector<Board> record;
input = myToUpper(input);
//cout << "MOVE: " << input << "\n\n";
if (input == "UNDO")
{
if (input == "UNDO"){
undo(tablero);
}
else if (input == "DISPLAYRECORD") //for debugging purposes
{
else if (input == "DISPLAYRECORD"){
cout<<"record: "<<endl;
cout<<record.size();
for (int i = 0; i < record.size(); ++i)
{
for (int i = 0; i < record.size(); ++i){
record[i].displayBoard();
}
cout<<"---------------------------------------------------END DISPLAY RECORD------------------------"<<endl;
}
else tablero.move(input);
}
void Board::snapshot(vector<Board>& inputVec, Board inputBoard){
if (inputVec.size() == 10)
{
if (inputVec.size() == 10){
inputVec.erase(inputVec.begin());
}
else if (inputVec.size() > 10)
{
else if (inputVec.size() > 10){
cout<<"QUEUE OVERFLOW!"<<endl;
}
@ -562,7 +545,7 @@ int Board::evaluate(char max, char min){
}
else {
cout << "Unidentified max, must be either X or O.\n";
cout << "Error in evaluate: unidentified max, must be either 'X' or 'O'.\n";
return 0;
}
}

View file

@ -28,18 +28,19 @@ class Board {
public:
Board();
Board(const Board& b);
void setValidFalse();
void setValidTrue();
bool isValid();
void setValidFalse(){ valid = false; }
void setValidTrue(){ valid = true; }
bool isValid(){ return valid; }
bool isPiece(int r, int c);
void isTaken(int r, int c);
Piece* getPiece(int r, int c);
void isTaken(int r, int c);
vector<Piece*> getXPieces() const { return xpieces; }
vector<Piece*> getOPieces() const { return opieces; }
char getTurnPls() const { return turn; }
moves parse(string input);
char getTurn() { return turn; }
bool isGameOver();
string whoWon();
void changeTurns();
void displayBoard();
string boardToString();

View file

@ -26,27 +26,23 @@ void Engine::startGame(){
vector<Board> record;
b->snapshot(record, *b);
while (gameOver != true)
{
while (gameOver != true){
gameOver = b->isGameOver();
while(b->getTurn() == 'O' && !b->isValid())
{
while(b->getTurn() == 'O' && !b->isValid()){
b->displayBoard();
cout<<"\nEnter command: ";
cin>>move;
cout << "\n";
b->interpret(move, *b);
if(b->isValid()) {
if(b->isValid()){
b->changeTurns();
b->setValidFalse();
}
}
//if(b->isValid()) cout << b->getTurn();
while(b->getTurn() == 'X' )
{
while(b->getTurn() == 'X' ){
AI();
}
@ -57,41 +53,30 @@ void Engine::startGame(){
}
}
void Engine::easyAI()
{
void Engine::easyAI(){
vector<moves> listOfMoves = b->viewPossibleMoves();
/*
for(int x = 0; x < listOfMoves.size(); ++x) {
cout << listOfMoves[x].row << " " << listOfMoves[x].column << " " << listOfMoves[x].moveType << "\n\n";
}
*/
srand(time(NULL));
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();
}
void Engine::AI(){
//cout << "----------------------BEGIN AI FUNCTION----------------------\n";
void Engine::AI(int depth){
vector<moves> listOfMoves = b->viewPossibleMoves();
Board* temp = new Board(*b);
//probably not needed, check later
/*
if (b->getTurn() != 'X'){
cout << "a changing of turns is needed. \n";
b->changeTurns();
}
*/
//create node with current board state
//only doing 1 branch right now because testing
/*for (int i = 0; i < listOfMoves.size(); ++i){
minMax(b, listOfMoves[i]);
}*/
//remove this soon
b->move(minMax(temp, listOfMoves[0], 0, 0));
b->changeTurns();
//verification of correct turn
@ -100,8 +85,8 @@ void Engine::AI(){
cout << "ERROR in Engine::AI: b is on the wrong turn. \n";
}
*/
b->displayBoard();
//cout << "----------------------END AI FUNCTION----------------------\n";
}
moves Engine::minMax(Board* temp, moves m, int c, int r){
@ -109,36 +94,24 @@ moves Engine::minMax(Board* temp, moves m, int c, int r){
//cout << "c: " << c << "\n\n";
//cout << "current turn: " << temp->getTurn() << "\n";
vector<moves> listOfMoves = temp->viewPossibleMoves();
//cout << "listOfMoves size: " << listOfMoves.size() << "\n\n";
/*
if (c > 5){
return m;
}
*/
if (temp->isGameOver() == true){
//cout << "END OF PATH REACHED\n";
cout << "END OF PATH REACHED\n\n";
return m;
}
else {
if(temp->getPiece(8 - m.row, m.column)->getType() == temp->getTurn() && temp->isThisMovePossible(8 - m.row, m.column, m.moveType)){
//cout << "piece has been moved in minMax\n";
temp->move(m);
temp->evaluate('X', 'O');
temp->changeTurns();
//temp->displayBoard();
}
else {
//cout << m.row << " " << m.column << "\n\n";
m = minMax(temp, listOfMoves[++r], c, r);
}
//temp->displayBoard();
vector<moves> listOfMoves = temp->viewPossibleMoves();
//cout << "listOfMoves size: " << listOfMoves.size() << "\n\n";
minMax(temp, listOfMoves[0], 0, 0);
//testing
return m;
}
}

View file

@ -9,9 +9,9 @@ class Engine {
public:
Engine();
Board* getBoard() { return b; }
void startGame();
void easyAI();
void AI();
Board* getBoard() { return b; }
void AI(int depth);
moves minMax(Board* temp, moves m, int c, int r);
};

View file

@ -6,16 +6,5 @@ int main()
{
//Board b;
Engine e;
/*
>>>>>>> master
int pn = 0;
cout << "Welcome to Breakthrough server launcher, please enter a host port number: \n";
cin >> pn;
cin.clear();
cin.ignore(10000,'\n');
<<<<<<< HEAD
e.startGame(pn);
=======
*/
e.startGame();
}