Chess Validation Help

Dec 18, 2016 at 7:07pm
So, I have gotten quite far in my mission to finish a chess game in c++. However, I have hit a bit of a small issue I would like to get some input.
SITUATION:
My PAWN, KING, KNIGHT move validations work perfect. But;
When moving a piece(such as a white ROOK) it follows most of the rules. For example, it will only move vertical or horizontal, it will not pass another white piece, it will not replace a white piece, and lastly it WILL replace a black (opposing) piece. The problem is when moving it past a another black piece, it allows passing in order to replace a piece thats past it. So lets say we have a white piece at x=2,y=6 and black piece at x=2,y=4, and another black piece at x=2,y=3. The White piece will be allowed to move to move to x=2,y=3, which should not be allowed. Would love to get some input on how to fix this. Current code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
bool Rook:: canMove(int startx, int starty, int endx, int endy)
{
    int i;
    if(board[endx][endy] !=NULL && board[endx][endy]->color==color)
        return false;
   // Collision detection
    if (startx == endx)
    {
        // Horizontal move
        if (starty < endy)
        {
            // Move down
            for (i = starty + 1; i <= endy; ++i)
                if (board[startx][i] != NULL && board[startx][i]->color==color)
                    return false;

        }
        else
        {
            // Move up
            for (i = starty - 1; i >= endy; --i)
                if (board[startx][i] != NULL && board[startx][i]->color==color) //cant allow passing of non color piece
                    return false;
        }
    }
    else if (starty == endy)
    {
        // Vertical move
        if (startx < endx)
        {
            // Move right
            for (i = startx + 1; i <= endx; ++i)
                if (board[i][starty] != NULL && board[i][starty]->color==color)
                    return false;
        }
        else
        {
            // Move left
            for (i = startx - 1; i >= endx; --i)
                if (board[i][starty] != NULL && board[i][starty]->color==color)
                    return false;
        }
    }
    else
    {
        // Not a valid rook move (neither horizontal nor vertical)
        return false;
    }

    return true;
}
Last edited on Dec 18, 2016 at 7:11pm
Dec 18, 2016 at 8:11pm
bump
Dec 18, 2016 at 9:25pm
it will not pass another white piece


Change the logic so that it will not pass a piece of any color.

[edit: Also don't bump your posts without providing more information and waiting for only an hour -- nobody is paid to read and respond to these posts. Have a little patience.]
Last edited on Dec 18, 2016 at 9:28pm
Topic archived. No new replies allowed.