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-03 23:27:33 -06:00

494 lines
9.3 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);
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);
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;
}
}
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;
}
Board Board::move(moves m){
int row = m.row;
int column = m.column;
Piece* piece;
if (isPiece(row, column))
piece = getPiece(row, column);
else{
cout<<"ERROR: attempting to move an invalid piece.\n";
return *this;
}
if (piece->getType() != turn) {
cout<<"ERROR: attempting to move the wrong side's piece.\n";
return *this;
}
else {
if(isThisMovePossible(row, column, m.moveType))
{
record.push_back(*this);
if (m.moveType == "FWD") {
piece->moveFwd();
}
else if (m.moveType == "LEFT") {
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") {
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 {
piece->moveRight();
}
}
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)) {
return true;
}
else if(temp->getType() != piece->getType()) {
char a = temp->getType();
char b = piece->getType();
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)) {
return true;
}
else if(temp->getType() != piece->getType()) {
char a = temp->getType();
char b = piece->getType();
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(r,c, "FWD");
output.push_back(temp);
}
if (isThisMovePossible(r,c,"LEFT"))
{
moves temp(r,c, "LEFT");
output.push_back(temp);
}
if (isThisMovePossible(r,c,"RIGHT"))
{
moves temp(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(r,c, "FWD");
output.push_back(temp);
}
if (isThisMovePossible(r,c,"LEFT"))
{
moves temp(r,c, "LEFT");
output.push_back(temp);
}
if (isThisMovePossible(r,c,"RIGHT"))
{
moves temp(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;
}
Board* Board::undo(){
//assuming this will only be called by a player, against an AI
if (record.size() == 0){
cout<< "ERROR: there is nothing to undo";
return this;
}
else{
Board* temp = new Board(record[record.size()-2]);
return temp;
}
}
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());
//check for taken conditions
if (checkTaken(min)){
val = val + 10;
}
if (checkTaken(max)){
val = val - 10;
}
//ultimate condition!
if (isGameOver()){
if (whoWon() == max)
val = val + 10;
else
val = val - 10;
}
return val;
}