Ejemplo n.º 1
0
 public static bool checkWin(List <Chess> chesses)
 {
     foreach (Chess chess in chesses)
     {
         int  x      = chess._point.X;
         int  y      = chess._point.Y;
         int  _color = chess.color;
         bool isWin  = ChessFinishiUtils.checkHorizontal(x, y, _color, chesses);
         if (isWin)
         {
             return(true);
         }
         isWin = ChessFinishiUtils.checkVertical(x, y, _color, chesses);
         if (isWin)
         {
             return(true);
         }
         isWin = ChessFinishiUtils.checkMainDiagonal(x, y, _color, chesses);
         if (isWin)
         {
             return(true);
         }
         isWin = ChessFinishiUtils.checkMinorDiagonal(x, y, _color, chesses);
         if (isWin)
         {
             return(true);
         }
     }
     return(false);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void ChessPanel_MouseClick(object sender, MouseEventArgs e)
        {
            //isGameOver=true,游戏结束,不响应鼠标点击事件,直接返回
            //mWhoseTurn==Message.COLOR_NONE,没有指定轮到谁,那么谁都不响应
            //mWPieces=null当前还没有组队
            //mColor.Equals(mWhoseTurn)==false没有轮到自己
            if (isGameOver || mWhoseTurn == Message.OPPONENT_NONE || !mColor.Equals(mWhoseTurn) || BPieces == null || APieces == null)
            {
                return;  //
            }

            Point point = new Point();
            Chess chess = new Chess(0, 0, 0);

            // 将点击的坐标转换成棋盘交叉点的坐标   0-14
            point.X      = (int)((e.X - mOffset) / mLineLength);
            point.Y      = (int)(e.Y / mLineLength);
            chess._point = point;

            //如果点击的格子里已经有棋子了,就返回
            if (BPieces.Contains(chess) || APieces.Contains(chess))
            {
                return;
            }
            //判断是否点到外面去了
            if (point.X < 0 || point.Y < 0 || point.X >= ChessPanel.MAX_LINE_COUNT || point.Y >= ChessPanel.MAX_LINE_COUNT)
            {
                return;
            }

            //转换成棋子在控件里的位置坐标
            int x, y;

            if (Width > Height)
            {
                x = (int)((point.X + ratioPieceOfLineHeight / 4) * mLineLength) + mOffset;
                y = (int)((point.Y + ratioPieceOfLineHeight / 4) * mLineLength);
            }
            else
            {
                x = (int)((point.X + ratioPieceOfLineHeight / 4) * mLineLength);
                y = (int)((point.Y + ratioPieceOfLineHeight / 4) * mLineLength) - mOffset;
            }
            int width = (int)(mLineLength * ratioPieceOfLineHeight);
            //创建控件的GDI+,准备绘制棋子
            Graphics g = CreateGraphics();
            //待绘制的棋子的位置
            Rectangle rect = new Rectangle(x, y, width, width);

            //画棋子
            if (mColor.Equals(Message.OPPONENT_A))   //规定AB方  a=black
            {
                g.DrawImage(ACurrentColors[0], rect);
                chess._point = point;
                chess.color  = colors.IndexOf(ACurrentColors[0]);

                APieces.Add(chess);
                isGameOver = ChessFinishiUtils.checkWin(APieces);
            }
            else if (mColor.Equals(Message.OPPONENT_B))
            {
                g.DrawImage(BCurrentColors[0], rect);
                chess.color = colors.IndexOf(BCurrentColors[0]);
                BPieces.Add(chess);
                isGameOver = ChessFinishiUtils.checkWin(BPieces);
            }
            //更新当前颜色队列
            int i = 0;

            //Image LastImage = colors[i];
            if (mColor.Equals(Message.OPPONENT_A))   //规定AB方  a=black
            {
                i = rd.Next(3);
                Image LastImage = colors[i];
                if (ACurrentColors.Count > 0)
                {
                    ACurrentColors.RemoveAt(0);
                }
                ACurrentColors.Add(LastImage);
                if (AColorQ.Count > 0)
                {
                    AColorQ.RemoveAt(0);
                }
                AColorQ.Add(i);
            }
            else if (mColor.Equals(Message.OPPONENT_B))
            {
                i = rd.Next(3) + 3;
                Image LastImage = colors[i];
                if (BCurrentColors.Count > 0)
                {
                    BCurrentColors.RemoveAt(0);
                }
                BCurrentColors.Add(LastImage);
                if (BColorQ.Count > 0)
                {
                    BColorQ.RemoveAt(0);
                }
                BColorQ.Add(i);
            }


            //本方点击,向服务器落子消息
            Message m = new Message();

            m.Action    = Message.ID_STATUS_PUT;
            m.WhoseTurn = mWhoseTurn;
            m.Receiver  = FormClient.yourUUID;
            m.Sender    = FormClient.myUUID;
            m.X         = point.X;
            m.Y         = point.Y;
            m.APieces   = APieces;
            m.BPieces   = BPieces;
            m.AColorQ   = AColorQ;
            m.BColorQ   = BColorQ;
            m.Color     = mColor;

            if (isGameOver)
            {
                m.IsGameOver = true;
                m.Winner     = mColor;
                DrawResult(m);
            }
            else
            {
                m.IsGameOver = false;
            }
            try
            {
                mClientSocket.Send(Encoding.UTF8.GetBytes(XmlUtils.XmlSerializer <Message>(m)));

                FormClient.delHelp("等待对方落子");
                FormClient.updataPic();
                FormClient.delStep(1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            //禁止本方点击
            mWhoseTurn = Message.OPPONENT_NONE;
        }