




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、 鄭 州 科 技 學 院課 程 設 計 論 文基于c#的五子棋游戲的設計與實現(xiàn)學生姓名:王新年學 號:201015066 年級專業(yè):10級計科二班 指導老師: 王玉萍學 院: 信息工程學院 1引言11.1五子棋介紹12軟件架構23五子棋設計說明23.1主要成員變量說明23.2回溯棧元素類stackelement33.3棋子點屬性類qzdianshuxing33.4主要成員函數(shù)說明43.5實現(xiàn)人機對弈的主要函數(shù)73.6實現(xiàn)菜單功能的函數(shù)273.6程序運行界面304心得體會311引言1.1五子棋介紹五子棋是起源于中國古代的傳統(tǒng)黑白棋種之一?,F(xiàn)代五子棋日文稱之為“連珠”,英譯為“renju”,英文稱
2、之為“gobang”或“fir”(five in a row的縮寫),亦有“連五子”、“五子連”、“串珠”、“五目”、“五目碰”、“五格”等多種稱謂。五子棋不僅能增強思維能力,提高智力,而且富含哲理,有助于修身養(yǎng)性。五子棋既有現(xiàn)代休閑的明顯特征“短、平、快”,又有古典哲學的高深學問“陰陽易理”;它既有簡單易學的特性,為人民群眾所喜聞樂見,又有深奧的技巧和高水平的國際性比賽;它的棋文化源淵流長,具有東方的神秘和西方的直觀;既有“場”的概念,亦有“點”的連接。它是中西文化的交流點,是古今哲理的結晶。人機對戰(zhàn)人人對戰(zhàn)主界面游戲控制游戲模式重新開始退出聲音控制悔棋開始2軟件架構軟件的總體架構如圖2.1
3、:圖2.1 軟件架構3五子棋設計說明3.1主要成員變量說明1) 選擇游戲模式標志m_renren用來表示當前玩家選擇游戲的情況,當m_renren為false時,表示人機對戰(zhàn);為true時,表示人人對弈。2) 游戲開始標志begin用來判斷當前游戲是否開始3) 音效標志sound在下棋過程中,判斷是否需要聲音,當sound為true時,表示玩家需要聲音,否則的話,玩家不需要聲音。4) 誰先下的標志first這個標志只對人機對弈時有效。當first為true時,表示人先下,否則,電腦先下。5) 棋盤數(shù)據(jù)pointspoints為棋盤情況數(shù)組,是用一個15*15的二維數(shù)組來表示的。pointsi,
4、j=2表示此處無子,pointsi,j=1表示此處為黑子 pointsi,j=0表示此處為白子。 6) 棋子顏色標志qzcolor用來表示當前棋子的顏色,qzcolor=1時表示黑棋,qzcolor=0時表示百棋。7) 棋子數(shù)據(jù)qz表示棋子所放的位子,是用一個15*15的picturebox類型的二維數(shù)組來表示。它還可以用來顯示當前棋子的圖片。8) oldmovepoint用來記錄鼠標經過后點的位置。9) backstack用于悔棋的棧。10)backtrackstack用于回溯的棧11)結局result 用枚舉類型來表示結局。如: public enum result : int/結局 lo
5、se = -1, equal, win 3.2回溯棧元素類stackelement成員變量:1) qzcolor 棋子的顏色2) bestfivepoints 最好點的位置3) pointscount 計算最好點的數(shù)目4) pointnumber 點的數(shù)目5) theresult 結局6) stepnumber 預測的步數(shù)3.3棋子點屬性類qzdianshuxing成員變量:1)blackconnect 黑棋子i個(包括活棋)的連接條數(shù)2)blackactive 黑活棋i個的連接條數(shù)3)whiteconnect 白棋子i個(包括活棋)的連接條數(shù)4)whiteactive 白活棋i個的連接條數(shù)5
6、)tempactive3 活棋數(shù)為3的連接條數(shù)3.4主要成員函數(shù)說明1) 初始化棋盤initializeqp初始化操作包括以下幾個步驟:l 設置棋子所在的位置l 設置棋子的大小l 初始化棋子的背景顏色l 將棋子的sizemode設置為 centerimagel 將棋子的可見性設置為falsel 將棋子添加到form上。2) 繪制棋盤form1_paint其主要是畫出以40*40的大小為每一小格,代碼如下: for (i = 0; i 15; i+) g.drawline(mypen, 30 + i * 40, 50, 30 + i * 40, 610); g.drawline(mypen, 3
7、0, 50 + i * 40, 590, 50 + i * 40); 3) 繪制光標form1_mousemove當鼠標在棋盤上移動時,當前的顯示畫紅方框,過去的顯示和背景一樣顏色的方框。當前的紅方框代碼如下: if (10 e.x & 10 e.y &e.x clientrectangle.width & e.y 0) temp = (point)backstack.pop(); qztemp.x, temp.y.image = global:五子棋.properties.resources.nullll; backstack.push(temp); else qzx, y.backgrou
8、ndimage = global:五子棋.properties.resources.whitestone; pointsx, y = 0; qzx, y.image = global:五子棋.properties.resources.lastwhitestone; if (backstack.count 0) temp = (point)backstack.pop(); qztemp.x, temp.y.image = global:五子棋.properties.resources.nullll; backstack.push(temp); 最后將其可見性設置為true。5) 開始函數(shù)star
9、t當棋局開始時,就應將棋盤初始化,使棋盤上沒有棋子。如果有悔棋,就要將悔棋棧清空。代碼如下:if (!begin) begin = true; for (x = 0; x 15; x+) for (y = 0; y 0) backstack.pop(); 3.5實現(xiàn)人機對弈的主要函數(shù)6) 察看兩點之間的棋子數(shù)函數(shù)connectqpcount這個函數(shù)主要求兩點之間可能形成五連子的qzcolor色棋的連子數(shù)(包括活期)。首先,求出兩點之間總共的棋子數(shù),并判斷棋子所在哪個方向。沿著這個方向每個點的坐標,并察看這幾個點中有沒有反色的棋子。如果有,棋子數(shù)設為0,否則的話,棋子數(shù)自加1。代碼如下:int
10、x, y, i, j, length, xplus = 0, yplus = 0, sum, maxsum = 0; length = math.max(math.abs(point1.x - point2.x), math.abs(point1.y - point2.y) + 1; if (point1.x != point2.x) xplus = 1; if (point1.y != point2.y) yplus = (point2.y - point1.y)/math.abs(point2.y - point1.y); for (i = 0; i length - 4; i+) x =
11、 point1.x + i * xplus; y = point1.y + i * yplus; sum = 0; for (j = 0; j 5; j+) /察看兩點之間當中有沒有反色 if (pointsx + j * xplus, y + j * yplus = qzcolor) sum+; else if (pointsx + j * xplus, y + j * yplus = -qzcolor+1) sum = 0; break; if (maxsum sum) maxsum = sum; return maxsum;7) 察看兩點之間是否存在活棋的函數(shù)activeconnectq
12、p 這個函數(shù)主要求兩點之間qzcolor色棋是否存在活棋。temp1變量表示在一直線上,比如, 一條向下的直線,則表示點point1上方可下的個數(shù);而temp2表示點point2下方可下的個數(shù)。代碼表示為: temp1 = math.min(math.min(math.min(5 - count, point1.x), point1.y), 14 - point1.y); temp2 = math.min(math.min(math.min(5 - count, 14 - point2.x), 14 - point2.y), point2.y);則長度表示為:length = math.max
13、(math.abs(point1.x - point2.x), math.abs(point1.y - point2.y) + 1 + temp1 + temp2;先求兩點之間qzcolor色棋的棋子個數(shù),做法和函數(shù)connectqpcount一樣。再判斷它是否是活棋。當參數(shù)count和所得兩點之間qzcolor色棋的棋子個數(shù)相等,并且兩頭都沒下棋子時,它為活棋。否則,反之。代碼如下: if (point1.x != point2.x) xplus = 1; if (point1.y != point2.y) yplus = (point2.y - point1.y) / math.abs(p
14、oint2.y - point1.y); for (i = 0; i 14 | y + 4 * yplus 14) break; sum = 0; for (j = 0; j 4; j+) if (pointsx + j * xplus, y + j * yplus = qzcolor) sum+; else if (pointsx + j * xplus, y + j * yplus = -qzcolor+1) sum = 0; break; if (0 x & 0 = y - yplus & y - yplus 0)/棧非空 tempstackelement = (stackelement
15、)backtrackstack.pop(); if (tempstackelement.pointnumber tempstackelement.pointscount) /在棋盤上下一棋 pointstempstackelement.bestfivepointstempstackelement.pointnumber.x, tempstackelement.bestfivepointstempstackelement.pointnumber.y = tempstackelement.qzcolor; if (win(tempstackelement.qzcolor, tempstackele
16、ment.bestfivepointstempstackelement.pointnumber) /贏棋,不在繼續(xù)探測 tempstackelement.theresulttempstackelement.pointnumber = result.win; tempstackelement.stepnumbertempstackelement.pointnumber = backtrackstack.count + 1; /在棋盤上退一棋 pointstempstackelement.bestfivepointstempstackelement.pointnumber.x, tempstack
17、element.bestfivepointstempstackelement.pointnumber.y = 2; tempstackelement.pointnumber+; backtrackstack.push(tempstackelement); else if (backtrackstack.count = m - 1) /將此元素壓入棧后棧滿,不在繼續(xù)探測 tempstackelement.theresulttempstackelement.pointnumber = result.equal; tempstackelement.stepnumbertempstackelement
18、.pointnumber = m; /在棋盤上退一棋 pointstempstackelement.bestfivepointstempstackelement.pointnumber.x, tempstackelement.bestfivepointstempstackelement.pointnumber.y = 2; tempstackelement.pointnumber+; backtrackstack.push(tempstackelement); else /另一方繼續(xù)下棋向下探測 tempstackelement.pointnumber+; backtrackstack.pus
19、h(tempstackelement); findbestfivepointsandformastackelement(-tempstackelement.qzcolor+1, ref tempstackelement); backtrackstack.push(tempstackelement); /end if else/棧頂元素無點或點均已試過 if (tempstackelement.pointscount = 0)/棧頂元素無點,且彈出后棧必非空 tempstackelement = (stackelement)backtrackstack.pop(); tempstackeleme
20、nt.theresulttempstackelement.pointnumber - 1 = result.win; tempstackelement.stepnumbertempstackelement.pointnumber - 1 = backtrackstack.count + 1; /在棋盤上退一棋 pointstempstackelement.bestfivepointstempstackelement.pointnumber - 1.x, tempstackelement.bestfivepointstempstackelement.pointnumber - 1.y = 2;
21、backtrackstack.push(tempstackelement); else/棧頂元素中點均已試過 /尋找棧頂元素中點的最好結局 totalresult = tempstackelement.theresult0; for (i = 0; i tempstackelement.pointscount; i+) if (totalresult tempstackelement.theresulti) totalresult = tempstackelement.theresulti; /尋找最佳步數(shù) if (totalresult = result.win) beststepnumbe
22、r = m + 2; for (i = 0; i tempstackelement.stepnumberi) beststepnumber = tempstackelement.stepnumberi; else/totalresult=result.equal或lose beststepnumber = 0; for (i = 0; i tempstackelement.pointscount; i+) if (totalresult = tempstackelement.theresulti & beststepnumber 0)/棧非空 tempstackelement = (stack
23、element)backtrackstack.pop(); tempstackelement.theresulttempstackelement.pointnumber - 1 = (result)(0 - totalresult); tempstackelement.stepnumbertempstackelement.pointnumber - 1 = beststepnumber; /在棋盤上退一棋 pointstempstackelement.bestfivepointstempstackelement.pointnumber - 1.x, tempstackelement.bestf
24、ivepointstempstackelement.pointnumber - 1.y = 2; backtrackstack.push(tempstackelement); for (i = 0; i tempstackelement.pointscount; i+) if (totalresult = tempstackelement.theresulti & beststepnumber = tempstackelement.stepnumberi) break; bestpoint = tempstackelement.bestfivepointsi; return true;10)
25、尋找最佳的五個點,并形成棧元素findbestfivepointsandformastackelement函數(shù)主要是找最佳點,并形成棧元素。如果找到,返回true;否則。返回false。要找 最佳點,就是找權值最大的點。首先,計算出棋盤上每一個點的權值,并找出最大的一 個。代碼如下: int, qppower = new int15, 15; bool blnhavefound; int x, y, i, max; tempstackelement.pointscount = 0; for (x = 0; x 15; x+) for (y = 0; y 15; y+) qppowerx, y
26、= getqppower(qzcolor, x, y); for (i = 0; i 5; i+) /求第i個最佳點 max = 0; for (x = 0; x 15; x+) for (y = 0; y 15; y+) if (max qppowerx, y) max = qppowerx, y; for (x = 0; x 15; x+) blnhavefound = false; for (y = 0; y 15; y+) if (max = qppowerx, y) tempstackelement.bestfivepointsi = new point(x, y); tempsta
27、ckelement.pointscount+; qppowerx, y = -1; blnhavefound = true; break; if (blnhavefound) break; if (tempstackelement.pointscount = 0) return false; else tempstackelement.qzcolor = qzcolor; tempstackelement.pointnumber = 0; return true; 11) 求權值getqppower定義八個方向:左,右,上,下,左上,右下,左下,右上。代碼如下: left = new poin
28、t(math.max(0, x - 4), y); right = new point(math.min(14, x + 4), y); top = new point(x, math.max(0, y - 4); down = new point(x, math.min(14, y + 4); temp = math.min(x - left.x, y - top.y); lefttop = new point(x - temp, y - temp); temp = math.min(x - left.x, down.y - y); leftdown = new point(x - temp
29、, y + temp); temp = math.min(right.x - x, y - top.y); righttop = new point(x + temp, y - temp); temp = math.min(right.x - x, down.y - y); rightdown = new point(x + temp, y + temp);如果顏色是黑色,處理黑棋連子情況:如果這個點沒有下棋子,則在此置為1,表示這點下的是黑棋。然后看這個點各個方向黑棋子的數(shù)目是多少,并看是否是活棋,再作出處理。最后,將此點處置為2。表示這點沒有下棋子。處理白棋連子情況:在此點處置為0,表示這點下的是白棋。然后看這個點各個方向白棋子的數(shù)目是多少,并在此放一白棋破壞黑棋,再做出相應的處理。如果各個方向上出現(xiàn)3個的活棋。就將tempactive3自加1。最后,將此點處置為2。表示這點沒有下棋子。設置權值:形成黑棋五個的權值設為150000;形成白棋五個的權值設為140000;形成黑活棋個數(shù)為4或形成兩條以上黑棋個數(shù)為4的權值設為130000;形成一條黑棋個數(shù)為4并且一條為黑活棋個數(shù)為3的權值為1200
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 專題5.2 平面向量基本定理及坐標表示(原卷版)-2024年高考數(shù)學一輪復習精講精練寶典(新高考專用)
- 2020-2021深圳市寶安區(qū)鵬暉中英文學校小學五年級數(shù)學下期中模擬試題及答案
- 肇慶車庫畫線施工方案
- 河北省邢臺隆堯縣聯(lián)考2025屆畢業(yè)升學考試模擬卷生物卷含解析
- 加油站車位出租合同范例
- 醫(yī)療專項設計合同范本
- 品牌故事的創(chuàng)作與傳播計劃
- 班級年度培訓計劃
- 班級理論知識競賽的組織與實施計劃
- 敏捷管理方法在團隊中的實踐計劃
- 2024解析:第二十章電與磁-講核心(解析版)
- DB4101T 25.2-2021 物業(yè)服務規(guī)范 第2部分:住宅
- 六年級數(shù)學下冊 負數(shù)練習題(人教版)
- 2024-2030年中國康復醫(yī)院行業(yè)管理模式分析及發(fā)展規(guī)劃研究報告
- 斐訊PSG1218路由器的上網(wǎng)設置教程
- 八年級下冊《經典常談》-2024年中考語文名著導讀專練
- 亡靈節(jié)課件教學課件
- 企業(yè)名稱預先核準通知書
- 內容運營崗位招聘筆試題與參考答案(某大型央企)
- 體格檢查:腹部檢查(二)
- 1.3.1-二項式定理-公開課一等獎課件
評論
0/150
提交評論