《本科毕业设计-网络五子棋.doc》由会员分享,可在线阅读,更多相关《本科毕业设计-网络五子棋.doc(26页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、AbstractWith the rapid development of recent computer, a variety of board games are one after another sign of the computer, so those who love chess, they often suffer from rival chess fan who has not been enough to keep chess addiction. And high levels of all such software, a great rival and the ten
2、dency of the human brain. Which had defeated world champion chess - Garry Kasparovs Deep Blue is the most convincing representatives; other like the Go light hand chess to family and also for its excellent deep artificial intelligence by chess fan favorite; and we will tell you is that backgammon al
3、gorithm.In order to make the reader have a backgammon search complexity image understanding, to cite a Chinese chess with backgammon comparison searches. Can see that compared with Chinese chess, backgammon coefficient of multi-branch, and the outcome of conditional also complicated. Significant fac
4、tor in the branch, the search procedure to increase the maximum search depth of 1 level, cost of computation time will increase significantly. Therefore, to design an efficient search algorithm is very important.Keywords : backgammon, Human Versus, Algorithm.前 言五子棋是起源于中国古代的传统黑白棋种之一。现代五子棋日文称之为“連珠”,英译
5、为“Renju”,英文称之为“Gobang”或“FIR”(Five in a Row的缩写),亦有“连五子”、“五子连”、“串珠”、“五目”、“五目碰”、“五格”等多种称谓。五子棋不仅能增强思维能力,提高智力,而且富含哲理,有助于修身养性。五子棋既有现代休闲的明显特征“短、平、快”,又有古典哲学的高深学问“阴阳易理”;它既有简单易学的特性,为人民群众所喜闻乐见,又有深奥的技巧和高水平的国际性比赛;它的棋文化源渊流长,具有东方的神秘和西方的直观;既有“场”的概念,亦有“点”的连接。它是中西文化的交流点,是古今哲理的结晶。1. 系统概述1.1开发背景当前网络上流传的五子棋游戏功能并不尽善尽美,其中
6、最主要的问题就是人机对战和网络对战不能够一起实现,所以我决定开发1一个既能够人机对战,又能够进行网络对战的五子棋系统。1.2 研究意义近来随着计算机的快速发展,各种各样的游戏层出不穷,使得人们能有更多的娱乐项目,而棋类游戏能起到锻炼人的思维和修身养性的作用,而且棋类游戏水平颇高,大有与人脑分庭抗礼之势。现在的五子棋游戏软件基本上可以分为两类:一类是网络五子棋游戏软件,另一类是单机版五子棋游戏软件。为了满足五子棋玩家人群的需求,作者设计了这款电脑版的五子棋游戏软件,可以供人们闲暇时间随时随地地进行娱乐提高自己的五子棋水平,实用性强。1.3 需求分析多五子棋主要功能如下:(1)选择不同模式的游戏
7、本游戏提供单机模式和网络模式。(2)单机模式下的人机对战 人与电脑玩。(3)单机模式下的双人对战 人与人之间使用同一台电脑下棋。(4)网络对战 可以联机对战且可以聊天。(5)声音设置自己播放背景音乐1.4可行性分析1.4.1技术可行性为实现本软件的程序设计,开发该软件采用Microsoft Windows XP作为操作平台实现与软件用户的交互工作,同时使用主流的面向对象的编程语言和Microsoft Visual Studio 2005开发工具开发该软件,在技术上是可行的。1.4.2操作可行性现今社会,电脑的普及应用已为广大用户所接受,而C#是面向对象的主流编程语言,Microsoft Vis
8、ual Studio 2005开发环境,是目前最流行的 Windows 平台应用程序开发环境,而且开发的系统界面简单易操作,初学者几乎不用经过培训就能操作,因此该系统是具有操作可行性。2. 总体设计2.1系统的总体功能模块本设计由单机模式、网络模式两大功能模块构成:(1) 单机模块在同一台电脑上玩五子棋(2) 网络模块不在同一台电脑上进行对弈。五子棋游戏游戏单机模式网络模式帮助人机模式模式游戏说明请求连接建立服务器双人模式结束开始图2-1 系统的总体功能图2.2系统调用图玩家五子棋主界面进入游戏操作说明退出游戏游戏说明BACK 图2-2系统调用图3. 具体系统实现功能介绍单机模式人机对战双人对
9、战 图3 系统实现功能图3.1人机对战模块图3-1 人机对战运行时的图人机对战主要代码如下:private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) /实现鼠标操作的函数 /游戏运行核心,分别完成人机对战 /完成下棋点的确定,界面信息的更新,发送网络对战信息 if (!win0) & (!win1) & (!nowin) /当未有一方取胜时 if (!over) /尚未结束 if (!wplayers) /不是网络对战状态 if (player1) /该白棋走 if (x 600 & y
10、 600) /取下棋点 m = (int)Math.Floor(double)x / 40); n = (int)Math.Floor(double)y / 40); if (shifouxiazim, n = 2) this.textBox1.Text = textBox1.Text + 白棋: + X + m + Y + n + t; if (label12.Text = 白棋) huihe+; label9.Text = + huihe; else if (label12.Text = 待定) label12.Text = 白棋; huihe+; label9.Text = + huih
11、e; /回合数调整,先手显示 shifouxiazim, n = 0; /表示该位已存白子 player1sum+; /白子总数加1 if (player2sum = 112) & (player1sum = 113) | (player2sum = 113) & (player1sum = 112) /如果出现平局 nowin = true; over = true; for (i = 0; i 572; i+) /对每种获胜情况的ifwin0,i进行计算,可能满足的情况ifwin0,i+ /不能满足的情况ifwin0,i置7(表无效) if (tableplayer1m, n, i & i
12、fwin0, i != 7) ifwin0, i+; if (tableplayer2m, n, i) tableplayer2m, n, i = false; ifwin1, i = 7; player1 = false; player2 = true; /交换下棋方 3.2双人对战模块 图3-2 双人对战运行时图 单机模式中双人对战模块,两个玩家同时用同一部电脑和鼠标对弈,代码如下:if (nplayers) /双人对战模式下 if (player2) if (x 600 & y 600) /黑棋走 m = (int)Math.Floor(double)x / 40); n = (int)
13、Math.Floor(double)y / 40); /取下棋点 if (shifouxiazim, n = 2) this.textBox1.Text = textBox1.Text + 黑棋: + X + m + Y + n + t; /加入历史记录 if (label12.Text = 黑棋) huihe+; label9.Text = + huihe; else if (label12.Text = 待定) label12.Text = 黑棋; huihe+; label9.Text = + huihe; /回合数调整,先手显示 shifouxiazim, n = 1; /表示该位已存
14、黑子 player2sum+; /黑子总数加1 if (player2sum = 112) & (player1sum = 113) | (player2sum = 113) & (player1sum = 112) nowin = true; over = true; for (i = 0; i 572; i+) /对每种获胜情况的ifwin0,i进行计算,可能满足的情况ifwin0,i+ /不能满足的情况ifwin0,i置7(表无效) if (tableplayer2m, n, i & ifwin1, i != 7) ifwin1, i+; if (tableplayer1m, n, i)
15、 tableplayer1m, n, i = false; ifwin0, i = 7; player1 = true; player2 = false; /交换下棋权利 3.3 Playingtable模块图3-3 Playingtable运行时图Playingtable模块属于网络对战模式,显示用户进入房间后看到的界面,并根据收发的信息作对应处理还有聊天的功能,代码如下: / 设置Label控件的文本信息 / private void SetLabel(Label label, string str) if (label.InvokeRequired = true) this.Invoke
16、(labelDelegate, label, str); else label.Text = str; / / 设置应该谁落子的提示信息 / public void SetLabelGo(string str, int downIndex) SetLabel(labelGo, str); this.downIndex = downIndex; / / 设置开始游戏标志 / public void Start(bool isStart) isPlaying = isStart; public void AskTie(string str) if (str != side.ToString() s
17、tring reply = False; string sideString = str = 0 ? 黑方 : 白方; if (MessageBox.Show(sideString + 请求和棋,同意吗?, , MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes) reply = True; SendToServer(string.Format(Reply,0,1,2, tableIndex, side, reply); public void ReplyTie(string askSide, string
18、str) if (str = True) Restart(); else if (askSide != side.ToString() string sideString = askSide = 0 ? 黑方 : 白方; MessageBox.Show(sideString + 不同意和棋!); private void SetButton(Button button, bool flag) if (button.InvokeRequired = true) this.Invoke(buttonDelegate, button, flag); else button.Enabled = fla
19、g; if (flag = true) button.ForeColor = Color.Black; public void SetPictureBoxImage(PictureBox pictureBox, Image image) if (pictureBox.InvokeRequired = true) this.Invoke(setPictureBoxDelegate, pictureBox, image); else pictureBox.Image = image; / / 向棋盘上放置棋子 / public void SetChess(int i, int j, int Che
20、ssColor) /-此句仅用于观察接收的信息,需要观察时可以去掉注释-/ AddToRichTextBox(string.Format(0:1,2, ChessColor,j, i); /-/ gridi, j = ChessColor; pictureBoxBoard.Invalidate(); public void Restart(string str) if (str != ) MessageBox.Show(str, , MessageBoxButtons.OK, MessageBoxIcon.Warning); ResetGrid(); if (side != 2) SetBut
21、ton(buttonStart, true); private void ResetGrid() for (int i = 0; i = grid.GetUpperBound(0); i+) for (int j = 0; j = grid.GetUpperBound(1); j+) gridi, j = ChessColor.None; pictureBoxBoard.Invalidate(); / / 初始化棋盘上各棋子的位置 / / 以/分开的棋子位置字符串 public void InitializeGrid(string str) string splitString = str.S
22、plit(/); int k = 0; for (int i = 0; i = grid.GetUpperBound(0); i+) for (int j = 0; j = grid.GetUpperBound(1); j+) gridi, j = int.Parse(splitStringk); k+; pictureBoxBoard.Invalidate(); / / Paint事件 / private void pictureBoxBoard_Paint(object sender, PaintEventArgs e) Graphics g = e.Graphics; for (int
23、i = 0; i = grid.GetUpperBound(0); i+) for (int j = 0; j = grid.GetUpperBound(1); j+) if (gridi, j != ChessColor.None) if (gridi, j = ChessColor.Black) g.DrawImage(blackBitmap, (i + 1) * 20, (j + 1) * 20); else g.DrawImage(whiteBitmap, (i + 1) * 20, (j + 1) * 20); / / 单击发送按钮触发的事件 / private void buttonSend_Click(object sender, EventArgs e) /字符串格式:Talk,桌号,对话内容 SendToServer(string.Format(Talk,0,1, tableIndex, textBox1.Text); textBox1.Clear(); / / 对话内容改变时触发的事件 / private void textBox1_KeyPress(object sender, KeyPressEventArgs e) if (e.KeyChar = (char)Keys.Enter)
限制150内