Exemplo n.º 1
0
        private static IEnumerable <Location> MovesInOneDirection(Location from, Board board, Location dir, bool infinit)
        {
            Cell fromCell = 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;
                }
                Cell toCell = board.Get(to);
                if (toCell.Piece == null)
                {
                    yield return(to);
                }
                else
                {
                    if (toCell.Color != fromCell.Color)
                    {
                        yield return(to);
                    }
                    yield break;
                }
            }
        }
Exemplo n.º 2
0
        public string GetWhiteStatus()
        {
            var underCheck = CheckForWhite();
            var canMove    = false;

            foreach (Loc figureLocation in board.Figures(Cell.White))
            {
                foreach (Loc figureMoves in board.Get(figureLocation).Figure.Moves(figureLocation, board))
                {
                    Cell oldDest = board.PerformMove(figureLocation, figureMoves);
                    if (!CheckForWhite( ))
                    {
                        canMove = true;
                    }
                    board.PerformUndoMove(figureLocation, figureMoves, oldDest);
                }
            }
            if (underCheck)
            {
                if (canMove)
                {
                    return("check");
                }
                else
                {
                    return("mate");
                }
            }
            if (canMove)
            {
                return("ok");
            }
            return("stalemate");
        }
Exemplo n.º 3
0
        public string GetWhiteStatus()
        {
            bool check         = checkToWhiteKing();
            bool checkNextMove = true;

            foreach (Loc oldFigureLoc in board.Figures(Cell.White))
            {
                foreach (Loc newFigureLoc in board.Get(oldFigureLoc).Figure.Moves(oldFigureLoc, board))
                {
                    Cell destCell = board.PerformMove(oldFigureLoc, newFigureLoc);
                    if (!checkToWhiteKing())
                    {
                        checkNextMove = false;
                        break;
                    }
                    board.PerformUndoMove(oldFigureLoc, newFigureLoc, destCell);
                }
            }
            if (check)
            {
                return(checkNextMove ? "mate" : "check");
            }
            else if (checkNextMove)
            {
                return("stalemate");
            }
            else
            {
                return("ok");
            }
        }
Exemplo n.º 4
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;
 }
Exemplo n.º 5
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;
				}
			}
		}
Exemplo n.º 6
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);
            }
        }
Exemplo n.º 7
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;
        }
Exemplo n.º 8
0
        // Определяет мат, шах или пат белым.
        public static void SolveTask()
        {
            var isCheck  = IsBad();
            var hasMoves = false;

            foreach (Location locFrom in board.GetPieces(PieceColor.White))
            {
                foreach (Location locTo in board.Get(locFrom).Piece.GetMoves(locFrom, board))
                {
                    var old = board.Get(locTo);
                    board.Set(locTo, board.Get(locFrom));
                    board.Set(locFrom, CellContent.Empty);
                    if (!IsBad())
                    {
                        hasMoves = true;
                    }
                    board.Set(locFrom, board.Get(locTo));
                    board.Set(locTo, old);
                }
            }
            if (isCheck)
            {
                if (hasMoves)
                {
                    Result = "check";
                }
                else
                {
                    Result = "mate";
                }
            }
            else
            if (hasMoves)
            {
                Result = "ok";
            }
            else
            {
                Result = "stalemate";
            }
        }
Exemplo n.º 9
0
 public void Undo()
 {
     board.Set(from, board.Get(to));
     board.Set(to, oldDestinationCell);
 }