the-mac / TheBoardLibrary

The most advanced mobile board game platform made in android that is available under the GPL, and it includes multiplayer, tutoring/guide facilities (to teach someone how to play your game), board editor function (to learn more about the games you play), and much more...
http://on.fb.me/1MIc51o
GNU Lesser General Public License v2.1
1 stars 5 forks source link

Extending Piece Moves #2

Closed madeyoso66 closed 5 years ago

madeyoso66 commented 9 years ago

I would like to share some code on how to extend a piece's move across the board. This will be useful for pieces like the Queen, Bishop, Rook, and other pieces that have the sliding ability.

The sliding piece's setUpMoves method would look lilke this

    @Override
    public void setUpMoves() {
        initMoveDirection = new int[4];
        initMoveDirection[0] = MOVE_UP;
        initMoveDirection[1] = MOVE_RIGHT;
        initMoveDirection[2] = MOVE_DOWN;
        initMoveDirection[3] = MOVE_LEFT;

        extendMoves();
    }

This method belongs to the Rook. The Rook can move left, right, up, and down. That is exactly whats listed here. Theres an array called "initMoveDirection" which contains the inital moves. For example, MOVE_UP isnt the position of the inital move but the direction from the Rook's own position. Basically if the piece can only make one adjacent move, those moves are included in this array.

After the Array is created, we call the method "extendMoves" which will extend the moves from the array. This method works by using the array created above and generating an araylist, called "moves", of possible moves for that piece.

The process of genreating this arraylist is looping. The first loop cycles through all the directions stated in the array.With that number, we use another loop to check every square in that direction to see if its legal. Once the square isnt legal, we exit the second loop and go to the next direction to loop through those squares. Once all the looping is finished, you will have the arraylist of legal moves.


    public void extendMoves() {

        List<Integer> MoveDirections = new ArrayList<Integer>();
        int n;
        Boolean next;

        //THE FIRST LOOP FOR EVERY DIRECTION
        for (int x = 0; x < initMoveDirections.length; x++) {
            n = 0;
            next = false;

            //THE SECOND LOOP FOR EVERY SQUARE IN THAT DIRECTION
            while (next == false) {

                //CHECKS IF THE SQUARE IS OUT OF BOUNDS
                if (fromCurPos(initMoveDirections[x] + initMoveDirections[x] * n) < 64
                        && fromCurPos(initMoveDirections[x] + initMoveDirections[x] * n) > -1
                        && (((initMoveDirections[x] == 7 || initMoveDirections[x] == -1 || initMoveDirections[x] == -9) && fromCurPos(initMoveDirections[x]
                                + initMoveDirections[x] * n + 1) % 8 != 0)
                                || ((initMoveDirections[x] == -7 || initMoveDirections[x] == 1 || initMoveDirections[x] == 9) && fromCurPos(initMoveDirections[x]
                                        + initMoveDirections[x] * n) % 8 != 0) || (initMoveDirections[x] == 8 || initMoveDirections[x] == -8))) {
                    Piece other = getPieceAt(fromCurPos(initMoveDirections[x] + initMoveDirections[x]
                            * n));

                    //CHECKS IF THE SQUARE IS EMPTY
                    if (other == null) {
                        MoveDirections.add(fromCurPos(initMoveDirections[x] + initMoveDirections[x] * n));

                    } 

                    //CHECKS IF THE SQUARE CONTAINS AN ENEMY PIECE
                    else if (other.isOpponent(color)) {
                        MoveDirections.add(fromCurPos(initMoveDirections[x] + initMoveDirections[x] * n));

                        next = true;
                    }

                    else {

                        next = true;
                    }

                } else {
                    next = true;
                }
                n++;
            }
        }

        for (int i = 0; i < moves.size(); i++) {

            setUpLegalSquare(moves.get(i));
        }
    }

At the end of this method, another loop is used with "setUpLegalSquare" for every index of the arraylist "moves".