public void ValidateMove(IFigure figure, IBoard board, Move move) { var rowDistance = Math.Abs(move.From.Row - move.To.Row); var colDistance = Math.Abs(move.From.Col - move.To.Col); if (rowDistance != colDistance) { throw new InvalidOperationException(BishopInvalidMove); } var from = move.From; var to = move.To; int rowIndex = from.Row; char colIndex = from.Col; var other = figure.Color == ChessColor.White ? ChessColor.Black : ChessColor.White; // top-right while (true) { rowIndex++; colIndex++; if (to.Row == rowIndex && to.Col == colIndex) { var figureAtPosition = board.GetFigureAtPosition(to); if (figureAtPosition != null && figureAtPosition.Color == figure.Color) { throw new InvalidOperationException("There is a figure on the way!"); } else { return; } } var position = Position.FromChessCoordinates(rowIndex, colIndex); var figAtPosition = board.GetFigureAtPosition(position); if (figAtPosition != null) { throw new InvalidOperationException("There is a figure on the way!"); } } }
public override void ValidateMove(IFigure figure, IBoard board, Move move) { var from = move.From; var to = move.To; if ((from.Row - 1 == to.Row && from.Col + 1 == to.Col) || (from.Row - 1 == to.Row && from.Col - 1 == to.Col)) { var otherFigure = board.GetFigureAtPosition(to); if (otherFigure != null) { throw new InvalidOperationException(InvalidMove); } return; } throw new InvalidOperationException(InvalidMove); }
public void ValidateMove(IFigure figure, IBoard board, Move move) { var color = figure.Color; var other = figure.Color == ChessColor.White ? ChessColor.White : ChessColor.Black; var from = move.From; var to = move.To; if (color == ChessColor.White && to.Row < from.Row) { throw new InvalidOperationException(PawnBackwardsErrorMessage); } if (color == ChessColor.Black && to.Row > from.Row) { throw new InvalidOperationException(PawnBackwardsErrorMessage); } if (color == ChessColor.White) { if (from.Row + 1 == to.Row && this.CheckDiagonalMove(from, to)) { if (this.CheckOtherFigureIfValid(board, to, other)) { return; } } } else if (color == ChessColor.Black) { if (from.Row - 1 == to.Row && this.CheckDiagonalMove(from, to)) { if (this.CheckOtherFigureIfValid(board, to, other)) { return; } } } // TODO: remove 2 magic number (const) if (from.Row == 2 && color == ChessColor.White) { if (from.Row + 2 == to.Row && this.CheckOtherFigureIfValid(board, to, other)) { return; } } else if (from.Row == 7 && color == ChessColor.Black) { if (from.Row - 2 == to.Row && this.CheckOtherFigureIfValid(board, to, other)) { return; } } if (from.Row + 1 == to.Row && color == ChessColor.White) { if (this.CheckOtherFigureIfValid(board, to, other)) { return; } } else if (from.Row - 1 == to.Row && color == ChessColor.Black) { if (this.CheckOtherFigureIfValid(board, to, other)) { return; } } throw new InvalidOperationException(PawnInvalidMove); }
public WebInputProvider(Move move) { this.move = move; }
public void GameEngine(string gameId, string userId, string moveFrom, string moveTo) { var game = this.GetGame(gameId); var playerID = userId; var players = this.GetPlayers(); var firstPlayer = players .FirstOrDefault(x => x.Id == game.FirstPlayerId); var secondPlayer = players .FirstOrDefault(x => x.Id == game.SecondPlayerId); var oldFen = game.Board; // if game doesn't exist if (game == null) { return; } if (firstPlayer == null || secondPlayer == null) { return; } // nothing to move if (moveFrom == moveTo) { return; } // check If player is 1 or 2 if (playerID != game.FirstPlayerId && playerID != game.SecondPlayerId) { // return you are not part of this game this.Clients.Group(gameId).move(oldFen); return; } if (playerID == game.FirstPlayerId && game.State != KingSurvivalGameState.TurnKing) { // this is not your turn this.Clients.Group(gameId).move(oldFen); return; } if (playerID == game.SecondPlayerId && game.State != KingSurvivalGameState.TurnPown) { // this is not your turn this.Clients.Group(gameId).move(oldFen); return; } var renderer = new WebRenderer(this.Clients, game, this.data); var furstPlayer = new Player(firstPlayer.UserName, ChessColor.White); var scondPlayer = new Player(secondPlayer.UserName, Chess.Common.ChessColor.White); var kingSurvivalInitilizeStrategy = new KingSurvivalGameWebInitializationStrategy(oldFen, furstPlayer, scondPlayer); var from = new Position(moveFrom[1] - '0', moveFrom[0]); var to = new Position(moveTo[1] - '0', moveTo[0]); var move = new Move(from, to); var inputProvider = new WebInputProvider(move); var movementStrategy = new KingSurvivalMovementStrategy(); var gameEngine = new KingSurvivalEngineWeb(renderer, inputProvider, movementStrategy); gameEngine.Initialize(kingSurvivalInitilizeStrategy); gameEngine.Play(); }