restructuring

This commit is contained in:
Rebecca Schofield 2015-10-27 10:40:55 -05:00
parent b03a041719
commit 96ddcfd632
6 changed files with 232 additions and 273 deletions

348
Board.cpp
View file

@ -5,33 +5,42 @@
using namespace std; using namespace std;
Board::Board() { Board::Board() {
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) {
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;
} }
} }
return false;
for (int i = 2; i < 6; ++i) { }
for (int j = 0; j < 8; ++j) {
boardArray[i][j] = '_'; 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;
} }
} }
return false;
for (int i = 6; i <= 7; ++i) {
for (int j = 0; j < 8; ++j) {
boardArray[i][j] = 'O';
}
}
} }
moves Board::parse(string input) moves Board::parse(string input)
{ {
input = myToUpper(input); input = myToUpper(input);
cout<<input<<endl;
int temp1; int temp1;
char temp2; char temp2;
string temp3; string temp3;
@ -54,7 +63,7 @@ moves Board::parse(string input)
temp3 = "FWD"; temp3 = "FWD";
} }
moves output(temp1,temp2,temp3); moves output(temp1, temp2, temp3);
return output; return output;
@ -62,18 +71,16 @@ moves Board::parse(string input)
bool Board::isGameOver() bool Board::isGameOver()
{ {
for (int i = 0; i < 8; ++i) for (int i = 0; i < xpieces.size(); ++i){
{ if (xpieces[i]->getX() == 0){
cout<<"\n\n\nPlayer X wins!\n\n\n"<<endl;
if (boardArray[0][i] == 'O')
{
cout<<"\n\n\nplayer O wins!\n\n\n"<<endl;
return true; return true;
} }
}
if (boardArray[7][i] == 'X')
{ for (int i = 0; i < opieces.size(); ++i){
cout<<"\n\n\nplayer X wins!\n\n\n"<<endl; if (opieces[i]->getX() == 7){
cout<<"\n\n\nPlayer O wins!\n\n\n"<<endl;
return true; return true;
} }
} }
@ -81,7 +88,6 @@ bool Board::isGameOver()
void Board::changeTurns() void Board::changeTurns()
{ {
cout << "TURN IS BEING CHANGED\n";
if (turn == 'O') turn = 'X'; if (turn == 'O') turn = 'X';
else turn = 'O'; else turn = 'O';
} }
@ -89,16 +95,21 @@ void Board::changeTurns()
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) {
{ int label = 8 - i;
int mango = 8 - i; cout<<"; "<<label<<" ";
cout<<"; "<<mango<<" "; for (int j = 0; j < 8; ++j){
for (int j = 0; j < 8; ++j) if (elemInXs(i, j))
{ cout << "|" << "X";
cout<<"|"<<boardArray[i][j]; else if (elemInOs(i, j))
cout << "|" << "O";
else
cout << "|" << "_";
} }
cout<<"|\n"; cout<<"|\n";
} }
cout<<'\n'<<endl; cout<<'\n'<<endl;
cout<<"turn: "<<turn << "\n"; cout<<"turn: "<<turn << "\n";
} }
@ -140,7 +151,7 @@ char Board::intToCharColumn(int input) //converts column number to int
return kolumn; return kolumn;
} }
/*
void Board::move(string inputMove) void Board::move(string inputMove)
{ {
moves jugada = parse(inputMove); moves jugada = parse(inputMove);
@ -150,225 +161,64 @@ void Board::move(string inputMove)
void Board::move(moves jugada) void Board::move(moves jugada)
{ {
int row = 8 - (jugada.row); int row = 8 - (jugada.row);
int kolumn = charToIntColumn(jugada.column); int column = charToIntColumn(jugada.column);
if (row > 8 || row < 0 || kolumn > 8 || kolumn < 0) cout << "move coordinates: (" << row << ", " << column << ")\n";
{
cout<<"ERROR: index out of bound in second move()!"<<endl; if (row > 8 || row < 0 || column > 8 || column < 0) {
cout<<"ERROR: index out of bound."<<endl;
} }
char temp = boardArray[row][kolumn]; char temp = boardArray[row][column]->getType();
if (temp != turn) cout << "temp: " << temp << "\n";
{
//cout<<"you can't move that piece at this turn!"<<endl; if (temp != turn || temp == '_') {
cout<<"ERROR: attempting to move an invalid piece."<<endl;
} }
else if (temp == '_') else {
{ if (jugada.moveType == "FWD") {
cout<<"there's no piece in that spot!"<<endl; boardArray[row][column]->moveFwd();
}
int reflector = 1;
if (temp == 'X' || temp == 'O')
{
if (temp == 'O')
{
reflector *= -1;
} }
else if (jugada.moveType == "LEFT") {
if (jugada.moveType == "FWD") if (column == 0)
{
if(boardArray[row+reflector][kolumn] != '_')
{
cout<<"you can't move that piece forward"<<endl;
}
else
{
boardArray[row][kolumn] = '_';
boardArray[row+reflector][kolumn] = temp;
changeTurns();
displayBoard();
}
}
else if (jugada.moveType == "LEFT")
{
if (kolumn == 0)
{
cout<<"Destination Spot out of range!"<<endl; cout<<"Destination Spot out of range!"<<endl;
}
else if (boardArray[row+reflector][kolumn-1] == temp) else if (boardArray[row][column-1]->getType() == temp)
{
cout<<"you hate your own team or something? you can't do that!"<<endl; cout<<"you hate your own team or something? you can't do that!"<<endl;
}
else else
{ boardArray[row][column]->moveLeft();
boardArray[row][kolumn] = '_';
boardArray[row+reflector][kolumn-1] = temp;
changeTurns();
displayBoard();
}
} }
else if (jugada.moveType == "RIGHT") else if (jugada.moveType == "RIGHT") {
{ if (column == 7)
if (kolumn == 7)
{
cout<<"Destination Spot out of range!"<<endl; cout<<"Destination Spot out of range!"<<endl;
}
else if (boardArray[row+reflector][kolumn+1] == temp) else if (boardArray[row][column+1]->getType() == temp)
{
cout<<"you hate your own team or something? you can't do that!"<<endl; cout<<"you hate your own team or something? you can't do that!"<<endl;
}
else else
{ boardArray[row][column]->moveRight();
boardArray[row][kolumn] = '_';
boardArray[row+reflector][kolumn+1] = temp;
changeTurns();
displayBoard();
}
} }
else
{
cout<<"Unrecognized movetype!"<<endl;
}
} }
else
{
cout<<"Invalid piece2!"<<endl;
}
} }
*/
//FIX THIS //FIX THIS
//take out the prints and turn changes from move //take out the prints and turn changes from move
void Board::moveWOPrint(moves jugada) void Board::moveWOPrint(moves jugada)
{ {
int row = 8 - (jugada.row); //
int kolumn = charToIntColumn(jugada.column);
if (row > 8 || row < 0 || kolumn > 8 || kolumn < 0)
{
cout<<"ERROR: index out of bound in second move()!"<<endl;
}
char temp = boardArray[row][kolumn];
if (temp != turn)
{
//cout<<"you can't move that piece at this turn!"<<endl;
}
else if (temp == '_')
{
cout<<"there's no piece in that spot!"<<endl;
}
int reflector = 1;
if (temp == 'X' || temp == 'O')
{
if (temp == 'O')
{
reflector *= -1;
}
if (jugada.moveType == "FWD")
{
if(boardArray[row+reflector][kolumn] != '_')
{
cout<<"you can't move that piece forward"<<endl;
}
else
{
boardArray[row][kolumn] = '_';
boardArray[row+reflector][kolumn] = temp;
changeTurns();
}
}
else if (jugada.moveType == "LEFT")
{
if (kolumn == 0)
{
cout<<"Destination Spot out of range!"<<endl;
}
else if (boardArray[row+reflector][kolumn-1] == temp)
{
cout<<"you hate your own team or something? you can't do that!"<<endl;
}
else
{
boardArray[row][kolumn] = '_';
boardArray[row+reflector][kolumn-1] = temp;
changeTurns();
}
}
else if (jugada.moveType == "RIGHT")
{
if (kolumn == 7)
{
cout<<"Destination Spot out of range!"<<endl;
}
else if (boardArray[row+reflector][kolumn+1] == temp)
{
cout<<"you hate your own team or something? you can't do that!"<<endl;
}
else
{
boardArray[row][kolumn] = '_';
boardArray[row+reflector][kolumn+1] = temp;
changeTurns();
}
}
else
{
cout<<"Unrecognized movetype!"<<endl;
}
}
else
{
cout<<"Invalid piece2!"<<endl;
}
} }
/*
bool Board::isThisMovePossible(int r, int c, string moveType) bool Board::isThisMovePossible(int r, int c, string moveType)
{ {
char pieceToMove = boardArray[r][c]; char pieceToMove = boardArray[r][c]->getType();
if (pieceToMove != turn) //trying to move invalid piece 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; return false;
} }
@ -383,22 +233,26 @@ bool Board::isThisMovePossible(int r, int c, string moveType)
{ {
if (moveType == "FWD") if (moveType == "FWD")
{ {
if (boardArray[r+reflector][c]->getType() == '_')
if (boardArray[r+reflector][c] == '_') return true; return true;
else return false; else
return false;
} }
else if (moveType == "RIGHT") else if (moveType == "RIGHT")
{ {
if (boardArray[r+reflector][c+1] != pieceToMove && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 <= 7) ) return true; if (boardArray[r+reflector][c+1]->getType() != pieceToMove && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 <= 7) )
else return false; return true;
else
return false;
} }
else if (moveType == "LEFT") else if (moveType == "LEFT")
{ {
if (boardArray[r+reflector][c-1] != pieceToMove && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 >= 0) ) return true; if (boardArray[r+reflector][c-1]->getType() != pieceToMove && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 >= 0) )
else return false; return true;
else
return false;
} }
else return false; else return false;
@ -413,7 +267,7 @@ vector<moves> Board::viewPossibleMoves()
{ {
for (int c = 0; c < 8; ++c) for (int c = 0; c < 8; ++c)
{ {
if (boardArray[r][c] == turn) if (boardArray[r][c]->getType() == turn)
{ {
if (isThisMovePossible(r,c,"FWD")) if (isThisMovePossible(r,c,"FWD"))
{ {
@ -439,7 +293,7 @@ vector<moves> Board::viewPossibleMoves()
return output; return output;
} }
*/
string Board::myToUpper(string input) string Board::myToUpper(string input)
{ {
string output; string output;
@ -456,11 +310,6 @@ string Board::myToUpper(string input)
else output.push_back(input[i]); else output.push_back(input[i]);
} }
for (int i = 0; i < output.size(); ++i)
{
cout<<output[i]<<endl;
}
return output; return output;
} }
@ -480,13 +329,14 @@ void Board::undo(Board& tablero)
{ {
for (int k = 0; k < 8; ++k) for (int k = 0; k < 8; ++k)
{ {
tablero.modifyAt(r,k,(record[record.size()-2]).elementAt(r,k)); //tablero.modifyAt(r,k,(record[record.size()-2]).elementAt(r,k));
} }
} }
record.pop_back(); record.pop_back();
} }
} }
/*
void Board::interpret(string input, Board& tablero) //determines what kind of command its input is void Board::interpret(string input, Board& tablero) //determines what kind of command its input is
{ {
vector<Board> record; vector<Board> record;
@ -510,7 +360,7 @@ void Board::interpret(string input, Board& tablero) //determines what kind of co
else tablero.move(input); else tablero.move(input);
} }
*/
void Board::snapshot(vector<Board>& inputVec, Board inputBoard) void Board::snapshot(vector<Board>& inputVec, Board inputBoard)
{ {
if (inputVec.size() == 10) if (inputVec.size() == 10)
@ -526,12 +376,18 @@ void Board::snapshot(vector<Board>& inputVec, Board inputBoard)
inputVec.push_back(inputBoard); inputVec.push_back(inputBoard);
} }
void Board::testPrint(){ int Board::evaluate(char max, char min){
for (int i = 7; i >= 0; --i){ //right now just evaluating number of pieces
for (int j = 7; j >= 0; --j){ if (max == 'X'){
cout << boardArray[i][j] << " "; return (xpieces.size() - opieces.size());
} }
cout << "\n"; else if (max == 'O'){
return (opieces.size() - xpieces.size());
}
else {
cout << "Unidentified max, must be either X or O.\n";
return 0;
} }
} }

20
Board.h
View file

@ -2,6 +2,7 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include "Piece.h"
using namespace std; using namespace std;
@ -18,13 +19,16 @@ struct moves {
}; };
class Board { class Board {
char boardArray [8][8]; //vector<vector<char>> boardArray;
vector<Piece*> xpieces;
vector<Piece*> opieces;
//char boardArray [8][8];
char turn = 'O'; char turn = 'O';
public: public:
Board(); Board();
char elementAt(int r, int k) { return boardArray[r][k]; } bool elemInXs(int r, int c);
void modifyAt(int r, int k, char input) { boardArray[r][k] = input; } bool elemInOs(int r, int c);
moves parse(string input); moves parse(string input);
char getTurn() { return turn; } char getTurn() { return turn; }
bool isGameOver(); bool isGameOver();
@ -32,14 +36,14 @@ public:
void displayBoard(); void displayBoard();
int charToIntColumn(char input); int charToIntColumn(char input);
char intToCharColumn(int input); char intToCharColumn(int input);
void move(string inputMove); //void move(string inputMove);
void move(moves jugada); //void move(moves jugada);
void moveWOPrint(moves jugada); void moveWOPrint(moves jugada);
bool isThisMovePossible(int r, int c, string moveType); //bool isThisMovePossible(int r, int c, string moveType);
vector<moves> viewPossibleMoves(); //vector<moves> viewPossibleMoves();
string myToUpper(string input); string myToUpper(string input);
void undo(Board& tablero); void undo(Board& tablero);
void interpret(string input, Board& tablero); void interpret(string input, Board& tablero);
void snapshot(vector<Board>& inputVec, Board inputBoard); void snapshot(vector<Board>& inputVec, Board inputBoard);
void testPrint(); int evaluate(char max, char min);
}; };

View file

@ -15,7 +15,6 @@ void Engine::startGame(){
cout<<"1. Play against AI?\n"; cout<<"1. Play against AI?\n";
cout<<"2. Play against a human?\n"; cout<<"2. Play against a human?\n";
//cout<<"CHANGE THIS TO PARSE THINGS\n";
cout<<"Enter choice: \n"; cout<<"Enter choice: \n";
int choice = -1; int choice = -1;
@ -37,28 +36,22 @@ void Engine::startGame(){
b->displayBoard(); b->displayBoard();
cout<<"\nEnter command: "; cout<<"\nEnter command: ";
cin>>move; cin>>move;
cout << "\n";
b->interpret(move, *b); b->interpret(move, *b);
} }
b->changeTurns();
while(b->getTurn() == 'X' ) while(b->getTurn() == 'X' )
{ {
AI(); cout << "\n\n\n\nit gets here\n\n\n\n";
easyAI();
} }
gameOver = b->isGameOver(); gameOver = b->isGameOver();
b->snapshot(record, *b); b->snapshot(record, *b);
} }
//for debugging purposes
cout<<"Record:"<<endl;
cout<<record.size();
for (int i = 0; i < record.size(); ++i)
{
record[i].displayBoard();
}
} }
void Engine::easyAI() void Engine::easyAI()
@ -97,14 +90,21 @@ void Engine::AI(){
/*for (int i = 0; i < listOfMoves.size(); ++i){ /*for (int i = 0; i < listOfMoves.size(); ++i){
minMax(temp, listOfMoves[i]); minMax(temp, listOfMoves[i]);
}*/ }*/
b->moveWOPrint(minMax(temp, listOfMoves[0], 0)); b->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(); b->displayBoard();
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 > 1000){ if (c > 5){
return m; return m;
} }
@ -123,10 +123,13 @@ moves Engine::minMax(Board temp, moves m, int c){
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){
cout << "listOfMoves[i]: (" << listOfMoves[i].row << ", " << listOfMoves[i].column << ")\n"; //return minMax(temp, listOfMoves[i]);
//minMax(temp, listOfMoves[i]);
} }
//limited recursion
return minMax(temp, listOfMoves[0], ++c); return minMax(temp, listOfMoves[0], ++c);
//testing
return m;
} }
} }

69
Piece.cpp Executable file
View file

@ -0,0 +1,69 @@
#include <iostream>
#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(){
//
}

25
Piece.h Executable file
View file

@ -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();
};

View file

@ -1,3 +1,4 @@
#include "Board.h"
#include "Engine.h" #include "Engine.h"
using namespace std; using namespace std;
@ -6,8 +7,9 @@ int main()
{ {
//board testing //board testing
Board b; Board b;
b.displayBoard();
//engine testing //engine testing
Engine e; //Engine e;
e.startGame(); //e.startGame();
} }