private bool IsSquareEmpty(Square square) { return Board[square.X][square.Y] == null; }
private bool PlayerHasCheckerOnSquare(Player player, Square square) { var checker = Board[square.X][square.Y]; return checker != null && (player != null ? checker.Player == player : checker.Player != null); }
public MoveValidation IsValidMove(Move move) { var result = new MoveValidation(); if (!move.From.IsValidSquare() || !move.To.IsValidSquare()) return result; if (!PlayerHasCheckerOnSquare(move.Player, move.From)) return result; if (!IsSquareEmpty(move.To)) return result; // Check if an actual move was sent var deltaX = move.From.X - move.To.X; var deltaY = move.From.Y - move.To.Y; if (deltaX == 0 || deltaY == 0) return result; if (Math.Abs(deltaX) != Math.Abs(deltaY)) return result; if (Math.Abs(deltaX) == 2) { // Attempted a jump var jumpedSquare = new Square((move.From.X + move.To.X)/2, (move.From.Y + move.To.Y)/2); var jumpedChecker = Board[jumpedSquare.X][jumpedSquare.Y]; if (jumpedChecker == null) return result; if (jumpedChecker.Player == move.Player) return result; result.JumpedChecker = jumpedChecker; result.JumpedSquare = jumpedSquare; } else if (Math.Abs(deltaX) != 1) return result; else if (PlayerHasJumpAvailable(move.Player)) return result; var checker = Board[move.From.X][move.From.Y]; if (!checker.IsKinged) { if (move.Player == Player.PlayerOne && deltaY > 0) return result; if (move.Player == Player.PlayerTwo && deltaY < 0) return result; } result.IsValid = true; return result; }