• 五子棋C#源码,网络对战版---转载--待学习


    五子棋C#源码,网络对战版

    支持网络对战的C#五子棋源码下载,VS2010环境编译后可运行,需要先输入ip地址才行,不是单机版的。部分代码摘录如下://将接收的消息转换成自定义集合MessClass
       MessClass msg = new ClassSerializers().DeSerializeBinary((new System.IO.MemoryStream(Data))) as MessClass;
       switch (msg.sendKind)//获取发送的类型
       {
       case SendKind.SendConn://连接
       {
       if (msg.ChessStyle)//判断当前棋子的类型
       ChessStyle = true;//黑棋
       else
       ChessStyle = false;//白棋
       CGrow = ChessStyle;//记录当前棋子的类型
       CKind = -1;//记录取胜的棋子种类
       temMsg.sendKind = SendKind.SendConnHit;//设置消息发送类型为连接成功
       temMsg.ChessStyle = ChessStyle;//在发送消息中设置当前棋子的类型
       //向远程计算机发送消息
       udpSocket1.Send(IPAddress.Parse(FrmClass.ServerIP), Convert.ToInt32(FrmClass.ClientPort), new ClassSerializers().SerializeBinary(temMsg).ToArray());
       break;
       }
       case SendKind.SendConnHit://连接成功
       {
       MessageBox.Show("连接成功");//显示连接成功
       button1.Tag = 1;//设置标识
       button1.Text = "重新开始";
       if (msg.ChessStyle)//如果是黑棋
       {
       ChessStyle = true;//设置本地的棋子类型为黑棋
       DownChess = true;//本地先下
       label2.Text = "黑棋";//显示本地为黑棋
       }
       else
       {
       ChessStyle = false;//设置本地的棋子类型为白棋
       DownChess = false;//本地后下
       label2.Text = "白棋";//显示本地为白棋
       }
       CGrow = ChessStyle;//记录本地的棋子类型
       panel2.Visible = false;//隐藏最后落子的标记
       break;
       }
       case SendKind.SendAfresh://重新下棋
       {
       //清空棋盘中各棋子的位置
       for (int i = 0; i < 15; i++)
       for (int j = 0; j < 15; j++)
       note[i, j] = -1;
       Graphics g = panel1.CreateGraphics();//创健panel1控件的Graphics类
       g.DrawImage(Properties.Resources.棋盘, 0, 0, panel1.Width, panel1.Height);//清空棋盘
       if (msg.ChessStyle)//如果是黑棋
       {
       ChessStyle = true;//设置本地的棋子类型为黑棋
       DownChess = true;//设置本地的棋子类型为黑棋
       label2.Text = "黑棋";//显示本地为黑棋
       }
       else
       {
       ChessStyle = false;//设置本地的棋子类型为白棋
       DownChess = false;//本地后下
       label2.Text = "白棋";//显示本地为白棋
       }
       CGrow = ChessStyle;//记录本地的棋子类型
       CKind = -1;//记录取胜的棋子种类
       panel2.Visible = false;//隐藏最后落子的标记
       break;
       }
       case SendKind.SendChessman://接收发送的棋子
       {
       int tem_CS = -1;
       Image tem_Image;//实例化Image类
       if (msg.Grow)//如果为黑棋
       {
       tem_CS = 1;//记录棋子类型为黑棋
       CGrow = true;//记录当前为黑棋
       tem_Image = Properties.Resources.黑棋子;//存储黑棋的图片
       }
       else
       {
       tem_CS = 0;//记录棋子类型为白棋
       CGrow = false;//记录当前为黑棋
       tem_Image = Properties.Resources.白棋子;//存储白棋的图片
       }
       note[msg.ChessX, msg.ChessY] = tem_CS;//在数组中记录当前棋子的位置
       Graphics g = panel1.CreateGraphics();
       g.DrawImage(tem_Image, msg.ChessX * 35 + 7, msg.ChessY * 35 + 7, 35, 35);//在棋盘中显示对方下的棋子
       panel2.Visible = true;//显示最后落子的标记
       panel2.Location = new System.Drawing.Point(msg.ChessX * 35 + 20, msg.ChessY * 35 + 20);//将标记显示在棋子上
       DownChess = msg.Walk;//记录对方是否下完棋
       CGrow = !msg.Grow;//记录本地的棋子类型
       Arithmetic(tem_CS, msg.ChessX, msg.ChessY);//计算对方是否获胜
       DownChess = true;//对方已下完棋
       break;
       }
       case SendKind.SendCut://断开连接
       {
       temMsg.sendKind = SendKind.SendCutHit;//设置发送的类型为断开连接
       //向远程计算机发送断开消息
       udpSocket1.Send(IPAddress.Parse(FrmClass.ServerIP), Convert.ToInt32(FrmClass.ClientPort), new ClassSerializers().SerializeBinary(temMsg).ToArray());
       button1.Text = "连接";//显当前可重新连接
       button1.Tag = 0;//设置连接标识
       break;
       }
       case SendKind.SendCutHit://断开成功
       {
       udpSocket1.Active = false;//关闭UDP的连接
       Application.Exit();//关闭当前工程
       break;
       }
      FrmClass.ServerIP = textBox1.Text;//记录远程计算机的IP地址
       udpSocket1.Active = false;//关闭UDP的连接
       udpSocket1.LocalPort = 11001;//设置端口号
       udpSocket1.Active = true;//打开UDP的连接
       temMsg.sendKind = SendKind.SendConn;//设置发送类型为连接
       temMsg.ChessStyle = !ChessStyle;//设置对方的棋子类型
  • 相关阅读:
    小熊派4G开发板初体验
    空间换时间,查表法的经典例子
    C语言、嵌入式应用:TCP通信实践
    【实践】基于RT-Thread的智慧路灯案例实验分享
    STM32串口打印的那些知识
    【RT-Thread笔记】BH1750软件包的使用
    【RT-Thread笔记】OneNet软件包的使用
    串口通讯你真的会了吗?不妨看看这些经验
    三小记(2)
    audio标签实现简单的自定义播放器
  • 原文地址:https://www.cnblogs.com/bedfly/p/12128491.html
Copyright © 2020-2023  润新知