Мы продолжаем написание шахматных алгоритмов. На этом уроке мы начинаем реализацию проверок можно ли пойти с одной клетки на другую.
Дата отправки отчёта:
9 июля 2018 г.
Задание выполнено: за
1 час. 27 мин.
Чему научился:
Тут мы делали проверку на возможность хода для фигур нужного цвета.
Что было сложным:
Есть проблемка: где-то пропустил момент (не вовремя моргнул, видимо), когда мы прописывали изменения в ФЭН после хода. У меня позиция всегда остаётся изначальной.
Вот тот код, который сейчас у меня в классе "Чесс": public class Chess { public string fen { get { return board.fen; } } Board board; Moves moves; public Chess(string fen = "brnqknrb/pppppppp/8/8/8/8/PPPPPPPP/RKBNQBNR w KQkq - 0 1") { board = new Board(fen); moves = new Moves(board); } Chess(Board board) { this.board = board; moves = new Moves(board); } public Chess Move(string move) { FigureMoving fm = new FigureMoving(move); if(!moves.CanMove(fm)) return this; Board nextBoard = board.Move(fm); Chess nextChess = new Chess(nextBoard); //Color color = Color.white; //color = color.FlipColor(); return nextChess; }
public char GetFigureAt(int x, int y) { Square square = new Square(x, y); Figure figure = board.GetFigureAt(square); //square.OnBoard(); return figure == Figure.none? '.': (char)figure; } public IEnumerable<string> YieldValidMoves() { foreach (FigureOnSquare fs in board.YieldMyFiguresOnSquares()) foreach (Square to in Square.YieldBoardSquares()) { FigureMoving fm = new FigureMoving(fs,to); if(moves.CanMove (fm)) yield return fm.ToString(); } } }
А вот класс "Программ": static void Main(string[] args) { Chess chess = new Chess(); while (true) { Console.WriteLine(chess.fen); Print(ChessToAscii(chess)); //foreach (string moves in chess.YieldValidMoves()) //Console.WriteLine(moves); string move = Console.ReadLine(); //if (move == "") break; chess = chess.Move(move); } } static string ChessToAscii(Chess chess) { StringBuilder sb = new StringBuilder(); sb.AppendLine(" +-----------------+"); for (int y = 7; y >= 0; y--) { sb.Append(y + 1); sb.Append(" | "); for (int x = 0; x < 8; x++) sb.Append(chess.GetFigureAt(x, y) + " "); sb.AppendLine("|"); } sb.AppendLine(" +-----------------+"); sb.AppendLine(" A B C D E F G H "); return sb.ToString(); } static void Print(string text) { ConsoleColor old = Console.ForegroundColor; foreach (char x in text) { if (x >= 'a' && x <= 'z') Console.ForegroundColor = ConsoleColor.Red; else if (x >= 'A' && x <= 'Z') Console.ForegroundColor = ConsoleColor.White; else Console.ForegroundColor = ConsoleColor.Yellow; Console.Write(x);
} Console.ForegroundColor = old; } }
Ну и class FigureMoving { public Figure figure { get; private set; } public Square from { get; private set; } public Square to { get; private set; } public Figure promotion { get; private set; }