1、3.1 开发环境:Microsoft Visual Studio 2008 Microsoft Visual Studio 2008是面向Windows Vista、Office 2007、Web 2.0的下一代开发工具,代号“Orcas”,是对Visual Studio 2005一次及时、全面的升级。VS2008引入了250多个新特性,整合了对象、关系型数据、XML的访问方式,语言更加简洁。使用Visual Studio 2008可以高效开发Windows应用。设计器中可以实时反映变更,XAML中智能感知功能可以提高开发效率。同时Visual Studio 2008支持项目模板、调试器和部署
2、程序。Visual Studio 2008可以高效开发Web应用,集成了AJAX 1.0,包含AJAX项目模板,它还可以高效开发Office应用和Mobile应用。3.2 引用命名空间以及相关类 命名空间: 除了一些基本的,本例中还引用了一些其他的命名空间,如下:System.IO 命名空间包含允许读写文件和数据流的类型以及提供基本文件和目录支持的类型。System.Net.Sockets 命名空间为需要严密控制网络访问的开发人员提供了 Windows Sockets (Winsock) 接口的托管实现。TcpClient、TcpListener和UdpClient类封装有关创建到 Inter
3、net 的 TCP 和 UDP 连接的详细信息。System.Threading 用于多线程编程,对线程进行管理,如创建线程、启动线程、终止线程、合并线程等等。System.Net 该命名空间为Internet网络上使用的多种协议提供了方便的编程接口,利用这个命名空间提供的类,不需要考虑所使用协议的具体细节,就能很快实现具体功能。 System.Windows.Forms命名空间包含用于创建基于 Windows 的应用程序的类,以充分利用 Microsoft Windows 操作系统中提供的丰富的用户界面功能相关类:BinaryReader 类,用特定的编码将基元数据类型读作二进制值。Bina
4、ryWriter类,以二进制形式将基元类型写入流,并支持用特定的编码写入字符串。TcpListener类,侦听来自 TCP 网络客户端的连接。Thread类,创建并控制线程,设置其优先级并获取其状态。IPaddress 类,提供网际协议 (IP) 地址。IPEndPoint类,将网络端点表示为 IP 地址和端口号。界面设计:使用的控件:(1)两个button负责启动和终止服务。(2)listBox显示客户状态信息以及服务器与客户端通信的内容。(3)两个textBox控件,分别控制游戏室允许进入的最多人数和游戏室同时开出的房间数。整体界面如图所示4服务器端主要代码介绍 运行VS,新建一个名为Go
5、bangServer的Windows应用程序项目。4.1 添加一个User.cs类,表示所有连接到服务器的客户。代码如下:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net.Sockets;using System.IO;namespace GobangServer class User public readonly TcpClient client; public readonly StreamReader sr; public readonly
6、 StreamWriter sw; public string userName; public User(TcpClient client) this.client = client; this.userName = ; NetworkStream netStream = client.GetStream(); sr = new StreamReader(netStream, System.Text.Encoding.Default); sw = new StreamWriter(netStream, System.Text.Encoding.Default); 4.2 添加一个名为Play
7、er.cs的类,表示坐在游戏桌两边的玩家,代码如下: class Player private User user; public User GameUser get return user; set user = value; private bool start; public bool Start return start; start = value; public Player() start = false; user = null;4.3 添加一个名为GobangBoard.cs的类,表示棋盘,代码如下: class GobangBoard public const int No
8、ne = -1; /无棋子 public const int Black = 0; /黑色棋子 public const int White = 1; /白色棋子 private int, grid = new int15, 15; /15*15的方格 / / 棋盘/summary public int, Grid return grid; private int nextIndex; public int NextIndex / 应该黑方放棋子还是白方放棋子 return nextIndex; nextIndex = value; public GobangBoard()/构造函数 Init
9、ializeBoard(); public void InitializeBoard() / 将15*15的方格中的每个交叉点均设置为无棋子 for (int i = 0; i = grid.GetUpperBound(0); i+) for (int j = 0; j = grid.GetUpperBound(1); j+) gridi, j = None; nextIndex = Black; public bool IsExist(int i, int j) / 判断放棋子的位置是否已有棋子 if (gridi, j != None) return true; else return f
10、alse; public bool IsWin(int i, int j) / 判断棋子落下后是否获胜 /与方格的第ij交叉点向四个方向的连子数,依次是水平,垂直,左上右下,左下右上 int numbers = new int4; numbers0 = GetRowNumber(i, j); numbers1 = GetColumnNumber(i, j); numbers2 = GetBacklashNumber(i, j); numbers3 = GetSlashNumber(i, j); for (int k = 0; k numbers.Length; k+) /检查是否获胜 if (
11、Math.Abs(numbersk) = 5) return false; / 判断横向相同颜色的棋子个数 private int GetRowNumber(int i, int j) int num = 1; /连子个数 int x = i + 1; /向右检查 while (x = 0) num+; x-; break; return num; private int GetColumnNumber(int i, int j) / 判断纵向相同颜色的棋子个数 int y = j + 1; /向下检查 while (y /前方棋子与ij点不同时跳出循环 if (gridi, y = grid
12、i, j) num+; y-;/ 判断左上到右下相同颜色的棋子个数 private int GetBacklashNumber(int i, int j) /右下方向 15 & y if (gridx, y = gridi, j) /前方棋子与ij点不同时跳出循环 x-; private int GetSlashNumber(int i, int j) / 判断左下到右上相同颜色的棋子个数 int x = i - 1; x = i + 1; if (gridx, y = gridi, j)4.4 添加一个名为Service.cs的类,代码如下:using System.Windows.Form
13、s; / 显示或发送信息 class Service private ListBox listbox; private delegate void AddListBoxItemCallback(string str); private AddListBoxItemCallback addListBoxItemCallback; public Service(ListBox listbox) this.listbox = listbox; addListBoxItemCallback = new AddListBoxItemCallback(AddListBoxItem); public voi
14、d AddListBoxItem(string str) /比较调用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(); / 将信息发送给指定的客户 p
15、ublic void SendToOne(User user, string str) try user.sw.WriteLine(str); user.sw.Flush(); AddListBoxItem(string.Format(向0发送1, user.userName, str); catch向0发送信息失败, user.userName); / 将信息发送给指定房间的所有人 public void SendToRoom(GameRoom gameRoom, string str) /向玩家发送 gameRoom.gamePlayer.Length; if (gameRoom.game
16、Playeri.GameUser != null) SendToOne(gameRoom.gamePlayeri.GameUser, str); /向旁观者发送 gameRoom.lookOnUser.Count; SendToOne(gameRoom.lookOnUseri, str); public void SendToAll(System.Collections.Generic.List userList, string str) userList.Count; SendToOne(userListi, str);4.5 添加一个名为GameRoom.cs的类,游戏室内的每个小房间,代
17、码如下: class GameRoom / 进入房间的旁观者 public List lookOnUser = new List(); / 坐在玩家位置上的黑白两个游戏者 public Player gamePlayer = new Player2; / 黑方玩家 public Player BlackPlayer return gamePlayer0; gamePlayer0 = value; / 白方玩家 public Player WhitePlayer return gamePlayer1; gamePlayer1 = value; private GobangBoard gameBo
18、ard = new GobangBoard(); public GobangBoard GameBoard return gameBoard; / 向listbox中添加显示信息以及向客户发送信息 private Service service; /构造函数 public GameRoom(ListBox listbox) gamePlayer0 = new Player(); gamePlayer1 = new Player(); service = new Service(listbox); /将棋盘上的棋子全部清除 gameBoard.InitializeBoard(); / 放置棋子
19、public void SetChess(int i, int j, int chessColor) /发送格式:SetChess,行,列,颜色 gameBoard.Gridi, 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
20、(this, NextChess, + gameBoard.NextIndex); private void ShowWin(int chessColor) gamePlayer0.Start = false; gamePlayer1.Start = false;Win,0, chessColor);4.6 重命名Form1.cs为FormServer.cs,修改代码如下:using System.ComponentModel;using System.Data;using System.Drawing;using System.Threading;using System.Net; public partia
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1