




已阅读5页,还剩6页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
源码:有点多,哈哈using System;using System.Windows.Forms;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.IO;using System.Net.Sockets;using System.Threading;using System.Diagnostics;using System.Net;using Microsoft.Win32;namespace WuZiQi public partial class Form1 : Form public Form1() InitializeComponent(); public Button, buttons;/棋盘数组 public bool gamer1 = false, gamer2 = false,netcanplay=false ;/ 两个玩家是否允下棋,连网状态时是否允许下棋 public bool gamer1click = new bool 225;/玩家一所下的棋 public bool gamer2click = new bool225;/玩家二所下的棋 public static int port = 3332;/连接端口 public string myipadd = ;/本机IP public Thread li = null;/创建监听进程 public Thread se = null;/创建发送上线信息进程 public Thread sen = null;/创建发送信息进程 public string sendmessage = ;/要发送的信息 public bool canplay = false;/是否可以开始游戏 private void Form1_Load(object sender, EventArgs e) cshqp(); /初始化棋盘 / / 初始化棋盘 / / 初始化是否成功 public bool cshqp() groupBox1.Controls.Clear(); buttons = new Button15,15; int i = 0; for (int x = 0; x / 下棋 / / 要下棋子的索引 / public bool xiaqi(int index) /MessageBox.Show(index.ToString(); foreach (Button b in buttons) if (b.TabIndex = index ) bool check = false; if (gamer1clickb.TabIndex = true) check = true; if (gamer2clickb.TabIndex = true) check = true; if (check != true) if (gamer1 = true) gamer1clickb.TabIndex = true; b.Image = global:WuZiQi.Properties.Resources.black; canxiaqi(gamer2); if (netpk.Checked = false | Asservice.Checked = true) if (checkwin(gamer1) label11.Text = 赢家:玩家一 + player1.Text; MessageBox.Show(恭喜玩家一: + player1.Text + ,你赢了!, 会飞的鱼提醒, MessageBoxButtons.OK, MessageBoxIcon.Information); canxiaqi(); send(win恭喜玩家一: + player1.Text + ,你赢了!); else if (gamer2 = true) gamer2clickb.TabIndex = true; b.Image = global:WuZiQi.Properties.Resources.write; canxiaqi(gamer1); if (netpk.Checked = false | Asservice.Checked = true) if (checkwin(gamer2) label11.Text = 赢家:玩家二 + player2.Text; MessageBox.Show(恭喜玩家二: + player2.Text + ,你赢了!, 会飞的鱼提醒, MessageBoxButtons.OK, MessageBoxIcon.Information); canxiaqi(); send(win恭喜玩家二: + player2.Text + ,你赢了!); else check = false; MessageBox.Show(这个位置上不能下棋!, 会飞的鱼提醒, MessageBoxButtons.OK, MessageBoxIcon.Warning); /MessageBox.Show(b.TabIndex.ToString(); return true; return false; /开始游戏 private void start_Click(object sender, EventArgs e) /if (player1.Text = | player2.Text = ) / / MessageBox.Show(请输入你的名字!, 会飞的鱼提醒, MessageBoxButtons.OK, MessageBoxIcon.Warning); / /else / if (canplay = true | netpk.Checked = false) if (radioButton1.Checked = true) canxiaqi(gamer1); else canxiaqi(gamer2); groupBox2.Enabled = false; start.Enabled = false; else MessageBox.Show(还没有建立连接或对方还没有准备好!不能开始游戏!, 会飞的鱼提醒, MessageBoxButtons.OK, MessageBoxIcon.Warning); / /改变下棋状态 / / 改变下棋状态 / / 可以下棋的人的名字 public void canxiaqi(string s) if (s=gamer1) int sum = 0; for (int i = 0; i / 判断输赢 / / 要判断的玩家的名字 / 是否赢 public bool checkwin(string s) if (s = gamer1) for (int i = 0; i /做为主机 private void Asservice_CheckedChanged_1(object sender, EventArgs e) if (Asservice.Checked = true) waitconnect.Enabled = true; Asclinet.Checked = false; Asclinet.Enabled = false; else waitconnect.Enabled = false; groupBox3.Enabled = true; Asservice.Enabled = true; Asclinet.Enabled = true; /做为客户机 private void Asclinet_CheckedChanged(object sender, EventArgs e) if (Asclinet.Checked = true) connect.Enabled = true; label12.Enabled = true; ip.Enabled = true; Asservice.Checked = false; Asservice.Enabled=false ; else connect.Enabled = false ; label12.Enabled = false; ip.Enabled = false; groupBox3.Enabled = true; Asservice.Enabled = true; Asclinet.Enabled = true; /等待客户机连接 private void waitconnect_Click(object sender, EventArgs e) IPHostEntry MyEntry = Dns.GetHostByName(Dns.GetHostName(); IPAddress MyAddress = new IPAddress(MyEntry.AddressList0.Address); myipadd = MyAddress.ToString(); ThreadStart s=new ThreadStart (Listen ); li = new Thread(s); li.Start(); statue.Text = 正在等待客户机连接!现在还不能开始游戏!; waitconnect.Enabled = false; /连接 private void connect_Click(object sender, EventArgs e) if (ip.Text != ) IPHostEntry MyEntry = Dns.GetHostByName(Dns.GetHostName(); IPAddress MyAddress = new IPAddress(MyEntry.AddressList0.Address); myipadd = MyAddress.ToString(); ThreadStart s = new ThreadStart(co); se = new Thread(s); se.Start(); statue.Text = 正在连接主机!现在还不能开始游戏!; ThreadStart sx = new ThreadStart(Listen); li = new Thread(sx); li.Start(); connect.Enabled = false; else MessageBox.Show(请输入一个正确的IP地址!, 会飞的鱼提醒, MessageBoxButtons.OK, MessageBoxIcon.Warning); /监听命令信息 / / 监听命令信息 / public void Listen() try TcpListener li = new TcpListener(port); li.Start(); while (true) TcpClient cl = li.AcceptTcpClient(); NetworkStream ns = cl.GetStream(); byte mbyte = new byte1024; int BytesRead = ns.Read(mbyte, 0, mbyte.Length); string command = System.Text.Encoding.Default.GetString(mbyte, 0, BytesRead).Trim(); execcommand(command); cl.Close(); ns.Close(); Thread.Sleep(500); catch /MessageBox.Show(建立连接出错!, 会飞的鱼提醒, MessageBoxButtons.OK, MessageBoxIcon.Warning); /解释命令 / / 解释命令 / / 要解释的命令 public void execcommand(string command) label13.Text = command; /MessageBox.Show(command); if (command != ) if (command.Substring(0, 7) = connect) statue.Text = 已经建立连接!你可以开始游戏了!; ip.Text = command.Substring(8, command.Length - 8); ready.Enabled = true; send(connect: + myipadd); else if (command.Substring(0, 5) = start) int p =Convert .ToInt32 (command.Substring(6, 1); /player1 .Text =command if (p = 1) netcanplay = true ; radioButton1.Checked = true; canxiaqi(gamer1); groupBox2.Enabled = false; else netcanplay = true; radioButton2.Checked = true; canxiaqi(gamer2); groupBox2.Enabled = false; if (radioButton1.Checked = true) MessageBox.Show(先手:玩家一!, 会飞的鱼提醒, MessageBoxButtons.OK, MessageBoxIcon.Information); else MessageBox.Show(先手:玩家二!, 会飞的鱼提醒, MessageBoxButtons.OK, MessageBoxIcon.Information); canplay = true; /ready.Enabled = false; statue.Text = 对方已经准备好,你可以开始游戏了!; else if (command.Substring(0, 5) = xiaqi) int x = Convert.ToInt32(command.Substring(5, 1); if (x=1) canxiaqi(gamer1); else canxiaqi(gamer2); /MessageBox.Show(command.Substring(6, command.Length - 6); xiaqi(Convert.ToInt32(command.Substring(7, command.Length - 7); netcanplay = true; else if (command.Substring(0, 3) = win) canxiaqi(); netcanplay = false; MessageBox.Show(command.Substring(3, command.Length - 3), 会飞的鱼提醒, MessageBoxButtons.OK, MessageBoxIcon.Information); /发送上线信息 / / 向主机发送上线信息 / public void co() while (true) send(connect:+myipadd ); Thread.Sleep(3000); /发送信息 / / 发送信息 / public void sendt() try /根据目标计算机地址建立连接 TcpClient MyTcpClient = new TcpClient(ip.Text, port); /获得用于网络访问的数据流 NetworkStream MyTcpStream = MyTcpClient.GetStream(); StreamWriter MyStream = new StreamWriter(MyTcpStream, System.Text.Encoding.GetEncoding(GB2312); /将字符串写入流 MyStream.Write(sendmessage ); /将缓冲数据写入基础流 MyStream.Flush(); /关闭网络流 MyStream.Close(); MyTcpClient.C
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年共享厨房行业竞争格局与市场拓展策略报告
- 企业数字工作空间的构建与管理技巧
- 教育精准扶贫项目实施中的社区教育功能与作用研究
- 2025年线下演出市场复苏:演出行业投资机会与风险预判报告
- 2025年饮料行业新型甜味剂法规监管与市场拓展策略报告
- 豆制品生产过程中的自动化技术应用考核试卷
- 蛋品加工企业生产计划编制与优化考核试卷
- 车站综合枢纽建设考核试卷
- 洗涤剂品牌形象塑造与维护考核试卷
- 光伏复合项目可行性研究报告
- 2025年河北邢台市水务发展集团有限公司社会招聘47人笔试参考题库附带答案详解
- 天津2025年天津市面向昌都籍未就业少数民族高校毕业生招聘事业单位人员笔试历年参考题库附带答案详解
- 2025年安全生产月主题培训课件:如何查找身边安全隐患
- 大学无机化学(吉林大学、武汉大学、南开大学版) 第17章 卤素—— 内蒙古民族大学)
- 榆林智能矿山项目招商引资方案【参考范文】
- 医院版LIS操作手册(共84页)
- 基于蓄热式加热炉PLC控制系统设计(共43页)
- 液压的爬模检查记录簿表
- 瓦楞纸箱检验标准
- 安全生产事故应急救援预案范本
- MT103的栏位说明
评论
0/150
提交评论