diff --git a/Server/.vscode/settings.json b/Server/.vscode/settings.json new file mode 100644 index 0000000..fde90c0 --- /dev/null +++ b/Server/.vscode/settings.json @@ -0,0 +1,47 @@ +{ + "files.associations": { + "atomic": "cpp", + "bit": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "exception": "cpp", + "initializer_list": "cpp", + "ios": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "iterator": "cpp", + "limits": "cpp", + "memory": "cpp", + "new": "cpp", + "ostream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "string": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "typeinfo": "cpp", + "utility": "cpp", + "xfacet": "cpp", + "xiosbase": "cpp", + "xlocale": "cpp", + "xlocinfo": "cpp", + "xlocnum": "cpp", + "xmemory": "cpp", + "xstddef": "cpp", + "xstring": "cpp", + "xtr1common": "cpp", + "xutility": "cpp" + } +} \ No newline at end of file diff --git a/Server/board.cc b/Server/board.cc index 4574d8a..b43cfef 100644 --- a/Server/board.cc +++ b/Server/board.cc @@ -90,6 +90,9 @@ board::board(piece* red, piece* black, piece* kingBlack, piece* kingRed){ // thi // This will move a piece on the board by going through a series of checks to make sure it is possible bool board::movePiece(int y1, int x1, int y2, int x2){ + // A variable to see if a piece has jumped another piece in this turn + bool hasJumped = false; + // Check to make sure there is a piece where the target is if(mainBoard[y1][x1].getIsEmpty() == true){ cout << "Error: there is no piece to move" << endl; @@ -134,6 +137,23 @@ bool board::movePiece(int y1, int x1, int y2, int x2){ mainBoard[y2][x2].makeKing(redKing); } } + + // At this point the move has been made and we can check to see if the + // original move jumped any pieces + if(abs(y1-y2) == 2 && abs(x1-x2) == 2){ + hasJumped = true; + } + + if(canDoubleJump(y2,x2) && hasJumped){ + string destination; + output(cout); + cout << "A double jump is possible!" << endl; + cout << "Where would you like the piece on " << y2 << x2 << " to jump?: "; + cin >> destination; + int y3 = destination[0] - '0'; + int x3 = destination[1] - '0'; + movePiece(y2, x2, y3, x3); + } return true; } @@ -258,11 +278,105 @@ bool board::isKingMe(int y2, std::string color) { } } +bool board::canDoubleJump(int y, int x) { + string color = mainBoard[y][x].getCurrentPiece()->getColor(); + + if(color == "red") { + return canDoubleJumpRed(y,x); + } + else if(color == "black"){ + return canDoubleJumpBlack(y,x); + } + return false; +} + +bool board::canDoubleJumpBlack(int y, int x){ + piece currentPiece = *mainBoard[y][x].getCurrentPiece(); + + if(y+2 < 8 && x+2 < 8 && currentPiece.getIsKing()){ // ++ + if(mainBoard[y+2][x+2].getIsEmpty()){ + if(!mainBoard[y+1][x+1].getIsEmpty()){ + if(mainBoard[y+1][x+1].getCurrentPiece()->getColor() != currentPiece.getColor()){ + return true; // All checks passed + } + } + } + } + if(y-2 >= 0 && x-2 >= 0){ // -- + if(mainBoard[y-2][x-2].getIsEmpty()){ + if(!mainBoard[y-1][x-1].getIsEmpty()){ + if(mainBoard[y-1][x-1].getCurrentPiece()->getColor() != currentPiece.getColor()){ + return true; // All checks passed + } + } + } + } + if(y-2 >= 0 && x+2 < 8){ // -+ + if(mainBoard[y-2][x+2].getIsEmpty()){ + if(!mainBoard[y-1][x+1].getIsEmpty()){ + if(mainBoard[y-1][x+1].getCurrentPiece()->getColor() != currentPiece.getColor()){ + return true; // All checks passed + } + } + } + } + if(y+2 < 8 && x-2 >= 0 && currentPiece.getIsKing()){ // +- + if(mainBoard[y+2][x-2].getIsEmpty()){ + if(!mainBoard[y+1][x-1].getIsEmpty()){ + if(mainBoard[y+1][x-1].getCurrentPiece()->getColor() != currentPiece.getColor()){ + return true; // All checks passed + } + } + } + } + return false; +} +bool board::canDoubleJumpRed(int y, int x){ + piece currentPiece = *mainBoard[y][x].getCurrentPiece(); + + if(y+2 < 8 && x+2 < 8){ // ++ + if(mainBoard[y+2][x+2].getIsEmpty()){ + if(!mainBoard[y+1][x+1].getIsEmpty()){ + if(mainBoard[y+1][x+1].getCurrentPiece()->getColor() != currentPiece.getColor()){ + return true; // All checks passed + } + } + } + } + if(y-2 >= 0 && x-2 >= 0 && currentPiece.getIsKing()){ // -- + if(mainBoard[y-2][x-2].getIsEmpty()){ + if(!mainBoard[y-1][x-1].getIsEmpty()){ + if(mainBoard[y-1][x-1].getCurrentPiece()->getColor() != currentPiece.getColor()){ + return true; // All checks passed + } + } + } + } + if(y-2 >= 0 && x+2 < 8 && currentPiece.getIsKing()){ // -+ Ensures that if it's jumping backwards that the piece is also a king + if(mainBoard[y-2][x+2].getIsEmpty()){ + if(!mainBoard[y-1][x+1].getIsEmpty()){ + if(mainBoard[y-1][x+1].getCurrentPiece()->getColor() != currentPiece.getColor()){ + return true; // All checks passed + } + } + } + } + if(y+2 < 8 && x-2 >= 0){ // +- + if(mainBoard[y+2][x-2].getIsEmpty()){ + if(!mainBoard[y+1][x-1].getIsEmpty()){ + if(mainBoard[y+1][x-1].getCurrentPiece()->getColor() != currentPiece.getColor()){ + return true; // All checks passed + } + } + } + } + return false; +} // a function that will print out the current state of the board void board::output(std::ostream& outs)const{ - outs << " 0 1 2 3 4 5 6 7\n"; + outs << "\n 0 1 2 3 4 5 6 7\n"; outs << " _________________\n"; for(int i = 7; i >= 0; i--){ outs << i << " "; diff --git a/Server/board.h b/Server/board.h index 42d6fad..77468e5 100644 --- a/Server/board.h +++ b/Server/board.h @@ -30,6 +30,9 @@ class board{ bool isRedLegalMove(int y1, int x1, int y2, int x2); bool isLegalJump(int y1, int x1, int y2, int x2, std::string color); bool isKingMe(int y2, std::string color); + bool canDoubleJump(int y, int x); // A function that will test if a double jump is available + bool canDoubleJumpBlack(int y, int x); + bool canDoubleJumpRed(int y, int x); }; diff --git a/Server/game.cc b/Server/game.cc index 8ad03f2..cdf1931 100644 --- a/Server/game.cc +++ b/Server/game.cc @@ -30,9 +30,7 @@ bool game::makeMove(std::string target, std::string destination){ return false; } -void game::startGame(){ - string target; - string destination; +void game::playGame(std::string target, std::string destination){ while(!checkWin()){ // Program will be in this loop until there is a winner cout << gameBoard; cout << "\nIt is " << getTurn() << " turn" << endl; @@ -124,12 +122,6 @@ std::string game::getTurn(){ // Output functions void game::output(std::ostream& outs)const{ - if(turnNum%2 == 0){ - outs << "Blacks turn!\n"; - } - else{ - outs << "Reds turn!\n"; - } outs << gameBoard; } diff --git a/Server/game.h b/Server/game.h index 05947b4..ae1f508 100644 --- a/Server/game.h +++ b/Server/game.h @@ -18,11 +18,12 @@ class game{ void setTurnNum(int num) {turnNum = num;} void setBlackPieces(int num) {blackPieces = num;} void setRedPieces(int num) {redPieces = num;} + void incrementTurnNum() {turnNum++;} // Helper functions bool legalMove(); bool makeMove(std::string target, std::string destination); - void startGame(); + void playGame(std::string target, std::string destination); // No longer in use bool checkWin(); // done bool checkRedWin(); // done bool checkBlackWin(); // done diff --git a/Server/test.cc b/Server/test.cc index d3eb299..b19a235 100644 --- a/Server/test.cc +++ b/Server/test.cc @@ -17,7 +17,21 @@ int main(){ redKing.makeKing(); game newGame(&red, &black, &blackKing, &redKing); - newGame.startGame(); + string target = ""; + string destination = ""; + + while(!newGame.checkWin()){ + cout << newGame; + cout << "It is " << newGame.getTurn() << " turn" << endl; + do { + cout << "What piece would you like to move: "; + cin >> target; + cout << "Where would you like to move " << target << ": "; + cin >> destination; + if(target == "end" || destination == "end") {return 0;} // This is strictly for testing + } while(!newGame.makeMove(target, destination)); + newGame.incrementTurnNum(); + } return 0; } \ No newline at end of file diff --git a/Server/tests/backwardsdoublejump.txt b/Server/tests/backwardsdoublejump.txt new file mode 100644 index 0000000..b0b5e23 --- /dev/null +++ b/Server/tests/backwardsdoublejump.txt @@ -0,0 +1,10 @@ +51 +42 +20 +31 +53 +44 +31 +53 +35 +end \ No newline at end of file diff --git a/Server/tests/doublejump.txt b/Server/tests/doublejump.txt new file mode 100644 index 0000000..d2cc0d0 --- /dev/null +++ b/Server/tests/doublejump.txt @@ -0,0 +1,18 @@ +51 +42 +22 +33 +55 +44 +26 +37 +64 +55 +20 +31 +73 +64 +33 +51 +73 +end \ No newline at end of file diff --git a/Server/tests/multijump.txt b/Server/tests/multijump.txt new file mode 100644 index 0000000..6c90b2f --- /dev/null +++ b/Server/tests/multijump.txt @@ -0,0 +1,24 @@ +51 +42 +22 +33 +55 +44 +26 +37 +64 +55 +20 +31 +73 +64 +17 +26 +55 +46 +33 +51 +73 +55 +33 +end \ No newline at end of file