Chess Game in Java: Should Square Class Have "Generate Possible Moves" Method?

bclayman

I'm writing a chess game. My basic design is to have a 2d array (8 x 8) consisting of square objects. Squares have a number of fields: int height, int width, Piece piece (null if empty, some type of Piece object otherwise.

NB: Rook, Knight, Bishop, etc. all extend Piece.

Now, I'm getting a little tripped up on how to figure out what moves are legal for a given piece, given my OOP design. Here's what I'm thinking:

1) User clicks square 2) We determine what piece is on square (if empty, return error message) 3) Generate legal moves for that piece on that square

I'm worried about writing code like:

if (clickedSquare.piece.instanceOf(Rook)) {
    return Rook.getLegalDestinationSquares(clickedSquare);
} else if (clickedSquare.piece.instanceOf(Bishop)) {
    return Bishop.getLegalDestinationSquares(clickedSquare);
} else if...

Which seems really bad. There must be a way to do this that conforms better to OOP but I'm still learning.

Thanks for the help, Mariogs

Patryk Dobrowolski

You don't need to create that if statement. Just get the current piece on the field and call some method like (getLegalMoves()) or something. If field is empty - return empty list of allowed moves.

public abstract class Piece {

    public abstract List<Field> getFieldsAllowed(Field field);
}
public class Rook extends Piece {

    @Override
    public List<Field> getFieldsAllowed(Field field) {
        // TODO Auto-generated method stub
        return null;
    }    
}
public class Field {

    public Piece getPiece() {
        // get current piece
    }
}

Something like this. Try to find your own solution. This one is not perfect.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java Chess: Should finding nearby square be a method of Square or Board class?

From Dev

Programming a chess game in Java, and gameOver boolean not working

From Dev

Adding difficulty levels into Java Chess Game

From Dev

In C++, should different game entities have different classes? Or should it be in one class which contains all behaviours?

From Dev

How to determine which class should have a specified method

From Dev

Should an immutable class member have an accessor method or allowed to be public?

From Dev

Java Square Class

From Dev

Should we have a newline after method name in Java?

From Dev

OOP Design for Chess Game in Java (Trouble with Piece / Board Interaction)

From Dev

Cannot find bug in NegaMax implementation for java chess game

From Dev

Java-chess game, data exchange from GUI to other classes

From Dev

Making a chess game in Java, I want to move the pieces

From Dev

Java Class extends Abstract class but must have static method

From Dev

Java Class extends Abstract class but must have static method

From Dev

Implementing "Check" in a Chess Game

From Dev

Complexity of a Chess Knight game

From Dev

Complexity of a Chess Knight game

From Dev

pass a string as argument in instance of a class, should have method that take string as an arg and perform according to instance.method

From Dev

Should a method return Class or Class<?>?

From Dev

Creating a Fuzzy Chess Game using Chess Engines

From Dev

why does the Collections utility class does not have a Iterator method in Java?

From Dev

Java - Use getService method who have Class<T> for parameter

From Dev

Should I place my game in a class pygame?

From Dev

Should class Square publicly inherit from class Rectangle?

From Dev

How to implement movement in a chess game ?

From Dev

Chess game design and Singleton pattern

From Dev

How to programm the AI for a chess game

From Dev

Find all combinations of Chess game

From Dev

Should getters have validation in java?

Related Related

  1. 1

    Java Chess: Should finding nearby square be a method of Square or Board class?

  2. 2

    Programming a chess game in Java, and gameOver boolean not working

  3. 3

    Adding difficulty levels into Java Chess Game

  4. 4

    In C++, should different game entities have different classes? Or should it be in one class which contains all behaviours?

  5. 5

    How to determine which class should have a specified method

  6. 6

    Should an immutable class member have an accessor method or allowed to be public?

  7. 7

    Java Square Class

  8. 8

    Should we have a newline after method name in Java?

  9. 9

    OOP Design for Chess Game in Java (Trouble with Piece / Board Interaction)

  10. 10

    Cannot find bug in NegaMax implementation for java chess game

  11. 11

    Java-chess game, data exchange from GUI to other classes

  12. 12

    Making a chess game in Java, I want to move the pieces

  13. 13

    Java Class extends Abstract class but must have static method

  14. 14

    Java Class extends Abstract class but must have static method

  15. 15

    Implementing "Check" in a Chess Game

  16. 16

    Complexity of a Chess Knight game

  17. 17

    Complexity of a Chess Knight game

  18. 18

    pass a string as argument in instance of a class, should have method that take string as an arg and perform according to instance.method

  19. 19

    Should a method return Class or Class<?>?

  20. 20

    Creating a Fuzzy Chess Game using Chess Engines

  21. 21

    why does the Collections utility class does not have a Iterator method in Java?

  22. 22

    Java - Use getService method who have Class<T> for parameter

  23. 23

    Should I place my game in a class pygame?

  24. 24

    Should class Square publicly inherit from class Rectangle?

  25. 25

    How to implement movement in a chess game ?

  26. 26

    Chess game design and Singleton pattern

  27. 27

    How to programm the AI for a chess game

  28. 28

    Find all combinations of Chess game

  29. 29

    Should getters have validation in java?

HotTag

Archive