Exemplo n.º 1
0
        ///<summary>
        ///抬起鼠标时判断行棋规则
        /// </summary>
        private void ChessBoard_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (isClick == false)
            {
                return;
            }
            if (selectedPiece != null)
            {
                //得到鼠标抬起网格行列数
                Point p        = getGridIndex(e.GetPosition(ChessBoard).X, e.GetPosition(ChessBoard).Y);
                int   mouseRow = (int)p.X;
                int   mouseCol = (int)p.Y;

                if (CanMove(selectedPiece, mouseRow, mouseCol))
                {
                    ChessPiece target = GetPiece(mouseRow, mouseCol);
                    if (target != null)
                    {
                        if (CanEat(selectedPiece, target))
                        {
                            //级别相同,两个都消失
                            if (selectedPiece.Name == target.Name)
                            {
                                ChessBoard.Children.Remove(target);
                                listChess.Remove(target);
                                ChessBoard.Children.Remove(selectedPiece);
                                listChess.Remove(selectedPiece);

                                Chess chess1 = new Chess();
                                chess1.isEat    = true;
                                chess1.Name     = selectedPiece.Name;
                                chess1.Color    = player;
                                chess1.PieceRow = selectedPiece.PieceRow;
                                chess1.PieceCol = selectedPiece.PieceCol;
                                Chess chess2 = new Chess();
                                chess2.isEat    = true;
                                chess2.Name     = target.Name;
                                chess2.Color    = (player + 1) % 2;
                                chess2.PieceRow = target.PieceRow;
                                chess2.PieceCol = target.PieceCol;

                                client.ChessMove(chess1, chess2, tableIndex);
                            }
                            else
                            {
                                ChessBoard.Children.Remove(target); //清除棋盘
                                listChess.Remove(target);           //清除数据
                                Grid.SetRow(selectedPiece, mouseRow);
                                Grid.SetColumn(selectedPiece, mouseCol);
                                selectedPiece.PieceRow = mouseRow;
                                selectedPiece.PieceCol = mouseCol;

                                Chess chess1 = new Chess();
                                chess1.isEat    = false;
                                chess1.Name     = selectedPiece.Name;
                                chess1.Color    = player;
                                chess1.PieceRow = mouseRow;
                                chess1.PieceCol = mouseCol;
                                Chess chess2 = new Chess();
                                chess2.isEat    = true;
                                chess2.Name     = target.Name;
                                chess2.Color    = (player + 1) % 2;
                                chess2.PieceRow = target.PieceRow;
                                chess2.PieceCol = target.PieceCol;

                                client.ChessMove(chess1, chess2, tableIndex);
                            }
                        }
                    }
                    else
                    {
                        Grid.SetRow(selectedPiece, mouseRow);
                        Grid.SetColumn(selectedPiece, mouseCol);
                        selectedPiece.PieceRow = mouseRow;
                        selectedPiece.PieceCol = mouseCol;

                        Chess chess1 = new Chess();
                        chess1.isEat    = false;
                        chess1.Name     = selectedPiece.Name;
                        chess1.Color    = player;
                        chess1.PieceRow = mouseRow;
                        chess1.PieceCol = mouseCol;
                        Chess chess2 = null;

                        client.ChessMove(chess1, chess2, tableIndex);
                    }
                }
            }
        }
Exemplo n.º 2
0
        ////<summary>
        ///更新棋局信息
        /// </summary>
        public void UpdateChessInfo(Chess selectChess, Chess targetChess)
        {
            if (selectChess != null && targetChess != null)
            {
                string name1 = selectChess.Color == 0 ? "红方" : "黑方";
                string name2 = targetChess.Color == 1 ? "黑方" : "红方";
                if (selectChess.isEat == true && targetChess.isEat == true)
                {
                    AddColorMessage(string.Format("{0}棋牌:{1}与{2}棋牌:{3}相撞!", name1, selectChess.Name, name2, targetChess.Name), Brushes.Red);
                }
                if (selectChess.isEat == false && targetChess.isEat == true)
                {
                    AddColorMessage(string.Format("{0}棋牌:{1} 吃掉 {2}棋牌:{3}!", name1, selectChess.Name, name2, targetChess.Name), Brushes.Red);
                }
                AddColorMessage(string.Format("请{0}走棋", selectChess.Color == 0 ? "黑方" : "红方"), Brushes.Red);
            }
            if (isClick == false)
            {
                ChessBoard.IsEnabled = true;
                isClick           = true;
                btnJump.IsEnabled = true;
            }
            else
            {
                ChessBoard.IsEnabled = false;
                isClick           = false;
                btnJump.IsEnabled = false;
            }
            ////
            if (selectChess != null && selectChess.Color == player)
            {
                foreach (ChessPiece p1 in listChess)
                {
                    p1.BorderBrush = new SolidColorBrush(Colors.YellowGreen);
                }
                foreach (ChessPiece c in listChess)
                {
                    if (c.Name.Equals(selectChess.Name) && c.Color == (selectChess.Color == 0 ? Colors.Red : Colors.Black))
                    {
                        c.isSelected = false;
                    }
                }
                this.selectedPiece = null;

                return;
            }
            ////
            if (selectChess != null)
            {
                //是否被吃
                if (selectChess.isEat == true)
                {
                    ChessPiece piece = GetPiece(selectChess.Name, selectChess.Color);
                    ChessBoard.Children.Remove(piece);
                    listChess.Remove(piece);
                }
                else
                {
                    ChessPiece piece = GetPiece(selectChess.Name, selectChess.Color);
                    Grid.SetRow(piece, selectChess.PieceRow);
                    Grid.SetColumn(piece, selectChess.PieceCol);
                    piece.PieceRow = selectChess.PieceRow;
                    piece.PieceCol = selectChess.PieceCol;
                }
            }
            if (targetChess != null)
            {
                //是否被吃
                if (targetChess.isEat == true)
                {
                    ChessPiece piece = GetPiece(targetChess.Name, targetChess.Color);
                    ChessBoard.Children.Remove(piece);
                    listChess.Remove(piece);
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 鼠标按下
        /// </summary>
        private void chessPiece_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (isClick == false)
            {
                return;
            }
            ChessPiece element = sender as ChessPiece;
            int        color   = element.Color == Colors.Red ? 0 : 1;

            if (element != null)
            {
                //点击一个没有翻开的棋子
                if (element.Status == false)
                {
                    ////如果有已经选中的棋子则点击无效
                    //if (this.selectedPiece != null)
                    //{
                    //    int index = this.selectedPiece.PieceCol * 4 + this.selectedPiece.PieceRow;
                    //    if (((ChessPiece)ChessBoard.Children[index]).isSelected == true)
                    //    {
                    //        return;
                    //    }
                    //}
                    element.Status = true;
                    //发送服务端
                    Chess chess = new Chess();
                    chess.Name     = element.Name;
                    chess.Color    = color;
                    chess.Status   = element.Status;
                    chess.PieceRow = element.PieceRow;
                    chess.PieceCol = element.PieceCol;
                    client.ChessStatus(chess, tableIndex, tableSide);
                }
                //点击一个已经翻开的棋子
                else
                {
                    //只能选择自己颜色的棋子,不能选择对方的棋子
                    if (player == color)
                    {
                        if (element.isSelected == false)
                        {
                            foreach (ChessPiece p in listChess)
                            {
                                p.BorderBrush = new SolidColorBrush(Colors.YellowGreen);
                                p.isSelected  = false;
                            }
                            element.BorderBrush = new SolidColorBrush(Colors.Red);
                            element.Cursor      = Cursors.Hand;
                            element.isSelected  = true;
                            this.selectedPiece  = element;
                        }
                        else
                        {
                            foreach (ChessPiece p in listChess)
                            {
                                p.BorderBrush      = new SolidColorBrush(Colors.YellowGreen);
                                this.selectedPiece = null;
                                element.isSelected = false;
                            }
                        }
                    }
                }
            }
        }