Merge branch 'beccadev' of https://github.tamu.edu/brj2013/Breakthrough into beccadev

This commit is contained in:
Alexander Huddleston 2015-10-27 11:33:42 -05:00
commit 2d5df46667
8 changed files with 195 additions and 198 deletions

251
Board.cpp
View file

@ -8,18 +8,22 @@ Board::Board() {
for (int i = 0; i < 2; ++i) { for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 8; ++j) { for (int j = 0; j < 8; ++j) {
xpieces.push_back(new Piece(i, j, 'X')); xpieces.push_back(new Piece(i, j, 'X'));
pieces.push_back(new Piece(i, j, 'X'));
} }
} }
for (int i = 6; i < 8; ++i) { for (int i = 6; i < 8; ++i) {
for (int j = 0; j < 8; ++j) { for (int j = 0; j < 8; ++j) {
opieces.push_back(new Piece(i, j, 'O')); opieces.push_back(new Piece(i, j, 'O'));
pieces.push_back(new Piece(i, j, 'O'));
} }
} }
} }
bool Board::elemInXs(int r, int c){ //make this efficient!
for (int i = 0; i < xpieces.size(); ++i){ bool Board::isPiece(int r, int c){
if (xpieces[i]->getX() == r && xpieces[i]->getY() == c){ for (int i = 0; i < pieces.size(); ++i){
if (pieces[i]->getX() == r && pieces[i]->getY() == c){
return true; return true;
} }
} }
@ -27,18 +31,18 @@ bool Board::elemInXs(int r, int c){
return false; return false;
} }
bool Board::elemInOs(int r, int c){ //make this efficient!
for (int i = 0; i < opieces.size(); ++i){ Piece* Board::getPiece(int r, int c){
if (opieces[i]->getX() == r && opieces[i]->getY() == c){ for (int i = 0; i < pieces.size(); ++i){
return true; if (pieces[i]->getX() == r && pieces[i]->getY() == c){
return pieces[i];
} }
} }
return false; return new Piece();
} }
moves Board::parse(string input) moves Board::parse(string input){
{
input = myToUpper(input); input = myToUpper(input);
int temp1; int temp1;
@ -66,43 +70,40 @@ moves Board::parse(string input)
moves output(temp1, temp2, temp3); moves output(temp1, temp2, temp3);
return output; return output;
} }
bool Board::isGameOver() bool Board::isGameOver(){
{
for (int i = 0; i < xpieces.size(); ++i){ for (int i = 0; i < xpieces.size(); ++i){
if (xpieces[i]->getX() == 0){ if (xpieces[i]->getX() == 7){
cout<<"\n\n\nPlayer X wins!\n\n\n"<<endl; cout<<"\n\n\nPlayer X wins!\n\n\n"<<endl;
return true; return true;
} }
} }
for (int i = 0; i < opieces.size(); ++i){ for (int i = 0; i < opieces.size(); ++i){
if (opieces[i]->getX() == 7){ if (opieces[i]->getX() == 0){
cout<<"\n\n\nPlayer O wins!\n\n\n"<<endl; cout<<"\n\n\nPlayer O wins!\n\n\n"<<endl;
return true; return true;
} }
} }
} }
void Board::changeTurns() void Board::changeTurns(){
{
if (turn == 'O') turn = 'X'; if (turn == 'O') turn = 'X';
else turn = 'O'; else turn = 'O';
} }
void Board::displayBoard() void Board::displayBoard(){
{ cout << "; A B C D E F G H"<<endl;
cout<<"; A B C D E F G H"<<endl;
for (int i = 0; i < 8; ++i) { for (int i = 0; i < 8; ++i) {
int label = 8 - i; int label = 8 - i;
cout<<"; "<<label<<" "; cout<<"; "<<label<<" ";
for (int j = 0; j < 8; ++j){ for (int j = 0; j < 8; ++j){
if (elemInXs(i, j)) if (isPiece(i, j))
cout << "|" << "X"; if (getPiece(i, j)->getType() == 'X')
else if (elemInOs(i, j)) cout << "|" << "X";
cout << "|" << "O"; else
cout << "|" << "O";
else else
cout << "|" << "_"; cout << "|" << "_";
} }
@ -114,8 +115,7 @@ void Board::displayBoard()
cout<<"turn: "<<turn << "\n"; cout<<"turn: "<<turn << "\n";
} }
int Board::charToIntColumn(char input) //converts column number to int int Board::charToIntColumn(char input){
{
int kolumn; int kolumn;
switch (input) switch (input)
@ -133,8 +133,7 @@ int Board::charToIntColumn(char input) //converts column number to int
return kolumn; return kolumn;
} }
char Board::intToCharColumn(int input) //converts column number to int char Board::intToCharColumn(int input){
{
char kolumn; char kolumn;
switch (input) switch (input)
@ -151,105 +150,84 @@ char Board::intToCharColumn(int input) //converts column number to int
return kolumn; return kolumn;
} }
/*
void Board::move(string inputMove) void Board::move(string inputMove){
{
moves jugada = parse(inputMove); moves jugada = parse(inputMove);
move(jugada); move(jugada);
} }
void Board::move(moves jugada) void Board::move(moves jugada){
{
int row = 8 - (jugada.row); int row = 8 - (jugada.row);
int column = charToIntColumn(jugada.column); int column = charToIntColumn(jugada.column);
cout << "move coordinates: (" << row << ", " << column << ")\n";
if (row > 8 || row < 0 || column > 8 || column < 0) { if (row > 8 || row < 0 || column > 8 || column < 0) {
cout<<"ERROR: index out of bound."<<endl; cout<<"ERROR: index out of bound."<<endl;
} }
char temp = boardArray[row][column]->getType(); Piece* piece;
cout << "temp: " << temp << "\n"; if (isPiece(row, column))
piece = getPiece(row, column);
if (temp != turn || temp == '_') { else{
cout<<"ERROR: attempting to move an invalid piece."<<endl; cout<<"ERROR: attempting to move an invalid piece.\n";
return;
}
if (piece->getType() != turn) {
cout<<"ERROR: attempting to move the wrong side's piece.\n";
} }
else { else {
if (jugada.moveType == "FWD") { if (jugada.moveType == "FWD") {
boardArray[row][column]->moveFwd(); piece->moveFwd();
} }
else if (jugada.moveType == "LEFT") { else if (jugada.moveType == "LEFT") {
if (column == 0) //add error checking
cout<<"Destination Spot out of range!"<<endl; piece->moveLeft();
else if (boardArray[row][column-1]->getType() == temp)
cout<<"you hate your own team or something? you can't do that!"<<endl;
else
boardArray[row][column]->moveLeft();
} }
else if (jugada.moveType == "RIGHT") { else if (jugada.moveType == "RIGHT") {
if (column == 7) //add error checking
cout<<"Destination Spot out of range!"<<endl; piece->moveRight();
else if (boardArray[row][column+1]->getType() == temp)
cout<<"you hate your own team or something? you can't do that!"<<endl;
else
boardArray[row][column]->moveRight();
} }
} }
} }
*/
//FIX THIS
//take out the prints and turn changes from move
void Board::moveWOPrint(moves jugada)
{
//
}
/*
bool Board::isThisMovePossible(int r, int c, string moveType)
{
char pieceToMove = boardArray[r][c]->getType();
if (pieceToMove != turn) //trying to move invalid piece bool Board::isThisMovePossible(int r, int c, string moveType){
{ Piece* piece;
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"; cout << "Error in Board::isThisMovePossible: trying to move a piece outside your turn.\n";
return false; return false;
} }
int reflector = 1; else{
int reflector = 1;
if (pieceToMove == 'O')
{ if (piece->getType() == 'O')
reflector *= -1; reflector *= -1;
}
if (moveType == "FWD"){
else if (isPiece(r+reflector, c))
{
if (moveType == "FWD")
{
if (boardArray[r+reflector][c]->getType() == '_')
return true; return true;
else else
return false; return false;
} }
else if (moveType == "RIGHT") else if (moveType == "RIGHT"){
{ if (isPiece(r+reflector, c+1) && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 <= 7))
if (boardArray[r+reflector][c+1]->getType() != pieceToMove && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 <= 7) )
return true; return true;
else else
return false; return false;
} }
else if (moveType == "LEFT") else if (moveType == "LEFT"){
{ if (isPiece(r+reflector, c+1) && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 >= 0))
if (boardArray[r+reflector][c-1]->getType() != pieceToMove && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 >= 0) )
return true; return true;
else else
return false; return false;
@ -259,43 +237,62 @@ bool Board::isThisMovePossible(int r, int c, string moveType)
} }
} }
vector<moves> Board::viewPossibleMoves() vector<moves> Board::viewPossibleMoves(){
{ int r, c = -1;
vector<moves> output; vector<moves> output;
for (int r = 0; r < 8; ++r) if (turn == 'X'){
{ for (int i = 0; i < xpieces.size(); ++i){
for (int c = 0; c < 8; ++c) r = xpieces[i]->getX();
{ c = xpieces[i]->getY();
if (boardArray[r][c]->getType() == turn) if (isThisMovePossible(r, c, "FWD"))
{ {
if (isThisMovePossible(r,c,"FWD")) moves temp(8-r,intToCharColumn(c+1), "FWD");
{ output.push_back(temp);
moves temp(8-r,intToCharColumn(c+1),"FWD"); }
output.push_back(temp);
} if (isThisMovePossible(r,c,"LEFT"))
{
if (isThisMovePossible(r,c,"LEFT")) moves temp(8-r,intToCharColumn(c+1), "LEFT");
{ output.push_back(temp);
moves temp(8-r,intToCharColumn(c+1),"LEFT"); }
output.push_back(temp);
} if (isThisMovePossible(r,c,"RIGHT"))
{
if (isThisMovePossible(r,c,"RIGHT")) moves temp(8-r,intToCharColumn(c+1), "RIGHT");
{ output.push_back(temp);
moves temp(8-r,intToCharColumn(c+1),"RIGHT"); }
output.push_back(temp); }
} }
else {
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; return output;
} }
*/
string Board::myToUpper(string input) string Board::myToUpper(string input){
{
string output; string output;
for (int i = 0 ; i < input.size(); ++i) for (int i = 0 ; i < input.size(); ++i)
@ -314,8 +311,7 @@ string Board::myToUpper(string input)
} }
void Board::undo(Board& tablero) void Board::undo(Board& tablero){
{
vector<Board> record; vector<Board> record;
if (record.size() < 2) if (record.size() < 2)
@ -336,9 +332,8 @@ void Board::undo(Board& tablero)
record.pop_back(); record.pop_back();
} }
} }
/*
void Board::interpret(string input, Board& tablero) //determines what kind of command its input is void Board::interpret(string input, Board& tablero){
{
vector<Board> record; vector<Board> record;
input = myToUpper(input); input = myToUpper(input);
@ -360,9 +355,8 @@ void Board::interpret(string input, Board& tablero) //determines what kind of co
else tablero.move(input); else tablero.move(input);
} }
*/
void Board::snapshot(vector<Board>& inputVec, Board inputBoard) void Board::snapshot(vector<Board>& inputVec, Board inputBoard){
{
if (inputVec.size() == 10) if (inputVec.size() == 10)
{ {
inputVec.erase(inputVec.begin()); inputVec.erase(inputVec.begin());
@ -390,4 +384,9 @@ int Board::evaluate(char max, char min){
cout << "Unidentified max, must be either X or O.\n"; cout << "Unidentified max, must be either X or O.\n";
return 0; return 0;
} }
} }

13
Board.h
View file

@ -22,13 +22,14 @@ class Board {
//vector<vector<char>> boardArray; //vector<vector<char>> boardArray;
vector<Piece*> xpieces; vector<Piece*> xpieces;
vector<Piece*> opieces; vector<Piece*> opieces;
vector<Piece*> pieces;
//char boardArray [8][8]; //char boardArray [8][8];
char turn = 'O'; char turn = 'O';
public: public:
Board(); Board();
bool elemInXs(int r, int c); bool isPiece(int r, int c);
bool elemInOs(int r, int c); Piece* getPiece(int r, int c);
moves parse(string input); moves parse(string input);
char getTurn() { return turn; } char getTurn() { return turn; }
bool isGameOver(); bool isGameOver();
@ -36,11 +37,11 @@ public:
void displayBoard(); void displayBoard();
int charToIntColumn(char input); int charToIntColumn(char input);
char intToCharColumn(int input); char intToCharColumn(int input);
//void move(string inputMove); void move(string inputMove);
//void move(moves jugada); void move(moves jugada);
void moveWOPrint(moves jugada); void moveWOPrint(moves jugada);
//bool isThisMovePossible(int r, int c, string moveType); bool isThisMovePossible(int r, int c, string moveType);
//vector<moves> viewPossibleMoves(); vector<moves> viewPossibleMoves();
string myToUpper(string input); string myToUpper(string input);
void undo(Board& tablero); void undo(Board& tablero);
void interpret(string input, Board& tablero); void interpret(string input, Board& tablero);

View file

@ -48,46 +48,58 @@ int main(int argc, char *argv[])
//Client has successfully joined //Client has successfully joined
char buffer[256]; char buffer[256];
char info[256]; char info[256];
string final_board;
cout<<"WELCOME\n"; cout<<"WELCOME\n";
cout<<"1. Play against AI?\n"; cout<<"1. Play against AI?\n";
cout<<"2. Play against a human?\n"; cout<<"2. Play AI against an AI?\n";
cout<<"Enter choice: \n";
string choice;
cin >> choice;
string choice = "";
while(choice != "1")
{
cout<<"Enter choice: \n";
cin >> choice;
if(choice == "2")
cout << "AI vs. AI mode not added yet!\n";
}
//Check for a valid option //Check for a valid option
cout << "OK!\n" << endl; cout << "OK!\n" << endl;
//Writes off the choice to the server //Writes off the choice to the server
n = write(sockfd, choice.c_str(), choice.length()); //Sends an input to the server n = write(sockfd, choice.c_str(), choice.length()); //Sends an input to the server
cin.clear();
cin.ignore(10000,'\n');
while(true) { while(true) {
bzero(buffer,256); //resets the input stream bzero(buffer,256); //resets the input stream
n = read(sockfd,buffer,255); //Receives the board from server n = read(sockfd,buffer,255); //Receives the board from server
printf("%s\n",buffer);//Prints the received message printf("%s\n",buffer);//Prints the received message
final_board = buffer;
printf("Please enter a move: "); printf("Please enter a move: ");
bzero(buffer,256); //resets input stream bzero(buffer,256); //resets input stream
fgets(buffer,255,stdin); //Enter a move fgets(buffer,255,stdin); //Enter a move
n = write(sockfd,buffer,strlen(buffer)); //Sends an inputted move to the server n = write(sockfd,buffer,strlen(buffer)); //Sends an input move to the server
bzero(info,256); //resets input stream bzero(info,256); //resets input stream
n = read(sockfd,info,255); //Reads from server if move was valid n = read(sockfd,info,255); //Reads from server if move was valid
string ref = info; string ref = info;
if(ref == "Game_Over") { if(ref == "Game_Over") {
n = read(sockfd,info,255); //Reads from server if move was valid
cout << "GAME OVER!!!" << endl; cout << "GAME OVER!!!" << endl;
break; break;
} }
else else
continue; continue;
} }
cout << "\nGAME WINNING MOVE: \n";
//cout << final_board << endl;
//bzero(info,256); //resets input stream
n = read(sockfd,info,255); //Reads from server if move was valid
printf("%s\n",info);//Prints the received message
usleep(1);
close(sockfd); close(sockfd);
return 0; return 0;
} }

View file

@ -38,13 +38,11 @@ void Engine::startGame(){
cin>>move; cin>>move;
cout << "\n"; cout << "\n";
b->interpret(move, *b); b->interpret(move, *b);
b->changeTurns();
} }
b->changeTurns();
while(b->getTurn() == 'X' ) while(b->getTurn() == 'X' )
{ {
cout << "\n\n\n\nit gets here\n\n\n\n";
easyAI(); easyAI();
} }
@ -56,23 +54,22 @@ void Engine::startGame(){
void Engine::easyAI() void Engine::easyAI()
{ {
//1) see all possible movements
vector<moves> listOfMoves = b->viewPossibleMoves(); vector<moves> listOfMoves = b->viewPossibleMoves();
//obvious moves cout << listOfMoves[0].row << "\n";
if (false){ cout << listOfMoves[0].column << "\n";
b->changeTurns(); cout << listOfMoves[0].moveType << "\n\n";
} b->move(listOfMoves[0]);
b->changeTurns();
//random /*
else { srand(time(NULL));
srand(time(NULL)); int randomChoice = rand() % (listOfMoves.size()-1) - 0; // choose a move betwen listOfMoves[0] to last element
int randomChoice = rand() % (listOfMoves.size()-1) - 0; // choose a move betwen listOfMoves[0] to last element
//3) execute movement
//3) execute movement int temp = randomChoice;
int temp = randomChoice; b->move(listOfMoves[randomChoice]);
b->move(listOfMoves[randomChoice]); b->changeTurns();
} */
} }
void Engine::AI(){ void Engine::AI(){
@ -91,7 +88,7 @@ void Engine::AI(){
minMax(temp, listOfMoves[i]); minMax(temp, listOfMoves[i]);
}*/ }*/
b->moveWOPrint(minMax(temp, listOfMoves[0], 0)); //b->moveWOPrint(minMax(temp, listOfMoves[0], 0));
//verification of correct turn //verification of correct turn
if (b->getTurn() != 'O'){ if (b->getTurn() != 'O'){
@ -116,7 +113,7 @@ moves Engine::minMax(Board temp, moves m, int c){
else { else {
if(temp.isThisMovePossible(8 - m.row, temp.charToIntColumn(m.column), m.moveType)){ if(temp.isThisMovePossible(8 - m.row, temp.charToIntColumn(m.column), m.moveType)){
cout << "piece has been moved in minMax\n"; cout << "piece has been moved in minMax\n";
temp.moveWOPrint(m); //temp.moveWOPrint(m);
} }
cout << "c: " << c << "\n\n"; cout << "c: " << c << "\n\n";
cout << "current turn: " << temp.getTurn() << "\n"; cout << "current turn: " << temp.getTurn() << "\n";

View file

@ -19,7 +19,6 @@ public:
int getY(){ return y; } int getY(){ return y; }
void setY(int c){ y = c; } void setY(int c){ y = c; }
char getType(){ return type; } char getType(){ return type; }
void setType(char t){ type = t; }
void makeEmpty(){ type = '_'; } void makeEmpty(){ type = '_'; }
void isTaken(); void isTaken();
}; };

10
README.md Normal file
View file

@ -0,0 +1,10 @@
# Breakthrough
Reposity for the second CSCE 315 project
compile the server with the folling command:
g++ -std=c++11 -o server Board.cpp Server.cpp
run:
./server [port]
Client is still WIP.

View file

@ -85,6 +85,8 @@ int main(int argc, char *argv[])
char buffer[256]; char buffer[256];
char info[256]; char info[256];
int choice; int choice;
int move_counter = 0;
string final_move;
//Waiting for client to select game type //Waiting for client to select game type
n = read(newsockfd,info,255); n = read(newsockfd,info,255);
@ -100,14 +102,14 @@ int main(int argc, char *argv[])
while(b.getTurn() == 'O' ) while(b.getTurn() == 'O' )
{ {
b.displayBoard(); //Display the board on the server b.displayBoard();//Display the board on the server
string boardState = b.boardToString(); string boardState = b.boardToString();
write(newsockfd, boardState.c_str(), boardState.length()); //Display the board to the client (line by line) final_move = b.boardToString();
write(newsockfd, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
cout<<"\nWaiting for client: "; cout<<"\nWaiting for client: ";
n = read(newsockfd,buffer,255); n = read(newsockfd,buffer,255);
move = buffer; move = buffer;
b.interpret(move,b); b.interpret(move,b);
gameOver = b.isGameOver(); gameOver = b.isGameOver();
if(gameOver == true) { if(gameOver == true) {
string endGame = "Game_Over"; string endGame = "Game_Over";
@ -119,42 +121,20 @@ int main(int argc, char *argv[])
write(newsockfd, continueGame.c_str(), continueGame.length()); //Display the board to the client (line by line) write(newsockfd, continueGame.c_str(), continueGame.length()); //Display the board to the client (line by line)
} }
} }
//gameOver = b.isGameOver();
// if(gameOver == true) {
// string endGame = "Game_Over";
// write(newsockfd, endGame.c_str(), endGame.length()); //Display the board to the client (line by line)
// break;
// }
// else {
// string continueGame = "Continue_Game";
// write(newsockfd, continueGame.c_str(), continueGame.length()); //Display the board to the client (line by line)
// }
vector<moves> possibleMoves = b.viewPossibleMoves(); vector<moves> possibleMoves = b.viewPossibleMoves();
if(choice == 1) if(choice == 1)
b.easyAI(); b.easyAI();
} }
string final_move = b.boardToString();
write(newsockfd, final_move.c_str(), final_move.length());//Display the board to the client (line by line)
write(newsockfd, final_move.c_str(), final_move.length());
cout << "\n\nGAME OVER!!!";
usleep(1);
close(newsockfd); close(newsockfd);
close(sockfd); close(sockfd);
break; break;
/*
bzero(buffer,512);
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
n = write(newsockfd,"I got your message",18);
if (n < 0) error("ERROR writing to socket");
if(buffer == "y") {
close(newsockfd);
close(sockfd);
break;
}
*/
} }
return 0; return 0;
} }

View file

@ -7,9 +7,8 @@ int main()
{ {
//board testing //board testing
Board b; Board b;
b.displayBoard();
//engine testing //engine testing
//Engine e; Engine e;
//e.startGame(); e.startGame();
} }