This repository has been archived on 2025-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
breakthroughpine64backup/Board.cpp
2015-11-02 16:42:47 -06:00

610 lines
12 KiB
C++

#include <ctime>
#include <cstdlib>
#include <iostream>
#include <limits.h>
#include <vector>
#include "Board.h"
using namespace std;
Board::Board() {
Piece* temp;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 8; ++j) {
temp = new Piece(i, j, 'X');
xpieces.push_back(temp);
pieces.push_back(temp);
}
}
for (int i = 6; i < 8; ++i) {
for (int j = 0; j < 8; ++j) {
temp = new Piece(i, j, 'O');
opieces.push_back(temp);
pieces.push_back(temp);
}
}
valid, xtaken, otaken = false;
}
Board::Board(const Board& b) {
vector<Piece*> xp = b.getTypePieces('X');
vector<Piece*> op = b.getTypePieces('O');
Piece* temp;
char tempturn = b.getTurn();
turn = tempturn;
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);
}
valid, xtaken, otaken = false;
}
//make this efficient!
bool Board::isPiece(int r, int c){
for (int i = 0; i < pieces.size(); ++i){
if (pieces[i]->getX() == r && pieces[i]->getY() == c){
return true;
}
}
return false;
}
//make this efficient!
Piece* Board::getPiece(int r, int c){
for (int i = 0; i < pieces.size(); ++i){
if (pieces[i]->getX() == r && pieces[i]->getY() == c){
return pieces[i];
}
}
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);
//cout << "otaken set to TRUE\n";
otaken = true;
}
}
}
else {
for(int x = 0; x < xpieces.size(); ++x) {
if (xpieces[x]->getX() == r && xpieces[x]->getY() == c) {
xpieces.erase(xpieces.begin() + x);
//cout << "xtaken set to TRUE\n";
xtaken = true;
}
}
}
pieces.erase(pieces.begin() + i);
break;
}
}
}
bool Board::checkTaken(char c){
if (c == 'X'){
return xtaken;
}
else if (c == 'O'){
return otaken;
}
else
return false;
}
vector<Piece*> Board::getTypePieces(char type) const{
if (type == 'X')
return xpieces;
else if (type == 'O')
return opieces;
else{
cout << "Invalid type!\n";
return pieces;
}
}
moves Board::parse(string input){
input = myToUpper(input);
int temp1;
int temp2;
string temp3;
temp2 = input[0] - 'A';
temp1 = input[1] - '0';
if (input[3] == 'L')
{
temp3 = "LEFT";
}
else if (input[3] == 'R')
{
temp3 = "RIGHT";
}
else
{
temp3 = "FWD";
}
moves output(temp1, temp2, temp3);
return output;
}
bool Board::isGameOver(){
for (int i = 0; i < xpieces.size(); ++i){
if (xpieces[i]->getX() == 7){
return true;
}
}
for (int i = 0; i < opieces.size(); ++i){
if (opieces[i]->getX() == 0){
return true;
}
}
return false;
}
char Board::whoWon(){
for (int i = 0; i < xpieces.size(); ++i){
if (xpieces[i]->getX() == 7){
return 'X';
}
}
for (int i = 0; i < opieces.size(); ++i){
if (opieces[i]->getX() == 0){
return 'O';
}
}
cout << "ERROR: function whoWon() called incorrectly. Game is not over. \n";
return 'a';
}
void Board::changeTurns(){
if (turn == 'O') turn = 'X';
else turn = 'O';
}
void Board::resetTaken(){
xtaken = false;
otaken = false;
}
void Board::displayBoard(){
cout << "; A B C D E F G H"<<endl;
for (int i = 0; i < 8; ++i) {
int label = 8 - i;
cout<<"; "<<label<<" ";
for (int j = 0; j < 8; ++j){
if (isPiece(i, j))
if (getPiece(i, j)->getType() == 'X')
cout << "|" << "X";
else
cout << "|" << "O";
else
cout << "|" << "_";
}
cout<<"|\n";
}
cout<<'\n'<<endl;
cout<<"turn: "<<turn << "\n";
}
string Board::boardToString(){
string output = "";
output += "; A B C D E F G H\n";
int label;
for (int i = 0; i < 8; ++i) {
label = 8 - i;
output += "; ";
output += '0' + label;
output += " ";
for (int j = 0; j < 8; ++j){
if (isPiece(i, j))
if (getPiece(i, j)->getType() == 'X')
output += "|X";
else
output += "|O";
else
output += "|_";
}
output += "|\n";
}
output += "\n\nturn: ";
output += turn;
output += "\n";
return output;
}
int Board::charToIntColumn(char input){
int kolumn;
switch (input)
{
case 'A': kolumn = 0; break;
case 'B': kolumn = 1; break;
case 'C': kolumn = 2; break;
case 'D': kolumn = 3; break;
case 'E': kolumn = 4; break;
case 'F': kolumn = 5; break;
case 'G': kolumn = 6; break;
case 'H': kolumn = 7; break;
}
return kolumn;
}
char Board::intToCharColumn(int input){
char kolumn;
switch (input)
{
case 1: kolumn = 'A'; break;
case 2: kolumn = 'B'; break;
case 3: kolumn = 'C'; break;
case 4: kolumn = 'D'; break;
case 5: kolumn = 'E'; break;
case 6: kolumn = 'F'; break;
case 7: kolumn = 'G'; break;
case 8: kolumn = 'H'; break;
}
return kolumn;
}
void Board::move(string inputMove){
moves m = parse(inputMove);
move(m);
}
Board Board::move(moves m){
int row = 8 - (m.row);
int column = m.column;
//cout << "INSIDE MOVE: " << row << " " << column << " " << m.moveType << "\n\n";
if (row > 8 || row < 0 || column > 8 || column < 0) {
cout<<"ERROR: index out of bound."<<endl;
return *this;
}
Piece* piece;
//cout << "TEST" << row << " " << column << "\n\n";
if (isPiece(row, column))
piece = getPiece(row, column);
else{
cout<<"ERROR: attempting to move an invalid piece.\n";
return *this;
}
//cout << piece->getX() << piece->getY() << "\n\n";
if (piece->getType() != turn) {
cout<<"ERROR: attempting to move the wrong side's piece.\n";
}
else {
if(isThisMovePossible(row, column, m.moveType))
{
if (m.moveType == "FWD") {
piece->moveFwd();
}
else if (m.moveType == "LEFT") {
//add error checking
if(piece->getType() == 'O') {
row--;
column--;
}
else {
row++;
column--;
}
if(isPiece(row, column)) {
if(getPiece(row, column)->getType() != piece->getType()) {
isTaken(row, column);
piece->moveLeft();
}
}
else {
piece->moveLeft();
}
}
else if (m.moveType == "RIGHT") {
//add error checking
//cout << "TESTING??\n\n";
if(piece->getType() == 'O') {
row--;
column++;
}
else {
row++;
column++;
}
if(isPiece(row, column)) {
if(getPiece(row, column)->getType() != piece->getType()) {
isTaken(row, column);
piece->moveRight();
}
}
else {
//cout << piece->getX() << " " << piece->getY() << "\n\n";
piece->moveRight();
//cout << piece->getX() << " " << piece->getY() << "\n\n";
}
}
setValidTrue();
}
else
{
cout << "Invalid move.\n\n";
setValidFalse();
}
}
return *this;
}
bool Board::isThisMovePossible(int r, int c, string moveType){
Piece* piece;
Piece* temp;
if (isPiece(r, c))
piece = getPiece(r, c);
else
return false;
if (piece->getType() != turn) {
cout << "Error in Board::isThisMovePossible: trying to move a piece outside your turn.\n";
return false;
}
else{
int reflector = 1;
if (piece->getType() == 'O')
reflector = -1;
if (moveType == "FWD"){
if (!isPiece(r + reflector, c))
return true;
else
return false;
}
else if (moveType == "RIGHT"){
temp = getPiece(r + reflector, c+1);
if(c < 7) {
if (!isPiece(r+reflector, c+1) && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 <= 7)) {
//cout << "What.\n\n";
return true;
}
else if(temp->getType() != piece->getType()) {
char a = temp->getType();
char b = piece->getType();
//cout << a << " " << b << "\n\n";
return true;
}
else {
return false;
}
}
else {
return false;
}
}
else if (moveType == "LEFT"){
temp = getPiece(r + reflector, c-1);
if(c > 0) {
if (!isPiece(r+reflector, c-1) && (r+reflector >= 0) && (r+reflector <= 7)) {
//cout << "What.\n\n";
return true;
}
else if(temp->getType() != piece->getType()) {
char a = temp->getType();
char b = piece->getType();
//cout << a << " " << b << "\n\n";
return true;
}
else {
return false;
}
}
else {
return false;
}
}
else return false;
}
}
vector<moves> Board::viewPossibleMoves(){
int r, c = -1;
vector<moves> output;
if (turn == 'X'){
for (int i = 0; i < xpieces.size(); ++i){
r = xpieces[i]->getX();
c = xpieces[i]->getY();
if (isThisMovePossible(r, c, "FWD"))
{
moves temp(8-r,c, "FWD");
output.push_back(temp);
}
if (isThisMovePossible(r,c,"LEFT"))
{
moves temp(8-r,c, "LEFT");
output.push_back(temp);
}
if (isThisMovePossible(r,c,"RIGHT"))
{
moves temp(8-r,c, "RIGHT");
output.push_back(temp);
}
}
}
else if (turn == 'O') {
for (int i = 0; i < opieces.size(); ++i){
r = opieces[i]->getX();
c = opieces[i]->getY();
if (isThisMovePossible(r, c, "FWD"))
{
moves temp(8-r,c, "FWD");
output.push_back(temp);
}
if (isThisMovePossible(r,c,"LEFT"))
{
moves temp(8-r,c, "LEFT");
output.push_back(temp);
}
if (isThisMovePossible(r,c,"RIGHT"))
{
moves temp(8-r,c, "RIGHT");
output.push_back(temp);
}
}
}
return output;
}
string Board::myToUpper(string input){
string output;
for (int i = 0 ; i < input.size(); ++i)
{
int numeric;
if ((input[i] - 0 >= 97) && (input[i] - 0 <= 122))
{
numeric = input[i] - 32;
output.push_back((char)numeric);
}
else output.push_back(input[i]);
}
return output;
}
void Board::undo(Board& tablero){
vector<Board> record;
if (record.size() < 2){
cout<<"nothing to undo"<<endl;
}
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));
}
}
record.pop_back();
}
}
void Board::interpret(string input, Board& tablero){
vector<Board> record;
input = myToUpper(input);
if (input == "UNDO"){
undo(tablero);
}
else if (input == "DISPLAYRECORD"){
cout<<"record: "<<endl;
cout<<record.size();
for (int i = 0; i < record.size(); ++i){
record[i].displayBoard();
}
}
else tablero.move(input);
}
void Board::snapshot(vector<Board>& inputVec, Board inputBoard){
if (inputVec.size() == 10){
inputVec.erase(inputVec.begin());
}
else if (inputVec.size() > 10){
cout<<"QUEUE OVERFLOW!"<<endl;
}
inputVec.push_back(inputBoard);
}
int Board::evaluate(char max, char min){
vector<Piece*> maxPieces = getTypePieces(max);
vector<Piece*> minPieces = getTypePieces(min);
int reflector, val, x, y = 0;
Piece* temp;
val += 2 * (maxPieces.size() - minPieces.size());
//cout << (checkTaken(max)) << "\n";
//cout << (checkTaken(min)) << "\n";
//check for taken conditions
if (checkTaken(min)){
//cout << "adding 10 to val\n";
val += 10;
}
if (checkTaken(max)){
//cout << "subtracting 10 from val\n";
val -= 10;
}
//ultimate condition!
if (isGameOver()){
if (whoWon() == max)
val = INT_MAX / 2;
else
val = INT_MIN / 2;
}
//cout << "val: " << val << "\n";
return val;
}