/// <summary> /// If it is the players turn it returns a TurnDTO with the available moves /// </summary> /// <param name="gameId"></param> /// <param name="player"></param> /// <returns></returns> public static OutgoingMessage StartTurn(Guid gameId, Player player) { Game game = GameRepository.Instance.getGame(gameId); if (AvailableMoveService.IsGameOver(game.board)) { return(new EndGameDTO() { reason = EndReason.GAME_OVER, winner = AvailableMoveService.GetWinner(game.board) }); } if (game.turn != player) { throw new ArgumentOutOfRangeException("It is not " + player.ToString() + "'s Turn."); } var jumpMoves = AvailableMoveService.GetAllJumpMoves(game.board, player); return(new TurnDTO() { board = game.board, moves = jumpMoves.Count != 0 ? jumpMoves : AvailableMoveService.GetMoves(game.board, game.turn) }); }
/// <summary> /// Applies the move to the board. If there are mandatory moves it returns a list containing it. /// If the turn is over, it returns a turnDTO with an empty list of moves /// </summary> /// <param name="gameId"></param> /// <param name="move"></param> /// <returns></returns> public static OutgoingMessage TakeTurn(Guid gameId, ActionDTO action) { //---Apply Move--- Game game = GameRepository.Instance.getGame(gameId); Move move = new Move() { MoveTo = action.moveTo, Piece = game.board.Get(action.moveFrom.Item1, action.moveFrom.Item2) }; bool isKingBefore = move.Piece.IsKing; if (!AvailableMoveService.GetMoves(game.board, game.turn).Contains(move)) { throw new ArgumentOutOfRangeException("Invalid move"); } game.board.ApplyMove(move); //---Check for Game Over--- if (AvailableMoveService.IsGameOver(game.board)) { return(new EndGameDTO() { reason = EndReason.GAME_OVER, winner = AvailableMoveService.GetWinner(game.board) }); } //---Check for forced jump moves--- var jumpMoves = new List <Move>(); var to = action.moveTo; var loc = action.moveFrom; bool isKingAfter = game.board.Get(to.Item1, to.Item2).IsKing; if (Math.Abs(loc.Item1 - to.Item1) == 2 && Math.Abs(loc.Item2 - to.Item2) == 2 && !(isKingBefore != isKingAfter)) { jumpMoves = AvailableMoveService.GetJumpMoves(game.board, game.board.Get(to.Item1, to.Item2)); } if (jumpMoves.Count == 0) { game.turn = (game.turn == Player.BLACK) ? Player.RED : Player.BLACK; } GameRepository.Instance.update(gameId, game); return(new TurnDTO() { board = game.board, moves = jumpMoves }); }