Chess: Duplicate code in movement validations - Java
NickName:Rithvik Tiecelle Ask DateTime:2019-01-02T21:43:28

Chess: Duplicate code in movement validations - Java

I am currently learning Java and working on a class assignment. We're supposed to make a "weird version" of Chess. Like in original chess, pieces can't move if there is a piece in their way, the obvious exception being the Horse, which can hop over all pieces except Kings.

I have an abstract class Piece that is inherited by all the piece types, each with their own rules of movement. Most of them have this movement restriction, and so I have defined the methods in this class:

    public boolean freeWayHorizontally(int xO, int yO, int xD) {
    //RIGHT
    if (xO < xD) {
        for (int x = xO + 1; x < xD; x++) {
            CrazyPiece thereIsPiece = Simulador.checkIfTheresPiece(x, yO);
            if (thereIsPiece != null){
                return false;
            }
        }
    //LEFT
    } else if (xO > xD) {
        for (int x = xO - 1; x > xD; x--) {
            CrazyPiece thereIsPiece = Simulador.checkIfTheresPiece(x, yO);
            if (thereIsPiece != null){
                return false;
            }
        }
    }
    return true;
}

public boolean freeWayVertically(int xO, int yO, int yD) {
    //UP
    if (yO < yD) {
        for (int y = yO + 1; y < yD; y++) {
            CrazyPiece thereIsPiece = Simulador.checkIfTheresPiece(xO, y);
            if (thereIsPiece != null){
                return false;
            }
        }
    //DOWN
    } else if (yO > yD) {
        for (int y = yO - 1; y > yD; y--) {
            CrazyPiece thereIsPiece = Simulador.checkIfThereIsPiece(xO, y);
            if (thereIsPiece != null){
                return false;
            }
        }
    }
    return true;
}

thereIsPiece(int x, int y) is a function from the Chess Simulator class that, given a position on the board, returns the piece in that position.

As is obvious, these two receive the same parameters (origin coordinates and a destination coordinate, where one of the destination coordinates is one of the piece's origin coordinates), so the only thing that really changes is the way thereIsPiece() is called. EDIT: And because of this, they're marked as duplicates, and from what I've been told, that's very bad!

However, I can't seem to figure out a way to solve this problem using only one of these methods; ALSO AN EDIT: I've tried overloading it, but then it'd work only vertically or horizontally (may have done it wrong).

The thing is I need these to be done separately to implement the Horse's movement, that overrides these methods:

public boolean freeWayHorizontally(int xO, int yO, int xD) { //Overriden by the Horse class
    //RIGHT
    if (xO < xD) {
        for (int x = xO + 1; x <= xD; x++) {
            CrazyPiece thereIsPiece = Simulador.checkIfTheresPiece(x, yO);
            if (thereIsPiece != null && thereIsPiece.isKing){
                return false;
            }
        }
    //LEFT
    } else if (xO > xD) {
        for (int x = xO - 1; x >= xD; x--) {
            CrazyPiece thereIsPiece = Simulador.checkIfTheresPiece(x, yO);
            if (thereIsPiece != null && thereIsPiece.isKing){
                return false;
            }
        }
    }
    return true;
}

public boolean freeWayVertically(int xO, int yO, int yD) { //Overriden by the Horse class
    //UP
    if (yO < yD) {
        for (int y = yO + 1; y <= yD; y++) {
            CrazyPiece thereIsPiece = Simulador.checkIfTheresPiece(xO, y);
            if (thereIsPiece != null && thereIsPiece.isKing){
                return false;
            }
        }
    //DOWN
    } else if (yO > yD) {
        for (int y = yO - 1; y >= yD; y--) {
            CrazyPiece thereIsPiece = Simulador.checkIfThereIsPiece(xO, y);
            if (thereIsPiece != null && thereIsPiece.isKing){
                return false;
            }
        }
    }
    return true;
}

And then calls its own type of movement check, also defined in the Piece class:

public boolean freeWayL(int xO, int yO, int xD, int yD) {
    boolean fH, fV;
    //Horizontal -> Vertical
    fH = this.freeWayHorizontally(xO, yO, xD);
    if (fH) {
        fV = this.freeWayVertically(xD, yO, yD);
        if (fV) {
            return true;
        }
    }
    //Vertical -> Horizontal
    fV = this.freeWayVertically(xO, yO, yD);
    if (dV) {
        fH = this.freeWayHorizontally(xO, yD, xD);
        if (fH) {
            return true;
        }
    }
    return false;
}

What can I do to avoid all this duplication, or even to make these validations better?

Copyright Notice:Content Author:「Rithvik Tiecelle」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/54007446/chess-duplicate-code-in-movement-validations-java

More about “Chess: Duplicate code in movement validations - Java” related questions

Chess: Duplicate code in movement validations - Java

I am currently learning Java and working on a class assignment. We're supposed to make a "weird version" of Chess. Like in original chess, pieces can't move if there is a piece in their way, the ob...

Show Detail

Queen movement in chess game(Javascript)

I am learning Javascript and as a project assignment making chess game in it. I have coded the logic for movement of Rook, Pawn, Knight and Bishop. Now I am stuck on Queen movement. A queen's move

Show Detail

How to track movement of a single chess piece?

I'm using python-chess and I would like to know what is a good way to track movement of let say a black King throughout a single match recorded in pgn format using python-chess. Essentially creatin...

Show Detail

Pawn Movement in Chess Game - Java

There is an issue with the movement in the chess app I am creating. Here is the method that checks if a move is valid: public boolean isMove(int row, int col, Pawn[][] board){ Pawn p = board[r...

Show Detail

Programming Chess Rook Movement

I am trying to create a board game where all the pieces are able to move the same as a rook in chess. (i.e. Horizontally or vertically as many spaces as they wish) My board is a simple 2d integer ...

Show Detail

<Java> Chess pawn movement issue

i'm making a chess game in java, and pawns are giving me a hell of a problem. I honestly don't know why, as their movement is the simplest. Here's the Piece declaration, and the Pawn (extends Piece)

Show Detail

Python Chess Piece Movement

I have run into problems generating valid pieces for chess pieces in an early work-in-progress chess game in Python... I have run into trouble with the Bishop. Here is a glimpse of my program... the

Show Detail

Chess in Java - Object orientation vs Efficiency

I'm developing a chess program in Java and considering the following problem: The Rook piece requires implementation for moving in straight lines. The Bishop piece requires implementation for movi...

Show Detail

chess game - error in abstract class

#pragma once #include &lt;string&gt; #include &lt;iostream&gt; using namespace std; class Chess_tool { public: Chess_tool(string color, char name); virtual bool legal_movement(int source[...

Show Detail

How to Optimize Diagonal Movement Code for Chess Engine?

I'm currently in the process of building my own chess engine and could really use some suggestions on how to make this segment of code for calculating diagonal moves more efficient. (This is obviou...

Show Detail