| | | 1 | | using Rudim.Board; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | |
| | | 5 | | namespace Rudim.Common |
| | | 6 | | { |
| | 62393390 | 7 | | public class Move(Square source, Square target, MoveType type) : IEquatable<Move> |
| | | 8 | | { |
| | 202459061 | 9 | | public Square Source { get; init; } = source; |
| | 165718145 | 10 | | public Square Target { get; init; } = target; |
| | 180024669 | 11 | | public MoveType Type { get; init; } = type; |
| | 438190519 | 12 | | public int Score { get; set; } |
| | | 13 | | |
| | 1 | 14 | | public static readonly Move NoMove = new(Square.NoSquare, Square.NoSquare, MoveTypes.Quiet); |
| | | 15 | | |
| | | 16 | | public bool IsCapture() |
| | | 17 | | { |
| | 72703183 | 18 | | return Type.IsCapture; |
| | | 19 | | } |
| | | 20 | | |
| | | 21 | | public string GetPromotionChar() |
| | | 22 | | { |
| | 104 | 23 | | return Type.PromotionChar; |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | public bool IsPromotion() |
| | | 27 | | { |
| | 11019084 | 28 | | return Type.Value >= MoveTypes.KnightPromotion.Value && Type.Value <= MoveTypes.QueenPromotionCapture.Value; |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | public bool IsCastle() |
| | | 32 | | { |
| | 11019084 | 33 | | return Type == MoveTypes.Castle; |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | public static Move ParseLongAlgebraic(string moveString) |
| | | 37 | | { |
| | 284 | 38 | | Square from = ParseFromString(moveString.Substring(0, 2)); |
| | 284 | 39 | | Square to = ParseFromString(moveString.Substring(2, 2)); |
| | 284 | 40 | | MoveType moveType = moveString.Length == 5 ? ParsePromotionType(moveString[4]) : MoveTypes.Quiet; |
| | | 41 | | |
| | 284 | 42 | | return new Move(from, to, moveType); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | private static MoveType ParsePromotionType(char piece) |
| | | 46 | | { |
| | 1 | 47 | | return Char.ToLower(piece) switch |
| | 1 | 48 | | { |
| | 1 | 49 | | 'q' => MoveTypes.QueenPromotion, |
| | 0 | 50 | | 'r' => MoveTypes.RookPromotion, |
| | 0 | 51 | | 'b' => MoveTypes.BishopPromotion, |
| | 0 | 52 | | 'n' => MoveTypes.KnightPromotion, |
| | 0 | 53 | | _ => throw new InvalidOperationException(), |
| | 1 | 54 | | }; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | private static Square ParseFromString(string squareString) |
| | | 58 | | { |
| | 568 | 59 | | _ = Enum.TryParse(squareString, out Square square); |
| | 568 | 60 | | return square; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | public override bool Equals(object obj) |
| | | 64 | | { |
| | 0 | 65 | | return Equals(obj as Move); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | public bool Equals(Move other) |
| | | 69 | | { |
| | 25386841 | 70 | | return other != null && |
| | 25386841 | 71 | | Source == other.Source && |
| | 25386841 | 72 | | Target == other.Target && |
| | 25386841 | 73 | | EqualityComparer<MoveType>.Default.Equals(Type, other.Type); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | public override int GetHashCode() |
| | | 77 | | { |
| | 0 | 78 | | return HashCode.Combine(Source, Target, Type); |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | public static bool operator ==(Move left, Move right) |
| | | 82 | | { |
| | 135952560 | 83 | | return EqualityComparer<Move>.Default.Equals(left, right); |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | public static bool operator !=(Move left, Move right) |
| | | 87 | | { |
| | 25386848 | 88 | | return !(left == right); |
| | | 89 | | } |
| | | 90 | | } |
| | | 91 | | } |