Ejemplo n.º 1
0
		public Move(Board board, Location from, Location to, CellContent oldDestinationCell)
		{
			this.board = board;
			this.from = from;
			this.to = to;
			this.oldDestinationCell = oldDestinationCell;
		}
Ejemplo n.º 2
0
        // Определяет мат, шах или пат белым.
        public string GetStringForWhiteKing(StreamReader reader)
        {
            _board = new Board(reader);

            var isWhiteKingUnderAttack = IsWhiteKingUnderAttack();
            var whiteKingHasMoves = WhiteKingSafeMone();
            return GetStrinForWhiteKingResult(isWhiteKingUnderAttack, whiteKingHasMoves);
        }
Ejemplo n.º 3
0
        public string GetWhiteKingStatus(Board board)
        {
            var whiteKingHasCheck = WhiteKingHasCheck(board);
            var whiteHasSafeMoves = WhiteHasSafeMoves(board);

            if (whiteKingHasCheck)
                return whiteHasSafeMoves ? "check" : "mate";

            return whiteHasSafeMoves ? "ok" : "stalemate";
        }
Ejemplo n.º 4
0
        private bool WhiteHasSafeMoves(Board board)
        {
            foreach (var pieceLocation in board.GetPieces(PieceColor.White))
            {
                if (board.Get(pieceLocation).Piece.GetMoves(pieceLocation, board).Any(moveTo => IsSafeMove(board, pieceLocation, moveTo)))
                {
                    return true;
                }
            }

            return false;
        }
Ejemplo n.º 5
0
 private bool WhiteKingHasCheck(Board board)
 {
     foreach (var location in board.GetPieces(PieceColor.Black))
     {
         var cell = board.Get(location);
         var moves = cell.Piece.GetMoves(location, board);
         if (moves.Any(destination => board.Get(destination).Is(PieceColor.White, Piece.King)))
         {
             return true;
         }
     }
     return false;
 }
Ejemplo n.º 6
0
		private static IEnumerable<Location> MovesInOneDirection(Location from, Board board, Location dir, bool infinit)
		{
			CellContent fromCellContent = board.Get(from);
			for (int i = 1; i < (infinit ? 8 : 2); i++)
			{
				var to = new Location(from.X + dir.X*i, from.Y + dir.Y*i);
				if (!to.InBoard) break;
				CellContent toCellContent = board.Get(to);
				if (toCellContent.Piece == null) yield return to;
				else
				{
					if (toCellContent.Color != fromCellContent.Color) yield return to;
					yield break;
				}
			}
		}
Ejemplo n.º 7
0
        private bool IsSafeMove(Board board, Location pieceLocation, Location moveTo)
        {
            var oldLocation = board.Get(moveTo);

            try
            {
                board.Set(moveTo, board.Get(pieceLocation));
                board.Set(pieceLocation, CellContent.Empty);
                return !WhiteKingHasCheck(board);
            }
            finally
            {
                board.Set(pieceLocation, board.Get(moveTo));
                board.Set(moveTo, oldLocation);
            }
        }
Ejemplo n.º 8
0
		public static void LoadFrom(StreamReader reader)
		{
			board = new Board(reader);
		}
Ejemplo n.º 9
0
 public void Load(StreamReader reader)
 {
     board = new Board(reader);
 }
Ejemplo n.º 10
0
		public IEnumerable<Location> GetMoves(Location location, Board board)
		{
			return ds.SelectMany(d => MovesInOneDirection(location, board, d, infinit));
		}
Ejemplo n.º 11
0
 public Chess(StreamReader reader)
 {
     board = new Board(reader);
 }