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

561 lines
9.9 KiB
C++
Raw Normal View History

2015-10-19 15:56:21 -05:00
#include <iostream>
#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() {
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 8; ++j) {
boardArray[i][j] = 'X';
}
}
2015-10-19 15:36:20 -05:00
2015-10-19 15:45:41 -05:00
for (int i = 2; i < 6; ++i) {
for (int j = 0; j < 8; ++j) {
boardArray[i][j] = '_';
2015-10-19 15:36:20 -05:00
}
2015-10-19 15:45:41 -05:00
}
for (int i = 6; i <= 7; ++i) {
for (int j = 0; j < 8; ++j) {
boardArray[i][j] = 'O';
2015-10-19 15:36:20 -05:00
}
}
2015-10-19 15:45:41 -05:00
}
moves Board::parse(string input)
{
2015-10-19 15:36:20 -05:00
2015-10-19 15:45:41 -05:00
input = myToUpper(input);
cout<<input<<endl;
int temp1;
char temp2;
string temp3;
temp2 = input[0];
temp1 = input[1] - '0';
if (input[3] == 'L')
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
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 < 8; ++i)
{
if (boardArray[0][i] == 'O')
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
cout<<"\n\n\nplayer O wins!\n\n\n"<<endl;
return true;
2015-10-19 15:36:20 -05:00
}
2015-10-19 15:45:41 -05:00
if (boardArray[7][i] == 'X')
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
cout<<"\n\n\nplayer X wins!\n\n\n"<<endl;
return true;
2015-10-19 15:36:20 -05:00
}
2015-10-19 15:45:41 -05:00
}
}
void Board::changeTurns()
{
if (turn == 'O') turn = 'X';
else turn = 'O';
}
2015-10-19 15:56:21 -05:00
void Board::displayBoard()
2015-10-19 15:45:41 -05:00
{
cout<<"\n\n";
2015-10-19 17:02:56 -05:00
cout<<"; A B C D E F G H"<<endl;
2015-10-19 15:45:41 -05:00
for (int i = 0; i < 8; ++i)
{
int mango = 8 - i;
2015-10-19 17:02:56 -05:00
cout<<"; "<<mango<<" ";
2015-10-19 15:45:41 -05:00
for (int j = 0; j < 8; ++j)
2015-10-19 15:36:20 -05:00
{
2015-10-19 17:02:56 -05:00
cout<<"|"<<boardArray[i][j];
2015-10-19 15:36:20 -05:00
}
2015-10-19 17:02:56 -05:00
cout<<"|\n";
2015-10-19 15:36:20 -05:00
}
2015-10-19 15:45:41 -05:00
cout<<'\n'<<endl;
cout<<"turn : "<<turn;
}
2015-10-19 15:36:20 -05:00
2015-10-19 15:45:41 -05:00
int Board::charToIntColumn(char input) //converts column number to int
{
int kolumn;
2015-10-19 15:36:20 -05:00
2015-10-19 15:45:41 -05:00
switch (input)
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
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;
2015-10-19 15:36:20 -05:00
}
2015-10-19 15:45:41 -05:00
return kolumn;
}
char Board::intToCharColumn(int input) //converts column number to int
{
char kolumn;
2015-10-19 15:36:20 -05:00
2015-10-19 15:45:41 -05:00
switch (input)
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
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;
2015-10-19 15:36:20 -05:00
}
2015-10-19 15:45:41 -05:00
return kolumn;
}
2015-10-19 15:36:20 -05:00
2015-10-19 15:45:41 -05:00
void Board::move(string inputMove)
{
2015-10-19 15:36:20 -05:00
2015-10-19 15:45:41 -05:00
moves jugada = parse(inputMove);
2015-10-19 15:36:20 -05:00
2015-10-19 15:45:41 -05:00
int row = 8 - (jugada.row);
int kolumn = charToIntColumn(jugada.column);
int temp = boardArray[row][kolumn];
int reflector = 1;
2015-10-19 15:36:20 -05:00
2015-10-19 15:45:41 -05:00
if (row > 8 || row < 0 || kolumn > 8 || kolumn < 0)
{
cout<<"ERROR: index out of bound!"<<endl;
2015-10-19 15:36:20 -05:00
}
2015-10-19 15:45:41 -05:00
else if (temp != turn)
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
cout<<"you can't move that piece at this turn!"<<endl;
2015-10-19 15:36:20 -05:00
}
2015-10-19 15:45:41 -05:00
else if (temp == '_')
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
cout<<"there's no piece in that spot!"<<endl;
2015-10-19 15:36:20 -05:00
}
2015-10-19 15:45:41 -05:00
else if (temp == 'X' || temp == 'O')
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
if (temp == 'O')
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
reflector *= -1;
2015-10-19 15:36:20 -05:00
}
2015-10-19 15:45:41 -05:00
if (jugada.moveType == "FWD")
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
if(boardArray[row+reflector][kolumn] != '_')
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
cout<<"you can't move that piece forward"<<endl;
2015-10-19 15:36:20 -05:00
}
2015-10-19 15:45:41 -05:00
else
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
boardArray[row][kolumn] = '_';
boardArray[row+reflector][kolumn] = temp;
2015-10-19 15:36:20 -05:00
2015-10-19 15:45:41 -05:00
changeTurns();
displayBoard();
2015-10-19 15:36:20 -05:00
2015-10-19 15:45:41 -05:00
}
}
else if (jugada.moveType == "LEFT")
{
if (kolumn == 0)
{
cout<<"Destination Spot out of range!"<<endl;
2015-10-19 15:36:20 -05:00
}
2015-10-19 15:45:41 -05:00
else if (boardArray[row+reflector][kolumn-1] == temp)
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
cout<<"you hate your own team or something? you can't do that!"<<endl;
2015-10-19 15:36:20 -05:00
}
2015-10-19 15:45:41 -05:00
else
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
boardArray[row][kolumn] = '_';
boardArray[row+reflector][kolumn-1] = temp;
changeTurns();
displayBoard();
}
}
else if (jugada.moveType == "RIGHT")
{
if (kolumn == 7)
{
cout<<"Destination Spot out of range!"<<endl;
}
2015-10-19 15:36:20 -05:00
2015-10-19 15:45:41 -05:00
else if (boardArray[row+reflector][kolumn+1] == temp)
{
cout<<"you hate your own team or something? you can't do that!"<<endl;
2015-10-19 15:36:20 -05:00
}
else
{
2015-10-19 15:45:41 -05:00
boardArray[row][kolumn] = '_';
boardArray[row+reflector][kolumn+1] = temp;
changeTurns();
displayBoard();
}
2015-10-19 15:36:20 -05:00
}
else
{
2015-10-19 15:45:41 -05:00
cout<<"Unrecognized movetype!"<<endl;
}
}
else
{
cout<<"Invalid piece!"<<endl;
}
}
2015-10-19 17:02:56 -05:00
2015-10-19 15:45:41 -05:00
void Board::move(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!"<<endl;
}
2015-10-19 15:36:20 -05:00
2015-10-19 15:45:41 -05:00
int temp = boardArray[row][kolumn];
if (temp != turn)
{
cout<<"you can't move that piece at this turn!"<<endl;
2015-10-19 15:36:20 -05:00
}
2015-10-19 15:45:41 -05:00
else if (temp == '_')
{
cout<<"there's no piece in that spot!"<<endl;
}
2015-10-19 15:36:20 -05:00
2015-10-19 15:45:41 -05:00
int reflector = 1;
if (temp == 'X' || temp == 'O')
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
if (temp == 'O')
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
reflector *= -1;
2015-10-19 15:36:20 -05:00
}
2015-10-19 15:45:41 -05:00
if (jugada.moveType == "FWD")
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
if(boardArray[row+reflector][kolumn] != '_')
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
cout<<"you can't move that piece forward"<<endl;
2015-10-19 15:36:20 -05:00
}
2015-10-19 15:45:41 -05:00
else
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
boardArray[row][kolumn] = '_';
boardArray[row+reflector][kolumn] = temp;
2015-10-19 15:36:20 -05:00
2015-10-19 15:45:41 -05:00
changeTurns();
displayBoard();
2015-10-19 15:36:20 -05:00
2015-10-19 15:45:41 -05:00
}
}
else if (jugada.moveType == "LEFT")
{
if (kolumn == 0)
{
cout<<"Destination Spot out of range!"<<endl;
2015-10-19 15:36:20 -05:00
}
2015-10-19 15:45:41 -05:00
else if (boardArray[row+reflector][kolumn-1] == temp)
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
cout<<"you hate your own team or something? you can't do that!"<<endl;
2015-10-19 15:36:20 -05:00
}
2015-10-19 15:45:41 -05:00
else
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
boardArray[row][kolumn] = '_';
boardArray[row+reflector][kolumn-1] = temp;
changeTurns();
displayBoard();
}
}
else if (jugada.moveType == "RIGHT")
{
if (kolumn == 7)
{
cout<<"Destination Spot out of range!"<<endl;
}
2015-10-19 15:36:20 -05:00
2015-10-19 15:45:41 -05:00
else if (boardArray[row+reflector][kolumn+1] == temp)
{
cout<<"you hate your own team or something? you can't do that!"<<endl;
2015-10-19 15:36:20 -05:00
}
else
{
2015-10-19 15:45:41 -05:00
boardArray[row][kolumn] = '_';
boardArray[row+reflector][kolumn+1] = temp;
changeTurns();
displayBoard();
2015-10-19 15:36:20 -05:00
}
}
else
{
2015-10-19 15:45:41 -05:00
cout<<"Unrecognized movetype!"<<endl;
2015-10-19 15:36:20 -05:00
}
}
2015-10-19 15:45:41 -05:00
else
{
cout<<"Invalid piece!"<<endl;
}
}
bool Board::isThisMovePossible(int r, int c, string moveType)
{
char pieceToMove = boardArray[r][c];
if (pieceToMove != turn) //trying to move invalid piece
{
return false;
}
int reflector = 1;
2015-10-19 15:36:20 -05:00
2015-10-19 15:45:41 -05:00
if (pieceToMove == 'O')
{
reflector *= -1;
}
2015-10-19 15:36:20 -05:00
2015-10-19 15:45:41 -05:00
else
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
if (moveType == "FWD")
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
if (boardArray[r+reflector][c] == '_') return true;
else return false;
2015-10-19 15:36:20 -05:00
}
2015-10-19 15:45:41 -05:00
else if (moveType == "RIGHT")
{
if (boardArray[r+reflector][c+1] != pieceToMove && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 <= 7) ) return true;
else return false;
}
2015-10-19 15:36:20 -05:00
2015-10-19 15:45:41 -05:00
else if (moveType == "LEFT")
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
if (boardArray[r+reflector][c-1] != pieceToMove && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 >= 0) ) return true;
else return false;
2015-10-19 15:36:20 -05:00
}
2015-10-19 15:45:41 -05:00
else return false;
}
}
vector<moves> Board::viewPossibleMoves()
{
vector<moves> output;
for (int r = 0; r < 8; ++r)
{
for (int c = 0; c < 8; ++c)
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
if (boardArray[r][c] == turn)
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
if (isThisMovePossible(r,c,"FWD"))
{
moves temp(8-r,intToCharColumn(c+1),"FWD");
output.push_back(temp);
}
if (isThisMovePossible(r,c,"LEFT"))
{
moves temp(8-r,intToCharColumn(c+1),"LEFT");
output.push_back(temp);
}
if (isThisMovePossible(r,c,"RIGHT"))
{
moves temp(8-r,intToCharColumn(c+1),"RIGHT");
output.push_back(temp);
}
2015-10-19 15:36:20 -05:00
}
}
}
2015-10-19 15:45:41 -05:00
return output;
}
2015-10-19 15:36:20 -05:00
2015-10-19 15:45:41 -05:00
string Board::myToUpper(string input)
{
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;
output.push_back((char)numeric);// = 'Q';//(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
}
2015-10-19 15:45:41 -05:00
for (int i = 0; i < output.size(); ++i)
2015-10-19 15:36:20 -05:00
{
2015-10-19 15:45:41 -05:00
cout<<output[i]<<endl;
}
2015-10-19 15:36:20 -05:00
2015-10-19 15:45:41 -05:00
return output;
2015-10-19 15:36:20 -05:00
2015-10-19 15:45:41 -05:00
}
void Board::displayPossibleMoves(vector<moves> input)
{
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;
2015-10-19 15:36:20 -05:00
}
2015-10-19 15:45:41 -05:00
}
2015-10-19 16:37:20 -05:00
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) //determines what kind of command its input is
{
2015-10-19 16:45:38 -05:00
vector<Board> record;
2015-10-19 16:37:20 -05:00
input = myToUpper(input);
if (input == "UNDO")
{
undo(tablero);
}
else if (input == "DISPLAYRECORD") //for debugging purposes
{
cout<<"record: "<<endl;
cout<<record.size();
for (int i = 0; i < record.size(); ++i)
{
record[i].displayBoard();
}
cout<<"---------------------------------------------------END DISPLAY RECORD------------------------"<<endl;
}
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);
}
2015-10-19 17:02:56 -05:00
//move this to its own file
2015-10-19 15:45:41 -05:00
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
}