Search in sources :

Example 1 with ShogiMove

use of com.playshogi.library.shogi.models.moves.ShogiMove in project playshogi by Tellmarch.

the class GameSetRepository method deleteGameFromGameSet.

public boolean deleteGameFromGameSet(final int gameId, final int gameSetId, final int userId) {
    boolean result = deleteFromGameSetGameTable(gameId, gameSetId, userId);
    if (!result) {
        // No permission
        return false;
    }
    PersistentGame game = gameRepository.getGameById(gameId);
    if (game == null) {
        return true;
    }
    PersistentKifu kifu = kifuRepository.getKifuById(game.getKifuId());
    if (kifu == null) {
        return true;
    }
    GameRecord gameRecord = kifu.getKifu();
    GameNavigation gameNavigation = new GameNavigation(new ShogiRulesEngine(), gameRecord.getGameTree());
    boolean senteWin = gameRecord.getGameResult() == GameResult.BLACK_WIN;
    boolean goteWin = gameRecord.getGameResult() == GameResult.WHITE_WIN;
    int lastPositionId = positionRepository.getOrSavePosition(gameNavigation.getPosition());
    decrementGameSetPosition(gameSetId, lastPositionId, senteWin, goteWin);
    while (gameNavigation.canMoveForward()) {
        Move move = gameNavigation.getMainVariationMove();
        gameNavigation.moveForward();
        int positionId = positionRepository.getOrSavePosition(gameNavigation.getPosition());
        decrementGameSetPosition(gameSetId, positionId, senteWin, goteWin);
        decrementGameSetMove(gameSetId, lastPositionId, UsfMoveConverter.toUsfString((ShogiMove) move), positionId);
        lastPositionId = positionId;
    }
    return true;
}
Also used : ShogiRulesEngine(com.playshogi.library.shogi.rules.ShogiRulesEngine) ShogiMove(com.playshogi.library.shogi.models.moves.ShogiMove) ShogiMove(com.playshogi.library.shogi.models.moves.ShogiMove) Move(com.playshogi.library.shogi.models.moves.Move) GameNavigation(com.playshogi.library.shogi.models.record.GameNavigation) GameRecord(com.playshogi.library.shogi.models.record.GameRecord)

Example 2 with ShogiMove

use of com.playshogi.library.shogi.models.moves.ShogiMove in project playshogi by Tellmarch.

the class GameSetRepository method saveGameFromKifuAndAddToGameSet.

public boolean saveGameFromKifuAndAddToGameSet(final int gameSetId, final int kifuId, final int venueId) {
    PersistentKifu kifu = kifuRepository.getKifuById(kifuId);
    GameRecord gameRecord = kifu.getKifu();
    String gameName = kifu.getName();
    int gameId = gameRepository.saveGame(kifuId, null, null, gameRecord.getGameInformation().getBlack(), gameRecord.getGameInformation().getWhite(), parseDate(gameRecord.getGameInformation().getDate()), venueId, gameName);
    addGameSetGameRecord(gameSetId, gameId);
    boolean senteWin = gameRecord.getGameResult() == GameResult.BLACK_WIN;
    boolean goteWin = gameRecord.getGameResult() == GameResult.WHITE_WIN;
    GameNavigation gameNavigation = new GameNavigation(new ShogiRulesEngine(), gameRecord.getGameTree());
    int lastPositionId = positionRepository.getOrSavePosition(gameNavigation.getPosition());
    kifuRepository.saveKifuPosition(kifuId, lastPositionId);
    incrementGameSetPosition(gameSetId, lastPositionId, senteWin, goteWin);
    while (gameNavigation.canMoveForward()) {
        Move move = gameNavigation.getMainVariationMove();
        gameNavigation.moveForward();
        int positionId = positionRepository.getOrSavePosition(gameNavigation.getPosition());
        kifuRepository.saveKifuPosition(kifuId, positionId);
        incrementGameSetPosition(gameSetId, positionId, senteWin, goteWin);
        incrementGameSetMove(gameSetId, lastPositionId, UsfMoveConverter.toUsfString((ShogiMove) move), positionId);
        lastPositionId = positionId;
    }
    return true;
}
Also used : ShogiRulesEngine(com.playshogi.library.shogi.rules.ShogiRulesEngine) ShogiMove(com.playshogi.library.shogi.models.moves.ShogiMove) ShogiMove(com.playshogi.library.shogi.models.moves.ShogiMove) Move(com.playshogi.library.shogi.models.moves.Move) GameNavigation(com.playshogi.library.shogi.models.record.GameNavigation) GameRecord(com.playshogi.library.shogi.models.record.GameRecord)

Example 3 with ShogiMove

use of com.playshogi.library.shogi.models.moves.ShogiMove in project playshogi by Tellmarch.

the class TsumeEscapeSolver method escapeTsume.

public EscapeTsumeResult escapeTsume(final ShogiPosition position) {
    if (position.getPlayerToMove() == Player.BLACK) {
        throw new IllegalStateException("Can only try to escape Tsume from Gote point of view");
    }
    if (!rulesEngine.isPositionCheck(position)) {
        return EscapeTsumeResult.NOT_CHECK;
    }
    int maxMove = 0;
    String variationUsf = "";
    for (ShogiMove move : rulesEngine.getAllPossibleMoves(position)) {
        rulesEngine.playMoveInPosition(position, move);
        if (!rulesEngine.isPositionCheck(position, Player.WHITE)) {
            // If Gote is still in check, there is no
            // need to ask the engine
            PositionEvaluation evaluation = queuedTsumeSolver.analyseTsume(position);
            if (evaluation.getBestMove() == null) {
                if ("timeout".equals(evaluation.getMateDetails())) {
                    return new EscapeTsumeResult(ESCAPE_BY_TIMEOUT, move);
                } else if ("nomate".equals(evaluation.getMateDetails())) {
                    return new EscapeTsumeResult(ESCAPE, move);
                } else {
                    throw new IllegalStateException("Unknown mate details: " + evaluation.getMateDetails());
                }
            } else {
                if (evaluation.getMainVariation().getNumMoves() + 1 > maxMove) {
                    maxMove = evaluation.getMainVariation().getNumMoves() + 1;
                    variationUsf = move.getUsfString() + " " + evaluation.getMainVariation().getUsf();
                }
            }
        }
        rulesEngine.undoMoveInPosition(position, move);
    }
    return new EscapeTsumeResult(maxMove, variationUsf);
}
Also used : ShogiMove(com.playshogi.library.shogi.models.moves.ShogiMove)

Example 4 with ShogiMove

use of com.playshogi.library.shogi.models.moves.ShogiMove in project playshogi by Tellmarch.

the class ShogiRulesEngineTest method getAllPossibleDropMovesForSente.

@Test
public void getAllPossibleDropMovesForSente() {
    String sfen = "lnsg3nl/2k2gr2/ppbp1p1pp/2p1P4/4s1S2/5B3/PPPP1P1PP/2S1GGR2/LN4KNL b 2Pp";
    ShogiPosition position = SfenConverter.fromSFEN(sfen);
    List<ShogiMove> allPossibleDropMoves = engine.getAllPossibleDropMoves(position, Player.BLACK);
    assertEquals(4, allPossibleDropMoves.size());
}
Also used : ShogiMove(com.playshogi.library.shogi.models.moves.ShogiMove) ShogiPosition(com.playshogi.library.shogi.models.position.ShogiPosition) Test(org.junit.Test)

Example 5 with ShogiMove

use of com.playshogi.library.shogi.models.moves.ShogiMove in project playshogi by Tellmarch.

the class ShogiRulesEngineTest method getAllPossibleDropMovesForGote.

@Test
public void getAllPossibleDropMovesForGote() {
    String sfen = "lnsg3nl/2k2gr2/ppbp1p1pp/2p1P4/4s1S2/5B3/PPPP1P1PP/2S1GGR2/LN4KNL b 2Pp";
    ShogiPosition position = SfenConverter.fromSFEN(sfen);
    List<ShogiMove> allPossibleDropMoves = engine.getAllPossibleDropMoves(position, Player.WHITE);
    assertEquals(10, allPossibleDropMoves.size());
}
Also used : ShogiMove(com.playshogi.library.shogi.models.moves.ShogiMove) ShogiPosition(com.playshogi.library.shogi.models.position.ShogiPosition) Test(org.junit.Test)

Aggregations

ShogiMove (com.playshogi.library.shogi.models.moves.ShogiMove)22 ChangeTutorialTextEvent (com.playshogi.website.gwt.client.events.tutorial.ChangeTutorialTextEvent)7 ShogiPosition (com.playshogi.library.shogi.models.position.ShogiPosition)6 Move (com.playshogi.library.shogi.models.moves.Move)4 NormalMove (com.playshogi.library.shogi.models.moves.NormalMove)4 Test (org.junit.Test)4 Timer (com.google.gwt.user.client.Timer)2 EventHandler (com.google.web.bindery.event.shared.binder.EventHandler)2 CaptureMove (com.playshogi.library.shogi.models.moves.CaptureMove)2 DropMove (com.playshogi.library.shogi.models.moves.DropMove)2 EditMove (com.playshogi.library.shogi.models.moves.EditMove)2 Square (com.playshogi.library.shogi.models.position.Square)2 GameNavigation (com.playshogi.library.shogi.models.record.GameNavigation)2 GameRecord (com.playshogi.library.shogi.models.record.GameRecord)2 ShogiRulesEngine (com.playshogi.library.shogi.rules.ShogiRulesEngine)2 HighlightMoveEvent (com.playshogi.website.gwt.client.events.gametree.HighlightMoveEvent)2 MovePlayedEvent (com.playshogi.website.gwt.client.events.gametree.MovePlayedEvent)1 OpeningsPlace (com.playshogi.website.gwt.client.place.OpeningsPlace)1 PositionMoveDetails (com.playshogi.website.gwt.shared.models.PositionMoveDetails)1