1、服务器端界面客户端界面棋盘界面决出胜负界面若一方下线,弹出的窗口如果服务器关闭出现的对话框退出游戏前的对话框登录前的设置客户端源代码using System;using System.Drawing;using System.Windows.Forms;using System.IO;namespace GameClientpublic partial class FormPlaying : Formprivate int tableIndex;private int side;private DotColor, grid = new DotColor15, 15;private Bitmap
2、 blackBitmap;private Bitmap whiteBitmap;private bool isReceiveCommand = false;private Service service;delegate void LabelDelegate(Label label, string str);delegate void ButtonDelegate(Button button, bool flag);delegate void RadioButtonDelegate(RadioButton radioButton, bool flag);delegate void SetDot
3、Delegate(int i, int j, int dotColor);LabelDelegate labelDelegate;ButtonDelegate buttonDelegate;RadioButtonDelegate radioButtonDelegate;public FormPlaying(int TableIndex, int Side, StreamWriter sw)InitializeComponent();this.tableIndex = TableIndex;this.side = Side;labelDelegate = new LabelDelegate(Se
4、tLabel);buttonDelegate = new ButtonDelegate(SetButton);radioButtonDelegate = new RadioButtonDelegate(SetRadioButton);blackBitmap = new Bitmap(Properties.Resources.black);whiteBitmap = new Bitmap(Properties.Resources.white);service = new Service(listBox1, sw);private void FormPlaying_Load(object send
5、er, EventArgs e) radioButton3.Checked = true;for (int i = 0; i 15 | y 15)if (gridx - 1, y - 1 != DotColor.None)int color = (int)gridx - 1, y - 1; service.SendToServer(string.Format(“UnsetDot,0,1,2,3,4“, tableIndex, side, x - 1, y - 1, color);public void SetTableSideText(string sideString, string lab
6、elSideString, string listBoxString)string s = “白方“;if (sideString = “0“)s = “黑方:“; if (sideString = side.ToString()SetLabel(labelSide1, s + labelSideString);elseSetLabel(labelSide0, s + labelSideString);service.AddItemToListBox(listBoxString);public void SetGradeText(string str0, string str1)if (sid
7、e = (int)DotColor.Black)SetLabel(labelGrade1, str0);SetLabel(labelGrade0, str1);elseSetLabel(labelGrade0, str0);SetLabel(labelGrade1, str1);public void ShowTalk(string talkMan, string str)service.AddItemToListBox(string.Format(“0说:1“, talkMan, str);public void ShowMessage(string str)service.AddItemT
8、oListBox(str);服务器源代码using System;using System.Collections.Generic;using System.Windows.Forms;using System.Net;using System.Net.Sockets;using System.Threading;namespace GameServerpublic partial class FormServer : Formprivate int maxUsers;System.Collections.Generic.List userList = new List();private i
9、nt maxTables;private GameTable gameTable;IPAddress localAddress;private int port = 51888;private TcpListener myListener;private Service service;public FormServer()InitializeComponent();service = new Service(listBox1);private void FormServer_Load(object sender, EventArgs e)listBox1.HorizontalScrollba
10、r = true;IPAddress addrIP = Dns.GetHostAddresses(Dns.GetHostName();localAddress = addrIP0;buttonStop.Enabled = false;private void buttonStart_Click(object sender, EventArgs e)if (int.TryParse(textBoxMaxTables.Text, out maxTables) = false| int.TryParse(textBoxMaxUsers.Text, out maxUsers) = false)Mess
11、ageBox.Show(“请输入在规定范围内的正整数 “);return;if (maxUsers 300)MessageBox.Show(“允许进入的人数只能在 1-300 之间“);return;if (maxTables 100)MessageBox.Show(“允许的桌数只能在 1-100 之间“);return;textBoxMaxUsers.Enabled = false;textBoxMaxTables.Enabled = false;gameTable = new GameTablemaxTables;for (int i = 0; i maxUsers)sendString
12、= “Sorry“;service.SendToOne(user, sendString);service.AddItem(“人数已满,拒绝“ +splitString1 + “进入游戏室“);exitWhile = true;elseuser.userName = string.Format(“0-1“, splitString1,client.Client.RemoteEndPoint);sendString = “Tables,“ + this.GetOnlineString();service.SendToOne(user, sendString);break;case “logout
13、“:service.AddItem(string.Format(“0退出游戏室“, user.userName);normalExit = true;exitWhile = true;break;case “sitdown“:tableIndex = int.Parse(splitString1);side = int.Parse(splitString2);gameTabletableIndex.gamePlayerside.user = user;gameTabletableIndex.gamePlayerside.someone = true;service.AddItem(string
14、.Format(“0在第1桌第2座入座“,user.userName, tableIndex + 1, side + 1);anotherSide = (side + 1) % 2;if (gameTabletableIndex.gamePlayeranotherSide.someone = true)sendString = string.Format(“SitDown,0,1“, anotherSide,gameTabletableIndex.gamePlayeranotherSide.user.userName);service.SendToOne(user, sendString);s
15、endString = string.Format(“SitDown,0,1“, side, user.userName);service.SendToBoth(gameTabletableIndex, sendString);service.SendToAll(userList, “Tables,“ + this.GetOnlineString();break;case “getup“:tableIndex = int.Parse(splitString1);side = int.Parse(splitString2);service.AddItem(string.Format(“0离座,
16、返回游戏室“, user.userName);gameTabletableIndex.StopTimer();service.SendToBoth(gameTabletableIndex,string.Format(“GetUp,0,1“, side, user.userName);gameTabletableIndex.gamePlayerside.someone = false;gameTabletableIndex.gamePlayerside.started = false;gameTabletableIndex.gamePlayerside.grade = 0;anotherSide
17、 = (side + 1) % 2;if (gameTabletableIndex.gamePlayeranotherSide.someone = true)gameTabletableIndex.gamePlayeranotherSide.started = false;gameTabletableIndex.gamePlayeranotherSide.grade = 0;service.SendToAll(userList, “Tables,“ + this.GetOnlineString();break;case “level“:tableIndex = int.Parse(splitS
18、tring1);gameTabletableIndex.SetTimerLevel(6 - int.Parse(splitString2) * 100);service.SendToBoth(gameTabletableIndex, receiveString);break;case “talk“:tableIndex = int.Parse(splitString1);sendString = string.Format(“Talk,0,1“, user.userName,receiveString.Substring(splitString0.Length +splitString1.Le
19、ngth);service.SendToBoth(gameTabletableIndex, sendString);break;case “start“:tableIndex = int.Parse(splitString1);side = int.Parse(splitString2);gameTabletableIndex.gamePlayerside.started = true;if (side = 0)anotherSide = 1;sendString = “Message,黑方已开始。 “;elseanotherSide = 0;sendString = “Message,白方已
20、开始。 “;service.SendToBoth(gameTabletableIndex, sendString);if (gameTabletableIndex.gamePlayeranotherSide.started = true)gameTabletableIndex.ResetGrid();gameTabletableIndex.StartTimer();break;case “unsetdot“:tableIndex = int.Parse(splitString1);side = int.Parse(splitString2);int xi = int.Parse(splitSt
21、ring3);int xj = int.Parse(splitString4);int color = int.Parse(splitString5);gameTabletableIndex.UnsetDot(xi, xj, color);break;default:service.SendToAll(userList, “什么意思啊:“ + receiveString);break;userList.Remove(user);client.Close();service.AddItem(string.Format(“有一个退出,剩余连接用户数: 0“, userList.Count);pri
22、vate void RemoveClientfromPlayer(User user)for (int i = 0; i gameTable.Length; i+)for (int j = 0; j 2; j+)if (gameTablei.gamePlayerj.user != null)if (gameTablei.gamePlayerj.user = user)StopPlayer(i, j);return;private void StopPlayer(int i, int j)gameTablei.StopTimer();gameTablei.gamePlayerj.someone
23、= false;gameTablei.gamePlayerj.started = false;gameTablei.gamePlayerj.grade = 0;int otherSide = (j + 1) % 2;if (gameTablei.gamePlayerotherSide.someone = true)gameTablei.gamePlayerotherSide.started = false;gameTablei.gamePlayerotherSide.grade = 0;if (gameTablei.gamePlayerotherSide.user.client.Connect
24、ed = true)service.SendToOne(gameTablei.gamePlayerotherSide.user,string.Format(“Lost,0,1“,j, gameTablei.gamePlayerj.user.userName);private string GetOnlineString()string str = “;for (int i = 0; i gameTable.Length; i+)for (int j = 0; j 2; j+)str += gameTablei.gamePlayerj.someone = true ? “1“ : “0“;ret
25、urn str;private void FormDDServer_FormClosing(object sender, FormClosingEventArgs e)if (myListener != null)buttonStop_Click(null, null); 见习感想:通过这几天的专业见习,让我明白了很多看似简单的事情真正操作起来并没有想象中那么容易的,生活中常见的黑白棋如果要用代码程序表达出来需要的是对 C#这门课程有着扎实的基础,同时也需要耐心和细心。通过细节一步步了解程序的原理,发现其问题所在,再进一步修复。同时在实践的过程中,也增强了我们的专业素养,这对我们未来就业都有着较为重要的作用。