Chess Game Jump Issue

So I have this beautiful chess game that i made, but the problem is the code that prevents the bishop from hopping other pieces. Down below is the current code. Can anyone help?

[code]
void bishop() //see the explanation of the piece functions, this just controls the bishop movement


{
void jump();
int tempx;
int tempy;
tempx = current_X;
tempy = current_Y;
if ((/*current_X - movement_X == 1 && current_Y - movement_Y == 1)||(current_X - movement_X == 2 && current_Y - movement_Y == 2)||(current_X - movement_X == 3 && current_Y - movement_Y == 3)||(current_X - movement_X == 4 && current_Y - movement_Y == 4)||(current_X - movement_X == 5 && current_Y - movement_Y == 5)||(current_X - movement_X == 6 && current_Y - movement_Y == 6)||(current_X - movement_X == 7 && current_Y - movement_Y == 7)||(current_X - movement_X == 8 && current_Y - movement_Y == 8)||(current_X - movement_X == -1 && current_Y - movement_Y == -1)||(current_X - movement_X == -2 && current_Y - movement_Y == -2)||(current_X - movement_X == -3 && current_Y - movement_Y == -3)||(current_X - movement_X == -4 && current_Y - movement_Y == -4)||(current_X - movement_X == -5 && current_Y - movement_Y == -5)||(current_X - movement_X == -6 && current_Y - movement_Y == -6)||(current_X - movement_X == -7 && current_Y - movement_Y == -7)||(current_X - movement_X == -8 && current_Y - movement_Y == -8) || (current_X - movement_X == -1 && current_Y - movement_Y == 1)||(current_X - movement_X == -2 && current_Y - movement_Y == 2)||(current_X - movement_X == -3 && current_Y - movement_Y == 3)||(current_X - movement_X == -4 && current_Y - movement_Y == 4)||(current_X - movement_X == -5 && current_Y - movement_Y == 5)||(current_X - movement_X == -6 && current_Y - movement_Y == 6)||(current_X - movement_X == -7 && current_Y - movement_Y == 7)||(current_X - movement_X == -8 && current_Y - movement_Y == 8)||*/(current_X - movement_X == 1 && current_Y - movement_Y == -1)||(current_X - movement_X == 2 && current_Y - movement_Y == -2)||(current_X - movement_X == 3 && current_Y - movement_Y == -3)||(current_X - movement_X == 4 && current_Y - movement_Y == -4)||(current_X - movement_X == 5 && current_Y - movement_Y == -5)||(current_X - movement_X == 6 && current_Y - movement_Y == -6)||(current_X - movement_X == 7 && current_Y - movement_Y == -7)||(current_X - movement_X == 8 && current_Y - movement_Y == -8))/*&&((current_X - movement_X == 1 && current_Y - movement_Y == 1)||(current_X - movement_X == 2 && current_Y - movement_Y == 2)||(current_X - movement_X == 3 && current_Y - movement_Y == 3)||(current_X - movement_X == 4 && current_Y - movement_Y == 4)||(current_X - movement_X == 5 && current_Y - movement_Y == 5)||(current_X - movement_X == 6 && current_Y - movement_Y == 6)||(current_X - movement_X == 7 && current_Y - movement_Y == 7)||(current_X - movement_X == 8 && current_Y - movement_Y == 8)||(current_X - movement_X == -1 && current_Y - movement_Y == -1)||(current_X - movement_X == -2 && current_Y - movement_Y == -2)||(current_X - movement_X == -3 && current_Y - movement_Y == -3)||(current_X - movement_X == -4 && current_Y - movement_Y == -4)||(current_X - movement_X == -5 && current_Y - movement_Y == -5)||(current_X - movement_X == -6 && current_Y - movement_Y == -6)||(current_X - movement_X == -7 && current_Y - movement_Y == -7)||(current_X - movement_X == -8 && current_Y - movement_Y == -8) || (current_X - movement_X == -1 && current_Y - movement_Y == 1)||(current_X - movement_X == -2 && current_Y - movement_Y == 2)||(current_X - movement_X == -3 && current_Y - movement_Y == 3)||(current_X - movement_X == -4 && current_Y - movement_Y == 4)||(current_X - movement_X == -5 && current_Y - movement_Y == 5)||(current_X - movement_X == -6 && current_Y - movement_Y == 6)||(current_X - movement_X == -7 && current_Y - movement_Y == 7)||(current_X - movement_X == -8 && current_Y - movement_Y == 8)||(current_X - movement_X == 1 && current_Y - movement_Y == -1)||(current_X - movement_X == 2 && current_Y - movement_Y == -2)||(current_X - movement_X == 3 && current_Y - movement_Y == -3)||(current_X - movement_X == 4 && current_Y - movement_Y == -4)||(current_X - movement_X == 5 && current_Y - movement_Y == -5)||(current_X - movement_X == 6 && current_Y - movement_Y == -6)||(current_X - movement_X == 7 && current_Y - movement_Y == -7)||(current_X - movement_X == 8 && current_Y - movement_Y == -8)=='p'||'r'||'n'||'b'||'q'||'k')*/)
{
if ((current_X - movement_X == 1 && current_Y - movement_Y == -1)||(current_X - movement_X == 2 && current_Y - movement_Y == -2)||(current_X - movement_X == 3 && current_Y - movement_Y == -3)||(current_X - movement_X == 4 && current_Y - movement_Y == -4)||(current_X - movement_X == 5 && current_Y - movement_Y == -5)||(current_X - movement_X == 6 && current_Y - movement_Y == -6)||(current_X - movement_X == 7 && current_Y - movement_Y == -7)||(current_X - movement_X == 8 && current_Y - movement_Y == -8)=='p'||'r'||'n'||'b'||'q'||'k')
//(tempx - movement_X <=tempx||tempy - movement_Y <=tempy)

{
cout<< "\nInvalid move. You would be jumping your own piece. Enter a number to continue\n";
tempx = '-';
tempy = '-';
cin >> contcn;
system("CLS");
//break;
}
if (board [movement_Y] [movement_X] == 'p' || board [movement_Y] [movement_X] == 'q' || board [movement_Y] [movement_X] == 'k' || board [movement_Y] [movement_X] == 'b' || board [movement_Y] [movement_X] == 'r' || board [movement_Y] [movement_X] == 'n')

{

cout << "\nInvalid move. You would be eliminating your own piece. Enter a number to continue\n";
cin >> contcn;
system("CLS");


}


//use jump function
//have jump function set a varriable as true/false
//if variable = true, invalid move, dont do anything in here. similar to the usual invalid move thijng
else if ((current_X - movement_X == 1 && current_Y - movement_Y == 1)||(current_X - movement_X == 2 && current_Y - movement_Y == 2)||(current_X - movement_X == 3 && current_Y - movement_Y == 3)||(current_X - movement_X == 4 && current_Y - movement_Y == 4)||(current_X - movement_X == 5 && current_Y - movement_Y == 5)||(current_X - movement_X == 6 && current_Y - movement_Y == 6)||(current_X - movement_X == 7 && current_Y - movement_Y == 7)||(current_X - movement_X == 8 && current_Y - movement_Y == 8)||(current_X - movement_X == -1 && current_Y - movement_Y == -1)||(current_X - movement_X == -2 && current_Y - movement_Y == -2)||(current_X - movement_X == -3 && current_Y - movement_Y == -3)||(current_X - movement_X == -4 && current_Y - movement_Y == -4)||(current_X - movement_X == -5 && current_Y - movement_Y == -5)||(current_X - movement_X == -6 && current_Y - movement_Y == -6)||(current_X - movement_X == -7 && current_Y - movement_Y == -7)||(current_X - movement_X == -8 && current_Y - movement_Y == -8) || (current_X - movement_X == -1 && current_Y - movement_Y == 1)||(current_X - movement_X == -2 && current_Y - movement_Y == 2)||(current_X - movement_X == -3 && current_Y - movement_Y == 3)||(current_X - movement_X == -4 && current_Y - movement_Y == -4)||(current_X - movement_X == -5 && current_Y - movement_Y == 5)||(current_X - movement_X == -6 && current_Y - movement_Y == 6)||(current_X - movement_X == -7 && current_Y - movement_Y == 7)||(current_X - movement_X == -8 && current_Y - movement_Y == 8)||(current_X - movement_X == 1 && current_Y - movement_Y == -1)||(current_X - movement_X == 2 && current_Y - movement_Y == -2)||(current_X - movement_X == 3 && current_Y - movement_Y == -3)||(current_X - movement_X == 4 && current_Y - movement_Y == -4)||(current_X - movement_X == 5 && current_Y - movement_Y == -5)||(current_X - movement_X == 6 && current_Y - movement_Y == -6)||(current_X - movement_X == 7 && current_Y - movement_Y == -7)||(current_X - movement_X == 8 && current_Y - movement_Y == -8)) //God help us

{

if (board [movement_Y] [movement_X] == 'P' || board [movement_Y] [movement_X] == 'B' || board [movement_Y] [movement_X] == 'R' || board [movement_Y] [movement_X] == 'Q' || board [movement_Y] [movement_Y] == 'K')

{

cout << "\nYou have eliminated the opponent's piece\n";
board [current_Y] [current_X] = '-';
board [movement_Y] [movement_X] = 'b';
system("CLS");


}

else

{

cout << "\nYou have not eliminated any pieces.\n";
board [current_Y] [current_X] = '-';
board [movement_Y] [movement_X] = 'b';
system("CLS");

}



}




}
.... but the problem is the code that prevents the bishop from hopping other pieces.


I don't understand, the bishop moves on diagonals, and has to have a clear run: it can't "hop" other pieces?

The conditions you have are a nightmare, there has to be a better way. Doesn't any given piece have a short list of valid positions it can move to? I imagine that wouldn't be too hard to implement, given the current position of the piece. For example, with the bishop, see what valid diagonal positions which radiate from it's current position.

The list of valid moves for a piece could be encapsulated in that piece's class.

Please always use code tags: http://www.cplusplus.com/articles/z13hAqkS/

Good Luck !!
Code decomposition may help you to visualize these things and save you a lot of writing. (Break those long if-conditions into smaller boolean functions that can be reused later)

Your queen will have to move diagonally, straight, back and forewards, so break those into booleans now before you have to work on her movement if statement.

This will also allow you to use a for/while loop to save even more time.


Edit:
Looks like some of your if conditions are trying to compare your long list of comparisons with chars at the end.

A comparison can only return true or false (1 or 0), so comparing a comparison with a char will always return false.

However, you do move on later to correctly compare your board[x][y] array with chars, which is what you want to do.


Edit 2:
I'd suggest taking a break for now. Check out bucky's c++ tutorials on youtube, then come back to this after you've gotten through that series, there are a lot of programming tools that will save you many headaches.
Last edited on
So I have this beautiful chess game

Frankly that code is unreadable. As TheIdeasMan said, "The conditions you have are a nightmare".

As others have said, bishops don't hop over other pieces, unless you're writing a chess game with your own rules, which you haven't made clear.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.


Please delete your other post in the beginners section. Duplicate posts are ultimately a time waster for those who reply - there is potential for the same things being said in both.

Another thing:

if ( (condition) =='p'||'r'||'n'||'b'||'q'||'k')

doesn't do what you think it does.

You could write these functions:

1
2
3
4
 
void CheckFwdLeftDiag(std::vector<Position>& ValidPositions, 
                    const Position& CurrentPosition, 
                    short int limit);


Similar for the other diagonals, and for Forward, Back, Left and Right. Edit: Or just write two functions, one for diagonals, and the other for Forward, Back, Left, Right. Then a particular piece could call which ever functions it needs. So this is how the functions are reused. The idea is that the functions would push_back Positions into the ValidPositions argument. The limit argument is so the function could also be used by the King and Pawns.

Hopefully this will give you some good ideas to move forward :+)
Last edited on
Topic archived. No new replies allowed.