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;
}
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;
}
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);
}
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());
}
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());
}
Aggregations