Updating Branches.
This commit is contained in:
parent
ec423cd36a
commit
37bbd43b84
9 changed files with 3 additions and 988 deletions
577
Board.cpp
577
Board.cpp
|
@ -1,575 +1,3 @@
|
||||||
<<<<<<< HEAD
|
|
||||||
#include <iostream>
|
|
||||||
#include <vector>
|
|
||||||
#include "Board.h"
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
Board::Board() {
|
|
||||||
Piece* temp;
|
|
||||||
bool valid = false;
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Board::Board(const Board& b) {
|
|
||||||
vector<Piece*> xp = b.getXPieces();
|
|
||||||
vector<Piece*> op = b.getOPieces();
|
|
||||||
Piece* temp;
|
|
||||||
bool valid = false;
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Board::setValidFalse() {
|
|
||||||
valid = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Board::setValidTrue() {
|
|
||||||
valid = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Board::isValid() {
|
|
||||||
return valid;
|
|
||||||
}
|
|
||||||
|
|
||||||
//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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
//break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
for(int x = 0; x < xpieces.size(); ++x) {
|
|
||||||
if (xpieces[x]->getX() == r && xpieces[x]->getY() == c) {
|
|
||||||
xpieces.erase(xpieces.begin() + x);
|
|
||||||
//break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pieces.erase(pieces.begin() + i);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//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();
|
|
||||||
}
|
|
||||||
|
|
||||||
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){
|
|
||||||
cout<<"\n\n\nPlayer X wins!\n\n\n"<<endl;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < opieces.size(); ++i){
|
|
||||||
if (opieces[i]->getX() == 0){
|
|
||||||
cout<<"\n\n\nPlayer O wins!\n\n\n"<<endl;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Board::changeTurns(){
|
|
||||||
if (turn == 'O') turn = 'X';
|
|
||||||
else turn = 'O';
|
|
||||||
}
|
|
||||||
|
|
||||||
void Board::displayBoard(){
|
|
||||||
/*
|
|
||||||
cout << "Debugging:\n";
|
|
||||||
for (int i = 0; i < pieces.size(); ++i){
|
|
||||||
if(i%8 == 0)
|
|
||||||
cout << "\n";
|
|
||||||
cout << pieces[i]->getX() << " " << pieces[i]->getY() << " " << pieces[i]->getType() << "\t";
|
|
||||||
}
|
|
||||||
cout << "Debugging:\n\n";
|
|
||||||
*/
|
|
||||||
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);
|
|
||||||
cout << "MOVE: " << m.row << " " << m.column << " " << m.moveType << "\n\n";
|
|
||||||
move(m);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Board::move(moves m){
|
|
||||||
int row = 8 - (m.row);
|
|
||||||
int column = m.column;
|
|
||||||
|
|
||||||
cout << "INSIDE MOVE: " << row << " " << column << "\n\n";
|
|
||||||
|
|
||||||
if (row > 8 || row < 0 || column > 8 || column < 0) {
|
|
||||||
cout<<"ERROR: index out of bound."<<endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
//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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 == '0') {
|
|
||||||
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,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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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);// = 'Q';//(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);
|
|
||||||
//cout << "MOVE: " << input << "\n\n";
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
int Board::evaluate(char max, char min){
|
|
||||||
//right now just evaluating number of pieces
|
|
||||||
if (max == 'X'){
|
|
||||||
return (xpieces.size() - opieces.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (max == 'O'){
|
|
||||||
return (opieces.size() - xpieces.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
cout << "Unidentified max, must be either X or O.\n";
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
=======
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "Board.h"
|
#include "Board.h"
|
||||||
|
@ -1139,8 +567,3 @@ int Board::evaluate(char max, char min){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
>>>>>>> master
|
|
||||||
|
|
60
Board.h
60
Board.h
|
@ -1,62 +1,3 @@
|
||||||
<<<<<<< HEAD
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <vector>
|
|
||||||
#include "Piece.h"
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
struct moves {
|
|
||||||
int row;
|
|
||||||
int column;
|
|
||||||
string moveType;
|
|
||||||
|
|
||||||
moves(int linea, int columna, string m) {
|
|
||||||
row = linea;
|
|
||||||
column = columna;
|
|
||||||
moveType = m;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class Board {
|
|
||||||
vector<Piece*> xpieces;
|
|
||||||
vector<Piece*> opieces;
|
|
||||||
vector<Piece*> pieces;
|
|
||||||
char turn = 'O';
|
|
||||||
bool valid = false;
|
|
||||||
|
|
||||||
public:
|
|
||||||
Board();
|
|
||||||
Board(const Board& b);
|
|
||||||
void setValidFalse();
|
|
||||||
void setValidTrue();
|
|
||||||
bool isValid();
|
|
||||||
bool isPiece(int r, int c);
|
|
||||||
void isTaken(int r, int c);
|
|
||||||
Piece* getPiece(int r, int c);
|
|
||||||
vector<Piece*> getXPieces() const { return xpieces; }
|
|
||||||
vector<Piece*> getOPieces() const { return opieces; }
|
|
||||||
moves parse(string input);
|
|
||||||
char getTurn() { return turn; }
|
|
||||||
bool isGameOver();
|
|
||||||
void changeTurns();
|
|
||||||
void displayBoard();
|
|
||||||
string boardToString();
|
|
||||||
int charToIntColumn(char input);
|
|
||||||
char intToCharColumn(int input);
|
|
||||||
void move(string inputMove);
|
|
||||||
void move(moves jugada);
|
|
||||||
void moveWOPrint(moves jugada);
|
|
||||||
bool isThisMovePossible(int r, int c, string moveType);
|
|
||||||
vector<moves> viewPossibleMoves();
|
|
||||||
string myToUpper(string input);
|
|
||||||
void undo(Board& tablero);
|
|
||||||
void interpret(string input, Board& tablero);
|
|
||||||
void snapshot(vector<Board>& inputVec, Board inputBoard);
|
|
||||||
int evaluate(char max, char min);
|
|
||||||
};
|
|
||||||
=======
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
@ -115,4 +56,3 @@ public:
|
||||||
void snapshot(vector<Board>& inputVec, Board inputBoard);
|
void snapshot(vector<Board>& inputVec, Board inputBoard);
|
||||||
int evaluate(char max, char min);
|
int evaluate(char max, char min);
|
||||||
};
|
};
|
||||||
>>>>>>> master
|
|
||||||
|
|
BIN
Client.class
BIN
Client.class
Binary file not shown.
217
Engine.cpp
217
Engine.cpp
|
@ -1,219 +1,3 @@
|
||||||
<<<<<<< HEAD
|
|
||||||
#include <iostream>
|
|
||||||
#include <vector>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <sstream>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include "Engine.h"
|
|
||||||
|
|
||||||
Engine::Engine(){
|
|
||||||
Board* brd = new Board();
|
|
||||||
b = brd;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Engine::startGame(int port){
|
|
||||||
|
|
||||||
cout << "Launching server..." << endl;
|
|
||||||
int sockfd, newsockfd, portno;
|
|
||||||
socklen_t clilen;
|
|
||||||
struct sockaddr_in serv_addr, cli_addr;
|
|
||||||
int n;
|
|
||||||
|
|
||||||
if (port < 2) {
|
|
||||||
cout << "ERROR, no port provided\n";
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
|
||||||
|
|
||||||
if (sockfd < 0)
|
|
||||||
cout << "ERROR opening socket";
|
|
||||||
|
|
||||||
bzero((char *) &serv_addr, sizeof(serv_addr));
|
|
||||||
portno = port;
|
|
||||||
serv_addr.sin_family = AF_INET;
|
|
||||||
serv_addr.sin_addr.s_addr = INADDR_ANY;
|
|
||||||
serv_addr.sin_port = htons(portno);
|
|
||||||
|
|
||||||
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
|
|
||||||
cout << "ERROR on binding";
|
|
||||||
|
|
||||||
listen(sockfd,5);
|
|
||||||
clilen = sizeof(cli_addr);
|
|
||||||
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
|
|
||||||
|
|
||||||
if (newsockfd < 0)
|
|
||||||
cout << "ERROR on accept";
|
|
||||||
|
|
||||||
string move;
|
|
||||||
bool gameOver = false;
|
|
||||||
vector<Board> record;
|
|
||||||
b->snapshot(record, *b);
|
|
||||||
char buffer[256];
|
|
||||||
char info[256];
|
|
||||||
int choice = -1;
|
|
||||||
int choice_difficulty = -1;
|
|
||||||
string final_move;
|
|
||||||
|
|
||||||
//Ask client about game type
|
|
||||||
string introduction = "WELCOME\n1. Play against AI?\n2. Watch AI vs. AI?\nEnter choice: \n";
|
|
||||||
write(newsockfd, introduction.c_str(), introduction.length());
|
|
||||||
n = read(newsockfd,info,255); //Reads choice as a string
|
|
||||||
istringstream convert(info); //Converts the read string to an integer
|
|
||||||
convert >> choice; //Sets value equal to the converted value
|
|
||||||
//Later in the project, we need to check for AI and Human modes
|
|
||||||
cout << "OK" << endl;
|
|
||||||
bzero(info,256); //Resets info back to normal
|
|
||||||
|
|
||||||
/*//Becca- once your AI code is working, use this code:
|
|
||||||
|
|
||||||
string difficulty_select = "Choose Difficulty\n1. Easy\n2. Medium\n4. Hard\nEnter choice: \n";
|
|
||||||
write(newsockfd, difficulty_select.c_str(), difficulty_select.length());
|
|
||||||
n = read(newsockfd,info,255);
|
|
||||||
istringstream convert(info);
|
|
||||||
convert >> choice_difficulty;
|
|
||||||
cout << "OK" << endl;
|
|
||||||
bzero(info,256);
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
while (gameOver != true)
|
|
||||||
{
|
|
||||||
gameOver = b->isGameOver();
|
|
||||||
|
|
||||||
while(b->getTurn() == 'O' && !b->isValid())
|
|
||||||
{
|
|
||||||
b->displayBoard();
|
|
||||||
string boardState = b->boardToString();
|
|
||||||
final_move = b->boardToString();
|
|
||||||
write(newsockfd, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
|
|
||||||
cout<<"\nStanding by for client... \n";
|
|
||||||
n = read(newsockfd,buffer,255);//Read the client's input
|
|
||||||
move = buffer;
|
|
||||||
bzero(buffer,256);
|
|
||||||
b->interpret(move, *b);
|
|
||||||
if(b->isValid()) {
|
|
||||||
b->changeTurns();
|
|
||||||
b->setValidFalse();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(b->isValid()) cout << b->getTurn();
|
|
||||||
|
|
||||||
while(b->getTurn() == 'X' )
|
|
||||||
{
|
|
||||||
easyAI();
|
|
||||||
/*Becca- once you finish your AI, uncomment this out and remove the line directly above this
|
|
||||||
if(choice_difficulty == 1)
|
|
||||||
easyAI();
|
|
||||||
else if(choice_difficulty == 2)
|
|
||||||
mediumAI();
|
|
||||||
else
|
|
||||||
hardAI();
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
gameOver = b->isGameOver();
|
|
||||||
b->setValidFalse();
|
|
||||||
b->snapshot(record, *b);
|
|
||||||
}
|
|
||||||
final_move = b->boardToString();
|
|
||||||
string game_over = "\n\nGAME OVER!!!\n";
|
|
||||||
write(newsockfd, final_move.c_str(), final_move.length()); //Display the board to the client (line by line)
|
|
||||||
write(newsockfd, game_over.c_str(), game_over.length()); //Tell the client the game is over
|
|
||||||
cout << game_over;
|
|
||||||
usleep(1);
|
|
||||||
close(newsockfd);
|
|
||||||
close(sockfd);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Engine::easyAI()
|
|
||||||
{
|
|
||||||
vector<moves> listOfMoves = b->viewPossibleMoves();
|
|
||||||
/*
|
|
||||||
for(int x = 0; x < listOfMoves.size(); ++x) {
|
|
||||||
cout << listOfMoves[x].row << " " << listOfMoves[x].column << " " << listOfMoves[x].moveType << "\n\n";
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
srand(time(NULL));
|
|
||||||
int randomChoice = rand() % (listOfMoves.size()-1) - 0;
|
|
||||||
|
|
||||||
int temp = randomChoice;
|
|
||||||
cout << "easy AI move: " << listOfMoves[randomChoice].row << listOfMoves[randomChoice].column << listOfMoves[randomChoice].moveType << "\n";
|
|
||||||
b->move(listOfMoves[randomChoice]);
|
|
||||||
b->changeTurns();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Engine::AI(){
|
|
||||||
cout << "----------------------BEGIN AI FUNCTION----------------------\n";
|
|
||||||
vector<moves> listOfMoves = b->viewPossibleMoves();
|
|
||||||
//Board* b = new Board(*b);
|
|
||||||
|
|
||||||
//probably not needed, check later
|
|
||||||
/*
|
|
||||||
if (b->getTurn() != 'X'){
|
|
||||||
cout << "a changing of turns is needed. \n";
|
|
||||||
b->changeTurns();
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
//only doing 1 branch right now because testing
|
|
||||||
/*for (int i = 0; i < listOfMoves.size(); ++i){
|
|
||||||
minMax(b, listOfMoves[i]);
|
|
||||||
}*/
|
|
||||||
|
|
||||||
b->move(minMax(listOfMoves[0], 0));
|
|
||||||
b->changeTurns();
|
|
||||||
|
|
||||||
//verification of correct turn
|
|
||||||
/*
|
|
||||||
if (b->getTurn() != 'O'){
|
|
||||||
cout << "ERROR in Engine::AI: b is on the wrong turn. \n";
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
b->displayBoard();
|
|
||||||
cout << "----------------------END AI FUNCTION----------------------\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
moves Engine::minMax(moves m, int c){
|
|
||||||
//testing purposes only, c = finite depth
|
|
||||||
/*
|
|
||||||
if (c > 5){
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (b->isGameOver() == true){
|
|
||||||
cout << "END OF PATH REACHED\n";
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
if(b->isThisMovePossible(8 - m.row, b->charToIntColumn(m.column), m.moveType)){
|
|
||||||
cout << "piece has been moved in minMax\n";
|
|
||||||
b->move(m);
|
|
||||||
b->changeTurns();
|
|
||||||
}
|
|
||||||
cout << "c: " << c << "\n\n";
|
|
||||||
cout << "current turn: " << b->getTurn() << "\n";
|
|
||||||
vector<moves> listOfMoves = b->viewPossibleMoves();
|
|
||||||
|
|
||||||
for (int i = 0; i < listOfMoves.size(); ++i){
|
|
||||||
//return minMax(b, listOfMoves[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
b->displayBoard();
|
|
||||||
//limited recursion
|
|
||||||
return minMax(b, listOfMoves[0], ++c);
|
|
||||||
|
|
||||||
//testing
|
|
||||||
return m;
|
|
||||||
}*/
|
|
||||||
}
|
|
||||||
=======
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
@ -358,4 +142,3 @@ moves Engine::minMax(Board* temp, moves m, int c, int r){
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
>>>>>>> master
|
|
||||||
|
|
18
Engine.h
18
Engine.h
|
@ -1,20 +1,3 @@
|
||||||
<<<<<<< HEAD
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "Board.h"
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
class Engine {
|
|
||||||
Board* b;
|
|
||||||
|
|
||||||
public:
|
|
||||||
Engine();
|
|
||||||
void startGame(int port);
|
|
||||||
void easyAI();
|
|
||||||
void AI();
|
|
||||||
moves minMax(moves m, int c);
|
|
||||||
=======
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Board.h"
|
#include "Board.h"
|
||||||
|
@ -31,5 +14,4 @@ public:
|
||||||
void AI();
|
void AI();
|
||||||
Board* getBoard() { return b; }
|
Board* getBoard() { return b; }
|
||||||
moves minMax(Board* temp, moves m, int c, int r);
|
moves minMax(Board* temp, moves m, int c, int r);
|
||||||
>>>>>>> master
|
|
||||||
};
|
};
|
||||||
|
|
72
Piece.cpp
72
Piece.cpp
|
@ -1,4 +1,3 @@
|
||||||
<<<<<<< HEAD
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "Piece.h"
|
#include "Piece.h"
|
||||||
|
|
||||||
|
@ -68,74 +67,3 @@ void Piece::moveRight(){
|
||||||
void Piece::isTaken(){
|
void Piece::isTaken(){
|
||||||
cout << getX() << " " << getY() << "\n\n";
|
cout << getX() << " " << getY() << "\n\n";
|
||||||
}
|
}
|
||||||
=======
|
|
||||||
#include <iostream>
|
|
||||||
#include "Piece.h"
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
Piece::Piece(){
|
|
||||||
x = -1;
|
|
||||||
y = -1;
|
|
||||||
type = '?';
|
|
||||||
}
|
|
||||||
|
|
||||||
Piece::Piece(int r, int c){
|
|
||||||
x = r;
|
|
||||||
y = c;
|
|
||||||
type = '_';
|
|
||||||
}
|
|
||||||
|
|
||||||
Piece::Piece(int r, int c, char t){
|
|
||||||
x = r;
|
|
||||||
y = c;
|
|
||||||
type = t;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Piece::moveFwd(){
|
|
||||||
if (type == 'X'){
|
|
||||||
x++;
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (type == 'O'){
|
|
||||||
x--;
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
cout << "Error: trying to move an empty piece forward.";
|
|
||||||
}
|
|
||||||
|
|
||||||
void Piece::moveLeft(){
|
|
||||||
if (type == 'X'){
|
|
||||||
x++;
|
|
||||||
y--;
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (type == 'O'){
|
|
||||||
x--;
|
|
||||||
y--;
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
cout << "Error: trying to move an empty piece left.";
|
|
||||||
}
|
|
||||||
|
|
||||||
void Piece::moveRight(){
|
|
||||||
if (type == 'X'){
|
|
||||||
x++;
|
|
||||||
y++;
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (type == 'O'){
|
|
||||||
x--;
|
|
||||||
y++;
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
cout << "Error: trying to move an empty piece left.";
|
|
||||||
}
|
|
||||||
|
|
||||||
void Piece::isTaken(){
|
|
||||||
cout << getX() << " " << getY() << "\n\n";
|
|
||||||
}
|
|
||||||
>>>>>>> master
|
|
||||||
|
|
27
Piece.h
27
Piece.h
|
@ -1,4 +1,3 @@
|
||||||
<<<<<<< HEAD
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
@ -23,29 +22,3 @@ public:
|
||||||
void makeEmpty(){ type = '_'; }
|
void makeEmpty(){ type = '_'; }
|
||||||
void isTaken();
|
void isTaken();
|
||||||
};
|
};
|
||||||
=======
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
class Piece {
|
|
||||||
int x;
|
|
||||||
int y;
|
|
||||||
char type;
|
|
||||||
|
|
||||||
public:
|
|
||||||
Piece();
|
|
||||||
Piece(int r, int c);
|
|
||||||
Piece(int r, int c, char t);
|
|
||||||
void moveFwd();
|
|
||||||
void moveLeft();
|
|
||||||
void moveRight();
|
|
||||||
int getX(){ return x; }
|
|
||||||
void setX(int r){ x = r; }
|
|
||||||
int getY(){ return y; }
|
|
||||||
void setY(int c){ y = c; }
|
|
||||||
char getType(){ return type; }
|
|
||||||
void makeEmpty(){ type = '_'; }
|
|
||||||
void isTaken();
|
|
||||||
};
|
|
||||||
>>>>>>> master
|
|
||||||
|
|
10
README.md
10
README.md
|
@ -1,15 +1,6 @@
|
||||||
# Breakthrough
|
# Breakthrough
|
||||||
Reposity for the second CSCE 315 project
|
Reposity for the second CSCE 315 project
|
||||||
|
|
||||||
<<<<<<< HEAD
|
|
||||||
To compile everything: g++ -std=c++11 test.cpp Engine.cpp Board.cpp Piece.cpp -o runner or, alternatively if you delete the Parser.h, Parser.cpp, and Client.cpp (there's no need for them currently) g++ -std=c++11 *.cpp -o runner
|
|
||||||
|
|
||||||
To launch the server: ./runner
|
|
||||||
|
|
||||||
Enter a port number when prompted, then the server is launched and waiting for a client to join.
|
|
||||||
|
|
||||||
Client can simply join via telnet: telnet linux.cse.tamu.edu
|
|
||||||
=======
|
|
||||||
To compile everything:
|
To compile everything:
|
||||||
make
|
make
|
||||||
or make all
|
or make all
|
||||||
|
@ -22,4 +13,3 @@ To launch the server:
|
||||||
|
|
||||||
Client can simply join via telnet:
|
Client can simply join via telnet:
|
||||||
telnet linux.cse.tamu.edu <port number>
|
telnet linux.cse.tamu.edu <port number>
|
||||||
>>>>>>> master
|
|
||||||
|
|
4
test.cpp
4
test.cpp
|
@ -4,9 +4,6 @@ using namespace std;
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
<<<<<<< HEAD
|
|
||||||
Engine e;
|
|
||||||
=======
|
|
||||||
//Board b;
|
//Board b;
|
||||||
Engine e;
|
Engine e;
|
||||||
/*
|
/*
|
||||||
|
@ -21,5 +18,4 @@ int main()
|
||||||
=======
|
=======
|
||||||
*/
|
*/
|
||||||
e.startGame();
|
e.startGame();
|
||||||
>>>>>>> master
|
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue