📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Game2DServer
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private const int Backcolor=-1; //背景色
private const int Black=0; //黑色
private const int White=1; //白色
private int BlackGrade; //黑方成绩
private int WhiteGrade; //白方成绩
private IPAddress myIP;
private IPEndPoint me;
private Socket socket,user1,user2;
private Thread thread1,thread2;
private string user1name,user2name;
private static ManualResetEvent Done=new ManualResetEvent(false); //线程初始状态为非终止
private int player_turn;
private Cell[,] Grid=new Cell[15,15];
private System.Windows.Forms.Timer timerWait;
private System.Windows.Forms.Timer timerSetDot;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button buttonStop;
private System.Windows.Forms.Button buttonStart;
private System.ComponentModel.IContainer components;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
for (int i=0;i<15;i++)
for(int j=0;j<15;j++)
{
Grid[i,j]=new Cell();
Grid[i,j].unsetDot(Backcolor);
}
BlackGrade=WhiteGrade=0;
player_turn=0; //0出黑点
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timerWait = new System.Windows.Forms.Timer(this.components);
this.timerSetDot = new System.Windows.Forms.Timer(this.components);
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.buttonStop = new System.Windows.Forms.Button();
this.buttonStart = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// timerWait
//
this.timerWait.Interval = 1000;
//
// timerSetDot
//
this.timerSetDot.Interval = 1000;
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(40, 16);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(552, 216);
this.richTextBox1.TabIndex = 2;
this.richTextBox1.Text = "";
//
// buttonStop
//
this.buttonStop.Location = new System.Drawing.Point(648, 112);
this.buttonStop.Name = "buttonStop";
this.buttonStop.Size = new System.Drawing.Size(104, 23);
this.buttonStop.TabIndex = 3;
this.buttonStop.Text = "stop listen";
this.buttonStop.Click += new System.EventHandler(this.buttonStop_Click);
//
// buttonStart
//
this.buttonStart.Location = new System.Drawing.Point(648, 56);
this.buttonStart.Name = "buttonStart";
this.buttonStart.Size = new System.Drawing.Size(104, 23);
this.buttonStart.TabIndex = 3;
this.buttonStart.Text = "start listen";
this.buttonStart.Click += new System.EventHandler(this.buttonStart_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(784, 246);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.buttonStop,
this.richTextBox1,
this.buttonStart});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
public void reset()
{
for( int i = 0; i < 15; i++ )
{
for( int j=0; j<15; j++)
{
Grid[i,j].unsetDot(Backcolor);
}
}
}
public int Game2DStart()
{
this.timerSetDot.Enabled=true;
this.timerWait.Enabled=true;
return 0;
}
private void timerSetdot_Tick(object sender, System.EventArgs e)
{
int i,j,color;
Random rnd=new System.Random();
do
{
i=rnd.Next(15);
j=rnd.Next(15);
}while(Grid[i,j].has_dot==true);
if(player_turn==Black)
{
color=Black;
player_turn=White;
}
else
{
color=White;
player_turn=Black;
}
setDot(i,j,color);
}
public int setDot(int i,int j,int color)
{
Grid[i,j].has_dot=true;
Grid[i,j].color=color;
return 0;
}
public int showform()
{
System.Windows.Forms.Form aa=new Form();
aa.Show();
return 0;
}
public int getSetDotInterval()
{
return this.timerSetDot.Interval;
}
public void setSetDotInterval(int intervalNumber)
{
this.timerSetDot.Interval=intervalNumber;
return;
}
private void carry1Message()
{
if(!user1.Connected) return;
bool isTrue=true;
while(isTrue)
{
byte[] mybyte=new byte[100];
if(user1.Receive(mybyte,mybyte.Length,0)<=0) continue;
string str=System.Text.Encoding.BigEndianUnicode.GetString(mybyte);
string s1;
switch(str[0])
{
case 'T': //T--timeInterval
this.timerSetDot.Interval=Int32.Parse(str.Substring(1));
s1=str+"--设置setdot时间\r\n";
this.richTextBox1.AppendText(s1);
sendMessage(1,"T",this.timerSetDot.Interval);
break;
case 'A': //A--IP Address
user1name=str.Substring(1,str.Length-2);
this.richTextBox1.AppendText("\r\n"+user1name+" 进入\r\n");
break;
case 'E': //E--end connect
this.richTextBox1.AppendText("与客户 "+user1name+" 断开连接");
user1.Shutdown(System.Net.Sockets.SocketShutdown.Both);
isTrue=false;
break;
default:
this.richTextBox1.AppendText(str+"--mj\r\n");
break;
}
}
}
private void carry2Message()
{
if(!user2.Connected) return;
byte[] mybyte=new byte[100];
bool isTrue=true;
while(isTrue)
{
if(user2.Receive(mybyte,mybyte.Length,0)<=0) continue;
string str=System.Text.Encoding.BigEndianUnicode.GetString(mybyte);
switch(str[0])
{
case 'T': //T--timeInterval
sendMessage(2,"T只有先进入者才能修改",0);
break;
case 'A': //A--IP Address
user2name=str.Substring(1,str.Length-2);
this.richTextBox1.AppendText("\r\n"+user2name+" 进入\r\n");
break;
case 'E': //E--end connect
this.richTextBox1.AppendText("与客户 "+user2name+" 断开连接");
user2.Shutdown(System.Net.Sockets.SocketShutdown.Both);
isTrue=false;
break;
default:
this.richTextBox1.AppendText(str+"???mj\r\n");
break;
}
}
}
private void sendMessage(int userID,string s1,int val)
{
byte[] bytee=new byte[640];
string ss=s1+val.ToString();
bytee=System.Text.Encoding.BigEndianUnicode.GetBytes(ss.ToCharArray());
if(userID==1)
user1.Send(bytee,bytee.Length,0);
else
user2.Send(bytee,bytee.Length,0);
}
private void buttonStop_Click(object sender, System.EventArgs e)
{
try
{
thread1.Abort();
thread2.Abort();
socket.Close();
this.richTextBox1.AppendText("stop listen.\r\n");
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
}
private void buttonStart_Click(object sender, System.EventArgs e)
{
try
{
myIP=IPAddress.Parse("202.196.101.244");
me=new IPEndPoint(myIP,6688);
socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
socket.Bind(me);
socket.Listen(20);
this.richTextBox1.AppendText("202.196.101.244的端口6688开始监听。\r\n");
user1=socket.Accept();
if(user1.Connected)
{
this.richTextBox1.AppendText("第一个玩家进入。\r\n");
}
// user2=socket.Accept();
thread1=new Thread(new ThreadStart(carry1Message));
thread1.Start();
// thread2=new Thread(new ThreadStart(carry2Message));
// thread2.Start();
}
catch(Exception err)
{
this.richTextBox1.AppendText("出错:"+err.Message);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -