网络对战五子棋。
这个是在宿舍相当无聊的时候写的一个相当低效率的五子棋。纯粹是打发时间。 而且这个里面也非常有可能会有非常多的Bug。本人对所有的Bug不负任何责任。
form1是选择客户端还是服务端,form2是建立连接,form3是五子棋界面。下面是form3的代码。
代码非常简单。主要部分贴出来吧。如果你想研究,还是把整个代码下载回来吧。
下载地址: http://download.youkuaiyun.com/source/669627
这个是在宿舍相当无聊的时候写的一个相当低效率的五子棋。纯粹是打发时间。 而且这个里面也非常有可能会有非常多的Bug。本人对所有的Bug不负任何责任。
form1是选择客户端还是服务端,form2是建立连接,form3是五子棋界面。下面是form3的代码。
代码非常简单。主要部分贴出来吧。如果你想研究,还是把整个代码下载回来吧。
下载地址: http://download.youkuaiyun.com/source/669627
- using System;
- using System.Drawing;
- using System.Net.Sockets;
- using System.Threading;
- using System.Windows.Forms;
- namespace Five
- {
- public partial class Form3 : Form
- {
- Bitmap Black = Properties.Resources.Black;
- Bitmap White = Properties.Resources.White;
- Bitmap BoardBg;
- Manage oManage;
- NetworkStream NS;
- Graphics Boardg;
- int[,] map = new int[Config.BoardSize, Config.BoardSize];
- int leave;
- delegate void dele();
- dele dd;
- public Form3(Manage oManage)
- {
- InitializeComponent();
- this.oManage = oManage;
- this.oManage.form3 = this;
- }
- private void Form3_Load(object sender, EventArgs e)
- {
- //到时候记得取消这个
- //Control.CheckForIllegalCrossThreadCalls = false;
- this.Text += " - " + Config.ConnType.ToString() + " " + Config.HostIP.ToString();
- BoardBg = new Bitmap((Config.BoardSize + 1) * 32, (Config.BoardSize + 1) * 32);
- pictureBox1.Image = new Bitmap((Config.BoardSize + 1) * 32, (Config.BoardSize + 1) * 32);
- Graphics g = Graphics.FromImage(BoardBg);
- Boardg = Graphics.FromImage(pictureBox1.Image);
- g.Clear(Color.FromArgb(240, 240, 240));
- for(int i = 1;i <= Config.BoardSize;i++)
- {
- g.DrawLine(Pens.Green, 32, i * 32, Config.BoardSize * 32, i * 32);
- g.DrawLine(Pens.Green, i * 32, 32, i * 32, Config.BoardSize * 32);
- }
- NS = oManage.Client.GetStream();
- new Thread(new ThreadStart(BeginNetStream)).Start();
- BeginPlay();
- }
- void BeginNetStream()
- {
- Thread.CurrentThread.IsBackground = true;
- byte b0, b1, b2, b3;
- try
- {
- NS.WriteByte(0xff);
- NS.WriteByte(0x00);
- NS.WriteByte(0x00);
- NS.WriteByte(0x00);
- NS.Flush();
- do
- {
- b0 = (byte)NS.ReadByte();
- b1 = (byte)NS.ReadByte();
- b2 = (byte)NS.ReadByte();
- b3 = (byte)NS.ReadByte();
- switch(b0)
- {
- case 0x01:
- NS.WriteByte(0x01);
- NS.WriteByte(0x00);
- NS.WriteByte(0x00);
- NS.WriteByte(0x00);
- NS.Flush();
- NS.Close();
- break;
- case 0x02:
- Put(new Point(b2, b3), false);
- if(oManage.State != Manage.eState.Win && oManage.State != Manage.eState.Fail && oManage.State != Manage.eState.Dogfall)
- oManage.State = Manage.eState.WaitSelf;
- break;
- case 0x03:
- DialogResult dr = DialogResult.None;
- dd = delegate
- {
- dr = MessageBox.Show("重玩?", "", MessageBoxButtons.YesNo);
- };
- this.Invoke(dd);
- if(dr == DialogResult.Yes)
- {
- NS.WriteByte(0x04);
- NS.WriteByte(0x00);
- NS.WriteByte(0x00);
- NS.WriteByte(0x00);
- NS.Flush();
- dd = delegate
- {
- BeginPlay();
- };
- this.Invoke(dd);
- }
- break;
- case 0x04:
- dd = delegate
- {
- BeginPlay();
- };
- this.Invoke(dd);
- break;
- }
- SetState();
- } while(b0 != 0x01);
- dd = delegate
- {
- this.Close();
- };
- this.Invoke(dd);
- }
- catch(Exception)
- {
- //MessageBox.Show(string.Format("Source:{0}/r/nMessage:{1}", e.Source, e.Message));
- //Process.GetCurrentProcess().Kill();
- }
- }
- void Put(Point P, bool isSelf)
- {
- dd = delegate
- {
- Point T = new Point(P.X * 32 + 16, P.Y * 32 + 16);
- if(isSelf ^ (Config.ConnType == Config.eConnType.Client))
- {
- map[P.X, P.Y] = 1;
- Boardg.DrawImage(Black, T);
- }
- else
- {
- map[P.X, P.Y] = 2;
- Boardg.DrawImage(White, T);
- }
- pictureBox1.Refresh();
- if(--leave == 0)
- {
- oManage.State = Manage.eState.Dogfall;
- //SetState();
- }
- for(int i = 0;i < Config.BoardSize;i++)
- for(int j = 0;j < Config.BoardSize;j++)
- {
- int f = map[i, j];
- if(f == 0)
- continue;
- Ck(i, j, 1, 0);
- Ck(i, j, 0, 1);
- Ck(i, j, 1, 1);
- if(oManage.State == Manage.eState.Win || oManage.State == Manage.eState.Fail)
- {
- SetState();
- //BeginPlay();
- return;
- }
- }
- };
- this.Invoke(dd);
- }
- void Ck(int x, int y, int vx, int vy)
- {
- int f = map[x, y];
- int g = 0;
- while(x + vx < Config.BoardSize && y + vy < Config.BoardSize && map[x + vx, y + vy] == f)
- {
- x += vx;
- y += vy;
- g++;
- }
- if(g >= 4)
- if(Config.ConnType == Config.eConnType.Server ^ f == 2)
- oManage.State = Manage.eState.Win;
- else
- oManage.State = Manage.eState.Fail;
- }
- void SetState()
- {
- dd = delegate
- {
- switch(oManage.State)
- {
- case Manage.eState.NoConnect:
- label2.Text = "无连接";
- break;
- case Manage.eState.Win:
- label2.Text = "你赢了";
- break;
- case Manage.eState.Fail:
- label2.Text = "你输了";
- break;
- case Manage.eState.Dogfall:
- label2.Text = "平局";
- break;
- case Manage.eState.WaitFriend:
- label2.Text = "等待对手";
- break;
- case Manage.eState.WaitSelf:
- label2.Text = "轮到你下";
- break;
- case Manage.eState.WaitConnect:
- label2.Text = "等待连接";
- break;
- case Manage.eState.Connected:
- label2.Text = "已连接";
- break;
- default:
- break;
- }
- };
- this.Invoke(dd);
- }
- void BeginPlay()
- {
- //if(Boardg != null)
- // Boardg.ReleaseHdc(Boardg.GetHdc());
- //this.pictureBox1.Image = (Image)BoardBg.Clone();
- //Boardg = Graphics.FromImage(this.pictureBox1.Image);
- //Boardg.Clear(Color.FromArgb(240, 240, 240));
- Boardg.DrawImage(BoardBg, new Point(0, 0));
- pictureBox1.Refresh();
- for(int i = 0;i < Config.BoardSize;i++)
- for(int j = 0;j < Config.BoardSize;j++)
- map[i, j] = 0;
- leave = Config.BoardSize * Config.BoardSize;
- if(Config.ConnType == Config.eConnType.Server)
- {
- oManage.State = Manage.eState.WaitSelf;
- pictureBox2.Image = Properties.Resources.Black;
- }
- if(Config.ConnType == Config.eConnType.Client)
- {
- oManage.State = Manage.eState.WaitFriend;
- pictureBox2.Image = Properties.Resources.White;
- }
- //SetState();
- }
- private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
- {
- label6.Text = e.Location.ToString();
- Point P = new Point((e.X - 16) / 32, (e.Y - 16) / 32);
- label4.Text = P.ToString();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- NS.WriteByte(0x01);
- NS.WriteByte(0x00);
- NS.WriteByte(0x00);
- NS.WriteByte(0x00);
- NS.Flush();
- }
- catch { }
- //NS.Close();
- this.Close();
- }
- private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
- {
- Point P = new Point((e.X - 16) / 32, (e.Y - 16) / 32);
- if((P.X >= Config.BoardSize || P.Y >= Config.BoardSize)
- || map[P.X, P.Y] != 0
- || oManage.State == Manage.eState.WaitFriend
- || oManage.State == Manage.eState.Fail
- || oManage.State == Manage.eState.Win
- || oManage.State == Manage.eState.Dogfall)
- return;
- try
- {
- NS.WriteByte(0x02);
- NS.WriteByte(0x00);
- NS.WriteByte((byte)P.X);
- NS.WriteByte((byte)P.Y);
- NS.Flush();
- }
- catch { }
- Put(P, true);
- if(oManage.State != Manage.eState.Win && oManage.State != Manage.eState.Fail && oManage.State != Manage.eState.Dogfall)
- oManage.State = Manage.eState.WaitFriend;
- SetState();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- try
- {
- NS.WriteByte(0x03);
- NS.WriteByte(0x00);
- NS.WriteByte(0x00);
- NS.WriteByte(0x00);
- NS.Flush();
- }
- catch { }
- }
- }
- }