Beispiel #1
0
        protected virtual List <ChessPosition> Vertical(ChessPosition pos_start, ChessPiece[,] Map_Chess)
        {
            List <ChessPosition> list  = new List <ChessPosition>();
            ChessColor           color = Map_Chess[pos_start.X, pos_start.Y].Color;
            ChessEnum            type  = Map_Chess[pos_start.X, pos_start.Y].ChessType;

            if (color == ChessColor.Die)
            {
                return(list);
            }
            for (int index = 0; index < 2; index++)
            {
                int val = maps_vertical_n_horizontal[index];
                while (pos_start.Y + val >= 0 && pos_start.Y + val <= 7)
                {
                    if (Map_Chess[pos_start.X, pos_start.Y + val].Color == color)
                    {
                        break;                                                          // if ally -> can't move
                    }
                    else
                    {
                        list.Add(new ChessPosition()
                        {
                            X = pos_start.X, Y = pos_start.Y + val
                        });
                        if (Map_Chess[pos_start.X, pos_start.Y + val].Color != ChessColor.Die)
                        {
                            break;                                                                    // if enemy -> eat
                        }
                    }
                    val = val + maps_vertical_n_horizontal[index];
                }
            }
            return(list);
        }
Beispiel #2
0
        public static List <ChessPosition> knightmove(ChessPosition pos_start, ChessPiece[,] Map_Chess)
        {
            List <ChessPosition> list  = new List <ChessPosition>();
            ChessColor           color = Map_Chess[pos_start.X, pos_start.Y].Color;

            if (color == ChessColor.Die)
            {
                return(list);
            }
            ChessEnum type = Map_Chess[pos_start.X, pos_start.Y].ChessType;

            for (int val = 0; val < 8; val++)
            {
                int _x = pos_start.X + maps_knight[val, 0];
                int _y = pos_start.Y + maps_knight[val, 1];
                if (_x < 0 | _x > 7 | _y < 0 | _y > 7)
                {
                    continue;                                    //outside
                }
                if (Map_Chess[_x, _y].Color == color)
                {
                    continue;                                   // if ally -> can't move
                }
                else
                {
                    list.Add(new ChessPosition()
                    {
                        X = _x, Y = _y
                    });
                }
            }
            return(list);
        }
Beispiel #3
0
        protected override List <ChessPosition> Vertical(ChessPosition pos_start, ChessPiece[,] Map_Chess)
        {
            List <ChessPosition> list  = new List <ChessPosition>();
            ChessColor           color = Map_Chess[pos_start.X, pos_start.Y].Color;
            ChessEnum            type  = Map_Chess[pos_start.X, pos_start.Y].ChessType;

            if (color == ChessColor.Die)
            {
                return(list);
            }
            for (int index = 0; index < 2; index++)
            {
                int val = maps_vertical_n_horizontal[index];
                while (pos_start.Y + val >= 0 & pos_start.Y + val <= 7)
                {
                    if ((int)color == maps_vertical_n_horizontal[index])
                    {
                        break;                                                   //Pawn can't move behind
                    }
                    if (Map_Chess[pos_start.X, pos_start.Y + val].Color == color)
                    {
                        break;                                                          // if ally -> can't move
                    }
                    else
                    {
                        if (Map_Chess[pos_start.X, pos_start.Y + val].Color != ChessColor.Die)
                        {
                            break;                                                                   // Pawn can't move
                        }
                        list.Add(new ChessPosition()
                        {
                            X = pos_start.X, Y = pos_start.Y + val
                        });
                        if (Map_Chess[pos_start.X, pos_start.Y + val].Color != ChessColor.Die)
                        {
                            break;                                                                    // if enemy -> eat
                        }
                    }
                    if (val == 2 | val == -2)
                    {
                        break;                       //Pawn max 2 step
                    }
                    int line = color == ChessColor.Black ? 1 : 6;
                    if (pos_start.Y != line)
                    {
                        break;
                    }
                    val = val + maps_vertical_n_horizontal[index];
                }
            }
            return(list);
        }
Beispiel #4
0
        private static List <ChessPosition> horizontal(ChessPosition pos_start, ChessPiece[,] Map_Chess)
        {
            List <ChessPosition> list  = new List <ChessPosition>();
            ChessColor           color = Map_Chess[pos_start.X, pos_start.Y].Color;

            if (color == ChessColor.Die)
            {
                return(list);
            }
            ChessEnum type = Map_Chess[pos_start.X, pos_start.Y].ChessType;

            if (type == ChessEnum.Knight | type == ChessEnum.Bishop | type == ChessEnum.Pawn)
            {
                return(list);                                                                             //Knight & Bishop & Pawn can't move horizontal
            }
            for (int index = 0; index < 2; index++)
            {
                int val = VerticalandHorizontal[index];
                while (pos_start.X + val >= 0 & pos_start.X + val <= 7)
                {
                    if (Map_Chess[pos_start.X + val, pos_start.Y].Color == color)
                    {
                        break;                                                          // if ally -> can't move
                    }
                    else
                    {
                        list.Add(new ChessPosition()
                        {
                            X = pos_start.X + val, Y = pos_start.Y
                        });
                        if (Map_Chess[pos_start.X + val, pos_start.Y].Color != ChessColor.Die)
                        {
                            break;                                                                   // if enemy -> eat
                        }
                    }
                    if (type == ChessEnum.King)
                    {
                        break;                          //king max 1 step
                    }
                    val = val + VerticalandHorizontal[index];
                }
            }
            return(list);
        }
Beispiel #5
0
        private PictureBox LoadPic(ChessColor color, ChessEnum chess, Point point, bool CreateEvent = true)
        {
            PictureBox Pic = new PictureBox()
            {
                Width       = ChessEdge,
                Height      = ChessEdge,
                Image       = Image.FromFile(FolderImage + color.ToString() + "\\" + chess.ToString() + ".png"),
                BackColor   = DefaultColor,
                BorderStyle = BorderStyle.FixedSingle,
                Location    = point
            };

            if (CreateEvent)
            {
                Pic.MouseMove += Pic_MouseMove;
                Pic.MouseUp   += Pic_MouseUp;
                Pic.MouseDown += Pic_MouseDown;
            }
            return(Pic);
        }
Beispiel #6
0
        protected virtual List <ChessPosition> Diagonal(ChessPosition pos_start, ChessPiece[,] Map_Chess)
        {
            List <ChessPosition> List  = new List <ChessPosition>();
            ChessColor           color = Map_Chess[pos_start.X, pos_start.Y].Color;

            if (color == ChessColor.Die)
            {
                return(List);
            }
            ChessEnum type = Map_Chess[pos_start.X, pos_start.Y].ChessType;

            for (int val = 0; val < 4; val++)
            {
                int _x = maps_diagonal[val, 0];
                int _y = maps_diagonal[val, 1];
                while (pos_start.X + _x >= 0 && pos_start.X + _x <= 7 && pos_start.Y + _y >= 0 && pos_start.Y + _y <= 7)
                {
                    if (Map_Chess[pos_start.X + _x, pos_start.Y + _y].Color == color)
                    {
                        break;                                                               // if ally -> can't move
                    }
                    else
                    {
                        List.Add(new ChessPosition()
                        {
                            X = pos_start.X + _x, Y = pos_start.Y + _y
                        });
                        if (Map_Chess[pos_start.X + _x, pos_start.Y + _y].Color != ChessColor.Die)
                        {
                            break;                                                                        // if enemy -> eat
                        }
                    }
                    _x = _x + maps_diagonal[val, 0];
                    _y = _y + maps_diagonal[val, 1];
                }
            }
            return(List);
        }
Beispiel #7
0
        protected override List <ChessPosition> Diagonal(ChessPosition pos_start, ChessPiece[,] Map_Chess)
        {
            List <ChessPosition> List  = new List <ChessPosition>();
            ChessColor           color = Map_Chess[pos_start.X, pos_start.Y].Color;

            if (color == ChessColor.Die)
            {
                return(List);
            }
            ChessEnum type = Map_Chess[pos_start.X, pos_start.Y].ChessType;

            for (int val = 0; val < 4; val++)
            {
                int _x = maps_diagonal[val, 0];
                int _y = maps_diagonal[val, 1];
                while (pos_start.X + _x >= 0 && pos_start.X + _x <= 7 & pos_start.Y + _y >= 0 && pos_start.Y + _y <= 7)
                {
                    if ((int)color == maps_diagonal[val, 1])
                    {
                        break;                                       //Pawn can't move behind
                    }
                    if (Map_Chess[pos_start.X + _x, pos_start.Y + _y].Color == color)
                    {
                        break;                                                              // if ally -> can't move
                    }
                    else if (Map_Chess[pos_start.X + _x, pos_start.Y + _y].Color != ChessColor.Die)
                    {
                        List.Add(new ChessPosition()
                        {
                            X = pos_start.X + _x, Y = pos_start.Y + _y
                        });
                    }
                    break;
                }
            }
            return(List);
        }
Beispiel #8
0
        /// <summary>
        /// 接收消息
        /// </summary>
        /// <param name="buffer">接收到的buffer数组</param>
        /// <param name="length">接收到的长度</param>
        private void ReceiveMessage(byte[] buffer, int length)
        {
            //Console.WriteLine("ReceiveMessage 0 :" + buffer[0].ToString());
            lock (Rcvmsg)
            {
                //string str = Encoding.UTF8.GetString(buffer, 0, length);
                //Console.WriteLine("ReceiveMessage 1 :" + str);
                #region 寻找玩家
                if (buffer[0] == (byte)StatusEnum.wait)
                {
                    this.labNow.Text          = "正在寻找玩家。";
                    this.labBlackorWhite.Text = "";
                    DrawPanel.Dispose();
                    try
                    {
                        //TODO :
                        ResetDrawPanel();
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("ReceiveMessage throw : " + e.Message);
                    }

                    this.labBlackorWhite.Text = "";

                    this.btnSend.Enabled   = false;
                    this.DrawPanel.Enabled = false;
                }
                #endregion
                #region 执白子
                else if (buffer[0] == (byte)StatusEnum.getwhite)
                {
                    this.chess = ChessEnum.white;
                    this.labBlackorWhite.Text = "执:白子";
                    this.labNow.Text          = "白子走。";
                    this.btnSend.Enabled      = true;
                    this.DrawPanel.Enabled    = true;
                }
                #endregion
                #region 执黑子
                else if (buffer[0] == (byte)StatusEnum.getblack)
                {
                    this.chess = ChessEnum.black;
                    this.labBlackorWhite.Text = "执:黑子";
                    this.labNow.Text          = "白子走。";
                    this.btnSend.Enabled      = true;
                }
                #endregion
                #region 当前得到白子,该黑子走
                else if (buffer[0] == (byte)StatusEnum.whitechess)
                {
                    DrawCheckerboard(buffer, length);
                    if (chess == ChessEnum.white)
                    {
                        DrawPanel.MouseClick -= new System.Windows.Forms.MouseEventHandler(this.DrawPanel_MouseClick);
                        //this.DrawPanel.Enabled = false;
                    }
                    else
                    {
                        //DrawPanel.MouseClick += new System.Windows.Forms.MouseEventHandler(this.DrawPanel_MouseClick);
                        //this.DrawPanel.Enabled = true;
                    }
                    this.labNow.Text = "黑子走。";
                }
                #endregion
                #region 当前得到黑子,该白子走
                else if (buffer[0] == (byte)StatusEnum.blackchess)
                {
                    DrawCheckerboard(buffer, length);
                    if (chess == ChessEnum.white)
                    {
                        //DrawPanel.MouseClick += new System.Windows.Forms.MouseEventHandler(this.DrawPanel_MouseClick);
                        //this.DrawPanel.Enabled = true;
                    }
                    else
                    {
                        DrawPanel.MouseClick -= new System.Windows.Forms.MouseEventHandler(this.DrawPanel_MouseClick);
                        //this.DrawPanel.Enabled = false;
                    }
                    this.labNow.Text = "白子走。";
                }
                #endregion
                #region 白子胜
                else if (buffer[0] == (byte)StatusEnum.whitewin)
                {
                    DrawCheckerboard(buffer, length);
                    MessageBox.Show("白子胜");
                    Application.Exit();
                }
                #endregion
                #region 黑子胜
                else if (buffer[0] == (byte)StatusEnum.blackwin)
                {
                    DrawCheckerboard(buffer, length);
                    MessageBox.Show("黑子胜");
                    Application.Exit();
                }
                #endregion
                #region 接收普通消息
                else if (buffer[0] == (byte)StatusEnum.message)
                {
                    string ReceiveMsg = Encoding.UTF8.GetString(buffer, 1, length - 1);
                    AddMsg(ReceiveMsg);
                }
                #endregion
                #region 报错了
                else
                {
                    MessageBox.Show("接收文件解析失败,\r\n服务器断开,请重新连接");
                }

                #endregion
            }
        }
Beispiel #9
0
        private static List <ChessPosition> vertical(ChessPosition pos_start, ChessPiece[,] Map_Chess)
        {
            List <ChessPosition> list  = new List <ChessPosition>();
            ChessColor           color = Map_Chess[pos_start.X, pos_start.Y].Color;
            ChessEnum            type  = Map_Chess[pos_start.X, pos_start.Y].ChessType;

            if (color == ChessColor.Die)
            {
                return(list);
            }
            if (type == ChessEnum.Knight | type == ChessEnum.Bishop)
            {
                return(list);                                                    //Knight & Bishop can't move vertical
            }
            for (int index = 0; index < 2; index++)
            {
                int val = VerticalandHorizontal[index];
                while (pos_start.Y + val >= 0 & pos_start.Y + val <= 7)
                {
                    if (type == ChessEnum.Pawn && (int)color == VerticalandHorizontal[index])
                    {
                        break;                                                                       //Pawn can't move behind
                    }
                    if (Map_Chess[pos_start.X, pos_start.Y + val].Color == color)
                    {
                        break;                                                          // if ally -> can't move
                    }
                    else
                    {
                        if (Map_Chess[pos_start.X, pos_start.Y + val].Color != ChessColor.Die & type == ChessEnum.Pawn)
                        {
                            break;                                                                                            // Pawn can't move
                        }
                        list.Add(new ChessPosition()
                        {
                            X = pos_start.X, Y = pos_start.Y + val
                        });
                        if (Map_Chess[pos_start.X, pos_start.Y + val].Color != ChessColor.Die)
                        {
                            break;                                                                    // if enemy -> eat
                        }
                    }
                    if (type == ChessEnum.King)
                    {
                        break;                          //king max 1 step
                    }
                    if (type == ChessEnum.Pawn)
                    {
                        if (val == 2 | val == -2)
                        {
                            break;                      //Pawn max 2 step
                        }
                        int line = color == ChessColor.Black ? 1 : 6;
                        if (pos_start.Y != line)
                        {
                            break;
                        }
                    }
                    val = val + VerticalandHorizontal[index];
                }
            }
            return(list);
        }
Beispiel #10
0
        private static List <ChessPosition> diagonal(ChessPosition pos_start, ChessPiece[,] Map_Chess)
        {
            List <ChessPosition> list  = new List <ChessPosition>();
            ChessColor           color = Map_Chess[pos_start.X, pos_start.Y].Color;

            if (color == ChessColor.Die)
            {
                return(list);
            }
            ChessEnum type = Map_Chess[pos_start.X, pos_start.Y].ChessType;

            if (type == ChessEnum.Knight | type == ChessEnum.Castle)
            {
                return(list);                                                    //Knight & Castle can't move diagonal
            }
            for (int val = 0; val < 4; val++)
            {
                int _x = Diagonal[val, 0];
                int _y = Diagonal[val, 1];
                while (pos_start.X + _x >= 0 & pos_start.X + _x <= 7 & pos_start.Y + _y >= 0 & pos_start.Y + _y <= 7)
                {
                    if (type == ChessEnum.Pawn && (int)color == Diagonal[val, 1])
                    {
                        break;                                                           //Pawn can't move behind
                    }
                    if (Map_Chess[pos_start.X + _x, pos_start.Y + _y].Color == color)
                    {
                        break;                                                              // if ally -> can't move
                    }
                    else
                    {
                        if (type == ChessEnum.Pawn)
                        {
                            if (Map_Chess[pos_start.X + _x, pos_start.Y + _y].Color != ChessColor.Die)
                            {
                                list.Add(new ChessPosition()
                                {
                                    X = pos_start.X + _x, Y = pos_start.Y + _y
                                });
                            }
                        }
                        else
                        {
                            list.Add(new ChessPosition()
                            {
                                X = pos_start.X + _x, Y = pos_start.Y + _y
                            });
                            if (Map_Chess[pos_start.X + _x, pos_start.Y + _y].Color != ChessColor.Die)
                            {
                                break;                                                                        // if enemy -> eat
                            }
                        }
                    }
                    if (type == ChessEnum.King | type == ChessEnum.Pawn)
                    {
                        break;                                                  //King & Pawn max 1 step
                    }
                    _x = _x + Diagonal[val, 0];
                    _y = _y + Diagonal[val, 1];
                }
            }
            return(list);
        }