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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:24:28+00:00 2026-05-19T00:24:28+00:00

I need help in designing a Chess game. I’ve already started but haven’t got

  • 0

I need help in designing a Chess game. I’ve already started but haven’t got far as I’m pretty new to Java, new to programming at all actually.

Anyway, I have my abstract class Piece and the various pieces as subclasses.
I have a method movePiece, in my abstract class, which I want to define for all subclasses.

All it currently does is move the piece from one square to another. I have a Square class which can hold a Piece object, the board consists of a 64×1 Square array.

I know how pieces move, but how do I actually do the programming?
I want to try to apply the MVC pattern but this is really the first time I will be using patterns.

Basically I was thinking on using Graphics2D to create a box for each Square. Then when a player clicks a piece, the squares that are available as destination after the move will be outlined in some colour. After the player clicks one of these squares, the code that I already have in my movePiece method will run.

What I want to do is override my movePiece method in each subclass of Piece. The question is, how could the code look in one of these methods? take the Pawn subclass for example.

I’m not asking for code to copy/paste, just some pointers on how to do this, eventually some sample code.

Thanks!

public class Game {


@SuppressWarnings("unused")
public static void main(String[] args){
    Board board = new Board();
} }

public class Board {

Square[] grid;

public Board(){
    grid = new Square[64];
}   
public Square getSquare(int i){
    return grid[i];
}   
public void setDefault(){

}   
public Boolean isMoveValid(){
    return null;    
} }

public class Square {

private Piece piece;

public void addPiece(Piece pieceType, String pieceColour, String pieceOwner) 
        throws ClassNotFoundException, InstantiationException, IllegalAccessException{

    PieceFactory factory = new PieceFactory();
    Piece piece = factory.createPiece(pieceType);

    piece.setColour(pieceColour);
    piece.setOwner(pieceOwner);

    this.piece = piece; 
}
public void addPiece(Piece pieceType){ 
    this.piece = pieceType; 
}
public void removePiece(){  
    piece = null;
}
public Piece getPiece(){
    return piece;       
}

class PieceFactory {     
     @SuppressWarnings("rawtypes")
     public Piece createPiece(Piece pieceType) 
            throws ClassNotFoundException, InstantiationException, IllegalAccessException{
         Class pieceClass = Class.forName(pieceType.toString());
         Piece piece = (Piece) pieceClass.newInstance();

         return piece;       
     } }

public void setColour(String colour){

} }

public abstract class Piece {

Board board;

public void setColour(String pieceColour) {
}

public void setOwner(String pieceOwner) {
}

public String getColour() {
    return "";
}

public String getOwner() {
    return "";      
}
public void movePiece(int oldIndex, int newIndex){
    board.getSquare(oldIndex).removePiece();
    board.getSquare(newIndex).addPiece(this);
}
public String toString(){
    return this.getClass().getSimpleName();
} }

You wanted to see the code, very basic I know. And I will change the [64] to [8][8].
I’m trying to not make it harder then it has to be. I can probably combine Colour and Owner as an attribute and make it an enum (either BLACK or WHITE).

Sorry if the formatting isn’t good.

  • 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-19T00:24:28+00:00Added an answer on May 19, 2026 at 12:24 am

    When designing software, I find it helpful to think about how I would use a method, then write down the method signature (and if you do test driven development, the unit test), and only then think about how I would implement it.

    Doing this here, I find that the requirement

    Then when a player clicks a piece, the
    squares that are available as
    destination after the move will be
    outlined in some colour.

    is impossible to satisfy with a method like

    void move(Square destination);
    

    because there is no way to find out the possible moves without actually making them. To highlight the squares, it would probably be best if we had a method like

    Collection<Square> getPossibleMoves();
    

    and then we could implement

    void move(Square destination) {
        if (!getPossibleMoves().contains(destination) {
            throw new IllegalMoveException();
        }
    
        this.location.occupyingPiece = null;
        this.location = destination;
        this.location.occupyingPiece = this;
    }
    

    as for implementing getPossibleMoves:

    class Pawn extends Piece {
        @Override Collection<Square> getPossibleMoves() {
            List<Square> possibleMoves = new ArrayList<Square>();
            int dy = color == Color.white ? 1 : -1;
            Square ahead = location.neighbour(0, dy);
            if (ahead.occupyingPiece == null) {
                possibleMoves.add(ahead);
            }
            Square aheadLeft = location.neighbour(-1, dy);
            if (aheadLeft != null && aheadLeft.occupyingPiece != null && aheadLeft.occupyingPiece.color != color) {
                possibleMoves.add(aheadLeft);
            }
            Square aheadRight = location.neighbour(1, dy);
            if (aheadRight != null && aheadRight.occupyingPiece != null && aheadRight.occupyingPiece.color != color) {
                possibleMoves.add(aheadRight);
            }
            return possibleMoves;
        }
    }
    

    Edit:

    class Knight extends Piece {
        @Override Collection<Square> getPossibleMoves() {
            List<Square> possibleMoves = new ArrayList<Square>();
            int[][] offsets = {
                {-2, 1},
                {-1, 2},
                {1, 2},
                {2, 1},
                {2, -1},
                {1, -2},
                {-1, -2},
                {-2, -1}
            };
            for (int[] o : offsets) {
                Square candidate = location.neighbour(o[0], o[1]);
                if (candidate != null && (candidate.occupyingPiece == null || candidate.occupyingPiece.color != color)) {
                    possibleMoves.add(candidate);
                }
            }
            return possibleMoves;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm relatively new to the world of WhiteBox Testing and need help designing a
I need help with this route map routes.MapRoute(Blog_Archive, Blog/Archive/{year}/{month}/{day}, new { controller = Blog,
I need help to replace all \n (new line) caracters for in a String,
I have a regex call that I need help with. I haven't posted my
I need help designing a query that will be used to return a website's
I need help designing an algorithm for recommendations on movies. Every user in the
I have a small database that I need help designing. I have a VB.NET
I need help in designing my PHP classes where I need to extend from
I need some help designing a friends system The mysql table: friends_ list -
All - I need some help designing a table for a Postgre SQL database.

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.