0

START OF INSTRUCTIONS

If there is a game piece on the clicked square (clickedSquare.getPiece()!= null) Make sure the game piece belongs to the current player. You can get the owning player by calling the getPlayerType() method on the AbstractGamePiece returned by getPiece(). You can then compare that to the currentPlayerTurn JailBreak class member.

If the piece on the clicked square belongs to the current player Set the selected square equal to the clicked square Call the select() method on the selected square to show the yellow border

END OF INSTRUCTIONS

I've figured out how to highlight the piece by implementing the select() method, butt I've tried several different implementations, such as AbstractGamePiece.getPlayerType()==currentPlayerTurn, using nested if statements to set conditions on clickedSquare.getPiece(), and a couple others that I can't think of off the top. I can't seem to get a reference to getPlayerType() from the abstract class. There is pre-written code in the class that seems to work fine as far as accessing the AbstractGamePiece, such as

private void changePlayerTurn()
    {
        if (currentPlayerTurn == AbstractGamePiece.PLAYER_OUTLAWS)
            currentPlayerTurn = AbstractGamePiece.PLAYER_POSSE;
        else
            currentPlayerTurn = AbstractGamePiece.PLAYER_OUTLAWS;
    }

I feel like I'm going about this wrong, but I can't seem to get a reference to the getPlayerType(). The one time I did, I created a new object of the abstract class, but the constructor needs 3 parameters that aren't really appropriate here.

Supporting code:

abstract public class AbstractGamePiece
{
    // All class members are provided as part of the activity starter!
    
    // These two constants define the Outlaws and Posse teams
    static public final int PLAYER_OUTLAWS = 0;
    static public final int PLAYER_POSSE = 1;

    // These variables hold the piece's column and row index
    protected int myCol;
    protected int myRow;
    
    // This variable indicates which team the piece belongs to
    protected int myPlayerType;
     
    // These two strings contain the piece's full name and first letter abbreviation
    private String myAbbreviation;
    private String myName;

    // All derived classes will need to implement this method
    abstract public boolean hasEscaped();
    
    // The student should complete this constructor by initializing the member
    // variables with the provided data.
    public AbstractGamePiece(String name, String abbreviation, int playerType)
    {
    myName = name;
    myAbbreviation = abbreviation;
    myPlayerType = playerType;
    }

    public int getPlayerType()
    {
        return myPlayerType;    
    }
    public void setPosition (int row, int col)
    {
     myRow = row;
     myCol = col;
    }
    public int getRow()
    {
        return myRow;
    }
    public int getCol()
    {
        return myCol;
    }
    public String getAbbreviation() 
    {
        return myAbbreviation;
    }
    public String toString()
    {
        return (myName + " at " + "(" + myRow + "," + myCol + ")");
        
    }
    public boolean canMoveToLocation(List<GameSquare> path)
    {
        return false;
    }
    public boolean isCaptured(GameBoard gameBoard)
    {
        return false;
    }

Code that highlights the pieces indiscriminately has been implemented successfully.

private void handleClickedSquare(GameSquare clickedSquare)
    {
        if (selectedSquare == null)
        {
            selectedSquare=clickedSquare;
            selectedSquare.select();
        }
        else if (selectedSquare == clickedSquare)
        {
        selectedSquare.deselect();
        selectedSquare = null;
        }
            
        else 
        {
        }

Why is it that I'm unable to create a reference to the getPlayerType() method?

Meezy98
  • 3
  • 3

1 Answers1

0

Just call getPlayerType on any expression of type X, where X is either AbstractGamePiece or any subclass thereof. For example, square.getPiece().getPlayerType().

Method references are a thing in java and are definitely not what you want, you're using words ('I'm unable to create a reference to getPlayerType') that mean something else. A method reference looks like AbstractGamePiece::getPlayerType, and let you ship the concept of invoking that method around to other code (for example, you could make a method that calls some code 10 times in a row - so this method takes, as argument, 'some code' - and method references are a way to that). It is not what you want here, you want to just invoke that method. Which is done with ref.getPlayerType() where ref is an expression whose type is compatible with AbstractGamePiece. From context, clickedSquare.getPiece() is that expression.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • so in essence, I'm using `clickedSquare.getPiece()` as a way to match an `AbstractGamePiece` with another? The main error I was getting was that an `AbstractGamePiece` could not be converted to an `int`, so I'm guessing this is the way around that. Thank you, this worked. Does this principle have a specific name? I would like to do some more research – Meezy98 Mar 25 '21 at 03:24