九江学院实训报告实训名称:Java编程指导老师:李兴南雷波姓名:肖世明学号:***********班级:理学A1221提交日期:2015年10月20日1.实训目的:通过本实训,应达到如下目标:理解委托的概念,掌握线程之间通过委托与控件交互的方法。
理解异常处理的机制,掌握异常处理语句的用法。
理解线程的概念,掌握多线程编程的一般方法。
掌握基于TCP的应用编程技术。
2.实训题目:通过开发一款网络对战五子棋游戏,熟练掌握Java网络编程技术,亲历一个项目开发的流程3.功能描述:程序基本功能描述如下:实训项目要求使用Java编程语言,基于客户机/服务器(C/S)模式,开发一款网络对战五子棋游戏。
服务器端可以支持多玩家同时在线游戏。
玩家可以通过客户端登陆到服务器端与其它玩家进行五子棋游戏对战。
项目基本要求描述如下:1.当服务器“启动服务”之后,可以允许玩家连接服务器;当服务器“停止服务”之后,不允许玩家连接服务器。
2.玩家请求登陆时需提交用户名,在没有“满员”和“重名”的情况下,服务器允许玩家登陆;在“满员”或“重名”的情况下,服务器拒绝玩家登陆。
3.玩家登陆成功之后,进入“游戏大厅”界面,并获得其他在线玩家的信息,其他玩家也能获得该玩家的信息。
4.玩家注销登陆之后,断开与服务器的连接,并关闭“游戏大厅”界面。
5.玩家“落座”之后,进入“游戏桌”界面,并获得该桌玩家的信息。
如果有对家落座,对家也能获得该玩家落座的信息。
其他玩家也能获得该玩家的状态的改变。
6.玩家“起身”之后,进入“游戏大厅”界面。
如果有对家落座,对家也能获得该玩家起身的信息。
其他玩家也能获得该玩家的状态的改变。
7.玩家“请求开始”游戏之后,初始化“棋盘”界面,玩家进入“就绪”状态。
如果有对家也处于“就绪”状态,则开始游戏。
其他玩家也能获得该玩家的状态的改变。
8.游戏开始之后,首先黑方“行棋”,之后双方轮流行棋。
9.玩家“落子”之后,对家也能获得“棋盘”状态的改变。
如果玩家“获胜”,对家能得知自己“失败”;否则双方继续“行棋”。
10.玩家“请求和棋”之后,对家能获得其请求。
对家可以“同意和棋”,也可以“拒绝和棋”。
如果对家同意和棋,则和局;否则游戏继续进11.上述要求是本项目的基本要求,也可新增积分排行榜和保存棋局等功能。
4.设计说明:服务器功能(1)服务器提供一个五子棋的游戏厅,拥有多个游戏桌(1~100),每桌有两个座位,0代表黑方,1代表白方。
(2)服务器启动服务后,创建一个线程专门监听玩家的连接请求。
服务器一旦接受一个连接,就创建一个与玩家对应的线程,用于接受玩家信息,并提供相应的服务。
玩家退出后,其对应的线程便自动终止,并给出相应提示。
可限制玩家人数(1~300)。
客户端功能(1)客户端连接成功后,创建一个接收线程,用于接受服务器发送信息。
(2)接受的信息处理。
5.源代码(1)sever:GameTable:class GameTable{private const int None = -1;private const int Black = 0;private const int White = 1;public Player[] gamePlayer;private int[,] grid = new int[15, 15];private System.Timers.Timer timer;private int NextdotColor = 0;private ListBox listbox;Random rnd = new Random();Service service;private int NextPlayer = -1;private int gameType = 1;public GameTable(ListBox listbox){gamePlayer = new Player[2];gamePlayer[0] = new Player();gamePlayer[1] = new Player();timer = new System.Timers.Timer();timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); timer.Enabled = false;this.listbox = listbox;service = new Service(listbox);ResetGrid();public void ResetGrid(){for (int i = 0; i <= grid.GetUpperBound(0); i++){for (int j = 0; j <= grid.GetUpperBound(1); j++) {grid[i, j] = None;}}gamePlayer[0].grade = 0;gamePlayer[1].grade = 0;}public void StartTimer(){if (NextdotColor == 0){NextPlayer = 0;NextdotColor = 1;}}public void StopTimer(){}public void SetTimerLevel(int interval){gameType = interval;}private void timer_Elapsed(object sender, EventArgs e) {}public void SetDot(int i, int j, int dotColor){grid[i, j] = dotColor;service.SendToBoth(this, string.Format("SetDot,{0},{1},{2}", i, j, dotColor));if (gameType == 1){if ((i > 1 && grid[i - 1, j] == dotColor && grid[i - 2, j] == dotColor) || (i > 0 && i < grid.GetUpperBound(0) - 1 && grid[i - 1, j] == dotColor && grid[i + 1, j] == dotColor)|| (i < grid.GetUpperBound(0) - 2 && grid[i + 1, j] == dotColor && grid[i + 2, j] == dotColor)){ShowWin(dotColor);}if ((j > 1 && grid[i, j - 1] == dotColor && grid[i, j - 2] == dotColor) || (j > 0 && j < grid.GetUpperBound(1) - 1 && grid[i, j - 1] == dotColor && grid[i, j + 1] == dotColor)|| (j < grid.GetUpperBound(1) - 2 && grid[i, j + 1] == dotColor && grid[i, j + 2] == dotColor)){ShowWin(dotColor);}if ((i > 1 && j > 1 && grid[i - 1, j - 1] == dotColor && grid[i - 2, j - 2] == dotColor)|| (i > 0 && i < grid.GetUpperBound(0) - 1 && j > 0 && j <grid.GetUpperBound(1) - 1 && grid[i - 1, j - 1] == dotColor && grid[i + 1, j + 1] == dotColor) || (i < grid.GetUpperBound(0) - 2 && j < grid.GetUpperBound(1) - 2 && grid[i + 1, j + 1] == dotColor && grid[i + 2, j + 2] == dotColor)){ShowWin(dotColor);}if((i > 1 && j < grid.GetUpperBound(1) - 2 && grid[i - 1, j + 1] == dotColor && grid[i - 2, j + 2] == dotColor)|| (i > 0 && i < grid.GetUpperBound(0) - 1 && j > 0 && j <grid.GetUpperBound(1) - 1 && grid[i - 1, j + 1] == dotColor && grid[i + 1, j - 1] == dotColor) || (i < grid.GetUpperBound(0) - 2 && j > 1 && grid[i + 1, j - 1] == dotColor && grid[i + 2, j - 2] == dotColor)){ShowWin(dotColor);}}else{if ((i > 3 && grid[i - 1, j] == dotColor && grid[i - 2, j] == dotColor && grid[i - 3, j] == dotColor && grid[i - 4, j] == dotColor)|| (i > 2 && i < grid.GetUpperBound(0) - 1 && grid[i - 1, j] == dotColor && grid[i - 2, j] == dotColor && grid[i - 3, j] == dotColor && grid[i + 1, j] == dotColor) || (i > 1 && i < grid.GetUpperBound(0) - 2 && grid[i - 1, j] == dotColor && grid[i - 2, j] == dotColor && grid[i + 1, j] == dotColor && grid[i + 2, j] == dotColor) || (i > 0 && i < grid.GetUpperBound(0) - 3 && grid[i - 1, j] == dotColor && grid[i + 1, j] == dotColor && grid[i + 2, j] == dotColor && grid[i + 3, j] == dotColor) || (i < grid.GetUpperBound(0) - 4 && grid[i + 1, j] == dotColor && grid[i + 2, j] == dotColor && grid[i + 3, j] == dotColor && grid[i + 4, j] == dotColor)){ShowWin(dotColor);}if ((j > 3 && grid[i, j - 1] == dotColor && grid[i, j - 2] == dotColor && grid[i, j - 3] == dotColor && grid[i, j - 4] == dotColor)|| (j > 2 && j < grid.GetUpperBound(1) - 1 && grid[i, j - 1] == dotColor && grid[i, j - 2] == dotColor && grid[i, j - 3] == dotColor && grid[i, j + 1] == dotColor) || (j > 1 && j < grid.GetUpperBound(1) - 2 && grid[i, j - 1] == dotColor && grid[i, j - 2] == dotColor && grid[i, j + 1] == dotColor && grid[i, j + 2] == dotColor) || (j > 0 && j < grid.GetUpperBound(1) - 3 && grid[i, j - 1] == dotColor && grid[i, j + 1] == dotColor && grid[i, j + 2] == dotColor && grid[i, j + 3] == dotColor) || (j < grid.GetUpperBound(1) - 4 && grid[i, j + 1] == dotColor && grid[i, j + 2] == dotColor && grid[i, j + 3] == dotColor && grid[i, j + 4] == dotColor)){ShowWin(dotColor);}if ((i > 3 && j > 3 && grid[i - 1, j - 1] == dotColor && grid[i - 2, j - 2] == dotColor && grid[i - 3, j - 3] == dotColor && grid[i - 4, j - 4] == dotColor)|| (i > 2 && i < grid.GetUpperBound(0) - 1 && j > 2 && j <grid.GetUpperBound(1) - 1 && grid[i - 1, j - 1] == dotColor && grid[i - 2, j - 2] == dotColor && grid[i - 3, j - 3] == dotColor && grid[i + 1, j + 1] == dotColor)|| (i > 1 && i < grid.GetUpperBound(0) - 2 && j > 1 && j <grid.GetUpperBound(1) - 2 && grid[i - 1, j - 1] == dotColor && grid[i - 2, j - 2] == dotColor && grid[i + 1, j + 1] == dotColor && grid[i + 2, j + 2] == dotColor)|| (i > 0 && i < grid.GetUpperBound(0) - 3 && j > 0 && j <grid.GetUpperBound(1) - 3 && grid[i - 1, j - 1] == dotColor && grid[i + 1, j + 1] == dotColor && grid[i + 2, j + 2] == dotColor && grid[i + 3, j + 3] == dotColor)|| (i < grid.GetUpperBound(0) - 4 && j < grid.GetUpperBound(1) - 4 && grid[i + 1, j + 1] == dotColor && grid[i + 2, j + 2] == dotColor && grid[i + 3, j + 3] == dotColor && grid[i + 4, j + 4] == dotColor)){ShowWin(dotColor);}if((i > 3 && j < grid.GetUpperBound(1) - 4 && grid[i - 1, j + 1] == dotColor && grid[i - 2, j + 2] == dotColor && grid[i - 3, j + 3] == dotColor && grid[i - 4, j + 4] == dotColor)|| (i > 2 && i < grid.GetUpperBound(0) - 1 && j > 0 && j <grid.GetUpperBound(1) - 3 && grid[i - 1, j + 1] == dotColor && grid[i - 2, j + 2] == dotColor && grid[i - 3, j + 3] == dotColor && grid[i + 1, j - 1] == dotColor)|| (i > 1 && i < grid.GetUpperBound(0) - 2 && j > 1 && j <grid.GetUpperBound(1) - 2 && grid[i - 1, j - 1] == dotColor && grid[i - 2, j - 2] == dotColor && grid[i + 1, j + 1] == dotColor && grid[i + 2, j + 2] == dotColor)|| (i > 0 && i < grid.GetUpperBound(0) - 3 && j > 2 && j <grid.GetUpperBound(1) - 1 && grid[i - 1, j + 1] == dotColor && grid[i + 1, j - 1] == dotColor && grid[i + 2, j - 2] == dotColor && grid[i + 3, j - 3] == dotColor)|| (i < grid.GetUpperBound(0) - 4 && j > 3 && grid[i + 1, j - 1] == dotColor && grid[i + 2, j - 2] == dotColor && grid[i + 3, j - 3] == dotColor && grid[i + 4, j - 4] == dotColor)){ShowWin(dotColor);}}}private void ShowWin(int dotColor){timer.Enabled = false;gamePlayer[0].started = false;gamePlayer[1].started = false;this.ResetGrid();service.SendToBoth(this, string.Format("Win,{0},{1},{2}",dotColor, gamePlayer[0].grade, gamePlayer[1].grade));}public void UnsetDot(int i, int j, int color){if (color == NextPlayer){SetDot(i, j, color);NextPlayer = (NextPlayer + 1) % 2;}}}Player:struct Player{public User user;public bool started;public int grade;public bool someone;}Service:class Service{private ListBox listbox;private delegate void AddItemDelegate(string str);private AddItemDelegate addItemDelegate;public Service(ListBox listbox){this.listbox = listbox;addItemDelegate = new AddItemDelegate(AddItem);}public void AddItem(string str){if (listbox.InvokeRequired){listbox.Invoke(addItemDelegate, str);}else{listbox.Items.Add(str);listbox.SelectedIndex = listbox.Items.Count - 1; listbox.ClearSelected();}}public void SendToOne(User user, string str){try{user.sw.WriteLine(str);user.sw.Flush();AddItem(string.Format("向{0}发送{1}", erName, str));}catch{AddItem(string.Format("向{0}发送信息失败", erName));}}public void SendToBoth(GameTable gameTable, string str){for (int i = 0; i < 2; i++){if (gameTable.gamePlayer[i].someone == true){SendToOne(gameTable.gamePlayer[i].user, str);}}}public void SendToAll(System.Collections.Generic.List<User> userList, string str) {for (int i = 0; i < userList.Count; i++){SendToOne(userList[i], str);}}}User:class User{public TcpClient client { get; private set; }public StreamReader sr { get; private set; }public StreamWriter sw { get; private set; }public string userName { get; set; }public User(TcpClient client){this.client = client;erName = "";NetworkStream netStream = client.GetStream();sr = new StreamReader(netStream, System.Text.Encoding.UTF8);sw = new StreamWriter(netStream, System.Text.Encoding.UTF8);}}Form1:public partial class Form1 : Form{private int maxUsers = 100;System.Collections.Generic.List<User> userList = new List<User>();private int maxTables = 50;private GameTable[] gameTable;IPAddress localAddress;private int port = 51888;private TcpListener myListener;private Service service;public Form1(){InitializeComponent();service = new Service(listBox1);}private void btn1_Click(object sender, EventArgs e){listBox1.Items.Clear();gameTable = new GameTable[maxTables];for (int i = 0; i < maxTables; i++){gameTable[i] = new GameTable(listBox1);}myListener = new TcpListener(localAddress, port);myListener.Start();service.AddItem(string.Format("开始在{0}:{1}监听客户连接", localAddress, port));ThreadStart ts = new ThreadStart(ListenClientConnect);Thread myThread = new Thread(ts);myThread.Start();btn1.Enabled = false;btn2.Enabled = true;}private void btn2_Click(object sender, EventArgs e){for (int i = 0; i < maxTables; i++){gameTable[i].StopTimer();}service.AddItem(string.Format("目前连接用户数:{0}", userList.Count)); service.AddItem("开始停止服务,并依次使用户退出!");for (int i = 0; i < userList.Count; i++){userList[i].client.Close();}myListener.Stop();btn1.Enabled = true;btn2.Enabled = false;}private void Form1_Load(object sender, EventArgs e){listBox1.HorizontalScrollbar = true;IPAddress[] addrIP = Dns.GetHostAddresses(Dns.GetHostName());localAddress = addrIP[0];btn2.Enabled = false;}private void ListenClientConnect(){while (true){TcpClient newClient = null;try{newClient = myListener.AcceptTcpClient();}catch{break;}ParameterizedThreadStart pts = new ParameterizedThreadStart(ReceiveData);Thread threadReceive = new Thread(pts);User user = new User(newClient);threadReceive.Start(user);userList.Add(user);service.AddItem(string.Format("{0}进入",newClient.Client.RemoteEndPoint));service.AddItem(string.Format("当前连接用户数:{0}", userList.Count)); }}private void ReceiveData(object obj){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.AddItem("接收数据失败");}if (receiveString == null){if (normalExit == false){if (client.Connected == true){service.AddItem(string.Format("与{0}失去联系,已终止接收该用户信息",client.Client.RemoteEndPoint));}RemoveClientfromPlayer(user);}break;}service.AddItem(string.Format("来自{0}:{1}", erName, receiveString));string[] splitString = receiveString.Split(',');int tableIndex = -1;int side = -1;int anotherSide = -1;string sendString = "";string command = splitString[0].ToLower();switch (command){case"login":if (userList.Count > maxUsers){sendString = "Sorry";service.SendToOne(user, sendString);service.AddItem("人数已满,拒绝" +splitString[1] + "进入游戏室");exitWhile = true;}else{erName = string.Format("[{0}--{1}]", splitString[1], client.Client.RemoteEndPoint);sendString = "Tables," + this.GetOnlineString();service.SendToOne(user, sendString);}break;case"logout":service.AddItem(string.Format("{0}退出游戏室", erName)); normalExit = true;exitWhile = true;break;case"sitdown":tableIndex = int.Parse(splitString[1]);side = int.Parse(splitString[2]);gameTable[tableIndex].gamePlayer[side].user = user;gameTable[tableIndex].gamePlayer[side].someone = true;service.AddItem(string.Format("{0}在第{1}桌第{2}座入座",erName, tableIndex + 1, side + 1));anotherSide = (side + 1) % 2;if (gameTable[tableIndex].gamePlayer[anotherSide].someone == true){sendString = string.Format("SitDown,{0},{1}", anotherSide,gameTable[tableIndex].gamePlayer[anotherSide]erName);service.SendToOne(user, sendString);}sendString = string.Format("SitDown,{0},{1}", side,erName);service.SendToBoth(gameTable[tableIndex], sendString);service.SendToAll(userList, "Tables," + this.GetOnlineString());break;case"getup":tableIndex = int.Parse(splitString[1]);side = int.Parse(splitString[2]);service.AddItem(string.Format("{0}离座,返回游戏室", erName));gameTable[tableIndex].StopTimer();service.SendToBoth(gameTable[tableIndex],string.Format("GetUp,{0},{1}", side, erName));gameTable[tableIndex].gamePlayer[side].someone = false;gameTable[tableIndex].gamePlayer[side].started = false;gameTable[tableIndex].gamePlayer[side].grade = 0;anotherSide = (side + 1) % 2;if (gameTable[tableIndex].gamePlayer[anotherSide].someone ==true){gameTable[tableIndex].gamePlayer[anotherSide].started = false;gameTable[tableIndex].gamePlayer[anotherSide].grade = 0;}service.SendToAll(userList, "Tables," + this.GetOnlineString());break;case"level":tableIndex = int.Parse(splitString[1]);gameTable[tableIndex].SetTimerLevel(int.Parse(splitString[2])); service.SendToBoth(gameTable[tableIndex], receiveString);break;case"talk":tableIndex = int.Parse(splitString[1]);sendString = string.Format("Talk,{0},{1}", erName,receiveString.Substring(splitString[0].Length +splitString[1].Length));service.SendToBoth(gameTable[tableIndex], sendString);break;case"start":tableIndex = int.Parse(splitString[1]);side = int.Parse(splitString[2]);gameTable[tableIndex].gamePlayer[side].started = true;if (side == 0){anotherSide = 1;sendString = "Message,黑方已开始。