public static bool IsPathFree(ChessGameField.Cell[,] field, Point posFrom, Point posTo) { if (posFrom.X == posTo.X) { int dir = posTo.Y - posFrom.Y > 0 ? 1 : -1; for (int i = posFrom.Y + dir; i != posTo.Y; i += dir) { if (field[posTo.X, i].figure != null) { return false; } } return true; } else if (posFrom.Y == posTo.Y) { int dir = posTo.X - posFrom.X > 0 ? 1 : -1; for (int i = posFrom.X + dir; i != posTo.X; i += dir) { if (field[i, posTo.Y].figure != null) { return false; } } return true; } else { return false; } }
public bool CanMove(ChessGameField.Cell[,] field, Point posFrom, Point posTo, ChessMove lastMove) { Figure targetFig = field[posTo.X, posTo.Y].figure; if (targetFig == null || targetFig.GetOwner() != GetOwner()) { return Rook.IsPathFree(field, posFrom, posTo) || Bishop.IsPathFree(field, posFrom, posTo); } return false; }
public ChessMove Move(ref ChessGameField.Cell[,] field, Point posFrom, Point posTo, ChessMove lastMove) { if (CanMove(field, posFrom, posTo, lastMove)) { ChessMove move = new ChessMove(); if (field[posTo.X, posTo.Y].figure != null) { move.AddAction(new ChessMove.RemoveAction(posTo)); } move.AddAction(new ChessMove.MoveAction(posFrom, posTo)); return move; } return null; }
public bool CanMove(ChessGameField.Cell[,] field, Point posFrom, Point posTo, ChessMove lastMove) { Figure targetFig = field[posTo.X, posTo.Y].figure; if (targetFig == null || targetFig.GetOwner() != GetOwner()) { int difX = Math.Abs(posTo.X - posFrom.X); int difY = Math.Abs(posTo.Y - posFrom.Y); if (difX <= 1 && difY <= 1) { return true; } } return false; }
public static bool IsPathFree(ChessGameField.Cell[,] field, Point posFrom, Point posTo) { if (Math.Abs(posTo.X - posFrom.X) == Math.Abs(posTo.Y - posFrom.Y)) { int dirX = posTo.X - posFrom.X > 0 ? 1 : -1; int dirY = posTo.Y - posFrom.Y > 0 ? 1 : -1; int length = Math.Abs(posTo.X - posFrom.X); for (int i = 1; i < length; ++i) { if (field[posFrom.X + dirX * i, posFrom.Y + dirY * i].figure != null) { return false; } } return true; } else { return false; } }