| | 1 | | using Rudim.Board; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | |
|
| | 5 | | namespace Rudim.Common |
| | 6 | | { |
| 62294219 | 7 | | public class Move(Square source, Square target, MoveType type) : IEquatable<Move> |
| | 8 | | { |
| 201977256 | 9 | | public Square Source { get; init; } = source; |
| 165448635 | 10 | | public Square Target { get; init; } = target; |
| 179745908 | 11 | | public MoveType Type { get; init; } = type; |
| 437730506 | 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 | | { |
| 72588434 | 18 | | return Type.IsCapture; |
| | 19 | | } |
| | 20 | |
|
| | 21 | | public string GetPromotionChar() |
| | 22 | | { |
| 104 | 23 | | return Type.PromotionChar; |
| | 24 | | } |
| | 25 | |
|
| | 26 | | public bool IsPromotion() |
| | 27 | | { |
| 11001630 | 28 | | return Type.Value >= MoveTypes.KnightPromotion.Value && Type.Value <= MoveTypes.QueenPromotionCapture.Value; |
| | 29 | | } |
| | 30 | |
|
| | 31 | | public bool IsCastle() |
| | 32 | | { |
| 11001630 | 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 | | { |
| 25265349 | 70 | | return other != null && |
| 25265349 | 71 | | Source == other.Source && |
| 25265349 | 72 | | Target == other.Target && |
| 25265349 | 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 | | { |
| 135642324 | 83 | | return EqualityComparer<Move>.Default.Equals(left, right); |
| | 84 | | } |
| | 85 | |
|
| | 86 | | public static bool operator !=(Move left, Move right) |
| | 87 | | { |
| 25265356 | 88 | | return !(left == right); |
| | 89 | | } |
| | 90 | | } |
| | 91 | | } |