1

I'm trying to make a checkers game as practice for personal reasons, as I am somewhat newer to Java and am trying to learn.

In class Board, I'm trying to call a method that requires a Board type as input, to see which pieces a specific piece could possibly jump.

Here's Board:

public class Board {
    private Piece[][] board;

    public Board(){
        board = new Piece[8][8];
    }

    public Piece pieceAtLocation(int x, int y){
        return board[x][y];
    }

    public boolean movePiece(int startX, int startY, int newX, int newY, String movementType){
        Piece temp = board[startX][startY];
        if(movementType.equals("move")){
            board[newX][newY] = temp;
            board[startX][startY] = null;
            return true;
        } else if(movementType.equals("jump")){
            int[][] possibleSpaces = temp.jumpableSpaces(this);
        }
        return false;
    }
}

Here's the Piece class it's calling from

public class Piece {
    private boolean isKinged;
    private boolean teamSided;
    private int x;
    private int y;

    public Piece(boolean teamSided, int x, int y){
        this.isKinged = false;
        this.teamSided = teamSided;
        this.x = x;
        this.y = y;
    }

    public boolean team(){
        return teamSided;
    }

    public boolean kinged(){
        return isKinged;
    }

    public int[][] jumpableSpaces(Board board){
        int[][] jumpTo = new int[4][2];
        if(x - 2 >= 0 && y + 2 < 8){
            if(board.pieceAtLocation(x - 2, y + 2) == null && board.pieceAtLocation(x - 1, y + 1).team() != teamSided){
                jumpTo[0][0] = x - 2;
                jumpTo[0][1] = y + 2;
            } else{
                jumpTo[0][0] = -1;
                jumpTo[0][1] = -1;
            }
        }
        if(x + 2 < 8 && y + 2 < 8){
            if(board.pieceAtLocation(x + 2, y + 2) == null && board.pieceAtLocation(x + 1, y + 1).team() != teamSided){
                jumpTo[1][0] = x + 2;
                jumpTo[1][1] = y + 2;
            } else{
                jumpTo[1][0] = -1;
                jumpTo[1][1] = -1;
            }
        }
        if(isKinged){
            if(x - 2 >= 0 && y - 2 >= 0){
                if(board.pieceAtLocation(x - 2, y - 2) == null && board.pieceAtLocation(x - 1, y - 1).team() != teamSided){
                    jumpTo[2][0] = x - 2;
                    jumpTo[2][1] = y - 2;
                }
            }
            if(x - 2 >= 0 && y + 2 < 8){
                if(board.pieceAtLocation(x - 2, y + 2) == null && board.pieceAtLoaction(x - 1, y + 1).team() != teamSided){
                    jumpTo[3][0] = x - 2;
                    jumpTo[3][1] = y + 2;
                }
            }
        } else{
            jumpTo[2][0] = -1;
            jumpTo[2][1] = -1;
            jumpTo[3][0] = -1;
            jumpTo[3][1] = -1;
        }
        return jumpTo;
    }
}

My question involves line 19 of Board. Is it possible to leave it as is and have it input itself using "this" or will I have to do something else? I'm not yet fully understanding of what "this" means, as I heard about its use in the constructors from a friend.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
MacNPieces
  • 11
  • 1

1 Answers1

0

You can absolutely pass 'this' as a parameter to an internal function. 'This' basically refers to the specific instance of the object, so any value that is stored within it will be passed along as well.

Waza-Ari
  • 21
  • 3