Updating branch.

This commit is contained in:
Alexander Huddleston 2015-10-27 11:06:44 -05:00
commit 8ece71023d
6 changed files with 638 additions and 711 deletions

108
Board.cpp
View file

@ -28,7 +28,6 @@ Board::Board() {
moves Board::parse(string input) moves Board::parse(string input)
{ {
input = myToUpper(input); input = myToUpper(input);
cout<<input<<endl; cout<<input<<endl;
@ -88,7 +87,6 @@ void Board::changeTurns()
void Board::displayBoard() void Board::displayBoard()
{ {
cout<<"\n\n";
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)
{ {
@ -101,30 +99,7 @@ void Board::displayBoard()
cout<<"|\n"; cout<<"|\n";
} }
cout<<'\n'<<endl; cout<<'\n'<<endl;
cout<<"turn : "<<turn; cout<<"turn : "<<turn << "\n";
}
string Board::boardToString()
{
string temp;
temp.append("\n");
temp.append("\n");
temp.append("; A B C D E F G H\n");
for (int i = 0; i < 8; ++i)
{
int mango = 8 - i;
temp.append("; " + to_string(mango) + " ");
for (int j = 0; j < 8; ++j)
{
temp.append("|");
temp.push_back(boardArray[i][j]);
}
temp.append("|\n");
}
temp.append("\n");
temp.append("turn : " + turn);
return temp;
} }
int Board::charToIntColumn(char input) //converts column number to int int Board::charToIntColumn(char input) //converts column number to int
@ -167,7 +142,6 @@ char Board::intToCharColumn(int input) //converts column number to int
void Board::move(string inputMove) void Board::move(string inputMove)
{ {
moves jugada = parse(inputMove); moves jugada = parse(inputMove);
int row = 8 - (jugada.row); int row = 8 - (jugada.row);
@ -177,7 +151,7 @@ void Board::move(string inputMove)
if (row > 8 || row < 0 || kolumn > 8 || kolumn < 0) if (row > 8 || row < 0 || kolumn > 8 || kolumn < 0)
{ {
cout<<"ERROR: index out of bound!"<<endl; cout<<"ERROR: index out of bound in move()!"<<endl;
} }
else if (temp != turn) else if (temp != turn)
@ -280,13 +254,12 @@ 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 kolumn = charToIntColumn(jugada.column);
if (row > 8 || row < 0 || kolumn > 8 || kolumn < 0) if (row > 8 || row < 0 || kolumn > 8 || kolumn < 0)
{ {
cout<<"ERROR: index out of bound!"<<endl; cout<<"ERROR: index out of bound in second move()!"<<endl;
} }
int temp = boardArray[row][kolumn]; int temp = boardArray[row][kolumn];
@ -489,17 +462,9 @@ string Board::myToUpper(string input)
} }
void Board::displayPossibleMoves(vector<moves> input) void Board::undo(Board& tablero)
{
cout<<"\n\nList of possible Moves:"<<endl;
for (int i = 0; i < input.size(); ++i)
{
cout<<"possible move: "<<input[i].row<<" "<<input[i].column<<" "<<input[i].moveType<<endl;
}
}
void Board::undo()
{ {
vector<Board> record;
if (record.size() < 2) if (record.size() < 2)
{ {
@ -508,14 +473,11 @@ void Board::undo()
else else
{ {
cout<<"BAZINGA"<<endl;
for (int r = 0; r < 8; ++r) for (int r = 0; r < 8; ++r)
{ {
for (int k = 0; k < 8; ++k) for (int k = 0; k < 8; ++k)
{ {
char shamwow = record[record.size()-2].elementAt(r,k); tablero.modifyAt(r,k,(record[record.size()-2]).elementAt(r,k));
//boardArray.modifyAt(r,k,shamwow);
boardArray[r][k] = shamwow;
} }
} }
record.pop_back(); record.pop_back();
@ -527,15 +489,15 @@ void Board::interpret(string input, Board& tablero) //determines what kind of co
vector<Board> record; vector<Board> record;
input = myToUpper(input); input = myToUpper(input);
if (input[0] == 'U') if (input == "UNDO")
{ {
undo(); undo(tablero);
} }
else if (input == "DISPLAYRECORD") //for debugging purposes else if (input == "DISPLAYRECORD") //for debugging purposes
{ {
cout<<"record: "<<endl; cout<<"record: "<<endl;
//cout<<record.size(); cout<<record.size();
for (int i = 0; i < record.size(); ++i) for (int i = 0; i < record.size(); ++i)
{ {
record[i].displayBoard(); record[i].displayBoard();
@ -546,59 +508,17 @@ void Board::interpret(string input, Board& tablero) //determines what kind of co
else tablero.move(input); else tablero.move(input);
} }
void Board::snapshot() void Board::snapshot(vector<Board>& inputVec, Board inputBoard)
{ {
if (record.size() == 10) if (inputVec.size() == 10)
{ {
record.erase(record.begin()); inputVec.erase(inputVec.begin());
} }
else if (record.size() > 10) else if (inputVec.size() > 10)
{ {
cout<<"QUEUE OVERFLOW!"<<endl; cout<<"QUEUE OVERFLOW!"<<endl;
} }
simpleBoard boardToPush; inputVec.push_back(inputBoard);
for (int r = 0; r < 8; ++r)
{
for (int k = 0; k < 8; ++k)
{
boardToPush.modifyAt(r,k,boardArray[r][k]);
}
}
record.push_back(boardToPush);
}
//move this to its own file
void Board::easyAI()
{
//1) see all possible movements
vector<moves> 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: "<<listOfMoves[temp].column<<" "<<listOfMoves[temp].row<<" "<<listOfMoves[temp].moveType<<endl; uncomment for debugging purposes
}
void Board::displayRecord()
{
for (int i = 0; i < record.size(); ++i)
{
record[i].display();
}
} }

38
Board.h
View file

@ -17,39 +17,9 @@ struct moves {
} }
}; };
struct simpleBoard{
char boardStamp [8][8];
char elementAt(int r, int k)
{
return boardStamp[r][k];
}
void modifyAt(int r, int k, char c)
{
boardStamp[r][k] = c;
}
void display()
{
for (int r = 0; r < 8; ++r)
{
cout<<'\n';
for (int k = 0; k < 8; ++k)
{
cout<<boardStamp[r][k]<<" ";
}
}
}
};
class Board { class Board {
char boardArray [8][8]; char boardArray [8][8];
char turn = 'O'; char turn = 'O';
vector<simpleBoard> record;
public: public:
Board(); Board();
@ -67,11 +37,7 @@ public:
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 displayPossibleMoves(vector<moves> input); void undo(Board& tablero);
void undo();
void interpret(string input, Board& tablero); void interpret(string input, Board& tablero);
void snapshot(); void snapshot(vector<Board>& inputVec, Board inputBoard);
void easyAI();
string boardToString();
void displayRecord();
}; };

92
Engine.cpp Executable file
View file

@ -0,0 +1,92 @@
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#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<Board> 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:"<<endl;
cout<<record.size();
for (int i = 0; i < record.size(); ++i)
{
record[i].displayBoard();
}
}
void Engine::easyAI()
{
//1) see all possible movements
vector<moves> 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<moves> listOfMoves = b->viewPossibleMoves();
//
}
void Engine::minMax(){
//do more things here
}

16
Engine.h Executable file
View file

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

BIN
a.out

Binary file not shown.

View file

@ -1,76 +1,9 @@
#include <iostream> #include "Engine.h"
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include "Board.h"
using namespace std; using namespace std;
int main() int main()
{ {
cout<<"WELCOME\n"; Engine e;
e.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;
cin >> choice;
cout << "OK" << endl;
Board b;
string move;
bool gameOver = false;
vector<Board> 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<moves> 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:"<<endl;
cout<<record.size();
for (int i = 0; i < record.size(); ++i)
{
record[i].displayBoard();
}
} }