Last active
May 5, 2020 00:24
-
-
Save yCatDev/d014249f01ea40c241013582673c0237 to your computer and use it in GitHub Desktop.
Knight chess problem solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = 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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment