《C#网络应用编程》课程设计Word文档格式.docx

上传人:b****4 文档编号:16677682 上传时间:2022-11-25 格式:DOCX 页数:40 大小:801.84KB
下载 相关 举报
《C#网络应用编程》课程设计Word文档格式.docx_第1页
第1页 / 共40页
《C#网络应用编程》课程设计Word文档格式.docx_第2页
第2页 / 共40页
《C#网络应用编程》课程设计Word文档格式.docx_第3页
第3页 / 共40页
《C#网络应用编程》课程设计Word文档格式.docx_第4页
第4页 / 共40页
《C#网络应用编程》课程设计Word文档格式.docx_第5页
第5页 / 共40页
点击查看更多>>
下载资源
资源描述

《C#网络应用编程》课程设计Word文档格式.docx

《《C#网络应用编程》课程设计Word文档格式.docx》由会员分享,可在线阅读,更多相关《《C#网络应用编程》课程设计Word文档格式.docx(40页珍藏版)》请在冰豆网上搜索。

《C#网络应用编程》课程设计Word文档格式.docx

3.1开发环境:

MicrosoftVisualStudio2008

MicrosoftVisualStudio2008是面向WindowsVista、Office2007、Web2.0的下一代开发工具,代号“Orcas”,是对VisualStudio2005一次及时、全面的升级。

VS2008引入了250多个新特性,整合了对象、关系型数据、XML的访问方式,语言更加简洁。

使用VisualStudio2008可以高效开发Windows应用。

设计器中可以实时反映变更,XAML中智能感知功能可以提高开发效率。

同时VisualStudio2008支持项目模板、调试器和部署程序。

VisualStudio2008可以高效开发Web应用,集成了AJAX1.0,包含AJAX项目模板,它还可以高效开发Office应用和Mobile应用。

3.2引用命名空间以及相关类

命名空间:

除了一些基本的,本例中还引用了一些其他的命名空间,如下:

System.IO命名空间包含允许读写文件和数据流的类型以及提供基本文件和目录支持的类型。

System.Net.Sockets命名空间为需要严密控制网络访问的开发人员提供了WindowsSockets(Winsock)接口的托管实现。

TcpClient 

、TcpListener 

和 

UdpClient 

类封装有关创建到Internet的TCP和UDP连接的详细信息。

System.Threading用于多线程编程,对线程进行管理,如创建线程、启动线程、终止线程、合并线程等等。

System.Net该命名空间为Internet网络上使用的多种协议提供了方便的编程接口,利用这个命名空间提供的类,不需要考虑所使用协议的具体细节,就能很快实现具体功能。

System.Windows.Forms 

命名空间包含用于创建基于Windows的应用程序的类,以充分利用MicrosoftWindows操作系统中提供的丰富的用户界面功能

相关类:

BinaryReader类,用特定的编码将基元数据类型读作二进制值。

BinaryWriter类,以二进制形式将基元类型写入流,并支持用特定的编码写入字符串。

TcpListener类,侦听来自TCP网络客户端的连接。

Thread类,创建并控制线程,设置其优先级并获取其状态。

IPaddress类,提供网际协议(IP)地址。

IPEndPoint类,将网络端点表示为IP地址和端口号。

界面设计:

使用的控件:

(1)两个button负责启动和终止服务。

(2)listBox显示客户状态信息以及服务器与客户端通信的内容。

(3)两个textBox控件,分别控制游戏室允许进入的最多人数和游戏室同时开出的房间数。

整体界面如图所示

4服务器端主要代码介绍

运行VS,新建一个名为GobangServer的Windows应用程序项目。

4.1添加一个User.cs类,表示所有连接到服务器的客户。

代码如下:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Net.Sockets;

usingSystem.IO;

namespaceGobangServer

{

classUser

{

publicreadonlyTcpClientclient;

publicreadonlyStreamReadersr;

publicreadonlyStreamWritersw;

publicstringuserName;

publicUser(TcpClientclient)

this.client=client;

this.userName="

"

;

NetworkStreamnetStream=client.GetStream();

sr=newStreamReader(netStream,System.Text.Encoding.Default);

sw=newStreamWriter(netStream,System.Text.Encoding.Default);

}

}

4.2添加一个名为Player.cs的类,表示坐在游戏桌两边的玩家,代码如下:

classPlayer

privateUseruser;

publicUserGameUser

get

returnuser;

set

user=value;

privateboolstart;

publicboolStart

returnstart;

start=value;

publicPlayer()

start=false;

user=null;

4.3添加一个名为GobangBoard.cs的类,表示棋盘,代码如下:

classGobangBoard

publicconstintNone=-1;

//无棋子

publicconstintBlack=0;

//黑色棋子

publicconstintWhite=1;

//白色棋子

privateint[,]grid=newint[15,15];

//15*15的方格

///<

summary>

///棋盘

/summary>

publicint[,]Grid

returngrid;

privateintnextIndex;

publicintNextIndex///应该黑方放棋子还是白方放棋子

returnnextIndex;

nextIndex=value;

publicGobangBoard()//构造函数

InitializeBoard();

publicvoidInitializeBoard()///将15*15的方格中的每个交叉点均设置为无棋子

for(inti=0;

i<

=grid.GetUpperBound(0);

i++)

for(intj=0;

j<

=grid.GetUpperBound

(1);

j++)

grid[i,j]=None;

nextIndex=Black;

publicboolIsExist(inti,intj)///判断放棋子的位置是否已有棋子

{if(grid[i,j]!

=None)

returntrue;

else

returnfalse;

publicboolIsWin(inti,intj)///判断棋子落下后是否获胜

//与方格的第ij交叉点向四个方向的连子数,依次是水平,垂直,左上右下,左下右上

int[]numbers=newint[4];

numbers[0]=GetRowNumber(i,j);

numbers[1]=GetColumnNumber(i,j);

numbers[2]=GetBacklashNumber(i,j);

numbers[3]=GetSlashNumber(i,j);

for(intk=0;

k<

numbers.Length;

k++)//检查是否获胜

if(Math.Abs(numbers[k])==5)

returnfalse;

///判断横向相同颜色的棋子个数

privateintGetRowNumber(inti,intj)

intnum=1;

//连子个数

intx=i+1;

//向右检查

while(x<

15)

if(grid[x,j]==grid[i,j])//前方棋子与ij点不同时跳出循环

num++;

x++;

break;

x=i-1;

//向左检查

while(x>

=0)

{num++;

x--;

{break;

returnnum;

privateintGetColumnNumber(inti,intj)///判断纵向相同颜色的棋子个数

inty=j+1;

//向下检查

while(y<

if(grid[i,y]==grid[i,j])//前方棋子与ij点不同时跳出循环

y++;

//向上检查

y=j-1;

while(y>

//前方棋子与ij点不同时跳出循环

if(grid[i,y]==grid[i,j])

{num++;

y--;

//判断左上到右下相同颜色的棋子个数

privateintGetBacklashNumber(inti,intj)

//右下方向

15&

&

y<

if(grid[x,y]==grid[i,j])//前方棋子与ij点不同时跳出循环

y++;

//左上方向(x-,y-)

//不超出棋格

=0&

y>

if(grid[x,y]==grid[i,j])//前方棋子与ij点不同时跳出循环

x--;

privateintGetSlashNumber(inti,intj)//判断左下到右上相同颜色的棋子个数

intx=i-1;

x=i+1;

if(grid[x,y]==grid[i,j])

4.4添加一个名为Service.cs的类,代码如下:

usingSystem.Windows.Forms;

//显示或发送信息

classService

privateListBoxlistbox;

privatedelegatevoidAddListBoxItemCallback(stringstr);

privateAddListBoxItemCallbackaddListBoxItemCallback;

publicService(ListBoxlistbox)

this.listbox=listbox;

addListBoxItemCallback=newAddListBoxItemCallback(AddListBoxItem);

publicvoidAddListBoxItem(stringstr)

//比较调用AddListBoxItem方法的线程和创建listBox的线程是否为同一个线程

if(listbox.InvokeRequired==true)

listbox.Invoke(addListBoxItemCallback,str);

if(listbox.IsDisposed==false)

listbox.Items.Add(str);

listbox.SelectedIndex=listbox.Items.Count-1;

listbox.ClearSelected();

//将信息发送给指定的客户

publicvoidSendToOne(Useruser,stringstr)

try

user.sw.WriteLine(str);

user.sw.Flush();

AddListBoxItem(string.Format("

向{0}发送{1}"

user.userName,str));

catch

向{0}发送信息失败"

user.userName));

//将信息发送给指定房间的所有人

publicvoidSendToRoom(GameRoomgameRoom,stringstr)

//向玩家发送

gameRoom.gamePlayer.Length;

if(gameRoom.gamePlayer[i].GameUser!

=null)

SendToOne(gameRoom.gamePlayer[i].GameUser,str);

//向旁观者发送

gameRoom.lookOnUser.Count;

SendToOne(gameRoom.lookOnUser[i],str);

publicvoidSendToAll(System.Collections.Generic.List<

User>

userList,stringstr)

{

userList.Count;

SendToOne(userList[i],str);

4.5添加一个名为GameRoom.cs的类,游戏室内的每个小房间,代码如下:

classGameRoom

//进入房间的旁观者

publicList<

lookOnUser=newList<

();

//坐在玩家位置上的黑白两个游戏者

publicPlayer[]gamePlayer=newPlayer[2];

//黑方玩家

publicPlayerBlackPlayer

returngamePlayer[0];

gamePlayer[0]=value;

///白方玩家

publicPlayerWhitePlayer

returngamePlayer[1];

gamePlayer[1]=value;

privateGobangBoardgameBoard=newGobangBoard();

publicGobangBoardGameBoard

returngameBoard;

///向listbox中添加显示信息以及向客户发送信息

privateServiceservice;

//构造函数

publicGameRoom(ListBoxlistbox)

gamePlayer[0]=newPlayer();

gamePlayer[1]=newPlayer();

service=newService(listbox);

//将棋盘上的棋子全部清除

gameBoard.InitializeBoard();

//放置棋子

publicvoidSetChess(inti,intj,intchessColor)

//发送格式:

SetChess,行,列,颜色

gameBoard.Grid[i,j]=chessColor;

gameBoard.NextIndex=gameBoard.NextIndex==0?

1:

0;

service.SendToRoom(this,string.Format("

SetChess,{0},{1},{2}"

i,j,chessColor));

if(gameBoard.IsWin(i,j))

ShowWin(chessColor);

service.SendToRoom(this,"

NextChess,"

+gameBoard.NextIndex);

privatevoidShowWin(intchessColor)

gamePlayer[0].Start=false;

gamePlayer[1].Start=false;

Win,{0}"

chessColor));

4.6重命名Form1.cs为FormServer.cs,修改代码如下:

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Threading;

usingSystem.Net;

publicpartia

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1