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
.... 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.
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.
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.
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 :+)