Skip to content

Instantly share code, notes, and snippets.

@yCatDev
Last active May 5, 2020 00:24
Show Gist options
  • Save yCatDev/d014249f01ea40c241013582673c0237 to your computer and use it in GitHub Desktop.
Save yCatDev/d014249f01ea40c241013582673c0237 to your computer and use it in GitHub Desktop.

Revisions

  1. yCatDev revised this gist May 5, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Knight.cs
    Original file line number Diff line number Diff line change
    @@ -48,7 +48,7 @@ public bool CanMove(char x, int y)
    //Просчитываем нашу позицию и ту куда нас хотят всунуть
    pp = pos + point;
    //Просчитываем нашу и ту куда мы можем пройти
    pt = pos + to;
    pt = to;

    //Код отрисовки для проверок игнорируем
    /*try
  2. yCatDev renamed this gist May 4, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. yCatDev created this gist May 4, 2020.
    129 changes: 129 additions & 0 deletions Knight
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,129 @@
    using System;

    namespace TSS
    {
    public class Knight
    {
    public char X;
    public int Y;

    /// <summary>
    /// Воспомогательный енам для преобразования
    /// </summary>
    private enum Symbols
    {
    ZERO, a, b, c, d, e, f, g, h
    }

    public Knight(char x = 'a', int y = 1)
    {
    X = x;
    Y = y;

    //Отрисовка поля игнорим
    /*
    for (int i = 0; i < 8; i++)
    {
    for (int j = 0; j < 8; j++)
    {
    Console.Write('.');
    }

    Console.WriteLine();
    }
    */
    }

    public bool CanMove(char x, int y)
    {
    //Выставляем все нужные переменные их странного буквено-цифреного формата в нормальный
    var pos = new Point(GetTrueX(X)-1, Y-1);
    //Console.SetCursorPosition(pos.X,pos.Y);
    //Console.Write('X');
    var to = new Point(GetTrueX(x)-1, y-1);
    //-1 потому что нахер от 1 считать оло
    foreach (var point in PossiblePoints())
    {
    Point pp, pt;
    //Просчитываем нашу позицию и ту куда нас хотят всунуть
    pp = pos + point;
    //Просчитываем нашу и ту куда мы можем пройти
    pt = pos + to;

    //Код отрисовки для проверок игнорируем
    /*try
    {
    Console.SetCursorPosition(pt.X, pt.Y);
    Console.Write('0');
    Console.SetCursorPosition(pp.X, pp.Y);
    Console.Write('+');
    }
    catch
    {
    ;
    }*/
    //Конец кода отрисовки

    //Финальное сравнение
    if (pp==pt)
    return true;
    }

    return false;
    }

    /// <summary>
    /// Уникальный массив для коня, так как он может ходить только по
    /// определенным участкам мы их сразу выделяем
    /// (НЕ РАБОТАЕТ ДЛЯ ДРУГИХ ФИГУР, ТАМ МОТЕМОТИКОЙ ПРОЩЕ)
    /// </summary>
    /// <returns></returns>
    private Point[] PossiblePoints() => new[]
    {
    new Point(2, 1), new Point(2, -1),
    new Point(-2, 1), new Point(-2, -1),
    new Point(1, 2), new Point(1, -2),
    new Point(-1, 2), new Point(-1, -2),
    };

    /// <summary>
    /// Трюк для легкого преобразования буквы в нужныю нам цифру
    /// </summary>
    private static int GetTrueX(char x) => (int) Enum.Parse<Symbols>(x.ToString());

    /// <summary>
    /// Поспомогательный класс для передачи позиций ибо постоянно юзать х у неудобно
    /// </summary>
    public class Point
    {
    public int X;
    public int Y;

    public Point(int x, int y)
    {
    X = x;
    Y = y;
    }

    /// <summary>
    /// Делоем сложение чтобы не писать двухметровые строки
    /// </summary>
    /// <param name="left"></param>
    /// <param name="right"></param>
    /// <returns></returns>
    public static Point operator +(Point left, Point right)
    => new Point(left.X + right.X, left.Y + right.Y);

    /// <summary>
    /// ПроверОчка
    /// </summary>
    /// <param name="left"></param>
    /// <param name="right"></param>
    /// <returns></returns>
    public static bool operator ==(Point left, Point right)
    => left.X == right.X && left.Y == right.Y;

    public static bool operator !=(Point left, Point right) => !(left == right);
    }
    }
    }