Skip to content

Commit

Permalink
Merge pull request #3 from kylewillis21/testing
Browse files Browse the repository at this point in the history
Added the ability for pieces to double jump
  • Loading branch information
kylewillis21 authored Apr 27, 2024
2 parents 31861e4 + 03544e7 commit fc307f7
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 12 deletions.
47 changes: 47 additions & 0 deletions Server/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
116 changes: 115 additions & 1 deletion Server/board.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 << " ";
Expand Down
3 changes: 3 additions & 0 deletions Server/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

};

Expand Down
10 changes: 1 addition & 9 deletions Server/game.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion Server/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion Server/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
10 changes: 10 additions & 0 deletions Server/tests/backwardsdoublejump.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
51
42
20
31
53
44
31
53
35
end
18 changes: 18 additions & 0 deletions Server/tests/doublejump.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
51
42
22
33
55
44
26
37
64
55
20
31
73
64
33
51
73
end
24 changes: 24 additions & 0 deletions Server/tests/multijump.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fc307f7

Please sign in to comment.