From 96ddcfd632ab7491473e0cf3e8427ba463ac5f7a Mon Sep 17 00:00:00 2001 From: Rebecca Schofield Date: Tue, 27 Oct 2015 10:40:55 -0500 Subject: [PATCH] restructuring --- Board.cpp | 348 ++++++++++++++++------------------------------------- Board.h | 20 +-- Engine.cpp | 37 +++--- Piece.cpp | 69 +++++++++++ Piece.h | 25 ++++ test.cpp | 6 +- 6 files changed, 232 insertions(+), 273 deletions(-) create mode 100755 Piece.cpp create mode 100755 Piece.h diff --git a/Board.cpp b/Board.cpp index 0282dc8..b423b00 100755 --- a/Board.cpp +++ b/Board.cpp @@ -5,33 +5,42 @@ using namespace std; Board::Board() { - for (int i = 0; i < 2; ++i) { + for (int i = 0; i < 2; ++i) { for (int j = 0; j < 8; ++j) { - boardArray[i][j] = 'X'; + xpieces.push_back(new Piece(i, j, 'X')); + } + } + for (int i = 6; i < 8; ++i) { + for (int j = 0; j < 8; ++j) { + opieces.push_back(new Piece(i, j, 'O')); + } + } +} + +bool Board::elemInXs(int r, int c){ + for (int i = 0; i < xpieces.size(); ++i){ + if (xpieces[i]->getX() == r && xpieces[i]->getY() == c){ + return true; } } - - for (int i = 2; i < 6; ++i) { - for (int j = 0; j < 8; ++j) { - boardArray[i][j] = '_'; + return false; +} + +bool Board::elemInOs(int r, int c){ + for (int i = 0; i < opieces.size(); ++i){ + if (opieces[i]->getX() == r && opieces[i]->getY() == c){ + return true; } } - - for (int i = 6; i <= 7; ++i) { - for (int j = 0; j < 8; ++j) { - boardArray[i][j] = 'O'; - } - } + return false; } moves Board::parse(string input) { input = myToUpper(input); - cout<getX() == 0){ + cout<<"\n\n\nPlayer X wins!\n\n\n"<getX() == 7){ + cout<<"\n\n\nPlayer O wins!\n\n\n"< 8 || row < 0 || kolumn > 8 || kolumn < 0) - { - cout<<"ERROR: index out of bound in second move()!"< 8 || row < 0 || column > 8 || column < 0) { + cout<<"ERROR: index out of bound."<getType(); + cout << "temp: " << temp << "\n"; + + if (temp != turn || temp == '_') { + cout<<"ERROR: attempting to move an invalid piece."<moveFwd(); } - - if (jugada.moveType == "FWD") - { - - if(boardArray[row+reflector][kolumn] != '_') - { - cout<<"you can't move that piece forward"<getType() == temp) cout<<"you hate your own team or something? you can't do that!"<moveLeft(); } - else if (jugada.moveType == "RIGHT") - { - if (kolumn == 7) - { + else if (jugada.moveType == "RIGHT") { + if (column == 7) cout<<"Destination Spot out of range!"<getType() == temp) cout<<"you hate your own team or something? you can't do that!"<moveRight(); } - - else - { - cout<<"Unrecognized movetype!"< 8 || row < 0 || kolumn > 8 || kolumn < 0) - { - cout<<"ERROR: index out of bound in second move()!"<getType(); if (pieceToMove != turn) //trying to move invalid piece { - //cout << "Error in Board::isThisMovePossible: trying to move a piece outside your turn.\n"; + cout << "Error in Board::isThisMovePossible: trying to move a piece outside your turn.\n"; return false; } @@ -383,22 +233,26 @@ bool Board::isThisMovePossible(int r, int c, string moveType) { if (moveType == "FWD") { - - if (boardArray[r+reflector][c] == '_') return true; - else return false; - + if (boardArray[r+reflector][c]->getType() == '_') + return true; + else + return false; } else if (moveType == "RIGHT") { - if (boardArray[r+reflector][c+1] != pieceToMove && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 <= 7) ) return true; - else return false; + if (boardArray[r+reflector][c+1]->getType() != pieceToMove && (r+reflector >= 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; + if (boardArray[r+reflector][c-1]->getType() != pieceToMove && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 >= 0) ) + return true; + else + return false; } else return false; @@ -413,7 +267,7 @@ vector Board::viewPossibleMoves() { for (int c = 0; c < 8; ++c) { - if (boardArray[r][c] == turn) + if (boardArray[r][c]->getType() == turn) { if (isThisMovePossible(r,c,"FWD")) { @@ -439,7 +293,7 @@ vector Board::viewPossibleMoves() return output; } - +*/ string Board::myToUpper(string input) { string output; @@ -456,11 +310,6 @@ string Board::myToUpper(string input) else output.push_back(input[i]); } - for (int i = 0; i < output.size(); ++i) - { - cout< record; @@ -510,7 +360,7 @@ void Board::interpret(string input, Board& tablero) //determines what kind of co else tablero.move(input); } - +*/ void Board::snapshot(vector& inputVec, Board inputBoard) { if (inputVec.size() == 10) @@ -526,12 +376,18 @@ void Board::snapshot(vector& inputVec, Board 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"; +int Board::evaluate(char max, char min){ + //right now just evaluating number of pieces + if (max == 'X'){ + return (xpieces.size() - opieces.size()); + } + + else if (max == 'O'){ + return (opieces.size() - xpieces.size()); + } + + else { + cout << "Unidentified max, must be either X or O.\n"; + return 0; } } \ No newline at end of file diff --git a/Board.h b/Board.h index 19cdc62..8f3284c 100755 --- a/Board.h +++ b/Board.h @@ -2,6 +2,7 @@ #include #include +#include "Piece.h" using namespace std; @@ -18,13 +19,16 @@ struct moves { }; class Board { - char boardArray [8][8]; + //vector> boardArray; + vector xpieces; + vector opieces; + //char boardArray [8][8]; char turn = 'O'; public: Board(); - char elementAt(int r, int k) { return boardArray[r][k]; } - void modifyAt(int r, int k, char input) { boardArray[r][k] = input; } + bool elemInXs(int r, int c); + bool elemInOs(int r, int c); moves parse(string input); char getTurn() { return turn; } bool isGameOver(); @@ -32,14 +36,14 @@ public: void displayBoard(); int charToIntColumn(char input); char intToCharColumn(int input); - void move(string inputMove); - void move(moves jugada); + //void move(string inputMove); + //void move(moves jugada); void moveWOPrint(moves jugada); - bool isThisMovePossible(int r, int c, string moveType); - vector viewPossibleMoves(); + //bool isThisMovePossible(int r, int c, string moveType); + //vector viewPossibleMoves(); string myToUpper(string input); void undo(Board& tablero); void interpret(string input, Board& tablero); void snapshot(vector& inputVec, Board inputBoard); - void testPrint(); + int evaluate(char max, char min); }; \ No newline at end of file diff --git a/Engine.cpp b/Engine.cpp index 5f4b602..c68e650 100755 --- a/Engine.cpp +++ b/Engine.cpp @@ -15,7 +15,6 @@ void Engine::startGame(){ 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; @@ -37,28 +36,22 @@ void Engine::startGame(){ b->displayBoard(); cout<<"\nEnter command: "; cin>>move; + cout << "\n"; b->interpret(move, *b); } - + + b->changeTurns(); + while(b->getTurn() == 'X' ) { - AI(); + cout << "\n\n\n\nit gets here\n\n\n\n"; + easyAI(); } gameOver = b->isGameOver(); b->snapshot(record, *b); } - - //for debugging purposes - cout<<"Record:"<moveWOPrint(minMax(temp, listOfMoves[0], 0)); + + //verification of correct turn + if (b->getTurn() != 'O'){ + 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){ //testing purposes only - if (c > 1000){ + if (c > 5){ return m; } @@ -123,10 +123,13 @@ moves Engine::minMax(Board temp, moves m, int c){ vector listOfMoves = temp.viewPossibleMoves(); for (int i = 0; i < listOfMoves.size(); ++i){ - cout << "listOfMoves[i]: (" << listOfMoves[i].row << ", " << listOfMoves[i].column << ")\n"; - //minMax(temp, listOfMoves[i]); + //return minMax(temp, listOfMoves[i]); } - + + //limited recursion return minMax(temp, listOfMoves[0], ++c); + + //testing + return m; } } \ No newline at end of file diff --git a/Piece.cpp b/Piece.cpp new file mode 100755 index 0000000..6796826 --- /dev/null +++ b/Piece.cpp @@ -0,0 +1,69 @@ +#include +#include "Piece.h" + +using namespace std; + +Piece::Piece(){ + x = -1; + y = -1; + type = '?'; +} + +Piece::Piece(int r, int c){ + x = r; + y = c; + type = '_'; +} + +Piece::Piece(int r, int c, char t){ + x = r; + y = c; + type = t; +} + +void Piece::moveFwd(){ + if (type == 'X'){ + x++; + } + + else if (type == 'O'){ + x--; + } + + else + cout << "Error: trying to move an empty piece forward."; +} + +void Piece::moveLeft(){ + if (type == 'X'){ + x++; + y--; + } + + else if (type == 'O'){ + x--; + y++; + } + + else + cout << "Error: trying to move an empty piece left."; +} + +void Piece::moveRight(){ + if (type == 'X'){ + x++; + y++; + } + + else if (type == 'O'){ + x--; + y--; + } + + else + cout << "Error: trying to move an empty piece left."; +} + +void Piece::isTaken(){ + // +} \ No newline at end of file diff --git a/Piece.h b/Piece.h new file mode 100755 index 0000000..60a3a00 --- /dev/null +++ b/Piece.h @@ -0,0 +1,25 @@ +#pragma once + +using namespace std; + +class Piece { + int x; + int y; + char type; + +public: + Piece(); + Piece(int r, int c); + Piece(int r, int c, char t); + void moveFwd(); + void moveLeft(); + void moveRight(); + int getX(){ return x; } + void setX(int r){ x = r; } + int getY(){ return y; } + void setY(int c){ y = c; } + char getType(){ return type; } + void setType(char t){ type = t; } + void makeEmpty(){ type = '_'; } + void isTaken(); +}; \ No newline at end of file diff --git a/test.cpp b/test.cpp index 65f07dc..fd5452f 100755 --- a/test.cpp +++ b/test.cpp @@ -1,3 +1,4 @@ +#include "Board.h" #include "Engine.h" using namespace std; @@ -6,8 +7,9 @@ int main() { //board testing Board b; + b.displayBoard(); //engine testing - Engine e; - e.startGame(); + //Engine e; + //e.startGame(); }