Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7584155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:51:09+00:00 2026-05-30T18:51:09+00:00

The following method is used to determine whether or not a chess piece is

  • 0

The following method is used to determine whether or not a chess piece is blocked from making a certain move. At the point that this method is called, the motion itself (i.e. a Bishop’s ability to move diagonally) has already been validated — this method will then look at the “path” that the piece must take.

As is painfully clear, this method is full of redundancy. In fact, there are 6 nearly-identical for-loops, the differences being 1) which variables are controlling the iteration, 2) whether the variable is incrementing or decrementing, and 3), in the case of the diagonal motion, the inclusion of a statement to increment/decrement both the x and y variables simultaneously.

I have made numerous attempts to abstract these statements into a separate method. Unfortunately, the limiting factor has been need to access the board[y][x] — When I’ve tried to abstract the logic, I lose sight of which variable represents the y and which the x.

So, my question is this: what tools can Java provide me to abstract this logic and reduce or eliminate redundancy in this method? I will point out that I am quite new to the language, so please don’t take my disregard for common idioms as intentional or simply obtuse; I’m learning!

Thanks.

private static boolean notBlocked(Piece[][] board, int xfrom, int yfrom, int xto, int yto) {

    int x = xfrom;
    int xstop = xto;
    int y = yfrom;
    int ystop = yto;

    int xinc = (x < xstop) ? 1 : -1;
    int yinc = (y < ystop) ? 1 : -1;

    Piece to = board[yto][xto];
    Piece from = board[yfrom][xfrom];

    if (xfrom == xto) {
        // x is constant, check in y direction
        if (y <= ystop) {
            for (; y <= ystop; y += yinc) {
                if (board[y][x] != null && board[y][x] != to && board[y][x] != from) {
                    return false;
                }
            }
        } else {
            for (; y >= ystop; y += yinc) {
                if (board[y][x] != null && board[y][x] != to && board[y][x] != from) {
                    return false;
                }
            }
        }
    } else if (yfrom == yto) {
        // y is constant, check in x direction
        if (x <= xstop) {
            for (; x <= xstop; x += xinc) {
                if (board[y][x] != null && board[y][x] != to && board[y][x] != from) {
                    return false;
                }
            }
        } else {
            for (; x >= xstop; x += xinc) {
                if (board[y][x] != null && board[y][x] != to && board[y][x] != from) {
                    return false;
                }
            }
        }
    } else if (Math.abs(xfrom - xto) == Math.abs(yfrom - yto)){
        // the move is diagonal
        if (y <= ystop) {
            for (; y <= ystop; y += yinc) {
                if (board[y][x] != null && board[y][x] != to && board[y][x] != from) {
                    return false;
                }
                x += xinc;
            }
        } else {
            for (; y >= ystop; y += yinc) {
                if (board[y][x] != null && board[y][x] != to && board[y][x] != from) {
                    return false;
                }
                x += xinc;
            }
        }
    }
    return true;
}

EDIT:

Wow… much better now!

private static boolean notBlocked(Piece[][] board, int xfrom, int yfrom, int xto, int yto) {

    Piece from = board[yfrom][xfrom];
    Piece to = board[yto][xto];

    // Determine the direction (if any) of x and y movement
    int dx = (xfrom < xto) ? 1 : ((xfrom == xto) ? 0 : -1);
    int dy = (yfrom < yto) ? 1 : ((yfrom == yto) ? 0 : -1);

    // Determine the number of times we must iterate
    int steps = Math.max(Math.abs(xfrom - xto), Math.abs(yfrom - yto));

    if (xfrom == xto || yfrom == yto || Math.abs(xfrom - xto) == Math.abs(yfrom - yto)) {
        for (int i = 1; i < steps; i++) {
            int x = xfrom + i * dx;
            int y = yfrom + i * dy;
            if (isBlocked(board, from, to, x, y)) {
                return false;
            }
        }
    }
    return true;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:51:10+00:00Added an answer on May 30, 2026 at 6:51 pm

    Think about writing this function in terms of one step in the right direction. Suppose that you can fill in two variables dx and dy that represent how much you move in the x and y directions on each step. You can compute this by looking at the difference between the start and end x and y locations. Once you have that, you can then write a single for loop that tries moving along that direction, checking each step. For example:

    for (int i = 1; i < numStepsRequired; i++) {
        int currX = x + i * dx;
        int currY = y + i * dy;
        if (board[currY][currX] != null) {
            return false;
        }
    }
    

    You would also need to compute how many steps are required, which would also be straightforward as long as you’re computing dx and dy. I’ll leave this as an exercise, since it’s good programming practice.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method (used to generate friendly error messages in unit tests):
How come when I use the following method, to be used to convert all
Can I use a native method which returns a pointer? I used the following
The following method, when called with something like String val = getCell(SELECT col FROM
The following method is launched from the constructor of UserControl. A cross thread exception
The following method does not apply the wpf changes (background = red) until the
The following method does not access any shared variable. It is still not thread
I came across some Java code that has a method containing the following: static
Of the following two options for method parameter names that have a unit as
I'm finding it difficult to determine how to extract the following information from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.