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 7614911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:31:22+00:00 2026-05-31T02:31:22+00:00

I’m making a chess program and currently have the movement done for the pawn

  • 0

I’m making a chess program and currently have the movement done for the pawn and knight, however I’m having a little trouble with the bishop. I’m creating my chess game with the MVC approach and have it creating a 2D array of BoardSquares which store information such as location, a piece (if there is one), and if it’s empty. For the moment I have the board take in input from a text file line-by-line. The current line I’m working on for the bishop movement is from c8 to a6, so moving Down and to the Left.

My movement is simple, where it takes the input from the file, translates it to 2D array coordinates and passes it into the model’s move method (along with the model) which calls a piece’s move method.

// In the File I/O Class
public void passinMove(BoardModel bm) {
    generateOriginRC(input, START_LOC1, END_LOC1);
    generateDestRC(input, START_LOC2, END_LOC2);
    System.out.println("Origin C/R: "+oCol+"/"+oRow);
    System.out.println("Dest C/R: "+dCol+"/"+dRow);
    bm.movePiece(bm.getBoard(), oRow, oCol, dRow, dCol);
}

// In the BoardModel Class
public void movePiece(BoardSquare[][] board, int oRow, int oCol, int dRow, int dCol){

    board[oRow][oCol].getPiece().move(board, board[dRow][dCol]); 

As of now to move I have my pieces each generate their own array of valid moves; which is a list of BoardSquares by moving it VIA brute force one square at a time until it hits a piece or the end of the board. Then I have it compare it’s desired destination square to that list.

@Override
void move(BoardSquare[][] board, BoardSquare target) {
    if (!getPossibleMove(board).contains(target)) {
        System.out
                .println("Sorry. Not a valid move, please enter a new move");

    } else {
        target.setSquare(board[col][row]);
    }
}

For the bishop I have it grab it’s position and manipulate it accordingly to move it. (To move up I subtract to go down the array, etc.).

However it seems my logic is flawed somewhere; since when it starts checking down and to the right it moves from 0/2 to 1/2. Then continues until it hits the right side wall and goes straight down into the corner.

@Override
Collection<BoardSquare> getPossibleMove(BoardSquare[][] board) {
    BoardSquare[][] copy = board;
    ArrayList<BoardSquare> validMoves = new ArrayList<BoardSquare>();

    // Checks spaces to the Up-Right of the Bishop by moving the Bishop
    // until it hits the end of the board or a piece
    for (int goUp = getCol(); goUp > -1; goUp--) {
        for (int goRight = getRow(); goRight < 8; goRight++) {
            System.out.println("Doing Up-Right C/R: "+getCol()+" / "+getRow());
            System.out.println("Moving to C/R: "+goUp+" / "+goRight);
            tempCol = getCol();
            tempRow = getRow();
            if (moveValidator(copy[goUp][goRight])) {
                validMoves.add(copy[goUp][goRight]);
                copy[goUp][goRight].setSquare(copy[tempCol][tempRow]);
            } else {
                break;
            }
        }
    }

    // Checks spaces to the Up-Left of the Bishop by moving the Bishop
    // until it hits the end of the board or a piece
    for (int goUp = getCol(); goUp > -1; goUp--) {
        for (int goLeft = getRow(); goLeft > -1; goLeft--) {
            System.out.println("Doing Up-Left C/R: "+getCol()+" / "+getRow());
            System.out.println("Moving to C/R: "+goUp+" / "+goLeft);
            tempCol = getCol();
            tempRow = getRow();
            if (moveValidator(copy[goUp][goLeft])) {
                validMoves.add(copy[goUp][goLeft]);
                copy[goUp][goLeft].setSquare(copy[tempCol][tempRow]);
            } else {
                break;
            }
        }
    }

    // Checks spaces to the Down-Right of the Bishop by moving the Bishop
    // until it hits the end of the board or a piece
    for (int goDown = getCol(); goDown < 8; goDown++) {
        for (int goRight = getRow(); goRight < 8; goRight++) {
            System.out.println("Doing Down-Right C/R: "+getCol()+" / "+getRow());
            System.out.println("Moving to C/R: "+goDown+" / "+goRight);
            tempCol = getCol();
            tempRow = getRow();
            if (moveValidator(copy[goDown][goRight])) {
                validMoves.add(copy[goDown][goRight]);
                copy[goDown][goRight].setSquare(copy[tempCol][tempRow]);
            } else {
                break;
            }
        }
    }

    // Checks spaces to the Down-Left of the Bishop by moving the Bishop
    // until it hits the end of the board or a piece
    for (int goDown = getCol(); goDown < 8; goDown++) {
        for (int goLeft = getRow(); goLeft > -1; goLeft--) {
            System.out.println("Doing Down-Left C/R: "+getCol()+" / "+getRow());
            System.out.println("Moving to C/R: "+goDown+" / "+goLeft);
            tempCol = getCol();
            tempRow = getRow();
            if (moveValidator(copy[goDown][goLeft])) {
                validMoves.add(copy[goDown][goLeft]);
                copy[goDown][goLeft].setSquare(copy[tempCol][tempRow]);
            } else {
                break;
            }
        }
    }
    return validMoves;
}

Some notes:
The tempCol and tempRow variable are just used to acquire the location the piece is on to move it to the “new” destination.

I’m still trying to figure it out, however I would like someone to review my code incase there’s something I’m not noticing or forgetting. Any tips or suggestions are much appreciated. Thanks.

  • 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-31T02:31:23+00:00Added an answer on May 31, 2026 at 2:31 am

    I think the problem you describe would happen if the moveValidator function returns always false until your piece reach the wall.

    You'll have
    For1 --> goDown=0
    For2 --> goRight=2 --> moveValidator is false so break;
    For1 --> goDown=1
    For2 --> goRight=2 --> moveValidator is false so break;
    ...
    And when goDown = 8
    For2 --> goRight=2 --> moveValidator is true
    For2 --> goRight=3 --> moveValidator is true
    ...
    

    So you may want to validate my guess debugging and trying to verify the moveValidator function (you didn’t give it in your code)

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
We're building an app, our first using Rails 3, and we're having to build

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.