Пример #1
0
        internal static int markAlmostWinPiecesYellow()
        {
            Form  form1 = Application.OpenForms[0];
            Piece p = null; int i = 0;

            foreach (Piece piecesMarkItem in piecesMarkYellowNode)
            {
                if (piecesMarkItem != null)
                {
                    p = new YellowPiece(piecesMarkItem.Location.X, piecesMarkItem.Location.Y);
                    //p.BringToFront();//不能先執行此行,因為加入表單控制項Controls.Add是以複製的方式傳入Controls集合
                    form1.Controls.Add(p);
                    form1.Controls[form1.Controls.Count - 1].BringToFront();
                    i++;
                }
            }
            return(i);
        }
Пример #2
0
        private static void checkAlmostWinner(Point lastPlaceNode)
        {
            //count記下從現在下的這子開始看到幾顆相同的棋子,「=1」表示在下的這子
            int   count = 1, countReverse = 0; //反方向檢查是不包括自己,故初始值為0
            int   targetX = 0, targetY = 0;    //檢查目標的節點
            int   pieceNoneCtr   = 0;          //記錄空子有幾個,因為快要贏,空位只能留一個
            Point pointYellowPos = new Point(-1, -1);

            for (int xDir = -1; xDir <= 1; xDir++)
            {
                for (int yDir = -1; yDir <= 1; yDir++)
                {
                    //略過中心點(自己位置)不檢查,要輻射出去八方
                    if (xDir == 0 && yDir == 0)
                    {
                        continue;//若是現在下的這一子則略過後面的程式碼,換下個方向檢查
                    }
                    while (count < 5)
                    {
                        //int centerX = lastPlaceNode.X;
                        //int centerY= lastPlaceNode.Y;

                        targetX = lastPlaceNode.X + xDir * count;
                        targetY = lastPlaceNode.Y + yDir * count;
                        //只會被邊界外與對手棋子給卡死,否則一直延伸5個位置,沒有被障礙到,都有機會贏
                        if (targetX < 0 || targetY < 0 || targetX >= Board.NODE_COUNT_ONESIDE ||
                            targetY >= Board.NODE_COUNT_ONESIDE ||
                            (Board.GetPieceType(targetX, targetY) != PieceType.NONE &
                             Board.GetPieceType(targetX, targetY) != currentPlayer))
                        //如果此方向已沒有棋子或換色時,就調頭去看還有沒有同色棋子
                        {
                            countReverse = 0;
                            while (countReverse + count < 5)
                            {
                                countReverse++;
                                targetX = lastPlaceNode.X + xDir * countReverse * -1;//調頭即*-1,即可反方向
                                targetY = lastPlaceNode.Y + yDir * countReverse * -1;
                                if (targetX < 0 || targetY < 0 || targetX >= Board.NODE_COUNT_ONESIDE ||
                                    targetY >= Board.NODE_COUNT_ONESIDE ||
                                    (Board.GetPieceType(targetX, targetY) != PieceType.NONE &
                                     Board.GetPieceType(targetX, targetY) != currentPlayer))
                                {
                                    //如果反方向已被堵死,就結束此方向的檢查
                                    goto nextdir;//跳出最接近這個break的封閉式迴圈while
                                }
                                else if (Board.GetPieceType(targetX, targetY) == PieceType.NONE)
                                {
                                    pieceNoneCtr++;
                                }
                                else
                                {
                                    pointYellowPos = Board.convertToFormPosition(new Point(targetX, targetY));
                                }
                                piecesMarkYellowNode[targetX, targetY] =
                                    new YellowPiece(new Point(
                                                        pointYellowPos.X + Piece.IMAGE_WIDTH / 2,
                                                        pointYellowPos.Y + Piece.IMAGE_WIDTH / 2));

                                if (countReverse + count == 5)
                                {
                                    if (pieceNoneCtr == 1)
                                    {
                                        almostWinner = currentPlayer;
                                        return;//快要贏的確定了,就不用再找了
                                    }
                                    Array.Clear(piecesMarkYellowNode, 0, piecesMarkYellowNode.Length);
                                    break;
                                }
                            }
                        }
                        else if (Board.GetPieceType(targetX, targetY) == PieceType.NONE)
                        {
                            pieceNoneCtr++;
                        }
                        //要五子連棋,找到同色子就加1
                        count++;
                        pointYellowPos = Board.convertToFormPosition(new Point(targetX, targetY));
                        piecesMarkYellowNode[targetX, targetY] =
                            new YellowPiece(new Point(
                                                pointYellowPos.X + Piece.IMAGE_WIDTH / 2,
                                                pointYellowPos.Y + Piece.IMAGE_WIDTH / 2));
                    }
nextdir:
                    if (count == 5)
                    {
                        if (pieceNoneCtr == 1)
                        {
                            almostWinner = currentPlayer;
                            return;//快要贏的確定了,就不用再找了
                        }
                    }
                    count        = 1;
                    pieceNoneCtr = 0;
                    Array.Clear(piecesMarkYellowNode, 0, piecesMarkYellowNode.Length);
                }
            }

            if (count == 5)
            {
                if (pieceNoneCtr == 1)
                {
                    almostWinner = currentPlayer;
                    return;//快要贏的確定了,就不用再找了
                }
                //return;
            }
            //找到就會中途離開函式,故到此應是沒有
            //if (almostWinner != PieceType.NONE)
            //    almostWinner = PieceType.NONE;
        }