基本信息
源码名称:C# 联网象棋对战源码
源码大小:0.65M
文件格式:.rar
开发语言:C#
更新时间:2017-12-09
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Drawing.Drawing2D;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace DTTest
{
    public partial class Form1 : Form
    {
        private string localPath = Application.StartupPath;
        private Image deskImg;
        private CHIChess chi;
        private ArrayList chessList;
        private ChiDesk desk;
        private GraphicsPath oldPath;
        private GraphicsPath newPath;
        private bool isMove = false;
        private ArrayList history;
        private int[][] curMap;
        private CHIChess eatedChess;
        private UdpClient client;
        private bool canMove = false;
        public string remoteIP;
        public bool isOwner = true;
        private Abouts bout;

        private IAsyncResult result;
        private ListenMsg lmsg;

        public Form1(string remoteip,bool isOwner)
        {
            InitializeComponent();
            this.remoteIP = remoteip;
            this.isOwner = isOwner;

            deskImg=Image.FromFile(localPath   "\\imgs\\desk.bmp");
            this.deskBox.Size = deskImg.Size;
            this.Size = new Size(deskImg.Width   9, deskImg.Height   60);
            this.MaximumSize = this.Size;
            this.MinimumSize = this.Size;
            desk = new ChiDesk();
            chessList = new ArrayList();
            #region
            //chi = new CHIChess();
            //chi.location = new Point(68,79);
            //chi.ChiType = CHITypes.JU;
            //chi.curImg = this.getCHiChessImg(chi.ChiType);
            #endregion
            curMap = desk.GetMaps(); ;
            this.LoadChesses(curMap);

            client = new UdpClient(new IPEndPoint(Dns.GetHostByName(Dns.GetHostName()).AddressList[0], 1234));

            lmsg = new ListenMsg(this.listenMsg);
            this.result = lmsg.BeginInvoke(null,null);
                        
        }

        private void LoadChesses(int[][] map)
        {
            if (history != null) history= null;
            history = new ArrayList();
            if (chessList != null) chessList.Clear();
            oldPath = null;
            newPath = null;
            for (int row = 0; row < 10; row  )
            {
                for (int col = 0; col < 9; col  )
                {
                    if (map[row][col] != 0)
                    {
                        CHIChess chiche = new CHIChess();
                        chiche.ChiType = desk.GetTypeByID(map[row][col]);
                        chiche.SetIndexs(new Indexs(col,row));
                        chiche.curImg = this.getCHiChessImg(chiche.ChiType);
                        chiche.CreatePath();
                        chessList.Add(chiche);
                    }
                }
            }
            this.canMove = this.isOwner;
            this.deskBox.Invalidate();
        }

        private void deskBox_Paint(object sender,PaintEventArgs e)
        {
            e.Graphics.DrawImageUnscaled(deskImg,new Point(0,0));
            foreach(CHIChess onechess in chessList)
            {
                if (onechess != chi)
                {
                    onechess.CreateSelf(e.Graphics);
                    //e.Graphics.DrawPath(Pens.Red, onechess.chiPath);
                }
            }

            if (newPath != null&&oldPath != null)
            {
                e.Graphics.DrawPath(new Pen(Color.Red, 2), newPath);
                e.Graphics.DrawPath(new Pen(Color.Green,2), oldPath);
            }
            if (chi != null)
            {
                chi.CreateSelf(e.Graphics);
                //e.Graphics.DrawPath(Pens.Red,chi.chiPath);
            }
        }

        public Image getCHiChessImg(CHITypes chitype)
        {
            Image chiImg = null;
            if (isOwner == true)//正方
            {
                switch (chitype)
                {
                    case CHITypes.JIANG: chiImg = Image.FromFile(localPath   "\\imgs\\111.png"); break;
                    case CHITypes.SHI: chiImg = Image.FromFile(localPath   "\\imgs\\222.png"); break;
                    case CHITypes.XIANG: chiImg = Image.FromFile(localPath   "\\imgs\\333.png"); break;
                    case CHITypes.MA: chiImg = Image.FromFile(localPath   "\\imgs\\444.png"); break;
                    case CHITypes.JU: chiImg = Image.FromFile(localPath   "\\imgs\\555.png"); break;
                    case CHITypes.PAO: chiImg = Image.FromFile(localPath   "\\imgs\\666.png"); break;
                    case CHITypes.ZU: chiImg = Image.FromFile(localPath   "\\imgs\\777.png"); break;

                    case CHITypes.JIANG2: chiImg = Image.FromFile(localPath   "\\imgs\\1.png"); break;
                    case CHITypes.SHI2: chiImg = Image.FromFile(localPath   "\\imgs\\2.png"); break;
                    case CHITypes.XIANG2: chiImg = Image.FromFile(localPath   "\\imgs\\3.png"); break;
                    case CHITypes.MA2: chiImg = Image.FromFile(localPath   "\\imgs\\4.png"); break;
                    case CHITypes.JU2: chiImg = Image.FromFile(localPath   "\\imgs\\5.png"); break;
                    case CHITypes.PAO2: chiImg = Image.FromFile(localPath   "\\imgs\\6.png"); break;
                    case CHITypes.ZU2: chiImg = Image.FromFile(localPath   "\\imgs\\7.png"); break;
                }
            }
            else
            {
                switch (chitype)
                {
                    case CHITypes.JIANG: chiImg = Image.FromFile(localPath   "\\imgs\\1.png"); break;
                    case CHITypes.SHI: chiImg = Image.FromFile(localPath   "\\imgs\\2.png"); break;
                    case CHITypes.XIANG: chiImg = Image.FromFile(localPath   "\\imgs\\3.png"); break;
                    case CHITypes.MA: chiImg = Image.FromFile(localPath   "\\imgs\\4.png"); break;
                    case CHITypes.JU: chiImg = Image.FromFile(localPath   "\\imgs\\5.png"); break;
                    case CHITypes.PAO: chiImg = Image.FromFile(localPath   "\\imgs\\6.png"); break;
                    case CHITypes.ZU: chiImg = Image.FromFile(localPath   "\\imgs\\7.png"); break;

                    case CHITypes.JIANG2: chiImg = Image.FromFile(localPath   "\\imgs\\111.png"); break;
                    case CHITypes.SHI2: chiImg = Image.FromFile(localPath   "\\imgs\\222.png"); break;
                    case CHITypes.XIANG2: chiImg = Image.FromFile(localPath   "\\imgs\\333.png"); break;
                    case CHITypes.MA2: chiImg = Image.FromFile(localPath   "\\imgs\\444.png"); break;
                    case CHITypes.JU2: chiImg = Image.FromFile(localPath   "\\imgs\\555.png"); break;
                    case CHITypes.PAO2: chiImg = Image.FromFile(localPath   "\\imgs\\666.png"); break;
                    case CHITypes.ZU2: chiImg = Image.FromFile(localPath   "\\imgs\\777.png"); break;
                }
            }
            return chiImg;
        }


        private void deskBox_MouseDown(object sender,MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point curpoint = e.Location;
                findSel fs = new findSel(this.findSel);
                fs.Invoke(e.Location);
                this.deskBox.Invalidate();
            }
        }

        private void findSel(Point e)
        {
            bool isfind = false;
            foreach (CHIChess onechess in chessList)
            {
                if (onechess.chiPath.IsVisible(e)&&Convert.ToInt16(onechess.ChiType)>=8&&canMove==true)
                {
                    chi = onechess;
                    isfind = true;
                    break;
                }
            }
        }

        private void deskBox_MouseMove(object sender,MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left&&chi!=null)
            {
                chi.location = e.Location;
                isMove = true;
                this.deskBox.Invalidate();
            }
        }

        private void deskBox_MouseUp(object sender,MouseEventArgs e)
        {
            this.MoveChess(e.Location);
        }

        private void MoveChess(Point p)
        {
            if (chi != null)
            {
                bool isnew = this.findNewPath(p,curMap);
                chi.CreatePath();
                if (isnew == true)
                {
                    newPath = (GraphicsPath)chi.chiPath.Clone();
                    this.SendMoveInfo();
                }
                chi = null;
                this.deskBox.Invalidate();
            }
        }

        private bool findNewPath(Point p,int[][] map)
        {
            Indexs oldIndex = chi.MyIndex;
            bool findnew = false;
            for (int row = 0; row < 10; row  )
            {
                for (int col = 0; col < 9; col  )
                {
                    //if(map[row][col]==0)
                    //{
                      GraphicsPath gpath = new GraphicsPath();
                      if (row < 5)
                      {
                          gpath.AddRectangle(new Rectangle(68   col* 39-20,38 row* 39-20,40,40));
                      }
                      else
                      {
                          gpath.AddRectangle(new Rectangle(68  col* 39-20,43 row* 39-20,40,40));
                      }
                      if (gpath.IsVisible(chi.location))
                      {
                          if (checkCanPass(chi.MyIndex, new Indexs(col, row), chi.ChiType, curMap))//能通过
                          {
                              if (chi.ChiType != CHITypes.PAO2 && chi.ChiType != CHITypes.PAO)//吃子
                              {
                                  if(map[row][col]!=0&&!EatChess(new Indexs(col,row)))
                                  {
                                      break;
                                  }
                              }
                              CHIChess chiclone1=chi.cloneSelf();
                              HistoryRecorder recoder = new HistoryRecorder(chiclone1,chiclone1.MyIndex, new Indexs(col, row));
                              if (eatedChess != null)
                              {
                                  recoder.eatCHI = this.eatedChess.cloneSelf();
                              }
                              eatedChess = null;
                              history.Add(recoder);

                              oldPath =(GraphicsPath)chi.chiPath.Clone();
                              map[chi.MyIndex.Y][chi.MyIndex.X] = 0;
                              chi.SetIndexs(new Indexs(col, row));
                              map[row][col] = desk.GetIDByType(chi.ChiType);
                              
                              findnew = true;
                          }
                          break;
                      }
                    //}
                }
            }
            if (!findnew) chi.SetIndexs(oldIndex);
            return findnew;
        }

        private bool checkCanPass(Indexs oldIndex, Indexs newIndexs,CHITypes types,int[][] map)
        {
            bool passed = true;
            //判断两边的将是否碰面了
            if (oldIndex.X >= 3 && oldIndex.X <= 5)
            {
                bool haslaotou = false;
                Indexs laotou1 = new Indexs(-1, -1);
                Indexs laotou2 = new Indexs(-1, -1);
                for (int y = 0; y <= 2; y  )
                {
                    if (map[y][oldIndex.X] == 5)
                    {
                        laotou1.X = oldIndex.X;
                        laotou1.Y = y;
                        break;
                    }
                }
                for (int y = 7; y <= 9; y  )
                {
                    if (map[y][oldIndex.X] == 12)
                    {
                        laotou2.X = oldIndex.X;
                        laotou2.Y = y;
                    }
                }
                if (laotou1.Y != -1 && laotou2.Y != -1 && laotou1.X==laotou2.X)//两个老头在同一条线上面
                {
                    int zicount = 0;
                    if (laotou2.Y > laotou1.Y)
                    {
                        for (int y = laotou1.Y 1; y <laotou2.Y; y  )
                        {
                            if ((map[y][oldIndex.X] != 0 && map[y][oldIndex.X] != map[oldIndex.Y][oldIndex.X]) 
                                ||oldIndex.X==newIndexs.X)//找到了隔点
                            {
                                zicount  ;
                                break;
                            }
                        }
                    }
                    

                    if (zicount<=0)
                    {
                        return false;
                    }
                }
            }

            if (types == CHITypes.JU || types == CHITypes.JU2)
            {
                if (newIndexs.X == oldIndex.X&&newIndexs.Y!=oldIndex.Y)//纵向移动
                {
                    if (oldIndex.Y < newIndexs.Y)
                    {
                        for (int y = oldIndex.Y 1;y<newIndexs.Y; y  )
                        {
                            if (map[y][oldIndex.X] != 0)//路径中间有障碍物
                            {
                                passed = false;
                                break;
                            }
                        }
                    }
                    else
                    {
                        for (int y=newIndexs.Y   1; y <oldIndex.Y; y  )
                        {
                            if (map[y][oldIndex.X] != 0)//路径中间有障碍物
                            {
                                passed = false;
                                break;
                            }
                        }
                    }
                    return passed;
                }
                else if (newIndexs.Y == oldIndex.Y&&newIndexs.X!=oldIndex.X)//横向移动
                {
                    if (oldIndex.X<newIndexs.X)
                    {
                        for (int x = oldIndex.X   1; x < newIndexs.X; x  )
                        {
                            if (map[oldIndex.Y][x] != 0)
                            {
                                passed = false;
                                break;
                            }
                        }
                    }
                    else
                    {
                        for (int x = newIndexs.X   1; x < oldIndex.X; x  )
                        {
                            if (map[oldIndex.Y][x] != 0)
                            {
                                passed = false;
                                break;
                            }
                        }
                    }
                }
                else
                {
                    return false;
                }
            }
            if (types == CHITypes.MA || types == CHITypes.MA2)
            {
                passed = false;
                if (newIndexs.X == oldIndex.X - 1 && newIndexs.Y == oldIndex.Y - 2&&map[oldIndex.Y-1][oldIndex.X]==0)
                {
                    return true;
                }
                if (newIndexs.X == oldIndex.X   1 && newIndexs.Y == oldIndex.Y - 2 && map[oldIndex.Y - 1][oldIndex.X] == 0)
                {
                    return true;
                }
                if (newIndexs.X == oldIndex.X - 1 && newIndexs.Y == oldIndex.Y   2 && map[oldIndex.Y 1][oldIndex.X] == 0)
                {
                    return true;
                }
                if (newIndexs.X == oldIndex.X   1 && newIndexs.Y==oldIndex.Y   2 && map[oldIndex.Y 1][oldIndex.X] == 0)
                {
                    return true;
                }
                if (newIndexs.X == oldIndex.X - 2 && newIndexs.Y == oldIndex.Y - 1 && map[oldIndex.Y][oldIndex.X - 1] == 0)
                {
                    return true;
                }
                if (newIndexs.X == oldIndex.X - 2 && newIndexs.Y == oldIndex.Y   1 && map[oldIndex.Y][oldIndex.X - 1] == 0)
                {
                    return true;
                }
                if (newIndexs.X == oldIndex.X 2 && newIndexs.Y==oldIndex.Y - 1 && map[oldIndex.Y][oldIndex.X  1] == 0)
                {
                    return true;
                }
                if (newIndexs.X == oldIndex.X 2 && newIndexs.Y == oldIndex.Y   1 && map[oldIndex.Y][oldIndex.X 1] == 0)
                {
                    return true;
                }
                return passed;
            }
            if (types == CHITypes.XIANG || types == CHITypes.XIANG2)
            {
                passed = false;
                if (types == CHITypes.XIANG&&newIndexs.Y<=4)
                {
                    if (newIndexs.X == oldIndex.X - 2 && newIndexs.Y == oldIndex.Y - 2 && map[oldIndex.Y - 1][oldIndex.X - 1] == 0)
                    {
                        return true;
                    }
                    if (newIndexs.X == oldIndex.X   2 && newIndexs.Y == oldIndex.Y - 2 && map[oldIndex.Y - 1][oldIndex.X   1] == 0)
                    {
                        return true;
                    }
                    if (newIndexs.X == oldIndex.X - 2 && newIndexs.Y == oldIndex.Y   2 && map[oldIndex.Y   1][oldIndex.X - 1] == 0)
                    {
                        return true;
                    }
                    if (newIndexs.X == oldIndex.X   2 && newIndexs.Y == oldIndex.Y   2 && map[oldIndex.Y   1][oldIndex.X   1] == 0)
                    {
                        return true;
                    }
                    return passed;
                }
                if(types==CHITypes.XIANG2&&newIndexs.Y>=5)
                {
                    if (newIndexs.X == oldIndex.X - 2 && newIndexs.Y == oldIndex.Y - 2 && map[oldIndex.Y - 1][oldIndex.X - 1] == 0)
                    {
                        return true;
                    }
                    if (newIndexs.X == oldIndex.X   2 && newIndexs.Y == oldIndex.Y - 2 && map[oldIndex.Y - 1][oldIndex.X   1] == 0)
                    {
                        return true;
                    }
                    if (newIndexs.X == oldIndex.X - 2 && newIndexs.Y == oldIndex.Y   2 && map[oldIndex.Y   1][oldIndex.X - 1] == 0)
                    {
                        return true;
                    }
                    if (newIndexs.X == oldIndex.X   2 && newIndexs.Y == oldIndex.Y   2 && map[oldIndex.Y   1][oldIndex.X   1] == 0)
                    {
                        return true;
                    }
                }
                return passed;
            }
            if (types == CHITypes.SHI || types == CHITypes.SHI2)
            {
                passed = false;
                if (Math.Abs(newIndexs.X-oldIndex.X)==1&&Math.Abs(newIndexs.Y-oldIndex.Y)==1)
                {
                    if (newIndexs.X >= 3 && newIndexs.X <= 5)
                    {
                        if((newIndexs.Y>=0&&newIndexs.Y<=2)||(newIndexs.Y>=7&&newIndexs.Y<=9))
                        {
                          return true;
                        }
                    }
                }
                return passed;
            }
            if (types == CHITypes.JIANG || types == CHITypes.JIANG2)
            {
                passed = false;
                if ((Math.Abs(newIndexs.X - oldIndex.X) == 1 && newIndexs.Y== oldIndex.Y)||
                    Math.Abs(newIndexs.Y - oldIndex.Y) == 1 && newIndexs.X == oldIndex.X)
                {
                    if (newIndexs.X >= 3 && newIndexs.X <= 5)
                    {
                        if ((newIndexs.Y >= 0 && newIndexs.Y <= 2) || (newIndexs.Y >= 7 && newIndexs.Y <= 9))
                        {
                            return true;
                        }
                    }
                }
                return passed;
            }
            if (types == CHITypes.PAO || types == CHITypes.PAO2)
            {
                int chessCount = 0;
                if (newIndexs.X == oldIndex.X && newIndexs.Y != oldIndex.Y)//纵向移动
                {
                    if (oldIndex.Y < newIndexs.Y)
                    {
                        for (int y = oldIndex.Y   1; y < newIndexs.Y; y  )
                        {
                            if (map[y][oldIndex.X] != 0)//路径中间有障碍物
                            {
                                chessCount  ;
                            }
                        }
                    }
                    else
                    {
                        for (int y = newIndexs.Y   1; y < oldIndex.Y; y  )
                        {
                            if (map[y][oldIndex.X] != 0)//路径中间有障碍物
                            {
                                chessCount  ;
                            }
                        }
                    }
                    if (chessCount == 0 && map[newIndexs.Y][newIndexs.X] == 0)//空走
                    {
                        return true;
                    }
                    if (chessCount == 1 && map[newIndexs.Y][newIndexs.X] != 0)//吃子走
                    {
                        if (EatChess(newIndexs))
                        {
                            return true;
                        }
                    }
                    return false;
                }
                else if (newIndexs.Y == oldIndex.Y && newIndexs.X != oldIndex.X)//横向移动
                {
                    if (oldIndex.X < newIndexs.X)
                    {
                        for (int x = oldIndex.X   1; x < newIndexs.X; x  )
                        {
                            if (map[oldIndex.Y][x] != 0)
                            {
                                chessCount  ;
                            }
                        }
                    }
                    else
                    {
                        for (int x = newIndexs.X   1; x < oldIndex.X; x  )
                        {
                            if (map[oldIndex.Y][x] != 0)
                            {
                                chessCount  ;
                            }
                        }
                    }

                    if (chessCount == 0&&map[newIndexs.Y][newIndexs.X]==0)//空走
                    {
                        return true;
                    }
                    if (chessCount == 1 && map[newIndexs.Y][newIndexs.X] != 0)//吃子走
                    {
                        if (EatChess(newIndexs))
                        {
                            return true;
                        }
                    }
                    return false;
                }
                else
                {
                    return false;
                }
            }
            if (types == CHITypes.ZU || types == CHITypes.ZU2)
            {
                passed = false;
                if (types == CHITypes.ZU)
                {
                    if (oldIndex.Y <= 4)
                    {
                        if (newIndexs.Y - oldIndex.Y ==1&&newIndexs.X==oldIndex.X)
                        {
                            return true;
                        }
                    }
                    else
                    {
                        if (!(newIndexs.X != oldIndex.X && newIndexs.Y != oldIndex.Y))
                        {
                            if(newIndexs.Y - oldIndex.Y==1)
                            {
                              return true;
                            }
                            if(Math.Abs(newIndexs.X-oldIndex.X)==1)
                            {
                                return true;
                            }
                        }
                    }
                    return passed;
                }
                if (types == CHITypes.ZU2)
                {
                    if (oldIndex.Y>=5)
                    {
                        if (newIndexs.Y - oldIndex.Y ==-1 && newIndexs.X == oldIndex.X)
                        {
                            return true;
                        }
                    }
                    else
                    {
                        if (!(newIndexs.X != oldIndex.X && newIndexs.Y != oldIndex.Y))
                        {
                            if (newIndexs.Y - oldIndex.Y == -1)
                            {
                                return true;
                            }
                            if(Math.Abs(newIndexs.X - oldIndex.X) == 1)
                            {
                                return true;
                            }
                        }
                    }
                }
                return passed;
            }
             return passed;
        }

        private bool EatChess(Indexs newchessIndex)
        {
            bool eatOk = false;
            object[] objs = chessList.ToArray();
            for (int i = 0; i < objs.Length; i  )
            {
                CHIChess onechess = (CHIChess)objs[i];
                if (onechess.MyIndex.X == newchessIndex.X && onechess.MyIndex.Y == newchessIndex.Y && onechess != chi)//找到了吃的子
                {
                    if ((chi.ChiType.ToString().IndexOf("2")!=-1&&onechess.ChiType.ToString().IndexOf("2")==-1)||
                        (chi.ChiType.ToString().IndexOf("2")==-1 && onechess.ChiType.ToString().IndexOf("2")!=-1))//只能吃对方的子
                    {
                        eatedChess = onechess.cloneSelf();
                        chessList.Remove(onechess);
                        eatOk = true;
                    }
                    break;
                }
            }
            return eatOk;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //if (history != null && history.Count > 0)
            //{
            //    UdpClient uclient = new UdpClient();
            //    uclient.Connect(IPAddress.Parse(this.textBox1.Text),2345);
            //    HistoryRecorder hisrecoder = (HistoryRecorder)history.ToArray()[history.Count - 1];
            //    byte[] bts = null;
            //    if (hisrecoder.eatCHI != null)
            //    {
            //        bts = Encoding.UTF8.GetBytes(desk.GetIDByType(hisrecoder.hisCHI.ChiType)
            //              "|"   hisrecoder.fromIndex.X   ","   hisrecoder.fromIndex.Y   "|"  
            //            hisrecoder.toIndex.X   ","   hisrecoder.toIndex.Y   "|"   desk.GetIDByType(hisrecoder.eatCHI.ChiType));
            //    }
            //    else
            //    {
            //        bts = Encoding.UTF8.GetBytes(desk.GetIDByType(hisrecoder.hisCHI.ChiType)
            //          "|"   hisrecoder.fromIndex.X   ","   hisrecoder.fromIndex.Y   "|"  
            //        hisrecoder.toIndex.X   ","   hisrecoder.toIndex.Y   "|0");
            //    }
            //    uclient.Send(bts, bts.Length);
            //}
        }

        private void SendMsg(byte[] msg)
        {
            UdpClient uclient = new UdpClient();
            uclient.Connect(IPAddress.Parse(remoteIP), 1234);
            uclient.Send(msg,msg.Length);
        }
        private void SendMoveInfo()
        {
            if (history != null && history.Count > 0)
            {
                HistoryRecorder hisrecoder = (HistoryRecorder)history.ToArray()[history.Count - 1];
                byte[] bts = null;
                if (hisrecoder.eatCHI != null)
                {
                    bts = Encoding.UTF8.GetBytes(desk.GetIDByType(hisrecoder.hisCHI.ChiType)
                          "|"   hisrecoder.fromIndex.X   ","   hisrecoder.fromIndex.Y   "|"  
                        hisrecoder.toIndex.X   ","   hisrecoder.toIndex.Y   "|"   desk.GetIDByType(hisrecoder.eatCHI.ChiType));
                }
                else
                {
                    bts = Encoding.UTF8.GetBytes(desk.GetIDByType(hisrecoder.hisCHI.ChiType)
                      "|"   hisrecoder.fromIndex.X   ","   hisrecoder.fromIndex.Y   "|"  
                    hisrecoder.toIndex.X   ","   hisrecoder.toIndex.Y   "|0");
                }
                this.canMove = false;
                this.SendMsg(bts);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
        }

        private void listenMsg()
        {
            while (true)
            {
                IPEndPoint remote = new IPEndPoint(Dns.GetHostByName(Dns.GetHostName()).AddressList[0], 1234);
                byte[] bts = this.client.Receive(ref remote);

                if (bts.Length >=10&&Encoding.UTF8.GetString(bts).Contains("|"))
                {
                    TransferObject msgobj = DecodeMsg(Encoding.UTF8.GetString(bts));

                    foreach (CHIChess ochess in chessList)
                    {
                        if (ochess.MyIndex.X == msgobj.fromIndex.X &&
                            ochess.MyIndex.Y == msgobj.fromIndex.Y &&
                            ochess.ChiType == desk.GetTypeByID(msgobj.hischiID))
                        {
                            chi = ochess; break;
                        }
                    }
                    if (chi != null)
                    {
                        chi.SetIndexs(msgobj.fromIndex);

                        if (msgobj.toIndexs.Y < 5)
                        {
                            chi.location = new Point(68   msgobj.toIndexs.X * 39, 38   msgobj.toIndexs.Y * 39);
                        }
                        else
                        {
                            chi.location = new Point(68   msgobj.toIndexs.X * 39, 43   msgobj.toIndexs.Y * 39);
                        }
                        this.MoveChess(chi.location);
                        this.canMove = true;
                    }
                    continue;
                }

                if (Encoding.UTF8.GetString(bts).Trim() == "hui".Trim())
                {
                    this.deskBox.Enabled = false;
                    if (MessageBox.Show("对方要求悔棋,你是否同意?", "悔棋",
                        MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        ToHistory();
                        this.SendMsg(Encoding.UTF8.GetBytes("huiyes"));//同意了
                    }
                    else
                    {
                        this.SendMsg(Encoding.UTF8.GetBytes("huino"));//不同意
                    }
                    this.deskBox.Enabled = true;
                    continue;
                }

                if (Encoding.UTF8.GetString(bts).Trim() == "new".Trim())
                {
                    this.deskBox.Enabled = false;
                    if (MessageBox.Show("对方要求重新开始,你是否同意?", "重新开始",
                        MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        this.SendMsg(Encoding.UTF8.GetBytes("newyes".Trim()));//同意重新开始
                        curMap = desk.GetMaps();
                        this.LoadChesses(curMap);
                    }
                    else
                    {
                        this.SendMsg(Encoding.UTF8.GetBytes("newno".Trim()));//不同意重新开始
                    }
                    this.deskBox.Enabled = true;
                    continue;
                }

                if (Encoding.UTF8.GetString(bts).Trim() == "newyes".Trim())//对方同意了重新开始
                {
                    curMap = desk.GetMaps();
                    this.LoadChesses(curMap);
                    continue;
                }
                if (Encoding.UTF8.GetString(bts).Trim() == "newno".Trim())//对方不同意重新开始
                {
                    this.deskBox.Enabled = false;
                    MessageBox.Show("对方不同意重新开始!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.deskBox.Enabled = true;
                    continue;
                }

                if (Encoding.UTF8.GetString(bts).Trim() == "renshu".Trim())//对方已经认输了, 重新开始
                {
                    this.deskBox.Enabled = false;
                    MessageBox.Show("恭喜你,对方已经认输了!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    curMap = desk.GetMaps();
                    this.LoadChesses(curMap);
                    this.deskBox.Enabled = true;
                    continue;
                }

                if (Encoding.UTF8.GetString(bts).Trim() == "qiuhe".Trim())//对方求和了
                {
                    this.deskBox.Enabled = false;
                    if (MessageBox.Show("对方求和,你是否同意?", "求和",
                     MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)//同意求和,重新开始
                    {
                        curMap = desk.GetMaps();
                        this.LoadChesses(curMap);
                        this.SendMsg(Encoding.UTF8.GetBytes("qiuheyes".Trim()));//同意求和
                    }
                    else
                    {
                        this.SendMsg(Encoding.UTF8.GetBytes("qiuheno".Trim()));//不同意求和
                    }
                    this.deskBox.Enabled = true;
                    continue;
                }

                if (Encoding.UTF8.GetString(bts).Trim() == "qiuheyes".Trim())//对方同意了你的求和,重新开始
                {
                    curMap = desk.GetMaps();
                    this.LoadChesses(curMap);
                    continue;
                }

                if (Encoding.UTF8.GetString(bts).Trim() == "qiuheno".Trim())//对方同意了你的求和,重新开始
                {
                    this.deskBox.Enabled = false;
                    MessageBox.Show("对方不同意你的求和!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.deskBox.Enabled = true;
                    continue;
                }

                if (Encoding.UTF8.GetString(bts).Trim() == "huiyes".Trim())
                {
                    ToHistory();
                    //this.deskBox.Enabled = true;
                    continue;
                }
                if (Encoding.UTF8.GetString(bts).Trim() == "huino".Trim())
                {
                    this.deskBox.Enabled = false;
                    MessageBox.Show("对方不同意你的悔棋!","信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    this.deskBox.Enabled = true;
                    continue;
                }

                
                if (Encoding.UTF8.GetString(bts).Trim() == "fclose".Trim())
                {
                    this.deskBox.Enabled = false;
                    this.Close();
                    MessageBox.Show("对方已经下线!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);                    
                    continue;
                }

            }
        }

        private TransferObject DecodeMsg(string msgstr)
        {
            string[] str = msgstr.Split(new char[] { '|' });
            TransferObject tobj = new TransferObject();
            if (str.Length > 0)
            {
                tobj.hischiID = Math.Abs(Convert.ToInt16(str[0]) - 7);
                string[] fromStr = str[1].Split(new char[] { ',' });
                tobj.fromIndex.X = Convert.ToInt16(fromStr[0]);
                tobj.fromIndex.Y = Math.Abs(9 - Convert.ToInt16(fromStr[1]));
                string[] toStr = str[2].Split(new char[] { ',' });
                tobj.toIndexs.X = Convert.ToInt16(toStr[0]);
                tobj.toIndexs.Y = Math.Abs(9 - Convert.ToInt16(toStr[1]));
                tobj.eatchiID = Math.Abs(Convert.ToInt16(str[3]) - 7);
            }
            return tobj;
        }

        private void lastToolStripMenuItem_Click(object sender, EventArgs e)//还原历史记录
        {
            if (history != null && history.Count > 0)
            {
                this.SendMsg(Encoding.UTF8.GetBytes("hui"));//发送悔棋的信息
                this.deskBox.Enabled = false;
            }
            else
            {
                MessageBox.Show("没有棋可悔!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void ToHistory()
        {
            if (history != null && history.Count > 0)
            {
                bool ishisOk = false;
                object[] hisobjs = history.ToArray();
                HistoryRecorder hisrecorder = (HistoryRecorder)hisobjs[hisobjs.Length - 1];//查找链表最后一条记录

                object[] objs = chessList.ToArray();
                for (int i = 0; i < objs.Length; i  )
                {
                    CHIChess chichess = (CHIChess)objs[i];
                    if (chichess.MyIndex.X == hisrecorder.toIndex.X &&
                        chichess.MyIndex.Y == hisrecorder.toIndex.Y)
                    {
                        chessList.Remove(chichess);//删除现在的
                        chessList.Add(hisrecorder.hisCHI.cloneSelf());//添加原来的
                        if (hisobjs.Length - 2 >= 0)
                        {
                            CHIChess chichess1 = (CHIChess)((HistoryRecorder)hisobjs[hisobjs.Length - 2]).hisCHI;
                            oldPath = (GraphicsPath)chichess1.chiPath.Clone();
                        }
                        newPath = (GraphicsPath)hisrecorder.hisCHI.chiPath.Clone();
                        curMap[hisrecorder.fromIndex.Y][hisrecorder.fromIndex.X] = desk.GetIDByType(chichess.ChiType);
                        curMap[hisrecorder.toIndex.Y][hisrecorder.toIndex.X] = 0;
                        ishisOk = true;
                        break;
                    }
                }

                if (hisrecorder.eatCHI != null)
                {
                    CHIChess cloneCHI = hisrecorder.eatCHI.cloneSelf();
                    chessList.Add(cloneCHI);
                    curMap[hisrecorder.eatCHI.MyIndex.Y][hisrecorder.eatCHI.MyIndex.X] = desk.GetIDByType(cloneCHI.ChiType);
                }
                if (ishisOk)
                {
                    if (this.canMove == true)
                    {
                        this.canMove = false;
                    }
                    else
                    {
                        this.canMove = true;
                    }
                    history.Remove(hisrecorder);
                }
                this.deskBox.Invalidate();
            }
        }

        private void newStartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SendMsg(Encoding.UTF8.GetBytes("new"));
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void remoteToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            SendMsg(Encoding.UTF8.GetBytes("fclose"));
            client.Close();
            client = null;
            this.DialogResult = DialogResult.OK;
            //try
            //{
            //    lmsg.EndInvoke(this.result);
            //}
            //catch
            //{
            //}
        }

        private void aboutgame_Click(object sender, EventArgs e)
        {
            if (bout == null)
            {
                bout = new Abouts();
                bout.Show();
            }
        }

        private void 认输ToolStripMenuItem_Click(object sender, EventArgs e)//认输了
        {
            SendMsg(Encoding.UTF8.GetBytes("renshu"));
            curMap = desk.GetMaps();
            this.LoadChesses(curMap);
        }

        private void 求和ToolStripMenuItem_Click(object sender, EventArgs e)//求和了
        {
            SendMsg(Encoding.UTF8.GetBytes("qiuhe"));
        }
    }
    public delegate void ListenMsg();
    public delegate void findSel(Point e);
}