updating
This commit is contained in:
parent
c037ea48cc
commit
864b354e7f
6 changed files with 46 additions and 22 deletions
31
Board.cpp
31
Board.cpp
|
@ -5,21 +5,42 @@
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
Board::Board() {
|
Board::Board() {
|
||||||
|
Piece* temp;
|
||||||
for (int i = 0; i < 2; ++i) {
|
for (int i = 0; i < 2; ++i) {
|
||||||
for (int j = 0; j < 8; ++j) {
|
for (int j = 0; j < 8; ++j) {
|
||||||
xpieces.push_back(new Piece(i, j, 'X'));
|
temp = new Piece(i, j, 'X');
|
||||||
pieces.push_back(new Piece(i, j, 'X'));
|
xpieces.push_back(temp);
|
||||||
|
pieces.push_back(temp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 6; i < 8; ++i) {
|
for (int i = 6; i < 8; ++i) {
|
||||||
for (int j = 0; j < 8; ++j) {
|
for (int j = 0; j < 8; ++j) {
|
||||||
opieces.push_back(new Piece(i, j, 'O'));
|
temp = new Piece(i, j, 'O');
|
||||||
pieces.push_back(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!
|
//make this efficient!
|
||||||
bool Board::isPiece(int r, int c){
|
bool Board::isPiece(int r, int c){
|
||||||
for (int i = 0; i < pieces.size(); ++i){
|
for (int i = 0; i < pieces.size(); ++i){
|
||||||
|
@ -92,7 +113,7 @@ void Board::changeTurns(){
|
||||||
if (turn == 'O') turn = 'X';
|
if (turn == 'O') turn = 'X';
|
||||||
else turn = 'O';
|
else turn = 'O';
|
||||||
}
|
}
|
||||||
|
|
||||||
void Board::displayBoard(){
|
void Board::displayBoard(){
|
||||||
cout << "; A B C D E F G H"<<endl;
|
cout << "; A B C D E F G H"<<endl;
|
||||||
for (int i = 0; i < 8; ++i) {
|
for (int i = 0; i < 8; ++i) {
|
||||||
|
|
5
Board.h
5
Board.h
|
@ -19,17 +19,18 @@ struct moves {
|
||||||
};
|
};
|
||||||
|
|
||||||
class Board {
|
class Board {
|
||||||
//vector<vector<char>> boardArray;
|
|
||||||
vector<Piece*> xpieces;
|
vector<Piece*> xpieces;
|
||||||
vector<Piece*> opieces;
|
vector<Piece*> opieces;
|
||||||
vector<Piece*> pieces;
|
vector<Piece*> pieces;
|
||||||
//char boardArray [8][8];
|
|
||||||
char turn = 'O';
|
char turn = 'O';
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Board();
|
Board();
|
||||||
|
Board(const Board& b);
|
||||||
bool isPiece(int r, int c);
|
bool isPiece(int r, int c);
|
||||||
Piece* getPiece(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);
|
moves parse(string input);
|
||||||
char getTurn() { return turn; }
|
char getTurn() { return turn; }
|
||||||
bool isGameOver();
|
bool isGameOver();
|
||||||
|
|
27
Engine.cpp
27
Engine.cpp
|
@ -43,7 +43,7 @@ void Engine::startGame(){
|
||||||
|
|
||||||
while(b->getTurn() == 'X' )
|
while(b->getTurn() == 'X' )
|
||||||
{
|
{
|
||||||
easyAI();
|
AI();
|
||||||
}
|
}
|
||||||
|
|
||||||
gameOver = b->isGameOver();
|
gameOver = b->isGameOver();
|
||||||
|
@ -67,12 +67,12 @@ 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 = new Board(*b);
|
||||||
|
|
||||||
//probably not needed, check later
|
//probably not needed, check later
|
||||||
if (temp.getTurn() != 'X'){
|
if (temp->getTurn() != 'X'){
|
||||||
cout << "a changing of turns is needed. \n";
|
cout << "a changing of turns is needed. \n";
|
||||||
temp.changeTurns();
|
temp->changeTurns();
|
||||||
}
|
}
|
||||||
|
|
||||||
//only doing 1 branch right now because testing
|
//only doing 1 branch right now because testing
|
||||||
|
@ -80,7 +80,8 @@ void Engine::AI(){
|
||||||
minMax(temp, listOfMoves[i]);
|
minMax(temp, listOfMoves[i]);
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
//b->moveWOPrint(minMax(temp, listOfMoves[0], 0));
|
b->move(minMax(temp, listOfMoves[0], 0));
|
||||||
|
b->changeTurns();
|
||||||
|
|
||||||
//verification of correct turn
|
//verification of correct turn
|
||||||
if (b->getTurn() != 'O'){
|
if (b->getTurn() != 'O'){
|
||||||
|
@ -91,30 +92,32 @@ void Engine::AI(){
|
||||||
cout << "----------------------END AI FUNCTION----------------------\n";
|
cout << "----------------------END AI FUNCTION----------------------\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
moves Engine::minMax(Board temp, moves m, int c){
|
moves Engine::minMax(Board* temp, moves m, int c){
|
||||||
//testing purposes only
|
//testing purposes only
|
||||||
if (c > 5){
|
if (c > 5){
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (temp.isGameOver() == true){
|
if (temp->isGameOver() == true){
|
||||||
cout << "END OF PATH REACHED\n";
|
cout << "END OF PATH REACHED\n";
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
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";
|
cout << "piece has been moved in minMax\n";
|
||||||
//temp.moveWOPrint(m);
|
temp->move(m);
|
||||||
|
temp->changeTurns();
|
||||||
}
|
}
|
||||||
cout << "c: " << c << "\n\n";
|
cout << "c: " << c << "\n\n";
|
||||||
cout << "current turn: " << temp.getTurn() << "\n";
|
cout << "current turn: " << temp->getTurn() << "\n";
|
||||||
vector<moves> listOfMoves = temp.viewPossibleMoves();
|
vector<moves> listOfMoves = temp->viewPossibleMoves();
|
||||||
|
|
||||||
for (int i = 0; i < listOfMoves.size(); ++i){
|
for (int i = 0; i < listOfMoves.size(); ++i){
|
||||||
//return minMax(temp, listOfMoves[i]);
|
//return minMax(temp, listOfMoves[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
temp->displayBoard();
|
||||||
//limited recursion
|
//limited recursion
|
||||||
return minMax(temp, listOfMoves[0], ++c);
|
return minMax(temp, listOfMoves[0], ++c);
|
||||||
|
|
||||||
|
|
2
Engine.h
2
Engine.h
|
@ -12,5 +12,5 @@ public:
|
||||||
void startGame();
|
void startGame();
|
||||||
void easyAI();
|
void easyAI();
|
||||||
void AI();
|
void AI();
|
||||||
moves minMax(Board temp, moves m, int c);
|
moves minMax(Board* temp, moves m, int c);
|
||||||
};
|
};
|
BIN
a.out
Executable file
BIN
a.out
Executable file
Binary file not shown.
3
test.cpp
3
test.cpp
|
@ -6,8 +6,7 @@ using namespace std;
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
//board testing
|
//board testing
|
||||||
Board b;
|
//Board b;
|
||||||
cout << b.boardToString();
|
|
||||||
|
|
||||||
//engine testing
|
//engine testing
|
||||||
Engine e;
|
Engine e;
|
||||||
|
|
Reference in a new issue