コード例 #1
0
        /// <summary>
        /// Подсветка хода
        /// </summary>
        /// <param name="pos"></param>
        /// <param name="moves"></param>
        /// <param name="attacks"></param>
        /// <returns></returns>
        public bool Hightlight(Position pos, out MyList <Position> moves, out MyList <Position> attacks,
                               out MyList <Position> castlings, out MyList <Position> inmoveattacks)
        {
            moves         = new MyList <Position>();
            attacks       = new MyList <Position>();
            castlings     = new MyList <Position>();
            inmoveattacks = new MyList <Position>();
            MyList <Position> moves0, attacks0, inmoveattacks0;

            if (state != GameState.WaitWhite && state != GameState.WaitBlack)
            {
                return(false);
            }
            Figure fig = Field.GetFigureAt(pos);

            if (fig != null)
            {
                if ((state == GameState.WaitBlack && fig.Side == Side.Black) ||
                    (state == GameState.WaitWhite && fig.Side == Side.White))
                {
                    // highlighting code
                    attacks0 = GetAttacks(fig);
                    moves0   = GetMoves(fig, attacks0);
                    foreach (Position move in attacks0)
                    {
                        if (!Field.IsBadMove(pos, move, fig.Side))
                        {
                            attacks.Add(move);
                        }
                    }
                    foreach (Position move in moves0)
                    {
                        if (!Field.IsBadMove(pos, move, fig.Side))
                        {
                            moves.Add(move);
                        }
                    }
                    if (fig.GetFigureType() == FigureTypes.King)
                    {
                        castlings = (fig as King).GetCastling();
                    }
                    if (fig.GetFigureType() == FigureTypes.Pawn)
                    {
                        inmoveattacks0 = (fig as Pawn).GetInMoveAttacks();
                        foreach (Position move in inmoveattacks0)
                        {
                            if (!Field.IsBadMove(pos, move, fig.Side))
                            {
                                inmoveattacks.Add(move);
                            }
                        }
                    }
                    // Если король под шахом и возможных ходов нет то конец игры FIX IT
                    //if (fig.GetFigureType() == FigureTypes.King & moves.Count == 0 & attacks.Count == 0)
                    //    EndGame(fig);
                    if (state == GameState.WaitBlack)
                    {
                        state = GameState.HighlightedBlack;
                    }
                    if (state == GameState.WaitWhite)
                    {
                        state = GameState.HighlightedWhite;
                    }
                    this.moves           = moves;
                    this.attacks         = attacks;
                    this.castlings       = castlings;
                    this.inmoveattacks   = inmoveattacks;
                    highlightedfigurepos = pos;
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
コード例 #2
0
        public MyList <Position> GetMoves(Figure figure, ChessField cf)
        {
            Position          pos = figure.Position;
            Position          tmppos;
            MyList <Position> l = new MyList <Position>();

            switch (figure.GetFigureType())
            {
            case FigureTypes.Pawn:
            {
                if (figure.Side == Side.Black)
                {
                    pos = figure.Reverse(pos);
                }
                if (pos.GetY() == 2)
                {
                    Position middlepos = new Position(pos.GetX(), pos.GetY() + 1);
                    middlepos = figure.Side == Side.White ? middlepos : figure.Reverse(middlepos);
                    if (cf.GetFigureAt(middlepos) == null)
                    {
                        tmppos = new Position(pos.GetX(), pos.GetY() + 2);
                        l.Add(figure.Side == Side.White ? tmppos : figure.Reverse(tmppos));
                    }
                }

                int y = pos.GetY();
                if (y + 1 < 9)
                {
                    tmppos = new Position(pos.GetX(), y + 1);
                    l.Add(figure.Side == Side.White ? tmppos : figure.Reverse(tmppos));
                }
                break;
            }

            case FigureTypes.King:
            {
                if (pos.GetY() > 1)
                {
                    l.Add(new Position(pos.GetX(), pos.GetY() - 1));
                }
                if (pos.GetY() < 8)
                {
                    l.Add(new Position(pos.GetX(), pos.GetY() + 1));
                }
                break;
            }

            default:     // Bishop, Queen & etc.
            {
                // +
                for (int i = pos.GetY() + 1; i <= 8; i++)
                {
                    l.Add(new Position(pos.GetX(), i));
                    if (cf.GetFigureAt(new Position(pos.GetX(), i)) != null)
                    {
                        break;
                    }
                }
                //--
                for (int i = pos.GetY() - 1; i > 0; i--)
                {
                    l.Add(new Position(pos.GetX(), i));
                    if (cf.GetFigureAt(new Position(pos.GetX(), i)) != null)
                    {
                        break;
                    }
                }
                break;
            }
            }
            return(l);
        }
コード例 #3
0
        public void Cell_Click(Position pos)
        {
            MyList <Position> moves, attacks, castlings, inmoveattacks;
            Figure            fig = Field.GetFigureAt(pos);

            if (!isHighlighted())
            {
                if (Hightlight(pos, out moves, out attacks, out castlings, out inmoveattacks))
                {
                    foreach (Position move in moves)
                    {
                        view.CellMove(move);
                    }
                    view.CellMove(pos);
                    foreach (Position move in attacks)
                    {
                        view.CellAttack(move);
                    }
                    foreach (Position castle in castlings)
                    {
                        view.CellCastling(castle);
                    }
                    foreach (Position attack in inmoveattacks)
                    {
                        view.CellAttack(attack);
                    }
                }
            }
            else
            {
                if (isCorrectMove(pos))
                {
                    view.AddToLog(Field.GetFigureAt(highlightedfigurepos).GetImage() + " " + highlightedfigurepos.ToString() + "-" + pos.ToString());
                    Move(pos);
                    view.DrawField();
                }

                if (isCorrectCastling(pos))
                {
                    view.AddToLog(Field.GetFigureAt(highlightedfigurepos).GetImage() + " Рокировка " + pos.ToString());
                    Castle(pos);
                    view.DrawField();
                }

                if (isCorrectInMoveAttack(pos))
                {
                    view.AddToLog(Field.GetFigureAt(highlightedfigurepos).GetImage() + " " + highlightedfigurepos.ToString() + "-" + pos.ToString());
                    InMoveAttack(pos);
                    view.DrawField();
                }

                // снять выделение
                if (isHighlightedFigure(pos))
                {
                    MyList <Position> needunhighlight = Escape();
                    foreach (Position unhpos in needunhighlight)
                    {
                        view.CellDefault(unhpos);
                    }
                }
            }

            view.SetTurnText();

            view.WhiteCount(player1.GetCount());
            view.BlackCount(player2.GetCount());
        }