2

I searched a lot for this but couldn't find it anywhere, but if you think that it had been asked before, please send me the link.

I have a parent class called chesspiece and I have 6 child classes: Pawn with the value of 1, Knight with the value of 2, Bishop with the value of 3, Rook with the value of 5, Queen with the value of 9 and king with the value of 1000.

I have two instance variable: value and the color of the piece

I want to know is it possible to say if you have the value of 1 you belong to the pawn class or if you have the value of 2, you belong to the knight class. So, right now I'm doing this using a method but it only returns a String and nothing more. Here's my code:

public class ChessPiece {
    private boolean     isWhite;
    private int         value;

    public ChessPiece (boolean isWhite, int value) {
        this.isWhite = isWhite;
        this.value = value;
    }
public String getNamebyValue() {
        switch (value) {
          case 1:
            return "Pawn";
          case 2:
            return "Knight";
          case 3:
            return "Bishop";
          case 5:
            return "Rook";
          case 9:
            return "Queen";
          case 1000:
            return "King";
        }
        return null;
    } 

And here's my child class:

public class Bishop extends ChessPiece {

    public Bishop(boolean isWhite) {
        super(isWhite, 3);

    }

5 Answers5

3

Suggestion:

ChessPiece.java

public class ChessPiece {
    private boolean     isWhite;
    private int         value;
    private String      name;

    public ChessPiece (boolean isWhite) {
        this.isWhite = isWhite;
    }

    public String getName() { return name; }

}

Bishop.java

public class Bishop extends ChessPiece {

    public Bishop(boolean isWhite) {
        super(isWhite);
        this.name = "Bishop";
        this.value = 3;
    }
    ...
}

In other words, let "class inheritance" do as much of the work as possible for you.

To answer your question: it's possible to share a variable between the parent and subclasses (as shown above).

It's also possible to override member variables in the child. Look here: Overriding member variables in Java ( Variable Hiding)

It all depends on your design - on your requirements, and on how you choose to implement those requirements.

FoggyDay
  • 11,962
  • 4
  • 34
  • 48
  • PS: I'm not sure there's much value in the "value" member (if a chess piece has a "type", like "Bishop", what's the point of "value=3"?). But I like Guy's suggestion [below](https://stackoverflow.com/a/60690398/3135317). If you need "value", then "enum" is better (more appropriate) than "int". – FoggyDay Mar 15 '20 at 19:15
1

You shouldn't create chess for every piece type, there isn't enough difference between them. Use enum instead

public class ChessPiece {
    private boolean isWhite;
    private ChessPieceType pieceType;

    enum ChessPieceType {
        Pawn(1),
        Knight(2),
        Bishop(3),
        //...
        King(1000);

        private int value;

        private ChessPieceType(int value) { 
            this.value = value; 
        }

        public int getValue() {
            return this.value;
        }
   }

    public ChessPiece (ChessPieceType type, boolean isWhite) {
        this.pieceType = type;
        this.isWhite = isWhite;
    }

    public ChessPieceType getPieceType() {
        return this.pieceType;
    }
}

Uses

ChessPiece piece = new ChessPiece(ChessPiece.ChessPieceType.Bishop, true);
ChessPiece.ChessPieceType pieceType = piece.getPieceType();

System.out.println(pieceType); //Bishop
System.out.println(pieceType.getValue()); //3
Guy
  • 46,488
  • 10
  • 44
  • 88
0

You can do this, but not the way you're coding it.

I would suggest having the method return a type of ChessPiece instead of a String, and then instantiating the type with calling that method and a number.

  public class ChessPiece {
private boolean     isWhite;
private int         value;

public ChessPiece (boolean isWhite, int value) {
    this.isWhite = isWhite;
    this.value = value;
}
public ChessPiece getNamebyValue(int value) {
    switch (value) {
      case 1:
        return new Pawn();
        break;
      case 2:
        return new Knight();
      break;
       case 3:
        return new Bishop();
        break;
      case 5:
        return new Rook();
       break;
      case 9:
        return new Queen();
       break;
      case 1000:
        return new King();
      break;
      default:
      return new ChessPiece();
      break;
    }
    return null;
} 

and then....

       ChessPiece cp= new getNameByValue(1000);

That will return an object of type King.

Also, if you don't have break statements in your switch, it will simply return the last value as it goes through each statement until there is a break.

Nathan777
  • 79
  • 8
0

If I have understood you correctly you may want to look at Factory Design Pattern you will send a function the "recipe" to create your object (in your case the value) and return an appropriate object

Abdullah Khaled
  • 309
  • 1
  • 2
  • 11
-1

In that case change the getNamebyValue() as below:

public ChessPiece getNamebyValue() {
    switch (value) {
      case 1:
        return new Pawn();
      case 2:
        return new Knight();
      case 3:
        return new Bishop();
      case 5:
        return new Rook()";
      case 9:
        return new Queen();
      case 1000:
        return new King();
    }
    return null;
  }

and create the subclasses as you shown for Bishop

mohd shoaib
  • 151
  • 1
  • 10