checkers.game_engine.game_rules.RuleManager.java Source code

Java tutorial

Introduction

Here is the source code for checkers.game_engine.game_rules.RuleManager.java

Source

/*
 * The MIT License
 *
 * Copyright 2014 Mateusz Czarnecki.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package checkers.game_engine.game_rules;

import checkers.game_engine.Board;
import checkers.game_engine.Game.GameState;
import checkers.game_engine.Player;
import checkers.game_engine.Position;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.SetMultimap;
import java.util.HashSet;
import java.util.Set;

/**
 *
 * @author Mateusz Czarnecki
 */
public class RuleManager implements RuleManagerInterface {

    @Override
    public boolean isLegal(Player activePlayer, Position startPosition, Position destPosition, Board board) {
        SetMultimap<Position, Position> allMoves = getAllAvailableMoves(activePlayer, board);
        return allMoves.containsEntry(startPosition, destPosition);
    }

    @Override
    public GameState getResult(Board board) {

        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public SetMultimap<Position, Position> getAllAvailableMoves(Player activePlayer, Board board) {
        SetMultimap<Position, Position> allMoves = getAllAvailableCaptures(activePlayer, board);
        if (!allMoves.isEmpty()) {
            return allMoves;
        }

        for (int i = 1; i < board.getSize(); i++) {
            for (int j = 1; j < board.getSize(); j++) {
                Position startPos = new Position(i, j);

                if (activePlayer == Player.PLAYER_ONE && (board.getFieldAt(i, j) == Board.WHITE_KING
                        || board.getFieldAt(i, j) == Board.WHITE_REGULAR_PIECE)) {
                    allMoves.putAll(startPos, getAvailableMoves(startPos, board));
                } else if (activePlayer == Player.PLAYER_TWO && (board.getFieldAt(i, j) == Board.BLACK_KING
                        || board.getFieldAt(i, j) == Board.BLACK_REGULAR_PIECE)) {
                    allMoves.putAll(startPos, getAvailableMoves(startPos, board));
                }
            }
        }
        return allMoves;
    }

    @Override
    public SetMultimap<Position, Position> getAllAvailableCaptures(Player activePlayer, Board board) {
        SetMultimap<Position, Position> allCaptures = HashMultimap.create();
        for (int i = 1; i < board.getSize(); i++) {
            for (int j = 1; j < board.getSize(); j++) {
                Position startPos = new Position(i, j);

                if (activePlayer == Player.PLAYER_ONE && (board.getFieldAt(i, j) == Board.WHITE_KING
                        || board.getFieldAt(i, j) == Board.WHITE_REGULAR_PIECE)) {
                    allCaptures.putAll(startPos, getAvailableCaptures(startPos, board));
                } else if (activePlayer == Player.PLAYER_TWO && (board.getFieldAt(i, j) == Board.BLACK_KING
                        || board.getFieldAt(i, j) == Board.BLACK_REGULAR_PIECE)) {
                    allCaptures.putAll(startPos, getAvailableCaptures(startPos, board));
                }
            }
        }
        return allCaptures;
    }

    @Override
    public Set<Position> getAvailableMoves(Position pos, Board board) {
        Set<Position> moves = getAvailableCaptures(pos, board);

        if (!moves.isEmpty()) {
            return moves;
        }

        int pieceType = board.getFieldAt(pos);
        if (pieceType == Board.EMPTY || pieceType == Board.BOUNDARY) {
            return moves;
        }

        for (int i = -1; i <= 1; i += 2) {
            for (int j = -1; j <= 1; j += 2) {
                if ((pieceType == Board.WHITE_REGULAR_PIECE && i == -1)
                        || (pieceType == Board.BLACK_REGULAR_PIECE && i == 1)) {
                    break;
                }

                if (board.getFieldAt(pos.getX() + i, pos.getY() + j) == Board.EMPTY) {
                    moves.add(new Position(pos.getX() + i, pos.getY() + j));
                }
            }
        }

        return moves;
    }

    @Override
    public Set<Position> getAvailableCaptures(Position pos, Board board) {
        int startX = pos.getX();
        int startY = pos.getY();
        int pieceType = board.getFieldAt(startX, startY);
        HashSet<Position> captures = new HashSet<>(4);

        if (pieceType == Board.EMPTY || pieceType == Board.BOUNDARY) {
            return captures;
        }

        for (int i = -1; i <= 1; i += 2) {
            for (int j = -1; j <= 1; j += 2) {
                if ((pieceType == Board.WHITE_REGULAR_PIECE && i == -1)
                        || (pieceType == Board.BLACK_REGULAR_PIECE && i == 1)) {
                    break;
                }

                //Oposite pieces are represented by oposite numbers.
                if (board.getFieldAt(startX + i, startY + j) != Board.BOUNDARY
                        && board.getFieldAt(startX + i, startY + j) * pieceType < 0
                        && board.getFieldAt(startX + 2 * i, startY + 2 * j) == Board.EMPTY) {
                    captures.add(new Position(startX + 2 * i, startY + 2 * j));
                }
            }
        }

        return captures;
    }

}