网络对战五子棋

网络对战五子棋。
这个是在宿舍相当无聊的时候写的一个相当低效率的五子棋。纯粹是打发时间。 而且这个里面也非常有可能会有非常多的Bug。本人对所有的Bug不负任何责任。

form1是选择客户端还是服务端,form2是建立连接,form3是五子棋界面。下面是form3的代码。

代码非常简单。主要部分贴出来吧。如果你想研究,还是把整个代码下载回来吧。
下载地址: http://download.youkuaiyun.com/source/669627
  1. using System;
  2. using System.Drawing;
  3. using System.Net.Sockets;
  4. using System.Threading;
  5. using System.Windows.Forms;
  6. namespace Five
  7. {
  8.     public partial class Form3 : Form
  9.     {
  10.         Bitmap Black = Properties.Resources.Black;
  11.         Bitmap White = Properties.Resources.White;
  12.         Bitmap BoardBg;
  13.         Manage oManage;
  14.         NetworkStream NS;
  15.         Graphics Boardg;
  16.         int[,] map = new int[Config.BoardSize, Config.BoardSize];
  17.         int leave;
  18.         delegate void dele();
  19.         dele dd;
  20.         public Form3(Manage oManage)
  21.         {
  22.             InitializeComponent();
  23.             this.oManage = oManage;
  24.             this.oManage.form3 = this;
  25.         }
  26.         private void Form3_Load(object sender, EventArgs e)
  27.         {
  28.             //到时候记得取消这个
  29.             //Control.CheckForIllegalCrossThreadCalls = false;
  30.             this.Text += " - " + Config.ConnType.ToString() + " " + Config.HostIP.ToString();
  31.             BoardBg = new Bitmap((Config.BoardSize + 1) * 32, (Config.BoardSize + 1) * 32);
  32.             pictureBox1.Image = new Bitmap((Config.BoardSize + 1) * 32, (Config.BoardSize + 1) * 32);
  33.             Graphics g = Graphics.FromImage(BoardBg);
  34.             Boardg = Graphics.FromImage(pictureBox1.Image);
  35.             g.Clear(Color.FromArgb(240, 240, 240));
  36.             for(int i = 1;i <= Config.BoardSize;i++)
  37.             {
  38.                 g.DrawLine(Pens.Green, 32, i * 32, Config.BoardSize * 32, i * 32);
  39.                 g.DrawLine(Pens.Green, i * 32, 32, i * 32, Config.BoardSize * 32);
  40.             }
  41.             NS = oManage.Client.GetStream();
  42.             new Thread(new ThreadStart(BeginNetStream)).Start();
  43.             BeginPlay();
  44.         }
  45.         void BeginNetStream()
  46.         {
  47.             Thread.CurrentThread.IsBackground = true;
  48.             byte b0, b1, b2, b3;
  49.             try
  50.             {
  51.                 NS.WriteByte(0xff);
  52.                 NS.WriteByte(0x00);
  53.                 NS.WriteByte(0x00);
  54.                 NS.WriteByte(0x00);
  55.                 NS.Flush();
  56.                 do
  57.                 {
  58.                     b0 = (byte)NS.ReadByte();
  59.                     b1 = (byte)NS.ReadByte();
  60.                     b2 = (byte)NS.ReadByte();
  61.                     b3 = (byte)NS.ReadByte();
  62.                     switch(b0)
  63.                     {
  64.                         case 0x01:
  65.                             NS.WriteByte(0x01);
  66.                             NS.WriteByte(0x00);
  67.                             NS.WriteByte(0x00);
  68.                             NS.WriteByte(0x00);
  69.                             NS.Flush();
  70.                             NS.Close();
  71.                             break;
  72.                         case 0x02:
  73.                             Put(new Point(b2, b3), false);
  74.                             if(oManage.State != Manage.eState.Win && oManage.State != Manage.eState.Fail && oManage.State != Manage.eState.Dogfall)
  75.                                 oManage.State = Manage.eState.WaitSelf;
  76.                             break;
  77.                         case 0x03:
  78.                             DialogResult dr = DialogResult.None;
  79.                             dd = delegate
  80.                             {
  81.                                 dr = MessageBox.Show("重玩?""", MessageBoxButtons.YesNo);
  82.                             };
  83.                             this.Invoke(dd);
  84.                             if(dr == DialogResult.Yes)
  85.                             {
  86.                                 NS.WriteByte(0x04);
  87.                                 NS.WriteByte(0x00);
  88.                                 NS.WriteByte(0x00);
  89.                                 NS.WriteByte(0x00);
  90.                                 NS.Flush();
  91.                                 dd = delegate
  92.                                 {
  93.                                     BeginPlay();
  94.                                 };
  95.                                 this.Invoke(dd);
  96.                             }
  97.                             break;
  98.                         case 0x04:
  99.                             dd = delegate
  100.                             {
  101.                                 BeginPlay();
  102.                             };
  103.                             this.Invoke(dd);
  104.                             break;
  105.                     }
  106.                     SetState();
  107.                 } while(b0 != 0x01);
  108.                 dd = delegate
  109.                 {
  110.                     this.Close();
  111.                 };
  112.                 this.Invoke(dd);
  113.             }
  114.             catch(Exception)
  115.             {
  116.                 //MessageBox.Show(string.Format("Source:{0}/r/nMessage:{1}", e.Source, e.Message));
  117.                 //Process.GetCurrentProcess().Kill();
  118.             }
  119.         }
  120.         void Put(Point P, bool isSelf)
  121.         {
  122.             dd = delegate
  123.             {
  124.                 Point T = new Point(P.X * 32 + 16, P.Y * 32 + 16);
  125.                 if(isSelf ^ (Config.ConnType == Config.eConnType.Client))
  126.                 {
  127.                     map[P.X, P.Y] = 1;
  128.                     Boardg.DrawImage(Black, T);
  129.                 }
  130.                 else
  131.                 {
  132.                     map[P.X, P.Y] = 2;
  133.                     Boardg.DrawImage(White, T);
  134.                 }
  135.                 pictureBox1.Refresh();
  136.                 if(--leave == 0)
  137.                 {
  138.                     oManage.State = Manage.eState.Dogfall;
  139.                     //SetState();
  140.                 }
  141.                 for(int i = 0;i < Config.BoardSize;i++)
  142.                     for(int j = 0;j < Config.BoardSize;j++)
  143.                     {
  144.                         int f = map[i, j];
  145.                         if(f == 0)
  146.                             continue;
  147.                         Ck(i, j, 1, 0);
  148.                         Ck(i, j, 0, 1);
  149.                         Ck(i, j, 1, 1);
  150.                         if(oManage.State == Manage.eState.Win || oManage.State == Manage.eState.Fail)
  151.                         {
  152.                             SetState();
  153.                             //BeginPlay();
  154.                             return;
  155.                         }
  156.                     }
  157.             };
  158.             this.Invoke(dd);
  159.         }
  160.         void Ck(int x, int y, int vx, int vy)
  161.         {
  162.             int f = map[x, y];
  163.             int g = 0;
  164.             while(x + vx < Config.BoardSize && y + vy < Config.BoardSize && map[x + vx, y + vy] == f)
  165.             {
  166.                 x += vx;
  167.                 y += vy;
  168.                 g++;
  169.             }
  170.             if(g >= 4)
  171.                 if(Config.ConnType == Config.eConnType.Server ^ f == 2)
  172.                     oManage.State = Manage.eState.Win;
  173.                 else
  174.                     oManage.State = Manage.eState.Fail;
  175.         }
  176.         void SetState()
  177.         {
  178.             dd = delegate
  179.             {
  180.                 switch(oManage.State)
  181.                 {
  182.                     case Manage.eState.NoConnect:
  183.                         label2.Text = "无连接";
  184.                         break;
  185.                     case Manage.eState.Win:
  186.                         label2.Text = "你赢了";
  187.                         break;
  188.                     case Manage.eState.Fail:
  189.                         label2.Text = "你输了";
  190.                         break;
  191.                     case Manage.eState.Dogfall:
  192.                         label2.Text = "平局";
  193.                         break;
  194.                     case Manage.eState.WaitFriend:
  195.                         label2.Text = "等待对手";
  196.                         break;
  197.                     case Manage.eState.WaitSelf:
  198.                         label2.Text = "轮到你下";
  199.                         break;
  200.                     case Manage.eState.WaitConnect:
  201.                         label2.Text = "等待连接";
  202.                         break;
  203.                     case Manage.eState.Connected:
  204.                         label2.Text = "已连接";
  205.                         break;
  206.                     default:
  207.                         break;
  208.                 }
  209.             };
  210.             this.Invoke(dd);
  211.         }
  212.         void BeginPlay()
  213.         {
  214.             //if(Boardg != null)
  215.             //    Boardg.ReleaseHdc(Boardg.GetHdc());
  216.             //this.pictureBox1.Image = (Image)BoardBg.Clone();
  217.             //Boardg = Graphics.FromImage(this.pictureBox1.Image);
  218.             //Boardg.Clear(Color.FromArgb(240, 240, 240));
  219.             Boardg.DrawImage(BoardBg, new Point(0, 0));
  220.             pictureBox1.Refresh();
  221.             for(int i = 0;i < Config.BoardSize;i++)
  222.                 for(int j = 0;j < Config.BoardSize;j++)
  223.                     map[i, j] = 0;
  224.             leave = Config.BoardSize * Config.BoardSize;
  225.             if(Config.ConnType == Config.eConnType.Server)
  226.             {
  227.                 oManage.State = Manage.eState.WaitSelf;
  228.                 pictureBox2.Image = Properties.Resources.Black;
  229.             }
  230.             if(Config.ConnType == Config.eConnType.Client)
  231.             {
  232.                 oManage.State = Manage.eState.WaitFriend;
  233.                 pictureBox2.Image = Properties.Resources.White;
  234.             }
  235.             //SetState();
  236.         }
  237.         private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  238.         {
  239.             label6.Text = e.Location.ToString();
  240.             Point P = new Point((e.X - 16) / 32, (e.Y - 16) / 32);
  241.             label4.Text = P.ToString();
  242.         }
  243.         private void button1_Click(object sender, EventArgs e)
  244.         {
  245.             try
  246.             {
  247.                 NS.WriteByte(0x01);
  248.                 NS.WriteByte(0x00);
  249.                 NS.WriteByte(0x00);
  250.                 NS.WriteByte(0x00);
  251.                 NS.Flush();
  252.             }
  253.             catch { }
  254.             //NS.Close();
  255.             this.Close();
  256.         }
  257.         private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  258.         {
  259.             Point P = new Point((e.X - 16) / 32, (e.Y - 16) / 32);
  260.             if((P.X >= Config.BoardSize || P.Y >= Config.BoardSize)
  261.                 || map[P.X, P.Y] != 0
  262.                 || oManage.State == Manage.eState.WaitFriend
  263.                 || oManage.State == Manage.eState.Fail
  264.                 || oManage.State == Manage.eState.Win
  265.                 || oManage.State == Manage.eState.Dogfall)
  266.                 return;
  267.             try
  268.             {
  269.                 NS.WriteByte(0x02);
  270.                 NS.WriteByte(0x00);
  271.                 NS.WriteByte((byte)P.X);
  272.                 NS.WriteByte((byte)P.Y);
  273.                 NS.Flush();
  274.             }
  275.             catch { }
  276.             Put(P, true);
  277.             if(oManage.State != Manage.eState.Win && oManage.State != Manage.eState.Fail && oManage.State != Manage.eState.Dogfall)
  278.                 oManage.State = Manage.eState.WaitFriend;
  279.             SetState();
  280.         }
  281.         private void button2_Click(object sender, EventArgs e)
  282.         {
  283.             try
  284.             {
  285.                 NS.WriteByte(0x03);
  286.                 NS.WriteByte(0x00);
  287.                 NS.WriteByte(0x00);
  288.                 NS.WriteByte(0x00);
  289.                 NS.Flush();
  290.             }
  291.             catch { }
  292.         }
  293.     }
  294. }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值