实验二五子棋程序_第1页
实验二五子棋程序_第2页
实验二五子棋程序_第3页
实验二五子棋程序_第4页
实验二五子棋程序_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、C#网络应用编程实验报告 实验题目:网络五子棋游戏 姓 名: 班 级:网络工程 学 号:一、实验目的1、熟悉基于C#的TCP编程2、练习NetworkStream的用法。 3、练习BinaryRead和BinaryWriter4、掌握线程的创建和使用方法。 5、练习解决TCP协议消息边界问题的另一种方法。 二、实验内容开发一个简单的基于TCP协议的网络五子棋游戏。(1)界面模仿教材例5-2。(2)服务器可以同时服务多桌,每桌允许两个玩家通过因特网对弈。(3) 允许玩家自由选择坐在哪一桌的哪一方。如果两个玩家坐在同一桌,双方应都能看到对方的状态。两个玩家均单击“开始”按钮,游戏就开始了。(4)某

2、桌游戏开始后,客户在固定的时间间隔在1515的棋盘方格内轮番下子,客户端在1515棋盘的相应位置显示棋子。(5)玩家坐到游戏桌座位上后,不论游戏是否开始,该玩家都可以随时调整服务器发送棋子的时间间隔。三、实验步骤及相关代码1、服务器端 GameServer(1)服务器端主要接收客户端的请求并对其进行处理和应答,实时对在线用户的信息进行监控。服务器启动后,需要创建一个线程专门用于监听玩家连接请求。在监听线程中,服务器一旦接受一个连接,就创建一个线程与该玩家对应的线程,用于接收该玩家发送的信息,并根据玩家发送的信息来提供相应的服务。(2)服务器端的具体设计服务器端包括:User.cs,用于保存与该

3、玩家的通信所需要的信息;Player.cs,用于保存已经坐到游戏桌位上玩家的情况;Service.cs,用于提供公用的方法;GameRoom.cs,用来保存玩家游戏的顺序分配及判定输赢;五子棋的代码GobangBoard.cs;FormServer作为主窗体。服务器的登录界面如下:(3) 主要实现代码:1) FormServer上实现监听用户的主要代码: /创建一个线程监听客户端连接请求 ThreadStart ts = new ThreadStart(ListenClientConnect); Thread myThread = new Thread(ts); myThread.Start(

4、); buttonStart.Enabled = false; buttonStop.Enabled = true; /判断用户是否连接服务器成功 private void ListenClientConnect() while (true) TcpClient newClient = null; try newClient = myListener.AcceptTcpClient(); catch break; ParameterizedThreadStart pts = new ParameterizedThreadStart(ReceiveData); Thread threadRece

5、ive = new Thread(pts); User user = new User(newClient); threadReceive.Start(user); userList.Add(user); service.AddListBoxItem(string.Format(0连接服务器成功。, newClient.Client.RemoteEndPoint); service.AddListBoxItem(string.Format(当前连接用户数:0, userList.Count); /判断当前用户连接是否正常private void ReceiveData(object obj)

6、User user = (User)obj; TcpClient client = user.client; bool normalExit = false; bool exitWhile = false; while (exitWhile = false) string receiveString = null; try receiveString = user.sr.ReadLine(); catch service.AddListBoxItem(接收数据失败); RemovePlayerfromRoom(user); if (receiveString = null) if (norma

7、lExit = false) if (client.Connected = true) service.AddListBoxItem(string.Format( 与0失去联系,已终止接收该用户信息, client.Client.RemoteEndPoint); RemovePlayerfromRoom(user); 2)五子棋主要代码,判断用户棋子位置并判断输赢class GobangBoard public const int None = -1;/无棋子 public const int Black = 0;/黑色棋子 public const int White = 1;/白色棋子 p

8、rivate int, grid = new int15, 15;/15*15的方格 public int, Grid get return grid; private int nextIndex; public int NextIndex get return nextIndex; set nextIndex = value; public GobangBoard() InitializeBoard(); public void InitializeBoard() for (int i = 0; i = grid.GetUpperBound(0); i+) for (int j = 0; j

9、 = grid.GetUpperBound(1); j+) gridi, j = None; nextIndex = Black; /判断下棋位置是否有棋子 public bool IsExist(int i, int j) if (gridi, j != None) return true; else return false; /获取棋子落下后是否获胜 public bool IsWin(int i, int j) /与方格的第i,j交叉点向四个方向的连子数,依次是水平,垂直,左上右下,左下右上 int numbers = new int4; numbers0 = GetRowNumber

10、(i, j); numbers1 = GetColumnNumber(i, j); numbers2 = GetBacklashNumber(i, j); numbers3 = GetSlashNumber(i, j); /检查是否获胜 for (int k = 0; k numbers.Length; k+) if (Math.Abs(numbersk) = 5) return true; return false; /判断横向相同颜色的棋子个数 private int GetRowNumber(int i, int j) /连子个数 int num = 1; /向右检查 int x = i

11、 + 1; while (x = 0) /前方棋子与i,j点不同时跳出循环 if (gridx, j = gridi, j) num+; x-; else break; return num; / 判断纵向相同颜色的棋子个数 private int GetColumnNumber(int i, int j) /连子个数 int num = 1; /向下检查 int y = j + 1; while (y = 0) /前方的棋子与i,j点不同时跳出循环 if (gridi, y = gridi, j) num+; y-; else break; return num; / 判断左上到右下相同颜色

12、的棋子个数 private int GetBacklashNumber(int i, int j) /连子个数 int num = 1; /右下方向 int x = i + 1; int y = j + 1; while (x 15 & y = 0 & y = 0) /前方的棋子与i,j点不同时跳出循环 if (gridx, y = gridi, j) num+; x-; y-; else break; return num; / 判断左下到右上相同颜色的棋子个数 private int GetSlashNumber(int i, int j) /连子个数 int num = 1; int x

13、 = i - 1; int y = j + 1; while (x = 0 & y 15) if (gridx, y = gridi, j) num+; x-; y+; else break; x = i + 1; y = j - 1; while (x = 0) if (gridx, y = gridi, j) num+; x+; y-; else break; return num; 2、 客户端GameClient对战界面:(1)客户端的主要实现代码:1)用户登录的实现: private void buttonConnect_Click(object sender, EventArgs

14、e) if (textBoxName.Text.Trim().Length = 0) /由于每桌根据昵称区分到底是哪个用户,所以要求必须有昵称 MessageBox.Show(请输入昵称。, , MessageBoxButtons.OK, MessageBoxIcon.Information); return; if (textBoxServer.Text.Length = 0) /如果不输入服务器DNS名称,则直接使用本机IP try client = new TcpClient(Dns.GetHostName(), 51888); catch MessageBox.Show(与服务器连接失

15、败, , MessageBoxButtons.OK, MessageBoxIcon.Information); return; else /如果输入服务器DNS名称,则用服务器DNS或IP创建TcpClient实例 try client = new TcpClient(textBoxServer.Text, 51888); catch MessageBox.Show(与服务器连接失败, , MessageBoxButtons.OK, MessageBoxIcon.Information); return; /由于已经与服务器连接上,不允许再与服务器建立连接 EnableConnectToSer

16、verAgain(false); /获取网络流 NetworkStream netStream = client.GetStream(); /用于接收信息 sr = new StreamReader(netStream, System.Text.Encoding.Default); /用于发送信息 sw = new StreamWriter(netStream, System.Text.Encoding.Default); /登录服务器,获取服务器各桌信息 /格式:Login,昵称 SendToServer(Login, + textBoxName.Text.Trim(); Thread th

17、readReceive = new Thread(new ThreadStart(ReceiveData); threadReceive.Start(); 2) 为在线的用户分配对手和游戏房间 switch (splitString0) case Login: /格式:Login,用户名 /该用户进入游戏室 AddToRichTextBox(string.Format(0进入游戏室。, splitString1); break; case Logout: /格式:Logout,用户名 /用户退出游戏室 if (splitString1 = textBoxName.Text) isNeedExi

18、tWhile = true; else AddToRichTextBox(string.Format(0退出游戏室。, splitString1); break; case Sorry: MessageBox.Show(游戏室人员已满,请等会再进入。, , MessageBoxButtons.OK, MessageBoxIcon.Information); isNeedLoginAgain = true; isNeedExitWhile = true; break; case NameUsed: MessageBox.Show(已有人用过该昵称,请更换昵称!, , MessageBoxButt

19、ons.OK, MessageBoxIcon.Information); isNeedLoginAgain = true; isNeedExitWhile = true; break; case RoomNumbers: /格式:RoomNumbers,房间数,已经进入游戏室的人数 this.roomNumbers = int.Parse(splitString1); AddTablesToPanel(); if (isOnlineNumberDisplayed = false) AddToRichTextBox(string.Format(游戏室原有人数:0人, int.Parse(spli

20、tString2) - 1); AddToRichTextBox(-); isOnlineNumberDisplayed = true; break; case Tables: /字符串格式:Tables,字符串 /每桌之间用逗号分隔 /每桌格式:黑方(1位)+昵称/白方(1位)+昵称 /0表示无人,1表示有人 string s = receiveString.Substring(7); /去掉前面的Tables, /表示是从服务器接收的信息,在用户控件的Validating事件中不处理各桌变化 isReceiveCommand = true; string s1 = s.Split(,);

21、for (int i = 0; i s1.Length; i+) string s2 = s1i.Split(/); if (s200 = 0) SetBoolProperty(tablesi, 0, false); SetTextProperty(tablesi, 0, ); else SetBoolProperty(tablesi, 0, true); SetTextProperty(tablesi, 0, s20.Substring(1); if (s210 = 0) SetBoolProperty(tablesi, 1, false); SetTextProperty(tablesi,

22、 1, ); else SetBoolProperty(tablesi, 1, true); SetTextProperty(tablesi, 1, s21.Substring(1); /为在用户控件中处理做准备 isReceiveCommand = false; break; case EnterRoom: /格式:EnterRoom,类别号,用/分隔的黑白旁观名,用/分隔的该房间所有人员 if (isCreatedRoom = false) CreateRoom(tableIndex); isCreatedRoom = true; playingTable.SetEnterUserTips

23、( splitString1, splitString2, splitString3); break; case Grid: /格式:Grid,棋盘上各子位置字符串 if (isCreatedRoom = false) CreateRoom(tableIndex); isCreatedRoom = true; playingTable.InitializeGrid(splitString1); break; case ExitRoom: /格式:ExitRoom,类别号,用/分隔的黑方白方及旁观名 if (side = int.Parse(splitString1) /自己离座,则肯定是自己关

24、闭playingTable窗体引起的 side = -1; isCreatedRoom = false; ShowNormalWindow(); else /对方或者旁观者离座,则playingTable窗体肯定未关闭 playingTable.SetExitUserTips(splitString1, splitString2); if (splitString1 != 2) playingTable.Restart(); break; case Start: /格式:Start playingTable.Start(true); /继续执行case NextChess goto case

25、NextChess; case Restart: /格式:Restart playingTable.Restart(); goto case NextChess; case NextChess: /格式:NextChess,颜色 if (splitString1 = 0) playingTable.SetLabelGo(现在该黑方放棋子, 0); else playingTable.SetLabelGo(现在该白方放棋子, 1); break; case SetChess: / 放置的棋子位置信息 /格式:SetChess,行,列,颜色 playingTable.SetChess( int.P

26、arse(splitString1), int.Parse(splitString2), int.Parse(splitString3); break; case Win: /格式:Win,胜方棋子的颜色 string winner = ; if (int.Parse(splitString1) = ChessColor.Black) winner = 黑方胜利!; else winner = 白方胜利!; playingTable.ShowMessage(winner); playingTable.Restart(winner); break; case AskTie: /格式:AskTie,座位号 playingTable.AskTie(splitString1

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论