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

495 lines
9.3 KiB
C++
Raw Normal View History

2015-10-29 13:54:16 -05:00
#include <ctime>
#include <cstdlib>
2015-10-19 15:56:21 -05:00
#include <iostream>
#include <limits.h>
2015-10-19 15:56:21 -05:00
#include <vector>
2015-10-19 15:45:41 -05:00
#include "Board.h"
2015-10-19 15:56:21 -05:00
using namespace std;
2015-10-19 15:45:41 -05:00
Board::Board() {
2015-11-03 23:00:12 -06:00
Piece* temp;
2015-10-27 10:40:55 -05:00
for (int i = 0; i < 2; ++i) {
2015-10-19 15:45:41 -05:00
for (int j = 0; j < 8; ++j) {
2015-10-27 12:55:23 -05:00
temp = new Piece(i, j, 'X');
xpieces.push_back(temp);
pieces.push_back(temp);
2015-10-19 15:45:41 -05:00
}
}
2015-10-27 11:31:44 -05:00
2015-10-27 10:40:55 -05:00
for (int i = 6; i < 8; ++i) {
2015-10-19 15:45:41 -05:00
for (int j = 0; j < 8; ++j) {
2015-10-27 12:55:23 -05:00
temp = new Piece(i, j, 'O');
opieces.push_back(temp);
pieces.push_back(temp);
2015-10-27 10:40:55 -05:00
}
}
valid, xtaken, otaken = false;
2015-10-27 10:40:55 -05:00
}
2015-10-27 12:55:23 -05:00
Board::Board(const Board& b) {
vector<Piece*> xp = b.getTypePieces('X');
vector<Piece*> op = b.getTypePieces('O');
2015-10-27 12:55:23 -05:00
Piece* temp;
char tempturn = b.getTurn();
2015-10-27 21:48:04 -05:00
turn = tempturn;
2015-10-27 12:55:23 -05:00
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;
2015-10-27 12:55:23 -05:00
}
2015-10-27 11:31:44 -05:00
//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){
2015-10-27 10:40:55 -05:00
return true;
2015-10-19 15:36:20 -05:00
}
2015-10-19 15:45:41 -05:00
}
2015-10-27 10:40:55 -05:00
return false;
}
2015-10-28 17:12:44 -05:00
//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;
}
}
2015-10-19 15:45:41 -05:00
2015-10-27 11:31:44 -05:00
bool Board::isGameOver(){
2015-10-27 10:40:55 -05:00
for (int i = 0; i < xpieces.size(); ++i){
2015-10-27 11:31:44 -05:00
if (xpieces[i]->getX() == 7){
2015-10-19 15:45:41 -05:00
return true;
2015-10-19 15:36:20 -05:00
}
2015-10-27 10:40:55 -05:00
}
for (int i = 0; i < opieces.size(); ++i){
2015-10-27 11:31:44 -05:00
if (opieces[i]->getX() == 0){
2015-10-19 15:45:41 -05:00
return true;
2015-10-19 15:36:20 -05:00
}
2015-10-19 15:45:41 -05:00
}
2015-10-28 17:12:44 -05:00
return false;
}
char Board::whoWon(){
2015-10-28 17:12:44 -05:00
for (int i = 0; i < xpieces.size(); ++i){
if (xpieces[i]->getX() == 7){
return 'X';
2015-10-28 17:12:44 -05:00
}
}
for (int i = 0; i < opieces.size(); ++i){
if (opieces[i]->getX() == 0){
return 'O';
2015-10-28 17:12:44 -05:00
}
}
cout << "ERROR: function whoWon() called incorrectly. Game is not over. \n";
return 'a';
2015-10-19 15:45:41 -05:00
}
2015-10-27 11:31:44 -05:00
void Board::changeTurns(){
2015-10-19 15:45:41 -05:00
if (turn == 'O') turn = 'X';
else turn = 'O';
}
2015-10-27 12:55:23 -05:00
void Board::resetTaken(){
xtaken = false;
otaken = false;
}
2015-10-27 11:31:44 -05:00
void Board::displayBoard(){
cout << "; A B C D E F G H"<<endl;
2015-10-27 10:40:55 -05:00
for (int i = 0; i < 8; ++i) {
int label = 8 - i;
cout<<"; "<<label<<" ";
for (int j = 0; j < 8; ++j){
2015-10-27 11:31:44 -05:00
if (isPiece(i, j))
if (getPiece(i, j)->getType() == 'X')
cout << "|" << "X";
else
cout << "|" << "O";
2015-10-27 10:40:55 -05:00
else
cout << "|" << "_";
2015-10-19 15:36:20 -05:00
}
2015-10-27 10:40:55 -05:00
2015-10-19 17:02:56 -05:00
cout<<"|\n";
2015-10-19 15:36:20 -05:00
}
2015-10-27 10:40:55 -05:00
2015-10-19 15:45:41 -05:00
cout<<'\n'<<endl;
2015-10-27 08:38:25 -05:00
cout<<"turn: "<<turn << "\n";
2015-10-19 15:45:41 -05:00
}
2015-10-27 11:51:37 -05:00
string Board::boardToString(){
string output = "";
output += "; A B C D E F G H\n";
2015-10-27 13:48:45 -05:00
int label;
2015-10-27 11:51:37 -05:00
for (int i = 0; i < 8; ++i) {
2015-10-27 13:48:45 -05:00
label = 8 - i;
2015-10-27 11:51:37 -05:00
output += "; ";
2015-10-27 13:48:45 -05:00
output += '0' + label;
2015-10-27 11:51:37 -05:00
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;
}
2015-10-27 11:31:44 -05:00
2015-10-29 08:58:32 -05:00
Board Board::move(moves m){
int row = m.row;
int column = m.column;
2015-10-27 11:31:44 -05:00
Piece* piece;
2015-11-03 23:00:12 -06:00
2015-10-27 11:31:44 -05:00
if (isPiece(row, column))
piece = getPiece(row, column);
2015-10-19 15:36:20 -05:00
2015-10-27 11:31:44 -05:00
else{
cout<<"ERROR: attempting to move an invalid piece.\n";
2015-10-29 08:58:32 -05:00
return *this;
2015-10-27 11:31:44 -05:00
}
2015-11-03 20:37:25 -06:00
2015-10-27 11:31:44 -05:00
if (piece->getType() != turn) {
cout<<"ERROR: attempting to move the wrong side's piece.\n";
2015-11-03 23:00:12 -06:00
return *this;
2015-10-27 10:40:55 -05:00
}
2015-10-19 15:45:41 -05:00
else {
if(isThisMovePossible(row, column, m.moveType))
2015-10-29 08:58:32 -05:00
{
2015-11-03 23:00:12 -06:00
record.push_back(*this);
if (m.moveType == "FWD") {
2015-10-29 08:58:32 -05:00
piece->moveFwd();
2015-10-27 13:48:45 -05:00
}
else if (m.moveType == "LEFT") {
if(piece->getType() == 'O') {
row--;
column--;
2015-10-27 13:48:45 -05:00
}
else {
row++;
column--;
2015-10-27 13:48:45 -05:00
}
if(isPiece(row, column)) {
if(getPiece(row, column)->getType() != piece->getType()) {
isTaken(row, column);
piece->moveLeft();
}
2015-10-27 13:48:45 -05:00
}
else {
piece->moveLeft();
2015-10-27 13:48:45 -05:00
}
}
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();
}
2015-10-27 13:48:45 -05:00
}
setValidTrue();
}
2015-11-03 23:00:12 -06:00
else
{
cout << "Invalid move.\n\n";
setValidFalse();
2015-10-19 15:36:20 -05:00
}
}
2015-10-29 08:58:32 -05:00
return *this;
2015-10-19 15:45:41 -05:00
}
2015-10-27 11:31:44 -05:00
bool Board::isThisMovePossible(int r, int c, string moveType){
Piece* piece;
Piece* temp;
2015-10-27 11:31:44 -05:00
if (isPiece(r, c))
piece = getPiece(r, c);
else
2015-10-19 15:45:41 -05:00
return false;
2015-10-19 15:36:20 -05:00
2015-10-27 11:31:44 -05:00
if (piece->getType() != turn) {
cout << "Error in Board::isThisMovePossible: trying to move a piece outside your turn.\n";
return false;
2015-10-19 15:45:41 -05:00
}
2015-10-19 15:36:20 -05:00
2015-10-27 11:31:44 -05:00
else{
int reflector = 1;
if (piece->getType() == 'O')
reflector = -1;
2015-10-27 11:31:44 -05:00
2015-10-29 08:58:32 -05:00
if (moveType == "FWD"){
if (!isPiece(r + reflector, c))
2015-10-29 08:58:32 -05:00
return true;
else
2015-10-27 10:40:55 -05:00
return false;
2015-10-19 15:36:20 -05:00
}
else if (moveType == "RIGHT"){
temp = getPiece(r + reflector, c+1);
2015-10-29 08:58:32 -05:00
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;
}
}
2015-10-29 08:58:32 -05:00
else {
2015-10-27 10:40:55 -05:00
return false;
}
2015-10-19 15:45:41 -05:00
}
2015-10-19 15:36:20 -05:00
else if (moveType == "LEFT"){
temp = getPiece(r + reflector, c-1);
2015-10-29 08:58:32 -05:00
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;
}
}
2015-10-29 08:58:32 -05:00
else {
2015-10-27 10:40:55 -05:00
return false;
}
2015-10-19 15:36:20 -05:00
}
2015-10-19 15:45:41 -05:00
else return false;
}
}
2015-10-27 11:31:44 -05:00
vector<moves> Board::viewPossibleMoves(){
int r, c = -1;
2015-10-19 15:45:41 -05:00
vector<moves> output;
2015-10-27 11:31:44 -05:00
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");
2015-10-27 11:31:44 -05:00
output.push_back(temp);
}
if (isThisMovePossible(r,c,"LEFT"))
{
moves temp(r,c, "LEFT");
2015-10-27 11:31:44 -05:00
output.push_back(temp);
}
if (isThisMovePossible(r,c,"RIGHT"))
{
moves temp(r,c, "RIGHT");
2015-10-27 11:31:44 -05:00
output.push_back(temp);
}
}
}
2015-10-27 23:22:08 -05:00
else if (turn == 'O') {
2015-10-27 11:31:44 -05:00
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");
2015-10-27 11:31:44 -05:00
output.push_back(temp);
}
if (isThisMovePossible(r,c,"LEFT"))
2015-10-19 15:36:20 -05:00
{
moves temp(r,c, "LEFT");
2015-10-27 11:31:44 -05:00
output.push_back(temp);
}
if (isThisMovePossible(r,c,"RIGHT"))
{
moves temp(r,c, "RIGHT");
2015-10-27 11:31:44 -05:00
output.push_back(temp);
2015-10-19 15:36:20 -05:00
}
}
}
2015-10-19 15:45:41 -05:00
return output;
}
2015-10-27 11:31:44 -05:00
string Board::myToUpper(string input){
2015-10-19 15:45:41 -05:00
string output;
2015-10-19 15:36:20 -05:00
2015-10-19 15:45:41 -05:00
for (int i = 0 ; i < input.size(); ++i)
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
int numeric;
2015-10-19 15:36:20 -05:00
2015-10-19 15:45:41 -05:00
if ((input[i] - 0 >= 97) && (input[i] - 0 <= 122))
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
numeric = input[i] - 32;
2015-10-28 17:12:44 -05:00
output.push_back((char)numeric);
2015-10-19 15:36:20 -05:00
}
2015-10-19 15:45:41 -05:00
else output.push_back(input[i]);
2015-10-19 15:36:20 -05:00
}
return output;
2015-10-19 15:45:41 -05:00
}
2015-11-03 23:00:12 -06:00
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;
2015-10-19 16:37:20 -05:00
}
2015-10-28 17:12:44 -05:00
else{
2015-11-03 23:00:12 -06:00
Board* temp = new Board(record[record.size()-2]);
return temp;
}
2015-10-19 16:37:20 -05:00
}
2015-10-27 11:31:44 -05:00
void Board::snapshot(vector<Board>& inputVec, Board inputBoard){
2015-10-28 17:12:44 -05:00
if (inputVec.size() == 10){
2015-10-19 16:37:20 -05:00
inputVec.erase(inputVec.begin());
}
2015-10-28 17:12:44 -05:00
else if (inputVec.size() > 10){
2015-10-19 16:37:20 -05:00
cout<<"QUEUE OVERFLOW!"<<endl;
}
inputVec.push_back(inputBoard);
2015-10-27 08:38:25 -05:00
}
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)){
2015-11-03 21:41:07 -06:00
val = val + 10;
2015-10-27 10:40:55 -05:00
}
if (checkTaken(max)){
2015-11-03 21:41:07 -06:00
val = val - 10;
2015-10-27 10:40:55 -05:00
}
//ultimate condition!
if (isGameOver()){
if (whoWon() == max)
2015-11-03 21:41:07 -06:00
val = val + 10;
else
2015-11-03 21:41:07 -06:00
val = val - 10;
2015-10-27 08:38:25 -05:00
}
2015-10-30 11:56:44 -05:00
2015-10-29 13:54:16 -05:00
return val;
2015-10-27 11:31:44 -05:00
}
2015-10-29 08:58:32 -05:00