Esempio n. 1
0
        public pis PlaceAPis(int x, int y, PisType_Enum type)
        {
            //TODO: 找到最近ㄉ節點(node)
            Point nodeID = FindTheClosetNode(x, y);

            //TODO: 如果沒有,回傳FALSE
            if (nodeID == NO_MATCH_NODE)
            {
                return(null); //沒有物件
            }
            //TODO:如果有, 檢查是否已有旗子
            if (pieces[nodeID.X, nodeID.Y] != null)//有棋子
            {
                return(null);
            }
            //根據Type,產生棋子
            Point formPos = convertToFormPosition(nodeID);

            if (type == PisType_Enum.BLACK)
            {
                pieces[nodeID.X, nodeID.Y] = new  Blackpis(formPos.X, formPos.Y);
            }
            else if (type == PisType_Enum.WHITE)
            {
                pieces[nodeID.X, nodeID.Y] = new Whitepis(formPos.X, formPos.Y);
            }

            //紀錄最後下子的位置
            lastPlacedNode = nodeID;

            return(pieces[nodeID.X, nodeID.Y]);
        }
Esempio n. 2
0
        public void CheckWinner()
        {
            int centerX = board.LastPlacedNode.X;
            int centerY = board.LastPlacedNode.Y;

            /*檢查8個不同方向
             * Dir=  -1 -1  往左下找
             * Dir=  -1 0  往左找
             * Dir=  -1 1  往左上找
             * Dir=   1 -1  往右下找
             * Dir=   1  0  往右找
             * Dir=  1  1  往右上找
             * Dir=   0 1  往上找
             * Dir=   0 -1  往下找   */
            for (int xDir = -1; xDir <= 1; xDir++)
            {
                for (int yDir = -1; yDir <= 1; yDir++)
                {
                    //排除中間的情況
                    if (xDir == 0 && yDir == 0) //同時是0
                    {
                        continue;               //跳過底下,繼續迴圈
                    }
                    //看最後一子附近有幾棵相同相連的棋子
                    int count = 1;
                    while (count < 5)
                    {
                        int targetX = centerX + count * xDir;
                        int targetY = centerY + count * yDir;

                        //檢查顏色是否相同
                        if (targetX < 0 || targetX >= board.NODE_COUNT ||
                            targetY < 0 || targetY >= board.NODE_COUNT ||
                            board.GetPisType(targetX, targetY) != currentPlayer_Enum)
                        {
                            break;
                        }


                        count++;
                    }
                    //若看到五子連珠
                    if (count == 5)
                    {
                        winner = currentPlayer_Enum;
                    }
                }
            }
        }
Esempio n. 3
0
        public pis PlaceAPis(int x, int y)   //Method Board 的  PlaceAPis
        {
            //  return board.PlaceAPis(x, y);
            pis piece = board.PlaceAPis(x, y, currentPlayer_Enum);

            if (piece != null)
            {
                //檢查是否現在下棋的人獲勝
                CheckWinner();

                //交換使用者
                if (currentPlayer_Enum == PisType_Enum.BLACK)
                {
                    currentPlayer_Enum = PisType_Enum.WHITE;
                }
                else
                {
                    currentPlayer_Enum = PisType_Enum.BLACK;
                }

                return(piece);
            }
            return(null);
        }
Esempio n. 4
0
 public void RestartGame()
 {
     winner = PisType_Enum.NONE;
     board.CleanBoard();
     currentPlayer_Enum = PisType_Enum.BLACK;
 }