Esempio 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;
		}
Esempio n. 2
0
        // Определяет мат, шах или пат белым.
        public string GetStringForWhiteKing(StreamReader reader)
        {
            _board = new Board(reader);

            var isWhiteKingUnderAttack = IsWhiteKingUnderAttack();
            var whiteKingHasMoves = WhiteKingSafeMone();
            return GetStrinForWhiteKingResult(isWhiteKingUnderAttack, whiteKingHasMoves);
        }
Esempio 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";
        }
Esempio 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;
        }
Esempio 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;
 }
Esempio 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;
				}
			}
		}
Esempio 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);
            }
        }
Esempio n. 8
0
		public static void LoadFrom(StreamReader reader)
		{
			board = new Board(reader);
		}
Esempio n. 9
0
 public void Load(StreamReader reader)
 {
     board = new Board(reader);
 }
Esempio n. 10
0
		public IEnumerable<Location> GetMoves(Location location, Board board)
		{
			return ds.SelectMany(d => MovesInOneDirection(location, board, d, infinit));
		}
Esempio n. 11
0
 public Chess(StreamReader reader)
 {
     board = new Board(reader);
 }