📄 game2dserver.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;
using System.IO;
namespace Game2DServer
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
///
public struct Player
{
public string name; //玩家主机名
public TcpClient client; //玩家套接字
public bool online; //是否在线
public bool start; //是否点击了开始按钮
}
public struct Cell
{
public bool has_dot; //该单元格内是否有点
public int color; //点的颜色
}
public class Game2DServer : System.Windows.Forms.Form
{
public const int None=-1; //背景色
public const int Black=0; //黑色
public const int White=1; //白色
public const int MaxUser=2; //允许的最多玩家
public static int BlackGrade=0; //黑方成绩
public static int WhiteGrade=0; //白方成绩
public static int player_turn; //下一次应该为哪个玩家产生颜色
public static Cell[,] Grid=new Cell[15,15]; //15×15的方格
public static bool isplaying=false; //游戏是否正在进行中
public static int userNumber; //当前在线的玩家数
public static Player[] user=new Player[2];
private TcpListener myListener;
public static System.Timers.Timer timerSetDot;
private System.Windows.Forms.Button buttonStop;
private System.Windows.Forms.Button buttonStart;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Label label1;
public Game2DServer()
{
InitializeComponent();
timerSetDot = new System.Timers.Timer();
timerSetDot.Elapsed += new System.Timers.ElapsedEventHandler(this.timerSetDot_Elapsed);
for(int i=0;i<MaxUser;i++)
{
user[i].name="";
user[i].online=false;
}
timerSetDot.Enabled=false;
timerSetDot.Interval=600; //每0.6秒产生一个点
for (int i=0;i<15;i++)
for(int j=0;j<15;j++)
{
Game2DServer.Grid[i,j]=new Cell();
}
}
/// <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.buttonStop = new System.Windows.Forms.Button();
this.buttonStart = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// buttonStop
//
this.buttonStop.Location = new System.Drawing.Point(288, 240);
this.buttonStop.Name = "buttonStop";
this.buttonStop.Size = new System.Drawing.Size(104, 23);
this.buttonStop.TabIndex = 3;
this.buttonStop.Text = "停止监听";
this.buttonStop.Click += new System.EventHandler(this.buttonStop_Click);
//
// buttonStart
//
this.buttonStart.Location = new System.Drawing.Point(120, 240);
this.buttonStart.Name = "buttonStart";
this.buttonStart.Size = new System.Drawing.Size(96, 23);
this.buttonStart.TabIndex = 3;
this.buttonStart.Text = "开始监听";
this.buttonStart.Click += new System.EventHandler(this.buttonStart_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(200, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(120, 23);
this.label1.TabIndex = 5;
this.label1.Text = "玩家登录与在线情况";
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(56, 56);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(416, 160);
this.listBox1.TabIndex = 6;
//
// Game2DServer
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(536, 277);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.buttonStop);
this.Controls.Add(this.buttonStart);
this.Name = "Game2DServer";
this.Text = "Game2DServer";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Game2DServer_Closing);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Game2DServer());
}
private void buttonStart_Click(object sender, System.EventArgs e)
{
myListener=new TcpListener(IPAddress.Parse("127.0.0.1"),6688);
myListener.Start();
listBox1.Items.Add("开始监听。。。");
//创建一个线程监听客户
Thread myThread=new Thread(new ThreadStart(ReceiveData));
myThread.Start();
}
private void buttonStop_Click(object sender, System.EventArgs e)
{
ListenExit();
}
private void ListenExit()
{
timerSetDot.Enabled=false;
try
{
myListener.Stop();
}
catch
{}
listBox1.Items.Add("exit listen.");
}
private void ReceiveData()
{
TcpClient newClient=null;
while(true)
{
try
{
//等待玩家进入
newClient=myListener.AcceptTcpClient();
}
catch
{
//当单击“停止监听”或者退出时AcceptTcpClient()会产生异常
break;
}
//有玩家进入
//如果游戏未开始并且玩家人数未达到规定的最多人数,则允许进入
if(isplaying==false && userNumber<MaxUser)
{
int i=0;
//i为0表示进入的玩家是user[0],i为1表示进入的玩家是user[1]
if(userNumber==0)
{
i=0;
}
else
{
i=user[0].online ? 1 : 0;
}
newClient.ReceiveBufferSize=512; //设置接收缓冲区大小
newClient.SendBufferSize=512; //设置发送缓冲区大小
newClient.NoDelay=true;//缓冲区未满时禁用延迟
user[i].online=true;
user[i].client=newClient;
user[i].name="";//玩家主机名初始为空,该名将来来自客户端
Receive tp=new Receive(newClient,ref listBox1);
Thread thread=new Thread(new ThreadStart(tp.ProcessService));
thread.Start();
userNumber++;
listBox1.Items.Add("玩家"+i.ToString()+"进入,当前在线玩家:"+userNumber.ToString()+"个。");
}
else
{
try
{
NetworkStream netStream=newClient.GetStream();
StreamWriter sw=new StreamWriter(netStream,System.Text.Encoding.Unicode);
sw.WriteLine("Sorry");
sw.Flush();
listBox1.Items.Add("又有人试图进入,已拒绝。");
}
catch
{
listBox1.Items.Add("error seading data");
}
}
}
}
private void timerSetDot_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
int i,j,color;
Random rnd=new System.Random();
//随机产生一个格内没有点的单元格位置
do
{
i=rnd.Next(15);//产生一个小于15的非负整数
j=rnd.Next(15);
}while(Game2DServer.Grid[i,j].has_dot==true);
//确定产生的颜色
if(userNumber==2)
{
if(Game2DServer.player_turn==Black)
{
color=Black;
Game2DServer.player_turn=White;
}
else
{
color=White;
Game2DServer.player_turn=Black;
}
}
else
{
color=user[0].online?Black:White;
player_turn=color;
}
setDot(i,j,color);
}
private void Game2DServer_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
ListenExit();
}
private void ShowWin(int color)
{
timerSetDot.Enabled=false;
isplaying=false;
user[0].start=false;
user[1].start=false;
SendData server=new SendData(listBox1);
if(user[0].online && user[1].online)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -