Example #1
0
        public bool DoMove(Move m)
        {
            Square from    = m.From();
            Square to      = m.To();
            bool   promote = m.Promote();

            int fKind = IsDrop(from) ? sideToMove == Color.BLACK ? (int)from : (int)from + Piece.WhiteBit
                                      : square[(int)from];
            int tKind   = fKind + (promote ? Piece.PromoteBit : 0);
            int capture = square[(int)to];

            square[(int)to] = tKind;

            // 2回書き込まないようにする
            if (m.Capture() == 0)
            {
                m.AddCapture(capture);
            }

            if (IsDrop(from))
            {
                stand[(int)sideToMove][(int)from]--;
            }
            else
            {
                square[(int)from] = Piece.Empty;
                if (capture != Piece.Empty)
                {
                    if (Piece.Abs(capture) != Piece.BK)
                    {
                        if (Piece.Abs(capture) != Piece.BPP)
                        {
                            stand[(int)sideToMove][Piece.Abs(capture)]++;
                        }
                        else
                        {
                            stand[(int)sideToMove][Piece.BP]++;
                        }
                    }
                }

                if (Piece.Abs(fKind) == Piece.BK)
                {
                    kingPos[(int)sideToMove] = to;
                }
            }

            Debug.Assert((Piece.Abs(capture) == Piece.BK) || SquareIs(KingPos(Color.BLACK)) == Piece.BK);
            Debug.Assert((Piece.Abs(capture) == Piece.BK) || SquareIs(KingPos(Color.WHITE)) == Piece.WK);

            // 手番変更
            sideToMove = (sideToMove == Color.BLACK) ? Color.WHITE : Color.BLACK;

            // トライ勝ち
            if (Piece.Abs(fKind) == Piece.BK)
            {
                if (sideToMove == Color.WHITE && Square.SQ_08 < to && to < Square.SQ_12)
                {
                    return(true);
                }
                if (sideToMove == Color.BLACK && Square.SQ_20 < to && to < Square.SQ_24)
                {
                    return(true);
                }
            }

            return((capture == Piece.BK || capture == Piece.WK) ? true : false);
        }
Example #2
0
        public bool IsLegalMove(Move m)
        {
            Square from    = m.From();
            Square to      = m.To();
            bool   promote = m.Promote();

            // out of range
            if (from < Square.SQ_01 ||
                from > Square.SQ_23 ||
                to < Square.SQ_01 ||
                to > Square.SQ_23)
            {
                return(false);
            }

            if (IsDrop(from))
            {
                if (promote)
                {
                    return(false);
                }
                if (square[(int)to] != Piece.Empty)
                {
                    return(false);
                }
                // 駒を持ってない
                if (Stand(sideToMove, (int)from) == 0)
                {
                    return(false);
                }
            }
            else
            {
                int piece = square[(int)from];

                // 同じ場所には移動できない
                if (from == to)
                {
                    return(false);
                }

                // 駒が存在しない、または壁
                if (piece == Piece.Empty ||
                    piece == Piece.Wall)
                {
                    return(false);
                }

                // 動かすのが自分の駒でない
                if ((sideToMove == Color.BLACK && Piece.IsWhite(piece)) ||
                    (sideToMove == Color.WHITE && Piece.IsBlack(piece)))
                {
                    return(false);
                }

                int cap = square[(int)to];

                // 取るのが相手の駒でない
                if (cap != Piece.Empty &&
                    ((sideToMove == Color.WHITE && Piece.IsWhite(cap)) ||
                     (sideToMove == Color.BLACK && Piece.IsBlack(cap))))
                {
                    return(false);
                }

                int inc = to - from;

                // 動けない方向に動いている
                if (!Piece.Inc[piece].Contains(inc))
                {
                    return(false);
                }

                if (promote && !Piece.CanPromote(piece, to))
                {
                    return(false);
                }
            }

            return(true);
        }