基于vb的五子棋
五子棋代码
Dim ComputerPlayer As BooleanDim ComputersChess As IntegerDim Gamelevel As IntegerDim Player1time As Single, PLayer2time As SingleDim PLayer As Integer, PLaytime As BooleanPrivate Sub XiaQi(x As Single, y As Single) '绘制棋子If Pic(x, y) <> 0 Then Exit SubIf PLayer = 1 ThenPicture1.FillColor = vbBlackPicture1.Circle (x, y), 0.3, vbBlackPLayer = -1Pic(x, y) = 1ElsePicture1.FillColor = vbWhitePicture1.Circle (x, y), 0.3, vbWhitePLayer = 1Pic(x, y) = -1End IfBushu = Bushu + 1 '记录走的步数If Bushu = 360 Then MsgBox "和棋", vbOKOnly, "提示"Shuying x, yJustX(Bushu) = xJustY(Bushu) = yIf Bushu = 361 ThenMsgBox "和棋", vbOKOnly, "提示"PLaytime = FalseCommand2.Enabled = FalseGamestop.Enabled = FalseEnd IfTimer1.Enabled = Not Timer1.Enabled '交换计时Timer2.Enabled = Not Timer2.EnabledIf PLayer = ComputersChess And PLaytime = True And ComputerPlayer = True Then COMPUTERXIAQI '判断是否让电脑下棋End SubPrivate Sub QIPAN() '绘制棋盘Picture1.ClsPicture1.FillColor = vbBlackPicture1.Scale (-10, 10)-(10, -10) '把棋盘设立成直角坐标系For i = -9 To 9 '绘制棋盘线条Picture1.Line (i, -9)-(i, 9)Picture1.Line (-9, i)-(9, i)Next iFor i = -6 To 6 Step 6 '绘制棋盘上九个点For j = -6 To 6 Step 6Picture1.Circle (i, j), 0.1, vbBlackNext jNext iEnd SubPrivate Sub Command1_Click() '开始游戏Dim i As Integer, j As IntegerIf PLaytime = True Or Command2.Caption = "继续游戏" Then m = MsgBox("确定重新开始游戏?", vbYesNo + 32, "友情提示")If m = vbNo Then Exit SubEnd IfTimer1.Enabled = TrueTimer2.Enabled = FalsePLaytime = True '游戏开始的标识符PLayer = 1Picture1.DrawWidth = 1QIPAN '调用绘制棋盘的函数For i = -10 To 10 '数据初始化,将棋盘以外的空点赋值为3,以防出错Pic(10, i) = 3Pic(i, 10) = 3Pic(-10, i) = 3Pic(i, -10) = 3Next iFor i = -9 To 9 '数据初始化,将棋盘数组清空For j = -9 To 9Pic(i, j) = 0Next jNext iBushu = 0If oneplayer.Checked = True Then '判断游戏人数ComputerPlayer = True '电脑对手设置为真If level1.Checked = True Then '判断所选难度等级,此为新手级Gamelevel = 1ComputersChess = -1 '新手级电脑后手End IfIf level2.Checked = True Then '判断所选难度等级,此为第二级Gamelevel = 2ComputersChess = 1XiaQi 0, 0 '电脑先手End IfElseComputerPlayer = False '电脑对手设置为假End IfCommand2.Enabled = TrueCommand2.Caption = "暂停游戏"Gamestop.Enabled = TrueLabel1.Caption = "黑子:10:00" ' 设置时间Label2.Caption = "白子:10:00"Player1time = 600PLayer2time = 600End SubPrivate Sub Shuying(x As Single, y As Single) '判断输赢If PANduan(x, y, 1, 1) = True Then Exit Sub '右上左下对角线判断If PANduan(x, y, 1, 0) = True Then Exit Sub '横向判断If PANduan(x, y, 0, 1) = True Then Exit Sub '纵向判断If PANduan(x, y, 1, -1) = True Then Exit Sub '右下左上对角线判断End SubPrivate Function PANduan(ByVal x As Single, ByVal y As Single, P As Integer, S As Integer) As Boolean '计算连子数,X Y表示下子位置,P S 表示方向~~Dim XX As Integer, YY As IntegerDim WINX1 As Integer, WINX2 As Integer, WINY1 As Integer, WINY2 As IntegerDim MaxR As Integer, MinR As IntegerXX = xYY = yMaxR = 0MinR = 0PANduan = FalseDo While Pic(XX, YY) = Pic(x, y)XX = XX + PYY = YY + SMaxR = MaxR + 1LoopWINX1 = XX - PWINY1 = YY - SXX = xYY = yDo While Pic(XX, YY) = Pic(x, y)XX = XX - PYY = YY - SMinR = MinR - 1LoopWINX2 = XX + PWINY2 = YY + SIf WIN(x, y, MaxR, MinR) = True ThenPicture1.DrawWidth = 4Picture1.Line (WINX1, WINY1)-(WINX2, WINY2), vbRedPANduan = TrueCommand2.Enabled = FalseGamestop.Enabled = FalseEnd IfEnd FunctionFunction WIN(x As Single, y As Single, MaxR As Integer, MinR As Integer) As Boolean '输出输赢If MaxR - MinR > 5 Then '如果连子数大于等于五则算胜If Pic(x, y) = 1 ThenPLaytime = FalseMsgBox "黑棋获胜", vbOKOnly, "胜利"Bushu = 0ElsePLaytime = FalseMsgBox "白棋获胜", vbOKOnly, "胜利"Bushu = 0End IfWIN = TrueElseWIN = FalseEnd IfEnd FunctionPrivate Sub COMPUTERXIAQI() '电脑下棋Select Case GamelevelCase 1THINKXiaQi ComputerX, ComputerYCase 2HigherThinkXiaQi ComputerX, ComputerYEnd SelectEnd SubPrivate Sub Command2_Click() '暂停,继续游戏If Command2.Caption = "暂停游戏" ThenCommand2.Caption = "继续游戏"Gamestop.Caption = "继续游戏"PLaytime = FalseElsePLaytime = TrueCommand2.Caption = "暂停游戏"Gamestop.Caption = "暂停游戏" End IfEnd SubPrivate Sub Form_Load()PLayer = 1QIPANPlayer1time = 600PLayer2time = 600Command2.Enabled = False Gamestop.Enabled = FalseEnd SubPrivate Sub Gamestart_Click()Call Command1_ClickEnd SubPrivate Sub Gamestop_Click() '菜单暂停继续游戏Call Command2_Click '调用command2的过程End SubPrivate Sub level1_Click()level1.Checked = Truelevel2.Checked = FalseEnd SubPrivate Sub level2_Click()level2.Checked = Truelevel1.Checked = FalseEnd SubPrivate Sub Picture1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)If PLaytime = False Then Exit SubIf Button = 1 Thenx = CInt(x)y = CInt(y)If x < -9 Or y < -9 Or x > 9 Or y > 9 Then Exit SubXiaQi x, yEnd IfEnd SubPrivate Sub Timer1_Timer()Dim ss As Single, dd As SingleIf PLaytime = False Then Exit Sub '判断游戏是否在进行Player1time = Player1time - 0.1If Player1time Mod 1 <> 0 Then Exit Subss = Player1time \ 60dd = Player1time Mod 60Label1.Caption = "黑子:" & ss & ":" & ddIf Player1time <= 0 ThenMsgBox "白棋胜", vbOKOnly, "黑棋超时"QIPANPLaytime = FalseCommand2.Enabled = FalseGamestop.Enabled = FalseEnd IfEnd SubPrivate Sub Timer2_Timer()Dim ss As Single, dd As SingleIf PLaytime = False Then Exit Sub '判断游戏是否在进行PLayer2time = PLayer2time - 0.1If Player1time Mod 1 <> 0 Then Exit Subss = PLayer2time \ 60dd = PLayer2time Mod 60Label2.Caption = "白子:" & ss & ":" & ddIf PLayer2time <= 0 ThenMsgBox "黑棋胜", vbOKOnly, "白棋超时"QIPANPLaytime = FalseCommand2.Enabled = FalseGamestop.Enabled = FalseEnd IfEnd SubPrivate Sub oneplayer_Click() '单人游戏twoplayer.Checked = Falseoneplayer.Checked = TrueEnd SubPrivate Sub twoplayer_Click() '双人游戏twoplayer.Checked = Trueoneplayer.Checked = FalseEnd Sub。
#vb60人工只能五子棋算法
目录目录I引言2第一部分五子棋的AI构想31.1求五子棋的获胜组合31.2建立和使用获胜表31.3分数的设定31.4攻击与防守4第二部分使用VB6.0编写五子棋42.1编写前的准备42.1.1用计算机的思想描述整个下棋的过程。
42.1.2利用vb6.0窗体和图形工具建立五子棋的棋盘界面。
52.1.3设置整体框价52.2声明全局数组和变量52.3初始化游戏62.4处理鼠标事件82.5获胜检查算法92.6电脑算法112.7绘制棋子13引言五子棋游戏以其优秀的人工智能深受广大玩家的喜爱,而对于初步探究人工智能的编程爱好者来说,编制五子棋程序因其规则简单而大受欢迎,然而它却要求程序员对五子棋规则有相当深入的了解。
程序员考虑得越周到,其五子棋程序就越智能。
五子棋游戏软件设计的主要内容是:根据五子棋的基本规则,要让电脑知道该在哪一点下子,就要根据盘面的形势,为每一个可能落子的点分别计算其重要程度,也就是该点的分值,然后通览全盘随机选出数值最大的一点,因为有时候分值最大的一点可能不止一个,计算机随机选择分值最大的一点,可以保证它在每盘棋中选择的落子点的位置有所不同,因此玩家不会用同一种棋局反复赢计算机。
人工智能也就是所谓的AI(ArtificialIntelligence>,它是一门很抽象的技术,AI程序的编写不需要依据任何既定的思考模式或者规则。
尤其是游戏中的AI可以完全依程序设计者本身的思考逻辑制作。
我个人认为人工智能的核心应该是使计算机具有自动的处理事件的能力,而我们的所有的研究也应该围绕着这一方向。
我们今天讨论的是策略类的人工智能。
策略类人工智能可以说是AI中比较复杂的一种,最常见的策略类AI游戏就是棋盘式游戏。
在这类游戏中,通常的策略类AI程序都是使计算机判断目前状况下所有可走的棋与可能的获胜状况,并计算当前计算机可走棋步的获胜分数或者玩家可走棋步的获胜分数,然后再决定出一个最佳走法。
下面我们先介绍一下五子棋的AI构想第一部分五子棋的AI构想有句话叫“当局者迷,旁观者清。
VB五子棋代码
界面设计:克匹闵用舀亘早怛客户服需系统2010-7-25 21:02ds*X]E• * -••----.亘葫开洽* ' ° * ■ B■* ■ "H 1■左井耳嘻會* P3-P3鬪J31D — n * ?T机CUCU.-.II玉戎北.两支寸 :;"EJ 子戶护孑 視 聊書五¥±|i^JA: ETUULS1 7j代码如下:Opti on ExplicitCo nst SubWidth = 400度Private P2PlayColor As In tegerPrivate MyColor As In tegerPrivate IfSucceed As Boolea nCon st pi = 3.14159Private cen terx As Si nglePrivate cen tery As Si nglePrivate radius As Si nglePrivate DataArray(14, 14) As Integer=1白棋=0)Private sumtime As In tegerPrivate ifStarteasy As Boolean(ifStarteasy=true时每落子一次计时开启一次)Private ifStartnormal As Boolean(ifStartnormal=true时每落子一次计时开启一次)Private ifStarthard As Boolean(ifStarthard=true时每落子一次计时开启一次)'定义画五子棋表格的每格长度和宽'实现黑白棋子的交替进行'标记黑白双方棋子颜色'表示是否胜利'定义字符常量pi=3.14159'保存棋盘中棋子的位置信息(空子=3黑棋'记录总时间来判断谁超时'标记简单难度下计时功能是否可以开启'标记中等难度下计时功能是否可以开启'标记困难难度下计时功能是否可以开启W77fe'J3空迎佚用音乐五子帆客户罪奇系统|L C:[TOCLS]2U1O-7-L521;UZ运行界面:2L'单击命令按钮"退出"退出Private Sub CmdExit_Click()EndEnd SubPrivate Sub CmdStart_Click()Dim i As IntegerDim m As IntegerDim n As Integer'绘制棋盘PicQiPan.ClsPicQiPan.ForeColor = vbBlackFor i = 1 To 14PicQiPan.Line (SubWidth, SubWidth * i)-(SubWidth * 14, _ SubWidth * i)PicQiPan.Line (SubWidth * i, SubWidth)-(SubWidth * i, _ SubWidth * 14)Next i'棋盘落点信息初始化For m = 0 To 14For n = 0 To 14 DataArray(m, n) = 3 Next nNext m'主要标记信息初始化P2PlayColor = 0 MyColor = 0 IfSucceed = FalseifStarteasy = False ifStartnormal = False ifStarthard = FalseTimer2.Enabled = FalseTimer3.Enabled = FalseTimer4.Enabled = FalseFrmMain.Clssumtime = -1End Sub'简单难度Private Sub fileeasy_Click() ifStarteasy = True sumtime = -1 MsgBox " 双方下每步棋的思考时间最多End Sub '通过文件"退出"退出Private Sub fileexit_Click()EndEnd Sub'困难难度Private Sub filehard_Click()ifStarthard = True sumtime = -1 MsgBox " 双方下每步棋的思考时间最多End Sub '中等难度Private Sub filenormal_Click() ifStartnormal = True sumtime = -1FrmMain.ClsMsgBox " 双方下每步棋的思考时间最多End Sub'通过文件"重新开始" 实现棋盘初始化Private Sub filerestart_Click()Call CmdStart_ClickEnd Sub 20 秒,否则超时清盘5 秒,否则超时清盘10 秒,否则超时清盘Private Sub Form_Load()Dim i As IntegerDim m As IntegerDim n As Integer'绘制棋盘PicQiPan.ClsPicQiPan.ForeColor = vbBlackFor i = 1 To 14 PicQiPan.Line (SubWidth, SubWidth * i)-(SubWidth * 14, _SubWidth * i) PicQiPan.Line (SubWidth * i, SubWidth)-(SubWidth * i, _SubWidth * 14)Next i'棋盘落点信息初始化For m = 0 To 14For n = 0 To 14DataArray(m, n) = 3Next nNext mPrint'确定表针位置的基本参量centerx = Pictime.Width / 2 centery = Pictime.Height / 2 radius = Pictime.Height / 2 * 0.9Pictime.PSet (centerx, centery)Pictime.Circle (centerx, centery), radiusEnd Sub '棋子落点判断(出界和重子情况)Private Sub PicQipan_MouseDown(Button As Integer, Shift As Integer, X As Single, y As Single)Dim x0 As IntegerDim y0 As IntegerDim i As IntegerDim j As IntegerIf X < SubWidth Or X > 14.5 * SubWidth Or y < SubWidth Or y > 14.5 * SubWidth ThenMsgBox " 超出棋盘界限,请重新下! Exit Sub End IfIf Abs(Int(X / SubWidth) - X / SubWidth) < 0.5 Then x0 = Int(X / SubWidth) Elsex0 = Int(X / SubWidth) + 1 End IfIf Abs(Int(y / SubWidth) - y / SubWidth) < 0.5 Then y0 = Int(y / SubWidth) Elsey0 = Int(y / SubWidth) + 1 End IfIf DataArray(x0, y0) <> 3 Then '当前位置已经有棋子了 , MsgBox " 当前位置已经有棋子了,请重新走!", vbCritical, "NOTE!"Exit SubEnd If sumtime = -1 Call DrawPill(x0, y0)Call RemenberCrossData(x0, y0)Call WhoWin '判断是否开启相应难度计时功能If ifStarteasy = True ThenTimer2.Enabled = True End IfIf ifStartnormal = True Then Timer3.Enabled = True End IfIf ifStarthard = True ThenTimer4.Enabled = True End IfEnd Sub '画棋子Private Sub DrawPill(xx0 As Integer, yy0 As Integer) If P2PlayColor ThenPicQiPan.ForeColor = vbWhite DoEventsPicQiPan.FillColor = vbWhite PicQiPan.FillStyle = 0'画棋子 '记录棋子信息'判断谁赢MyColor = 0ElsePicQiPan.ForeColor = vbBlackDoEventsPicQiPan.FillColor = vbBlackPicQiPan.FillStyle = 0MyColor = 1End IfP2PlayColor = Not P2PlayColorPicQiPan.Circle (xx0 * SubWidth, yy0 * SubWidth), SubWidth * 0.5 End Sub'以下A B C 三个事件共同实现下棋的同时听音乐功能'APrivate Sub Dir1_Change()File1.Path = Dir1.PathEnd Sub'BPrivate Sub Drv_Change()Dir1.Path = Drv.DriveEnd Sub'CPrivate Sub File1_Click()mp3.URL = File1.Path & "\" & File1.FileNameEnd Sub'棋盘皮肤Private Sub qipanstylefurA_Click()PicQiPan.BackColor = &HC0FFFFCall CmdStart_ClickEnd SubPrivate Sub qipanstylefurB_Click()PicQiPan.BackColor = &HC0C000Call CmdStart_ClickEnd SubPrivate Sub qipanstylefurC_Click()PicQiPan.BackColor = &HE0E0E0Call CmdStart_ClickEnd SubPrivate Sub qipanstylefurD_Click()PicQiPan.BackColor = &H8080FFCall CmdStart_ClickEnd Sub'添加四种背景音乐Private Sub stylemusicA_Click() mp3.URL = App.Path & "\" & "music01.mp3" End SubPrivate Sub stylemusicB_Click()mp3.URL = App.Path & "\" & "music02.mp3" End SubPrivate Sub stylemusicC_Click()mp3.URL = App.Path & "\" & "music03.mp3" End SubPrivate Sub stylemusicD_Click()mp3.URL = App.Path & "\" & "music04.mp3" End Sub' 表针走动Timer1.Enabled=true 在属性框中设定Private Sub Timer1_Timer()Dim s As IntegerDim m As IntegerDim h As IntegerDim sngLenS As SingleDim sngLenM As SingleDim sngLenH As SingleDim i As Integer碉试几次并查询VB常用函数,最后确定应该使用Now而不是Time(不过之前使用Time确实可以) s = Second(Now)m = Minute(Now)h = Hour(Now) + m / 60 sngLenS = radius * 0.8 sngLenM = radius * 0.6 sngLenH = radius * 0.4Pictime.ClsPictime.Scale (-centerx, centery)-(centerx, -centery)Pictime.Line (0, 0)-(sngLenS * Sin(2 * pi * s / 60), sngLenS * Cos(2 * pi * s / 60)), vbGreen Pictime.Line (0, 0)-(sngLenM * Sin(2 * pi * m / 60), sngLenM * Cos(2 * pi * m / 60)), vbGreenIf h > 12 Thenh = h - 12End IfPictime.Line (0, 0)-(sngLenH * Sin(2 * pi * h / 12), sngLenH * Cos(2 * pi * h / 12)), vbGreenPictime.Circle (0, 0), radius * 0.9For i = 1 To 12Pictime.Circle (radius * 0.9 * 0.85 * Sin(2 * pi * i / 12), radius * 0.9 * 0.85 * Cos(2 * pi * i / 12)), radius * 0.01, vbGreenNext iEnd Sub'判断谁赢了Private Sub WhoWin()Dim i As IntegerDim j As IntegerFor j = 1 To 14For i = 1 To 14If DataArray(i, j) = MyColor And Not IfSucceed ThenIf (14 - i) >= 4 And (14 - j) >= 4 ThenIf DataArray(i + 1, j + 1) = MyColor ThenIf DataArray(i + 2, j + 2) = MyColor ThenIf DataArray(i + 3, j + 3) = MyColor ThenIf DataArray(i + 4, j + 4) = MyColor Then IfSucceed = True Exit ForExit ForEnd IfEnd IfEnd IfEnd IfEnd IfIf i > 4 And (14 - j) >= 4 ThenIf DataArray(i - 1, j + 1) = MyColor ThenIf DataArray(i - 2, j + 2) = MyColor ThenIf DataArray(i - 3, j + 3) = MyColor ThenIf DataArray(i - 4, j + 4) = MyColor Then IfSucceed = True Exit ForExit ForEnd IfEnd IfEnd IfEnd IfEnd IfIf (14 - i) >= 4 ThenIf DataArray(i + 1, j) = MyColor ThenIf DataArray(i + 2, j) = MyColor ThenIf DataArray(i + 3, j) = MyColor ThenIf DataArray(i + 4, j) = MyColor Then IfSucceed = True Exit ForExit ForEnd IfEnd IfEnd IfEnd IfEnd IfIf (14 - j) >= 4 ThenIf DataArray(i, j + 1) = MyColor ThenIf DataArray(i, j + 2) = MyColor ThenIf DataArray(i, j + 3) = MyColor Then If DataArray(i, j + 4) = MyColor ThenIfSucceed = True Exit For Exit For End IfEnd IfEnd IfEnd IfEnd IfEnd IfNext iNext jIf IfSucceed ThenIf Not P2PlayColor ThenTimer2.Enabled = False ' 白方赢计时停止Timer3.Enabled = FalseTimer4.Enabled = FalseMsgBox "白方胜!", vbOKOnly CmdStart_ClickElseTimer2.Enabled = False ' 黑方赢计时停止Timer3.Enabled = FalseTimer4.Enabled = FalseMsgBox "黑方胜!", vbOKOnly CmdStart_ClickEnd IfEnd IfEnd Sub '分别记录黑白棋子的分布Private Sub RemenberCrossData(x0_ As Integer, y0_ As Integer) If MyColor ThenDataArray(x0_, y0_) = 1ElseDataArray(x0_, y0_) = 0End IfEnd Sub'简单难度思考时间20 秒PrivateSub Timer2_Timer() Dim i AsIntegeri = 1sumtime = sumtime + i '计时FrmMain.ClsPrint 20 - sumtime '剩余时间提示If sumtime = 20 ThenIf MyColor = 1 ThenTimer2.Enabled = FalseMsgBox "白棋超时"Call CmdStart_ClickElseTimer2.Enabled = FalseMsgBox "黑棋超时"Call CmdStart_Click End IfEnd If '白方超时计时停止'黑方超时计时停止End Sub '中等难度思考时间10 秒Private Sub Timer3_Timer()Dim i As Integeri = 1sumtime = sumtime + i '计时FrmMain.ClsPrint 10 - sumtime '剩余时间提示If sumtime = 10 ThenIf MyColor = 1 ThenTimer3.Enabled = FalseMsgBox "白棋超时"Call CmdStart_ClickElseTimer3.Enabled = FalseMsgBox "黑棋超时"Call CmdStart_ClickEnd IfEnd IfEnd Sub '困难难度思考时间5 秒Private Sub Timer4_Timer()Dim i As Integeri = 1sumtime = sumtime + i '计时FrmMain.ClsPrint 5 - sumtime '剩余时间提示'白方超时计时停止'黑方超时计时停止If sumtime = 5 ThenIf MyColor = 1 ThenTimer4.Enabled = FalseMsgBox "白棋超时"Call CmdStart_Click ElseTimer4.Enabled = FalseMsgBox "黑棋超时"Call CmdStart_Click End If '白方超时计时停止'黑方超时计时停止End If End Sub。
五子棋游戏实验报告
采用数据库技术实现棋局保存:
数据库设计:
数据表字段:棋局名、x坐标、y坐标、该点状态值、下棋状态。
每条记录表示棋盘上一个存储的点的信息。
1、一定要认真听讲做笔记,这是提高效率最快的方法,就像老师说的。
有了上机课,我才能真正懂得听课的重要性,其实有些课件虽然老师为了学生能够及时的进行自我复习传上去一些,但有些东西终归是老师上课讲的,而并没有写入课件的。所以,当真正自己写的时候,一遍一遍的被程序提示出错,内心其实很着急,因为老师只有一个,而且也不会围着你团团转,所以这时候,请教同学是唯一的方法,但是这唯一的方法也不是时时都能发挥作用,同学也有不会,还是需要老师。其实我知道,大部分的问题都是上课老师已经讲过的,只是自己没听。或者是一知半解,经常写丢一些重要程序,导致程序一直无法运行。比如我经常把退出子过程exit sub弄丢,自己当时没觉得它很重要,虽然老师讲过如果没有这个,后边的程序会很麻烦。当时,我也只是那么一听,没在意,后来编程黑白子竖方向赢棋时没把它加上,之后麻烦就出现了,我的黑白子一直都没法完成竖方向赢棋总是有一个方向没法判断,起初老师告诉我看看是否有算法上的错误,但是,我找了一节多课(我承认我这个人太固执,必须要知道为什么错了,才会做下一步),我觉得我的算法一定没问题,但是不明白为什么错,后来在读别人的程序时,才发现是我的exit sub没写,造成的,只有退出我上一个的子过程,这个过程才能正常运行。所以可见没听老师的话,是多么可怕,尤其还是在我也没听他讲课的前提下……根本不懂代码意思,乱删改造成的后果。所以,不管是在哪里,学知识是对任何事情在打基础。做每一步都要问一句问什么,凡是都有原因。
基于VB2005的五子棋游戏设计
2.3 主要算法设计 (1)分别随机生成100个一维数组picturebox控件,把一维数组转变成10×10二维数组摆放。在picturebox控件中默认显示为白色背景,如果用鼠标点击每个图片就会变成黑色的圆圈。主要的玩法是,查找周围横竖撇捺的连续相邻的5个位置的图片是否相同,如果相同则胜出,游戏结束。所以游戏的主要算法是实现了一个位置的周围横竖撇捺的连续相邻的5个位置的查找。。
基于VB2005的五子棋游戏设计
摘 要: 参考社会上流行的五子棋游戏,使用VB2005进行程序模拟设计,用数组的变换实现查找功能,可增加学生学习编写程序的兴趣,提高《面向对象程序设计》课程教学的效果。关键词: 五子棋游戏; VB2005; 循环; 数组
随着微软公司的系列不断推出,在游戏编程方面的强大特性逐渐显露出来,强大功能使之非常适用于各类游戏的开发,本文介绍的是用VB2005模拟系统自带的扫雷游戏设计与实现。1 Visual 2005简介 Visual 以Visual 为基础,是Visual 的主要组成部分。它虽然从Visual Basic 6.0发展而来,但并不是对Visual Basic 6.0的简单改良,而是从基础开始建立了一个新的平台。Visual 是在现有Visual Basic 6.0基础之上的一次重大飞跃,它是一个全新的平台,而不是Visual Basic 6.0的一个升级版本。VB2005 是Visual 2005的其中一部分,主要是以VB2005编写程序的模式。2 五子棋游戏的设计与实现 五子棋是起源于中国古代的传统黑白棋种之一。现代五子棋日文称之为“连珠”,英译为“Renju”,英文称之为“Gobang”或“FIR”(Five in a Row的缩写),亦有“连五子”、“五子连”、“串珠”、“五目”、“五目碰”、“五格”等多种称谓。 五子棋不仅能增强思维能力,提高智力,而且富含哲理,有助于修身养性。五子棋既有现代休闲的明显特征“短、平、快”,又有古典哲学的高深学问“阴阳易理”;它既有简单易学的特性,为人民群众所喜闻乐见,又有深奥的技巧和高水平的国际性比赛;它的棋文化源渊流长,具有东方的神秘和西方的直观;既有“场”的概念,亦有“点”的连接。它是中西文化的交流点,是古今哲理的结晶。 五子棋游戏是社会上流行的一个简单游戏,大多数的人都玩过,游戏的规则也是很多人都会,即在一个规定的二维数组中,横竖撇捺的5个连续一样就为胜利。使用VB2005设计与实现的该游戏既实现了游戏规则的自动设定,又实现了五子棋游戏的各项功能。2.1 功能设计 五子棋游戏主要有以下功能模块:(1)级别/等级分管理:设置五子棋游戏的棋盘大小,根据游戏者自选等级(程序中暂时设置了3个等级:初级,棋盘的维数是10×10二维,计算机的自动计算水平简单,玩家能简单赢取胜利,以吸引玩家的注意与好奇;中级,棋盘的维数是20×20二维,计算机的自动计算水平有所提高,玩家不能简单赢取胜利,需要用一定的时间与计算才能取得胜利;高级,棋盘的维数是30×30二维,计算机的自动计算水平为最高,玩家需要用更多的时间与计算才能取得胜利),使得游戏更加公平合理。(2)游戏英雄榜:可以存储游戏者前10名成绩。(3)背景颜色:游戏者可以通过按钮选择喜欢的游戏背景颜色。(4)棋子与鼠标的背景选择与更改:选择玩家喜欢的棋子背景与鼠标图案,玩家可以选择先后下棋,以更好地吸引玩家进行游戏。(5)背景音乐:在五子棋游戏进行过程中还提供背景音乐,背景音乐可由游戏者设置。(6)帮助文档:为游戏者提供文档帮助信息,包括游戏简介、界面的使用、鼠标和键盘的操作、游戏规则、疑难问答、版本信息等。2.2 界面设计 游戏界面采用2005实现。以初级为例,棋盘为100个的一维数组,即有100个图片,在图1中变换为10×10二维数组。
五子棋实验报告(含代码)
实验报告实验一五子棋游戏北方工业大学 2013级计算机技术米鹏一、实验原理及方法五子棋游戏开发借用Visual Studio 2012软件开发平台,选用C#语言进行编写。
整体程序主要分为三部分:界面操作部分、AI逻辑部分和棋子定点分析部分。
1、界面操作部分界面操作部分代码主要针对图像呈现、对应矩阵存储、下棋过程控制等可见的操作环节进编写。
同时负责整个程序的初始化工作。
图像呈现采用C#中Graphics进行绘制。
棋盘被划分为15行15列,每个划分出的小方格均为30*30的正方形,棋盘可操作的范围规定在(20,20)、(460,460)两点的确定的正方形区域内。
通过鼠标左击来确定下子地点。
程序会根据鼠标鼠标点击的位置进行计算,计算得到时对应矩阵的行列,之后再改变对应矩阵的内容后,在通过行列值乘以小方格边长计算得到在显示区域中的具体位置,再稍加变动后画到显示区域中。
以X点坐标为例,下面是计算X(Column)的流程图:在对应矩阵存储方面,后面AI逻辑和棋子分析所用到的矩阵都是来源这里。
同时AI 逻辑和棋子分析不能去修改对应矩阵内容。
图像呈现点的位置、重绘的根据都是来源这里。
在下棋过程控制方面采用信号亮的机制,当操作者下过后,根据信号AI会立即计算将要下点的位置同时改变信号亮变量。
当AI下过棋子后,由于信号亮的的限制就等待操作者去下棋,同时改变信号亮变量内容。
AI和操作者的所有下子、修改矩阵、显示棋子的过程都是统一的。
在每一盘游戏开始时程序会对一些重要的变量进行初始化这里包括矩阵、信号亮、第一步棋子颜色、呈现图像等内容进行初始化。
同时AI会在棋盘中央下第一子。
2、AI逻辑部分AI逻辑部分算是整个程序策略的灵魂。
其中的一些关键性判别的前后关系将影响AI 的下棋的结果。
同时加大和降低AI的难度也是这里。
下面是我设计的策略过程:从下棋者的考虑角度进行考虑,尽可能保证每一次下子都是有必要的、都是在情理当中的。
我所设计的策略并不是完整,漏洞在与没有考虑三棋子连续的情况。
基于vc++的五子棋程序设计与实现大学论文
河北农业大学现代科技学院毕业论文(设计)题目:基于VC++的五子棋程序设计与实现摘要C++语言是一种面向对象的语言,尽管在当前,可视化语言发展迅速,普及很快,但C++语言作为一种基础的语言,它的有时依然存在,甚至有时它是不可替代的,特别是和硬件接口技术相联系的软件。
五子棋游戏是一种简单大众的游戏,自计算机实现以来,深受广大电脑玩家的喜爱,现在流行的五子棋游戏软件大多缺乏美观的界面,和容易的操作方法,电脑的AI值也不是很高。
本文通过C++语言在计算机图形方面的编程,设计了五子棋游戏软件,使该软件具有还算美观和操作简单的界面,在人机对战时,分为低级,中级和专家级,加大对游戏的乐趣,当然也可以实行人人对战。
本游戏是以C++语言作为开发工具,采用搜索算法设计最优落子点开发的游戏软件。
本文就是介绍五子棋软件设计的全过程。
关键字C++语言面向对象最优落子AbstractC + + language is an object-oriented language, although in the current, visual language developed rapidly, spread quickly, but the C + + language as a basic language, it is sometimes still exists, and sometimes it is irreplaceable, especially with hardware interface technology associated software. V olkswagen backgammon game is a simple game, since the computer to realize, the majority of PC gamers love, now popular backgammon game software lacks most beautiful interface, and easy method of operation, AI value of the computer is not very high. In this paper, C + + programming language in computer graphics, design a backgammon game software, so that the software has the appearance and operation is fairly simple interface, the man-machine war, divided into low, intermediate and expert level, increase the game fun, of course, can also be implemented for all war. This game is based on C + + language as a development tool, the search algorithm design optimal point developed game software. This article is to introduce the whole process of backgammon software design. Keyword C++ language Object-oriented the optimal initial目录1绪论 (5)1.1五子棋的简介 (5)1.2 Visual C++的介绍 (6)1.3 开发环境及运行环境 (6)1.3.1 开发环境 (6)1.3.2 运行环境 (6)2 系统分析 (7)2.1系统调研 (7)2.2可行性分析 (7)2.3 技术可行性分析 (7)3软件架构 (9)3.1 棋盘类 (9)3.1.1主要成员变量 (9)3.2 主要成员函数说明 (9)3.2.1 清空棋盘 (9)3.2.2 绘制棋子 (9)3.2.3 左键消息 (10)3.2.4 绘制棋盘 (10)3.2.5 对方落子完毕 (10)3.2.6 胜负的判断 (10)3.3 游戏模式类 (10)3.3.1主要成员变量 (10)3.3.2主要成员函数 (11)3.4 资源编辑 (11)3.5消息说明 (13)3.5.1落子消息 (14)3.5.2声音消息 (14)3.5.3提示消息 (14)3.5.4悔棋消息 (14)3.5.5输赢消息 (14)3.5.6再次开局消息 (14)4主要算法 (15)4.1判断胜负 (15)4.2人机对弈算法 (16)4.2.1获胜组合 (16)4.2.2落子后处理 (16)4.2.3查找棋盘空位 (16)4.2.4落子打分 (17)4.2.5防守策略 (17)4.2.6选取最佳落子 (17)5 软件实现 (18)5.1游戏运行 (18)6补充说明 (21)7结论 (22)致谢 (23)1绪论1.1五子棋的简介五子棋是起源于中国古代的传统黑白棋种之一。
Qbasic编写五子棋
DATA 0, -1, 1, -1, 1, 0, 1, 1, 0, 1, -1, 1, -1, 0, -1, -1
' 棋谱数据
LeanData:
' ┌─────┬─────┬─────┬─────┬─────┬─────┐
' │○○○○○│○○○○●│○○○●○│○○○●●│○○●○○│○○●○●│
CONST MAXX = 342 '手光标最大移动宽度
CONST MAXY = 250 '手光标最大移动高度
CONST Debug.X = 45 '棋盘位置微调
CONST Dt AS STRING * 6
END TYPE
' 常量定义
CONST Null = 0 '空值
CONST True = -1 '逻辑真
CONST False = NOT True '逻辑假
IF Welcome THEN PlayTimes Times
GameOver
SYSTEM
' 时间陷井
DoClock:
D$ = " 日期:" + DATE$ + " 时间:" + TIME$
L = LEN(D$): FOR I = 0 TO 80 \ L: D$ = D$ + D$: NEXT
' ├──┬──┼──┬──┼──┬──┼──┬──┼──┬──┼──┬──┤
' │优先│位置│优先│位置│优先│位置│优先│位置│优先│位置│优先│位置│
五子棋软件及使用
五子棋软件使用(五子棋装B进阶教程)前言:请以牛B的姿态看待此教程。
本教程只传授装B技术。
通过本教程,希望广大装B 爱好者如本屌一般,一次性装B到位,从而达到生活处处可装B的高级境界。
而本屌目前的境界是:装得随心所欲,本身却是无心装B的,颇有一番无心插柳柳成荫的无奈。
下面进入正题,五子棋装B教程开始。
五子棋装逼进阶之 1 ---------- 单一软件装 B工欲善其事,必先利其器。
先把武器准备好。
链接:/1kTuYR4N 密码:o9wa 有些用得上,有些用不上。
1.Goro3.92.黑石3.7 3.黑石3.824.yixin5.连珠终结者086.连珠终结者097.连珠宝典8.MultiGo 49.连珠妙手其中有对弈用,有摆棋用,有挂载引擎用。
对于初入棋坛,棋技菜菜,又想装逼的我们,对弈软件应运而生。
我不会下棋,软件怎么走,我就怎么走。
下无禁手:可选择1.连珠妙手2.Goro3.9 3. yixin软件下有禁手: 可选择2 4 9号软件。
使用后,基本就能算是入门级了。
此阶段容易产生舍我其谁的感觉,以为神器在手,天下我有。
天老大,我老二。
熟不知,并不是你一个人有软件。
本屌给的忠告就是,此时万不可强行装B。
会死得很惨的。
五子棋装逼进阶之 2 ----------- 选择软件装 B这一阶段,是经过第一阶段的实践,已经小有心得,大体了解各软件棋力。
产生了自己认为强大的软件。
知道了各软件的算杀及对弈特点。
这里本屌说下本屌总结的各软件特点以及战斗力评定。
Goro3.9 :无禁手软件,等级☆☆☆☆,综合评语:界面不错,行棋算杀能力中上,不推荐单一使用。
连珠妙手:无禁有禁合一,等级☆☆☆,综合评语:操作简单,行棋算杀能力中等,不推荐使用。
Yixin :功能大全,等级☆☆☆☆☆,综合评语:界面简洁略单调,审局偶有坑爹,多数不错;算杀较快。
推荐使用。
期待作者后续更新。
黑石3.7:五子棋软手必备软件。
经典中的经典。
可惜过时了。
VB五子棋代码
代码如下:Option ExplicitConst SubWidth = 400 '定义画五子棋表格的每格长度和宽度Private P2PlayColor As Integer '实现黑白棋子的交替进行Private MyColor As Integer '标记黑白双方棋子颜色Private IfSucceed As Boolean '表示是否胜利Const pi = 3.14159 '定义字符常量pi=3.14159Private centerx As SinglePrivate centery As SinglePrivate radius As SinglePrivate DataArray(14, 14) As Integer '保存棋盘中棋子的位置信息(空子=3 黑棋=1 白棋=0)Private sumtime As Integer '记录总时间来判断谁超时Private ifStarteasy As Boolean '标记简单难度下计时功能是否可以开启(ifStarteasy=true时每落子一次计时开启一次)Private ifStartnormal As Boolean '标记中等难度下计时功能是否可以开启(ifStartnormal=true时每落子一次计时开启一次)Private ifStarthard As Boolean '标记困难难度下计时功能是否可以开启(ifStarthard=true时每落子一次计时开启一次)'单击命令按钮"退出"退出Private Sub CmdExit_Click()EndEnd SubPrivate Sub CmdStart_Click()Dim i As IntegerDim m As IntegerDim n As Integer'绘制棋盘PicQiPan.ClsPicQiPan.ForeColor = vbBlackFor i = 1 To 14PicQiPan.Line (SubWidth, SubWidth * i)-(SubWidth * 14, _ SubWidth * i)PicQiPan.Line (SubWidth * i, SubWidth)-(SubWidth * i, _ SubWidth * 14)Next i'棋盘落点信息初始化For m = 0 To 14For n = 0 To 14DataArray(m, n) = 3Next nNext m'主要标记信息初始化P2PlayColor = 0MyColor = 0IfSucceed = FalseifStarteasy = FalseifStartnormal = FalseifStarthard = FalseTimer2.Enabled = FalseTimer3.Enabled = FalseTimer4.Enabled = FalseFrmMain.Clssumtime = -1End Sub'简单难度Private Sub fileeasy_Click()ifStarteasy = Truesumtime = -1MsgBox "双方下每步棋的思考时间最多20秒,否则超时清盘"End Sub'通过文件"退出"退出Private Sub fileexit_Click()EndEnd Sub'困难难度Private Sub filehard_Click()ifStarthard = Truesumtime = -1MsgBox "双方下每步棋的思考时间最多5秒,否则超时清盘"End Sub'中等难度Private Sub filenormal_Click()ifStartnormal = Truesumtime = -1FrmMain.ClsMsgBox "双方下每步棋的思考时间最多10秒,否则超时清盘"End Sub'通过文件"重新开始"实现棋盘初始化Private Sub filerestart_Click()Call CmdStart_ClickEnd SubPrivate Sub Form_Load()Dim i As IntegerDim m As IntegerDim n As Integer'绘制棋盘PicQiPan.ClsPicQiPan.ForeColor = vbBlackFor i = 1 To 14PicQiPan.Line (SubWidth, SubWidth * i)-(SubWidth * 14, _SubWidth * i)PicQiPan.Line (SubWidth * i, SubWidth)-(SubWidth * i, _SubWidth * 14)Next i'棋盘落点信息初始化For m = 0 To 14For n = 0 To 14DataArray(m, n) = 3Next nNext mPrint'确定表针位置的基本参量centerx = Pictime.Width / 2centery = Pictime.Height / 2radius = Pictime.Height / 2 * 0.9Pictime.PSet (centerx, centery)Pictime.Circle (centerx, centery), radiusEnd Sub'棋子落点判断(出界和重子情况)Private Sub PicQipan_MouseDown(Button As Integer, Shift As Integer, X As Single, y As Single)Dim x0 As IntegerDim y0 As IntegerDim i As IntegerDim j As IntegerIf X < SubWidth Or X > 14.5 * SubWidth Or y < SubWidth Or y > 14.5 * SubWidth Then MsgBox "超出棋盘界限,请重新下!"Exit SubEnd IfIf Abs(Int(X / SubWidth) - X / SubWidth) < 0.5 Thenx0 = Int(X / SubWidth)Elsex0 = Int(X / SubWidth) + 1End IfIf Abs(Int(y / SubWidth) - y / SubWidth) < 0.5 Theny0 = Int(y / SubWidth)Elsey0 = Int(y / SubWidth) + 1End IfIf DataArray(x0, y0) <> 3 Then'当前位置已经有棋子了,MsgBox "当前位置已经有棋子了,请重新走!", vbCritical, "NOTE!"Exit SubEnd Ifsumtime = -1Call DrawPill(x0, y0) '画棋子Call RemenberCrossData(x0, y0) '记录棋子信息Call WhoWin '判断谁赢'判断是否开启相应难度计时功能If ifStarteasy = True ThenTimer2.Enabled = TrueEnd IfIf ifStartnormal = True ThenTimer3.Enabled = TrueEnd IfIf ifStarthard = True ThenTimer4.Enabled = TrueEnd IfEnd Sub'画棋子Private Sub DrawPill(xx0 As Integer, yy0 As Integer)If P2PlayColor ThenPicQiPan.ForeColor = vbWhiteDoEventsPicQiPan.FillColor = vbWhitePicQiPan.FillStyle = 0MyColor = 0ElsePicQiPan.ForeColor = vbBlackDoEventsPicQiPan.FillColor = vbBlackPicQiPan.FillStyle = 0MyColor = 1End IfP2PlayColor = Not P2PlayColorPicQiPan.Circle (xx0 * SubWidth, yy0 * SubWidth), SubWidth * 0.5 End Sub'以下A B C 三个事件共同实现下棋的同时听音乐功能'APrivate Sub Dir1_Change()File1.Path = Dir1.PathEnd Sub'BPrivate Sub Drv_Change()Dir1.Path = Drv.DriveEnd Sub'CPrivate Sub File1_Click()mp3.URL = File1.Path & "\" & File1.FileName End Sub'棋盘皮肤Private Sub qipanstylefurA_Click()PicQiPan.BackColor = &HC0FFFFCall CmdStart_ClickEnd SubPrivate Sub qipanstylefurB_Click()PicQiPan.BackColor = &HC0C000Call CmdStart_ClickEnd SubPrivate Sub qipanstylefurC_Click()PicQiPan.BackColor = &HE0E0E0Call CmdStart_ClickEnd SubPrivate Sub qipanstylefurD_Click()PicQiPan.BackColor = &H8080FFCall CmdStart_ClickEnd Sub'添加四种背景音乐Private Sub stylemusicA_Click()mp3.URL = App.Path & "\" & "music01.mp3"End SubPrivate Sub stylemusicB_Click()mp3.URL = App.Path & "\" & "music02.mp3"End SubPrivate Sub stylemusicC_Click()mp3.URL = App.Path & "\" & "music03.mp3"End SubPrivate Sub stylemusicD_Click()mp3.URL = App.Path & "\" & "music04.mp3"End Sub'表针走动Timer1.Enabled=true在属性框中设定Private Sub Timer1_Timer()Dim s As IntegerDim m As IntegerDim h As IntegerDim sngLenS As SingleDim sngLenM As SingleDim sngLenH As SingleDim i As Integer'调试几次并查询VB常用函数,最后确定应该使用Now 而不是Time(不过之前使用Time确实可以)s = Second(Now)m = Minute(Now)h = Hour(Now) + m / 60sngLenS = radius * 0.8sngLenM = radius * 0.6sngLenH = radius * 0.4Pictime.ClsPictime.Scale (-centerx, centery)-(centerx, -centery)Pictime.Line (0, 0)-(sngLenS * Sin(2 * pi * s / 60), sngLenS * Cos(2 * pi * s / 60)), vbGreen Pictime.Line (0, 0)-(sngLenM * Sin(2 * pi * m / 60), sngLenM * Cos(2 * pi * m / 60)), vbGreenIf h > 12 Thenh = h - 12End IfPictime.Line (0, 0)-(sngLenH * Sin(2 * pi * h / 12), sngLenH * Cos(2 * pi * h / 12)), vbGreen Pictime.Circle (0, 0), radius * 0.9For i = 1 To 12Pictime.Circle (radius * 0.9 * 0.85 * Sin(2 * pi * i / 12), radius * 0.9 * 0.85 * Cos(2 * pi * i / 12)), radius * 0.01, vbGreenNext iEnd Sub'判断谁赢了Private Sub WhoWin()Dim i As IntegerDim j As IntegerFor j = 1 To 14For i = 1 To 14If DataArray(i, j) = MyColor And Not IfSucceed ThenIf (14 - i) >= 4 And (14 - j) >= 4 ThenIf DataArray(i + 1, j + 1) = MyColor ThenIf DataArray(i + 2, j + 2) = MyColor ThenIf DataArray(i + 3, j + 3) = MyColor ThenIf DataArray(i + 4, j + 4) = MyColor ThenIfSucceed = TrueExit ForExit ForEnd IfEnd IfEnd IfEnd IfEnd IfIf i > 4 And (14 - j) >= 4 ThenIf DataArray(i - 1, j + 1) = MyColor ThenIf DataArray(i - 2, j + 2) = MyColor ThenIf DataArray(i - 3, j + 3) = MyColor ThenIf DataArray(i - 4, j + 4) = MyColor ThenIfSucceed = TrueExit ForExit ForEnd IfEnd IfEnd IfEnd IfEnd IfIf (14 - i) >= 4 ThenIf DataArray(i + 1, j) = MyColor ThenIf DataArray(i + 2, j) = MyColor ThenIf DataArray(i + 3, j) = MyColor ThenIf DataArray(i + 4, j) = MyColor ThenIfSucceed = TrueExit ForExit ForEnd IfEnd IfEnd IfEnd IfEnd IfIf (14 - j) >= 4 ThenIf DataArray(i, j + 1) = MyColor ThenIf DataArray(i, j + 2) = MyColor ThenIf DataArray(i, j + 3) = MyColor ThenIf DataArray(i, j + 4) = MyColor ThenIfSucceed = TrueExit ForExit ForEnd IfEnd IfEnd IfEnd IfEnd IfEnd IfNext iNext jIf IfSucceed ThenIf Not P2PlayColor ThenTimer2.Enabled = False '白方赢计时停止Timer3.Enabled = FalseTimer4.Enabled = FalseMsgBox "白方胜!", vbOKOnlyCmdStart_ClickElseTimer2.Enabled = False '黑方赢计时停止Timer3.Enabled = FalseTimer4.Enabled = FalseMsgBox "黑方胜!", vbOKOnlyCmdStart_ClickEnd IfEnd IfEnd Sub'分别记录黑白棋子的分布Private Sub RemenberCrossData(x0_ As Integer, y0_ As Integer)If MyColor ThenDataArray(x0_, y0_) = 1ElseDataArray(x0_, y0_) = 0End IfEnd Sub'简单难度思考时间20秒Private Sub Timer2_Timer()Dim i As Integeri = 1sumtime = sumtime + i '计时FrmMain.ClsPrint 20 - sumtime '剩余时间提示If sumtime = 20 ThenIf MyColor = 1 ThenTimer2.Enabled = False '白方超时计时停止MsgBox "白棋超时"Call CmdStart_ClickElseTimer2.Enabled = False '黑方超时计时停止MsgBox "黑棋超时"Call CmdStart_ClickEnd IfEnd IfEnd Sub'中等难度思考时间10秒Private Sub Timer3_Timer()Dim i As Integeri = 1sumtime = sumtime + i '计时FrmMain.ClsPrint 10 - sumtime '剩余时间提示If sumtime = 10 ThenIf MyColor = 1 ThenTimer3.Enabled = False '白方超时计时停止MsgBox "白棋超时"Call CmdStart_ClickElseTimer3.Enabled = False '黑方超时计时停止MsgBox "黑棋超时"Call CmdStart_ClickEnd IfEnd IfEnd Sub'困难难度思考时间5秒Private Sub Timer4_Timer()Dim i As Integeri = 1sumtime = sumtime + i '计时FrmMain.ClsPrint 5 - sumtime '剩余时间提示If sumtime = 5 ThenIf MyColor = 1 ThenTimer4.Enabled = False '白方超时计时停止MsgBox "白棋超时"Call CmdStart_ClickElseTimer4.Enabled = False '黑方超时计时停止MsgBox "黑棋超时"Call CmdStart_ClickEnd IfEnd IfEnd Sub。
vb6.0的五子棋源代码
vb6.0的五子棋源代码Option ExplicitDim blackturn As Boolean '轮到黑方下子Dim whiteturn As Boolean '轮到黑方下子Dim qipan(0 To 15, 0 To 15) As Integer '用此二维数组表示棋盘Dim inti As Integer '数组元素Dim intj As IntegerDim boolF As Boolean '表示棋局状态:进行/结束Private Sub begin_Click() '重新开始Me.Cls '清除窗口For inti = 0 To 15For intj = 0 To 15qipan(inti, intj) = 0 '数组清零NextNextForm_Load '重画棋盘End SubPrivate Sub exit_Click() '退出Unload MeSet Form1 = NothingEnd SubPrivate Sub Form_Load()Form_Paint '画棋盘blackturn = True '黑方先下boolF = True '开始Label1.Caption = "黑方先下"End Sub'下子Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)Dim intx As Integer '落子横向位置Dim inty As Integer '落子竖向位置If boolF = False Then '确定棋局是否在进行中,否,跳出Label1.Caption = "结束"Exit SubEnd If'确定落子的确切位置'如果鼠标点击位置不在棋盘中,则跳出If x < 10 Or x > 310 Or y < 10 Or y > 310 ThenExit SubEnd If'如果鼠标点击位置在棋盘中,则转化为相应棋盘落子点的坐标If (x - 10) Mod 20 < 10 Thenintx = x - (x - 10) Mod 20Elseintx = x + 20 - (x - 10) Mod 20End IfIf (y - 10) Mod 20 < 10 Theninty = y - (y - 10) Mod 20Elseinty = y + 20 - (y - 10) Mod 20End If'把坐标转换成数组中的相应元素inti = (intx - 10) / 20intj = (inty - 10) / 20'如果该数组元素不为零,即表示棋盘中相应点已有棋子,则跳出If qipan(inti, intj) <> 0 ThenExit SubEnd If'画子(圆)If blackturn = True Then'黑色Me.FillColor = RGB(0, 0, 0)qipan(inti, intj) = 1 '黑子赋1Label1.Caption = "白方"Else'白色Me.FillColor = RGB(255, 255, 255)qipan(inti, intj) = 2 '白子赋2Label1.Caption = "黑方"End IfMe.FillStyle = 0 '不可缺Me.Circle (intx, inty), 8'判断是否有五子连线Call IfFiveLine'轮流blackturn = Not blackturn '取反End SubPrivate Sub IfFiveLine() '判断是否有五子连线Dim strwho As String '下子方名称If qipan(inti, intj) = 1 Then '表示黑方下的子strwho = "黑方"Elsestrwho = "白方"End If'分别判断横竖,对角线是否有五子,此段代码比较复杂,可能那以理解,但其执行效率极高'非常适合与棋盘格子很多的情况If samelinenums(1, 0) >= 5 Or samelinenums(0, 1) >= 5 Or samelinenums(1, 1) >= 5 Or samelinenums(-1, 1) >= 5 Then MsgBox strwho & "胜!"boolF = False '棋局结束End IfEnd SubFunction samelinenums(changei As Integer, changej As Integer) '判断同一直线上的棋子数Dim i As IntegerDim j As IntegerDim num As Integer '同一线上相同颜色棋子数'计算落子一边同颜色的棋子数i = inti: j = intjDoIf qipan(i, j) <> qipan(inti, intj) Thennum = max(Abs(inti - i), Abs(intj - j))Exit DoEnd Ifi = i + changei: j = j + changejLoop Until i < 0 Or i > 15 Or j < 0 Or j > 15'计算落子另一边同颜色的棋子数i = inti: j = intjDoIf qipan(i, j) <> qipan(inti, intj) Thennum = num - 1 + max(Abs(inti - i), Abs(intj - j))Exit DoEnd Ifi = i - changei: j = j - changejLoop Until i < 0 Or i > 15 Or j < 0 Or j > 15'MsgBox numsamelinenums = numEnd Function'求较大值Function max(inta As Integer, intb As Integer)max = intaIf max < intb Then max = intbEnd FunctionPrivate Sub Form_Paint() '以(10,10)为左上角坐标画一个16*16,每格边长为20象素的棋盘Cls '清除Dim i As IntegerScaleMode = 3 '设定窗体画布的单位为象素For i = 10 To 330 Step 20Me.Line (10, i)-(330, i)Me.Line (i, 10)-(i, 330)NextEnd Sub。
VB课设五子棋
目录摘要....................... 错误!未定义书签。
第一章绪论............................... - 3 -1. 1 五子棋的历史...................... - 3 -1.2 传统五子棋软件与网络五子棋软件的异同- 5 - 第二章系统分析........................... - 6 -2.1 需求分析........................... - 6 -2.2 总体设计........................... - 8 - 第三章总体设计及详细设计................. - 9 -3.1总体设计 ........................... - 9 -3.2详细设计代码的实现................. - 10 - 第四章调试与测试........................ - 14 - 总结与心得............................... - 16 - 附录(源代码)........................... - 17 - 参考文献.............................. - 24 -摘要伴随着计算机技术和网络技术的发展,世界第一款图形化网络游戏《网络创世纪》问世。
挟计算机与网络技术之高精尖,融传统视听与数码娱乐之精华,集娱乐性、竞技性、仿真性、互动性于一体,《网络创世纪》迅速风靡全球,备受青睐。
自此拉开了网络游戏的序幕。
五子棋在我国的历史可谓是源远流长,喜爱它的玩家也是大有人在。
但目前的五子棋软件一般都是单机版的,游戏无法在异地的玩家之间进行。
基于上述原因,本人决定以当今广为流行五子棋软件为模版,以Microsoft公司的Visual Basic程序设计语言为工具,结合软件工程的思路开发一款基于网络的五子棋对弈软件。
Excel制作五子棋vba源代码
Excel制作象棋vba源代码' The algorithm of judge and urgentpoint function are exported from one VC program which I downloaded from web.' Sorry I can't remebered the program and the author name.' The original VC program have three options for different level. I simplized it to the hardest one in this VBA sample.'Dim m_Board(17, 17) As IntegerPrivate Type Cpointx As Integery As IntegerEnd TypeDim m_nType As IntegerDim iWho As IntegerPrivate Sub Excelba_Click()End SubPrivate Sub cmdStart_Click()Cells(17, 1) = "Start"Cells(17, 3) = 0 ' total number of stones'clear the boardRange(Cells(17 + 1, 1), Cells(17 + 15, 15)).Value = 0'clear all picture in this sheet except for two orginal pictureFor Each ipic In ActiveSheet.ShapesIf <> "Picture 9" And <> "Picture 10" And Left(, 7) = "Picture" Thenipic.DeleteEnd IfNext'start itIf optComputer.Value = True ThenCall drawit(8, 8, 1)Call setarray(8, 8, 1)Cells(17, 3) = 1End IfEnd SubPrivate Sub optComputer_Click()Cells(17, 1) = ""Cells(17, 2) = 2Cells(17, 3) = 0End SubPrivate Sub optYou_Click()Cells(17, 1) = ""Cells(17, 2) = 1Cells(17, 3) = 0End SubPrivate Function confArray(ix As Integer, iy As Integer) As IntegerconfArray = Cells(17 + ix, iy)End FunctionPrivate Sub setarray(ix As Integer, iy As Integer, iz As Integer)Cells(17 + ix, iy) = izEnd SubPrivate Sub drawit(ix As Integer, iy As Integer, iz As Integer)Dim strP As StringIf ix < 1 Or ix > 15 Or iy < 1 Or iy > 15 Or iz < 1 Or iz > 2 ThenMsgBox "Wrong Entry Number, please check it!", vbCritical, "Wrong Entry"EndEnd IfIf iz = 1 ThenstrP = "Picture 9"ElseIf iz = 2 ThenstrP = "Picture 10"End IfApplication.ScreenUpdating = FalseActiveSheet.Shapes(strP).SelectSelection.CopyCells(16, 4).SelectActiveSheet.Paste-(Selection.Left - Cells(ix, iy).Left) + 1-(Selection.Top - Cells(ix, iy).Top) + 1Cells(17, 4).SelectApplication.ScreenUpdating = TrueEnd SubPrivate Function UrgentPoint(ByVal iz As Integer) As StringDim i, i0, j, j0 As IntegerDim ptUrgent(2025) As CpointDim nGrade1 As IntegerDim nGrade2 As IntegerDim nUrgent1 As IntegerDim nUrgent2 As IntegerDim nUrgent As IntegerDim iEnd As IntegerDim iStep As IntegerDim jEnd As IntegerDim jStep As IntegerFor i = 0 To 2024ptUrgent(i).x = -1ptUrgent(i).y = -1Next iIf ((Rnd() * 32767) Mod 2) = 0 Theni0 = 0Elsei0 = 14End IfIf i0 = 0 TheniEnd = 14iStep = 1ElseiEnd = 0iStep = -1End IfFor i = i0 To iEnd Step iStepIf ((Rnd() * 32767) Mod 2) = 0 Thenj0 = 0Elsej0 = 14End IfIf j0 = 0 ThenjEnd = 14jStep = 1ElsejEnd = 0jStep = -1End IfFor j = j0 To jEnd Step jStepIf (m_Board(i, j) = 0) ThennGrade1 = Judge(i, j, iz)nGrade2 = Judge(i, j, iz + 1)Select Case (nGrade1)Case 0nUrgent1 = 0Case 1nUrgent1 = 2Case 2nUrgent1 = 4Case 3nUrgent1 = 5Case 4nUrgent1 = 8Case 5nUrgent1 = 10Case 6nUrgent1 = 11nUrgent1 = 12 Case 8nUrgent1 = 13 Case 9nUrgent1 = 14 Case 10nUrgent1 = 15 Case 11nUrgent1 = 16 Case 12nUrgent1 = 17 Case 13nUrgent1 = 18 Case 14nUrgent1 = 19 Case 15nUrgent1 = 20 Case 16nUrgent1 = 32 Case 17nUrgent1 = 34 Case 18nUrgent1 = 36 Case 19nUrgent1 = 38 Case 20nUrgent1 = 40 Case ElsenUrgent1 = 40 End SelectSelect Case (nGrade2)Case 0nUrgent2 = 1 Case 1nUrgent2 = 3 Case 2nUrgent2 = 6 Case 3nUrgent2 = 7 Case 4nUrgent2 = 9 Case 5nUrgent2 = 21nUrgent2 = 22Case 7nUrgent2 = 23Case 8nUrgent2 = 24Case 9nUrgent2 = 25Case 10nUrgent2 = 26Case 11nUrgent2 = 27Case 12nUrgent2 = 28Case 13nUrgent2 = 29Case 14nUrgent2 = 30Case 15nUrgent2 = 31Case 16nUrgent2 = 33Case 17nUrgent2 = 35Case 18nUrgent2 = 37Case 19nUrgent2 = 39Case 20nUrgent2 = 41Case ElsenUrgent2 = 41End SelectnUrgent = WorksheetFunction.Min(nUrgent1, nUrgent2) * 45 + WorksheetFunction.Max(nUrgent1, nUrgent2)ptUrgent(nUrgent).x = iptUrgent(nUrgent).y = jEnd IfNext jNext iFor i = 0 To 2024If ((ptUrgent(i).x <> -1) And (ptUrgent(i).y <> -1)) ThenExit ForEnd IfNext iIf (ptUrgent(i).x = -1 And ptUrgent(i).y = -1) ThenMsgBox "Make Draw"End IfUrgentPoint = ptUrgent(i).x & "|" & ptUrgent(i).yEnd FunctionPrivate Sub Worksheet_SelectionChange(ByVal Target As Range)Dim ix As IntegerDim iy As IntegerDim iz As IntegerDim ix1 As IntegerDim iy1 As IntegerDim stmp As StringDim i As IntegerDim j As IntegerIf > 1 Or Target.Areas(1).Columns.Count > 1 Or Target.Areas(1).Rows.Count > 1 Then Exit SubEnd Ifix = Target.Areas(1).Rowiy = Target.Areas(1).Columniz = confArray(ix, iy)If ix > 15 Or iy > 15 ThenExit SubEnd IfIf iz <> 1 And iz <> 2 And Cells(17, 1) = "Start" ThenFor i = 0 To 14For j = 0 To 14m_Board(j, i) = Cells(17 + i + 1, j + 1)Next jNext iCall drawit(ix, iy, 2)Call setarray(ix, iy, 2)m_Board(iy - 1, ix - 1) = 2Call Judge(iy - 1, ix - 1, 2)Call ringIf Cells(17, 1) = "Start" Thenstmp = UrgentPoint(1)iy1 = Left(stmp, InStr(1, stmp, "|") - 1)ix1 = Mid(stmp, InStr(1, stmp, "|") + 1)Call drawit(ix1 + 1, iy1 + 1, 1)Call setarray(ix1 + 1, iy1 + 1, 1)m_Board(iy1, ix1) = 1Call Judge(iy1, ix1, 1)Call ringEnd IfEnd IfEnd SubPrivate Function Judge(ByVal nX As Integer, ByVal nY As Integer, ByVal cValue As Integer) As IntegerDim nGrade As IntegerDim i As IntegerDim j As IntegerDim k As IntegerDim l As IntegerDim nXStart As IntegerDim nXEnd As IntegerDim nYStart As IntegerDim nYEnd As IntegerDim nXYStart As IntegerDim nXYEnd As IntegerDim nYXStart As IntegerDim nYXEnd As IntegerDim nXStartAdd As IntegerDim nYStartAdd As IntegerDim nXYStartAdd As IntegerDim nYXStartAdd As IntegerDim nXEndAdd As IntegerDim nYEndAdd As IntegerDim nXYEndAdd As IntegerDim nYXEndAdd As IntegerDim bXStartEmpty As BooleanDim bXEndEmpty As BooleanDim bXStartEmpty1 As BooleanDim bXEndEmpty1 As BooleanDim bYStartEmpty As BooleanDim bYEndEmpty As BooleanDim bYStartEmpty1 As BooleanDim bYEndEmpty1 As BooleanDim bXYStartEmpty As BooleanDim bXYEndEmpty As BooleanDim bXYStartEmpty1 As BooleanDim bXYEndEmpty1 As BooleanDim bYXStartEmpty As BooleanDim bYXEndEmpty As BooleanDim bYXStartEmpty1 As BooleanDim bYXEndEmpty1 As BooleannXStart = nXnXEnd = nXnYStart = nYnYEnd = nYnXYStart = nXnXYEnd = nXnYXStart = nXnYXEnd = nXnXStartAdd = 0nYStartAdd = 0nXYStartAdd = 0nYXStartAdd = 0nXEndAdd = 0nYEndAdd = 0nXYEndAdd = 0nYXEndAdd = 0bXStartEmpty = FalsebYStartEmpty = FalsebXYStartEmpty = FalsebYXStartEmpty = FalsebXEndEmpty = FalsebYEndEmpty = FalsebXYEndEmpty = FalsebYXEndEmpty = FalsebXStartEmpty1 = FalsebYStartEmpty1 = FalsebXYStartEmpty1 = FalsebYXStartEmpty1 = FalsebXEndEmpty1 = FalsebYEndEmpty1 = FalsebXYEndEmpty1 = FalsebYXEndEmpty1 = FalseFor i = nX - 1 To 0 Step -1 ' <-If m_Board(i, nY) = cV alue ThennXStart = iElseIf m_Board(i, nY) = 0 ThenbXStartEmpty = TrueFor j = i - 1 To 0 Step -1 ' <-If m_Board(j, nY) = cValue ThennXStartAdd = i - jElseIf m_Board(j, nY) = 0 ThenbXStartEmpty1 = TrueExit ForElseExit ForEnd IfNext jExit ForElseExit ForEnd IfNext iFor i = nX + 1 To 14 ' ->If m_Board(i, nY) = cV alue ThennXEnd = iElseIf m_Board(i, nY) = 0 ThenbXEndEmpty = TrueFor j = i + 1 To 14 ' ->If m_Board(j, nY) = cValue ThennXEndAdd = j - iElseIf m_Board(j, nY) = 0 ThenbXEndEmpty1 = TrueExit ForElseExit ForEnd IfNext jExit ForElseExit ForEnd IfNext iFor i = nY - 1 To 0 Step -1 ' ^|^If m_Board(nX, i) = cV alue ThennYStart = iElseIf m_Board(nX, i) = 0 ThenbYStartEmpty = TrueFor j = i - 1 To 0 Step -1 ' <-If m_Board(nX, j) = cValue ThennYStartAdd = i - jElseIf m_Board(nX, j) = 0 ThenbYStartEmpty1 = TrueExit ForElseExit ForEnd IfNext jExit ForElseExit ForEnd IfNext iFor i = nY + 1 To 14 ' v|vIf m_Board(nX, i) = cV alue ThennYEnd = iElseIf m_Board(nX, i) = 0 ThenbYEndEmpty = TrueFor j = i + 1 To 14 ' ->If m_Board(nX, j) = cValue ThennYEndAdd = j - iElseIf m_Board(nX, j) = 0 ThenbYEndEmpty1 = TrueExit ForElseExit ForEnd IfNext jExit ForElseExit ForEnd IfNext ii = nX - 1j = nY + 1Do While i >= 0 And j < 15'j++If m_Board(i, j) = cValue ThennXYStart = iElseIf m_Board(i, j) = 0 ThenbXYStartEmpty = Truek = i - 1l = j + 1Do While k >= 0 And l < 15If m_Board(k, l) = cValue ThennXYStartAdd = i - kElseIf m_Board(k, l) = 0 ThenbXYStartEmpty1 = TrueExit DoElseExit DoEnd Ifk = k - 1l = l + 1LoopElseExit DoEnd Ifi = i - 1j = j + 1Loopi = nX + 1j = nY - 1Do While i < 15 And j >= 0'j--If m_Board(i, j) = cValue ThennXYEnd = iElseIf m_Board(i, j) = 0 ThenbXYEndEmpty = Truek = i + 1l = j - 1Do While l >= 0 And k < 15If m_Board(k, l) = cValue ThennXYEndAdd = i - kElseIf m_Board(k, l) = 0 ThenbXYEndEmpty1 = TrueExit DoElseExit DoEnd Ifk = k + 1l = l - 1LoopExit DoElseExit DoEnd Ifi = i + 1j = j - 1Loopi = nX - 1j = nY - 1Do While i >= 0 And j >= 0'j--If m_Board(i, j) = cValue ThennYXStart = iElseIf m_Board(i, j) = 0 ThenbYXStartEmpty = Truel = j - 1Do While l >= 0 And k >= 0If m_Board(k, l) = cValue ThennYXStartAdd = i - kElseIf m_Board(k, l) = 0 ThenbYXStartEmpty1 = TrueExit DoElseExit DoEnd Ifk = k - 1l = l - 1LoopExit DoElseExit DoEnd Ifi = i - 1j = j - 1Loopi = nX + 1j = nY + 1Do While i < 15 And j < 15'j--If m_Board(i, j) = cValue ThennYXEnd = iElseIf m_Board(i, j) = 0 ThenbYXEndEmpty = Truek = i - 1l = j - 1Do While l < 15 And k < 15If m_Board(k, l) = cValue ThennYXEndAdd = i - kElseIf m_Board(k, l) = 0 ThenbYXEndEmpty1 = TrueExit DoElseExit DoEnd Ifk = k + 1l = l + 1LoopExit DoElseExit DoEnd Ifi = i + 1j = j + 1LoopnXStep = nXEnd - nXStart + 1nXStep = nXEnd - nXStart + 1nYStep = nYEnd - nYStart + 1nXYStep = nXYEnd - nXYStart + 1nYXStep = nYXEnd - nYXStart + 1Dim bX_4 As BooleanDim bY_4 As BooleanDim bXY_4 As BooleanDim bYX_4 As BooleanDim bX4 As BooleanDim bY4 As BooleanDim bXY4 As BooleanDim bYX4 As BooleanbX_4 = (nXStep = 4) And (bXStartEmpty And bXEndEmpty)bY_4 = (nYStep = 4) And (bYStartEmpty And bYEndEmpty)bXY_4 = (nXYStep = 4) And (bXYStartEmpty And bXYEndEmpty) bYX_4 = (nYXStep = 4) And (bYXStartEmpty And bYXEndEmpty) bX4 = (nXStep = 4) And (bXStartEmpty Or bXEndEmpty)bY4 = (nYStep = 4) And (bYStartEmpty Or bYEndEmpty)bXY4 = (nXYStep = 4) And (bXYStartEmpty Or bXYEndEmpty) bYX4 = (nYXStep = 4) And (bYXStartEmpty Or bYXEndEmpty) Dim bX_3 As BooleanDim bY_3 As BooleanDim bXY_3 As BooleanDim bYX_3 As BooleanDim bX3 As BooleanDim bY3 As BooleanDim bXY3 As BooleanDim bYX3 As BooleanbX_3 = (nXStep = 3) And (bXStartEmpty And bXEndEmpty)bY_3 = (nYStep = 3) And (bYStartEmpty And bYEndEmpty)bXY_3 = (nXYStep = 3) And (bXYStartEmpty And bXYEndEmpty) bYX_3 = (nYXStep = 3) And (bYXStartEmpty And bYXEndEmpty) bX3 = (nXStep = 3) And (bXStartEmpty Or bXEndEmpty)bY3 = (nYStep = 3) And (bYStartEmpty Or bYEndEmpty)bXY3 = (nXYStep = 3) And (bXYStartEmpty Or bXYEndEmpty) bYX3 = (nYXStep = 3) And (bYXStartEmpty Or bYXEndEmpty) Dim bX_2 As BooleanDim bY_2 As BooleanDim bXY_2 As BooleanDim bYX_2 As BooleanDim bX2 As BooleanDim bY2 As BooleanDim bXY2 As BooleanDim bYX2 As BooleanbX_2 = (nXStep = 2) And (bXStartEmpty And bXEndEmpty)bY_2 = (nYStep = 2) And (bYStartEmpty And bYEndEmpty)bXY_2 = (nXYStep = 2) And (bXYStartEmpty And bXYEndEmpty) bYX_2 = (nYXStep = 2) And (bYXStartEmpty And bYXEndEmpty) bX2 = (nXStep = 2) And (bXStartEmpty Or bXEndEmpty)bY2 = (nYStep = 2) And (bYStartEmpty Or bYEndEmpty)bXY2 = (nXYStep = 2) And (bXYStartEmpty Or bXYEndEmpty) bYX2 = (nYXStep = 2) And (bYXStartEmpty Or bYXEndEmpty) Dim bX_1 As BooleanDim bY_1 As BooleanDim bXY_1 As BooleanDim bYX_1 As BooleanbX_1 = (nXStep = 1) And (bXStartEmpty And bXEndEmpty)bY_1 = (nYStep = 1) And (bYStartEmpty And bYEndEmpty)bXY_1 = (nXYStep = 1) And (bXYStartEmpty And bXYEndEmpty) bYX_1 = (nYXStep = 1) And (bYXStartEmpty And bYXEndEmpty) Dim nXAdd As IntegerDim nYAdd As IntegerDim nXYAdd As IntegerDim nYXAdd As IntegernXAdd = 0nYAdd = 0nXYAdd = 0nYXAdd = 0If nXEndAdd >= nXStartAdd ThennXAdd = nXEndAddbXEndEmpty = bXEndEmpty1ElsenXAdd = nXStartAddbXStartEmpty = bXStartEmpty1End IfIf (nYEndAdd >= nYStartAdd) ThennYAdd = nYEndAddbYEndEmpty = bYEndEmpty1ElsenYAdd = nYStartAddbYStartEmpty = bYStartEmpty1End IfIf (nXYEndAdd >= nXYStartAdd) ThennXYAdd = nXYEndAddbXYEndEmpty = bXYEndEmpty1ElsenXYAdd = nXYStartAddbXYStartEmpty = bXYStartEmpty1End IfIf (nYXEndAdd >= nYXStartAdd) ThennYXAdd = nYXEndAddbYXEndEmpty = bYXEndEmpty1ElsenYXAdd = nYXStartAddbYXStartEmpty = bYXStartEmpty1End IfDim b1X_4 As BooleanDim b1Y_4 As BooleanDim b1XY_4 As BooleanDim b1YX_4 As BooleanDim b1X4 As BooleanDim b1Y4 As BooleanDim b1XY4 As BooleanDim b1YX4 As Booleanb1X_4 = (nXStep + nXAdd >= 4) And (bXStartEmpty And bXEndEmpty)b1Y_4 = (nYStep + nY Add >= 4) And (bYStartEmpty And bYEndEmpty)b1XY_4 = (nXYStep + nXYAdd >= 4) And (bXYStartEmpty And bXYEndEmpty) b1YX_4 = (nYXStep + nYXAdd >= 4) And (bYXStartEmpty And bYXEndEmpty) b1X4 = (nXStep + nXAdd >= 4) And (bXStartEmpty Or bXEndEmpty)b1Y4 = (nYStep + nY Add >= 4) And (bYStartEmpty Or bYEndEmpty)b1XY4 = (nXYStep + nXY Add >= 4) And (bXYStartEmpty Or bXYEndEmpty)b1YX4 = (nYXStep + nYXAdd >= 4) And (bYXStartEmpty Or bYXEndEmpty) Dim b1X_3 As BooleanDim b1Y_3 As BooleanDim b1XY_3 As BooleanDim b1YX_3 As Booleanb1X_3 = (nXStep + nXAdd >= 3) And (bXStartEmpty And bXEndEmpty)b1Y_3 = (nYStep + nY Add >= 3) And (bYStartEmpty And bYEndEmpty)b1XY_3 = (nXYStep + nXYAdd >= 3) And (bXYStartEmpty And bXYEndEmpty) b1YX_3 = (nYXStep + nYXAdd >= 3) And (bYXStartEmpty And bYXEndEmpty) m_nType = -1'////////If (nXStep >= 5) Or (nYStep >= 5) Or (nXYStep >= 5) Or (nYXStep >= 5) Then nGrade = 0m_nType = 0ElseIf (bX_4 Or bY_4 Or bXY_4 Or bYX_4) ThennGrade = 1m_nType = 1ElseIf ((bX4 And (bY4 Or bXY4 Or bYX4 Or b1Y4 Or b1XY4 Or b1YX4)) Or _(bY4 And (bX4 Or bXY4 Or bYX4 Or b1X4 Or b1XY4 Or b1YX4)) Or _(bXY4 And (bY4 Or bX4 Or bYX4 Or b1Y4 Or b1X4 Or b1YX4)) Or _(bYX4 And (bY4 Or bXY4 Or bX4 Or b1Y4 Or b1XY4 Or b1X4)) Or _(b1X4 And (bY4 Or bXY4 Or bYX4 Or b1Y4 Or b1XY4 Or b1YX4)) Or _(b1Y4 And (bX4 Or bXY4 Or bYX4 Or b1X4 Or b1XY4 Or b1YX4)) Or _(b1XY4 And (bY4 Or bX4 Or bYX4 Or b1Y4 Or b1X4 Or bYX4)) Or _(b1YX4 And (bY4 Or bXY4 Or bX4 Or b1Y4 Or b1XY4 Or b1X4))) Then nGrade = 2m_nType = 1ElseIf ((bX4 And (bY_3 Or bXY_3 Or bYX_3 Or b1Y_3 Or b1XY_3 Or b1YX_3)) Or _ (bY4 And (bX_3 Or bXY_3 Or bYX_3 Or b1X_3 Or b1XY_3 Or b1YX_3)) Or _(bXY4 And (bY_3 Or bX_3 Or bYX_3 Or b1Y_3 Or b1X_3 Or b1YX_3)) Or _(bYX4 And (bY_3 Or bXY_3 Or bX_3 Or b1Y_3 Or b1XY_3 Or b1X_3)) Or _(b1X4 And (bY_3 Or bXY_3 Or bYX_3 Or b1Y_3 Or b1XY_3 Or b1YX_3)) Or _(b1Y4 And (bX_3 Or bXY_3 Or bYX_3 Or b1X_3 Or b1XY_3 Or b1YX_3)) Or _(b1XY4 And (bY_3 Or bX_3 Or bYX_3 Or b1Y_3 Or b1X_3 Or b1YX_3)) Or _(b1YX4 And (bY_3 Or bXY_3 Or bX_3 Or b1Y_3 Or b1XY_3 Or b1X_3))) ThennGrade = 3m_nType = 1ElseIf ((bX_3 And (bY_3 Or bXY_3 Or bYX_3 Or b1Y_3 Or b1XY_3 Or b1YX_3)) Or _ (bY_3 And (bX_3 Or bXY_3 Or bYX_3 Or b1X_3 Or b1XY_3 Or b1YX_3)) Or _(bXY_3 And (bY_3 Or bX_3 Or bYX_3 Or b1Y_3 Or b1X_3 Or b1YX_3)) Or _(bYX_3 And (bY_3 Or bXY_3 Or bX_3 Or b1Y_3 Or b1XY_3 Or b1X_3)) Or _(b1X_3 And (bY_3 Or bXY_3 Or bYX_3 Or b1Y_3 Or b1XY_3 Or b1YX_3)) Or _(b1Y_3 And (bX_3 Or bXY_3 Or bYX_3 Or b1X_3 Or b1XY_3 Or b1YX_3)) Or _(b1XY_3 And (bY_3 Or bX_3 Or bYX_3 Or b1Y_3 Or b1X_3 Or b1YX_3)) Or _(b1YX_3 And (bY_3 Or bXY_3 Or bX_3 Or b1Y_3 Or b1XY_3 Or b1X_3))) ThennGrade = 4m_nType = 2ElseIf ((bXY4 And (bYX_2 Or bY_2 Or bX_2)) Or _(bYX4 And (bXY_2 Or bY_2 Or bX_2)) Or _(bX4 And (bXY_2 Or bYX_2 Or bY_2)) Or _(bY4 And (bXY_2 Or bYX_2 Or bX_2))) ThennGrade = 5m_nType = 1ElseIf ((bXY4 And (bYX3 Or bY3 Or bX3)) Or _(bYX4 And (bXY3 Or bY3 Or bX3)) Or _(bX4 And (bXY3 Or bYX3 Or bY3)) Or _(bY4 And (bXY3 Or bYX3 Or bX3))) Then nGrade = 6m_nType = 1ElseIf ((bXY4 And (bYX_1 Or bY_1 Or bX_1)) Or _(bYX4 And (bXY_1 Or bY_1 Or bX_1)) Or _(bX4 And (bXY_1 Or bYX_1 Or bY_1)) Or _(bY4 And (bXY_1 Or bYX_1 Or bX_1))) Then nGrade = 7m_nType = 2ElseIf ((bXY4 And (bYX2 Or bY2 Or bX2)) Or _(bYX4 And (bXY2 Or bY2 Or bX2)) Or _(bX4 And (bXY2 Or bYX2 Or bY2)) Or _(bY4 And (bXY2 Or bYX2 Or bX2))) Then nGrade = 8m_nType = 1ElseIf (bXY4 Or bYX4 Or bX4 Or bY4) ThennGrade = 9m_nType = 1ElseIf ((bXY_3 And (bYX_2 Or bY_2 Or bX_2)) Or _(bYX_3 And (bXY_2 Or bY_2 Or bX_2)) Or _(bX_3 And (bXY_2 Or bYX_2 Or bY_2)) Or _(bY_3 And (bXY_2 Or bYX_2 Or bX_2))) Then nGrade = 10m_nType = 2ElseIf ((bXY_3 And (bYX3 Or bY3 Or bX3)) Or _(bYX_3 And (bXY3 Or bY3 Or bX3)) Or _(bX_3 And (bXY3 Or bYX3 Or bY3)) Or _(bY_3 And (bXY3 Or bYX3 Or bX3))) Then nGrade = 11m_nType = 2ElseIf ((bXY_3 And (bYX_1 Or bY_1 Or bX_1)) Or _(bYX_3 And (bXY_1 Or bY_1 Or bX_1)) Or _(bX_3 And (bXY_1 Or bYX_1 Or bY_1)) Or _(bY_3 And (bXY_1 Or bYX_1 Or bX_1))) Then nGrade = 12m_nType = 2ElseIf ((bXY_3 And (bYX2 Or bY2 Or bX2)) Or _(bYX_3 And (bXY2 Or bY2 Or bX2)) Or _(bX_3 And (bXY2 Or bYX2 Or bY2)) Or _(bY_3 And (bXY2 Or bYX2 Or bX2))) ThennGrade = 13m_nType = 2ElseIf (bXY_3 Or bYX_3 Or bX_3 Or bY_3) ThennGrade = 14m_nType = 2ElseIf (bXY_2 Or bYX_2 Or bX_2 Or bY_2) ThennGrade = 16ElseIf (bXY3 Or bYX3 Or bX3 Or bY3) ThennGrade = 17ElseIf (bXY2 Or bYX2 Or bX2 Or bY2) ThennGrade = 18ElseIf (bXY_1 Or bYX_1 Or bX_1 Or bY_1) ThennGrade = 19ElsenGrade = 20End IfIf (nGrade > 15) ThenIf (((nX - 2) >= 0) And ((nY - 2) >= 0)) ThenIf ((m_Board(nX, nY - 1) = 0 Or m_Board(nX, nY - 1) = cValue) And _(m_Board(nX + 1, nY) = 0 Or m_Board(nX + 1, nY) = cValue) And _(m_Board(nX, nY + 1) = 0 Or m_Board(nX, nY + 1) = cValue) And _(m_Board(nX - 1, nY) = 0 Or m_Board(nX - 1, nY) = cValue) And _((nX + 2 < 15 And nY - 2 >= 0 And (m_Board(nX + 2, nY - 2) = 0 Or m_Board(nX + 2, nY - 2) = cValue) And _m_Board(nX, nY - 2) = cValue And m_Board(nX + 1, nY - 1) = cValue And m_Board(nX + 2, nY) = cValue) Or _(nX + 2 < 15 And nY + 2 < 15 And (m_Board(nX + 2, nY + 2) = 0 Or m_Board(nX + 2, nY + 2) = cValue) And _m_Board(nX, nY + 2) = cV alue And m_Board(nX + 1, nY + 1) = cValue And m_Board(nX + 2, nY) = cValue) Or _(nX - 2 >= 0 And nY + 2 < 15 And (m_Board(nX - 2, nY + 2) = 0 Or m_Board(nX - 2, nY + 2) = cValue) And _m_Board(nX, nY + 2) = cValue And m_Board(nX - 1, nY + 1) = cV alue And m_Board(nX - 2, nY) = cValue) Or _(nX - 2 >= 0 And nY - 2 >= 0 And (m_Board(nX - 2, nY - 2) = 0 Or m_Board(nX - 2, nY - 2) = cValue) And _m_Board(nX, nY - 2) = cV alue And m_Board(nX - 1, nY - 1) = cValue And m_Board(nX - 2, nY) = cValue))) ThennGrade = 15End IfEnd IfEnd IfiWho = cValueJudge = nGradeEnd FunctionPrivate Sub ring()Cells(17, 3) = Cells(17, 3) + 1If m_nType = 0 ThenIf iWho = 2 ThenIf Cells(17, 3) < 16 ThenMsgBox "Congratulation!! You win by only total " & Cells(17, 3) & " stones, you are cool!"ElseMsgBox "Congratulation!! You win by total " & Cells(17, 3) & " stones, still OK!"End IfElseIf Cells(17, 3) < 16 ThenMsgBox "Sorry!! Computer win by only total " & Cells(17, 3) & " stones, you are bad player!"ElseMsgBox "Sorry!! Computer win by total " & Cells(17, 3) & " stones, you need more practices!"End IfEnd IfCells(17, 1) = ""Cells(17, 3) = 0Range(Cells(17 + 1, 1), Cells(17 + 15, 15)).Value = 0EndEnd IfEnd Sub。
毕业设计基于labview设计的五子棋游戏
常州信息职业技术学院学生毕业设计(论文)报告系别:电子与电气工程学院专业:电气自动化班号:电气092 学生姓名:刘利学生学号: 0905093210 设计(论文)题目:基于LabVIEW的五子棋游戏开发指导教师:陈琳设计地点:常州信息职业技术学院起迄日期: 2011.09.06—2011.11.20毕业设计(论文)任务书专业电气自动化班级电气092 姓名刘利一、课题名称:基于LabVIEW的五子棋游戏开发二、主要研究内容:1. 游戏的行棋规则:黑先、白后,任一方先在棋盘上形成纵向、竖向、斜向的连续相同颜色五个棋子的一方为胜。
2. LabVIEW程序结构的合理应用。
3. 游戏界面的设计及美化。
三、工作内容和要求:1. 游戏开始,设计子程序对棋盘进行初始化,初始化子程序用循环结构实现,数组显示当期棋盘状态。
2. 初始化结束后,选择游戏模式,选择人机对弈模式或双人对弈模式。
应用条件结构进行选择。
3. 选择游戏模式后,进行游戏,双方下棋子的位置要用数组显示当前棋盘状态。
如果选择了双人对弈,就只要记住双方下子位置。
如果选择了人机对弈,就要搜索当前局面的最佳应对着法,由电脑计算下子位置。
4.游戏中,任一方先在棋盘上形成纵向、竖向、斜向的连续相同颜色的五个棋子的一方为胜。
如果有一方获胜,此时跳出对话框,显示一方获胜,游戏结束。
按下确定,同时初始化棋盘,可以重新开始游戏。
四、主要参考文献:[1] 林飞. 中国艺术经典全书之五子棋[M], 吉林摄影出版社.2003.12[2] 彭建国、那威. 连珠五子棋入门[M], 金盾出版社.1997.6[3] 阮奇桢. 我和LabVIEW——一个NI工程师的十年编程经验[M], 北京航空航天大学出版社,2009.09 [4] 王长飞、蔡强、李海生. 智能五子棋算法的设计实现[M], 系统仿真学报第21卷第四期2009. 02[5] 王永庆. 人工智能原理与方法[M], 西安交通大学出版社.1998.8[6] 崔良沂、赵永昌. 人工智能[M], 第3 版清华大学出版社.2005.9[7] 贾功贤、刘成康等. 基于PC的虚拟仪器的发展趋势[J], 电子技术应用.1999[8] NI. Measurement and Automation Catalog[Z], 2003.9[9] LabVIEW 虚拟仪器程序设计及应用.人民邮电出版社.2008.12[10] LabVIEW8.20程序设计从入门到精通.清华大学出版.2007.7学生(签名)年月日指导教师(签名)年月日教研室主任(签名)年月日系主任(签名)年月日毕业设计(论文)开题报告基于LabVIEW的五子棋游戏目录摘要Abstract第1章前言 (1)第2章 LabVIEW简介 (2)2.1 LabVIEW的概念 (2)2.2 LabVIEW的特点 (2)第3章总体设计方案 (3)3.1五子棋游戏规则 (3)3.2游戏设计框图 (3)3.3游戏设计流程图 (4)3.3.1总体设计流程图 (4)3.3.2人机对弈模式下的游戏流程图 (4)3.3.3双人对弈模式下的游戏流程图 (5)第4章各模块程序设计 (7)4.1初始棋盘模块 (7)4.2多步计算模块 (7)4.2.1多步计算流程图 (7)4.2.2多步计算前面板及程序框图 (8)4.3决定下子方模块 (10)4.4判定胜负模块 (10)4.4.1判定胜负模块设计前面板及程序框图 (10)4.5胜负对话模块 (11)第5章主程序设计 (14)5.1游戏主程序设计 (14)5.1.1五子棋主程序的程序框图 (14)5.1.2五子棋的游戏界面 (15)5.2结果演示 (16)第6章结束语 (17)参考文献 (18)答谢辞 (19)摘要随着生活水平的不断提高,人们不再仅仅满足于物质生活,闲暇时光人们会选择自己喜欢的娱乐活动用做消遣。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
VB语言制作五子棋
a)需求分析:五子棋游戏是一个深受人们喜爱的游戏,通常是人机对弈,本程序设计为人与人对弈,一方执黑棋,一方执白棋,轮流走棋,每方都试图在游戏结束前让自己的棋子五子相连。
按键盘上的方向键可以移动光标,回车键可以摆放棋子。
这个程序是对编程基本功的一个训练,将分支、循环、数组、函数综合应用,而不仅限于编制独立的小程序,通过游戏过程增加编程的兴趣,提高编程水平。
编制该程序我对以下的知识点进行的巩固和掌握:
1.数组元素为结构体的数组应用。
2.全局变量应用。
3.按键的处理。
4.数组元素对应关系。
5.图形方式等等。
虽然该程序是一个普通而又简单的程序,但是对于语言初级阶段的我来说,是一个很好的锻炼甚至可以说是一个很好的提高。
b)概要设计:棋盘设计为19×19格,初始状态光标在棋盘的中央,白棋先走,轮流落子,当一方连成五子或下满棋盘时,游戏结束(连成五子的一方获胜,下满棋盘为和棋)。
当游戏一方胜利后显示胜利信息,提示信息利用汉字点阵输出。
从程序表面看,这是一个二维平面图,所以数据用二维数组来表示,数组两个下标可以表示棋盘上的位置,数组元素的值代表棋格上的状态,共有三种情况,分别是0代表空格,1代表白棋,2代表黑棋。
这样程序的主要工作是接收棋手按键操作,棋手1用Up、Down、Left、Right 控制光标移动,回车键表示落子。
棋手2用W、S、A、D控制光标移动,空格键表示落子。
一旦接收到回车键或空格键,说明棋手落子,先判断是否是有效位置,也就是说已经有棋子的位置不能重叠落子。
落子成功后,马上判断以该位置为中心的八个方向:上、下、左、右、左上、左下、右上、右下是否有相同颜色的棋子连成五子,如果连成五子,则游戏结束,显示获胜信息。
c)详细设计:
1.变量定义
Dim MapBlack(1 To 19, 1 To 19, 0 To 4) As Single
Dim MapWhite(1 To 19, 1 To 19, 0 To 4) As Single
Dim NowOpt As Boolean
Dim CanOpt As Boolean
Dim BlackWin As Integer
Dim BlackLost As Integer
Dim WhiteWin As Integer
Dim WhiteLost As Integer
Const Grid = 240
2.Main()主函数
按照语法规则,首先定义使用到的常数、全局变量、函数原型说明。
棋盘状态用数组yes[20][20],初值为0,表示空格。
函数的实体比较简单,调用图形驱动、画棋盘、人人对弈等自定义函数,一旦游戏结束后,关闭图形驱动,程序结束。
3.drawqp()画棋盘函数
背景颜色设为黑色,从坐标(100,100)开始每隔20个单位用绿色画一条水平直线,一条垂直线,构成棋盘,用循环语句实现。
画线函数line()的参数为线的起点和终点坐标,注意要计算正确。
函数
setfillstyle()可以设置填充模式和填充颜色,fillellipse(int x,int xradius,int yradius)以x和y为圆心,xradius和yradius为水平和垂直轴画一填充椭圆,当xradius和yradius相等时,则画出的是圆,用它表示棋子。
4.OpinionWin ()判断是否胜出函数
本函数根据每次落子的位置,分别向上、下、左、右、左上、左下、右上、右下八个方向判断是否有相同颜色的棋子连成五子,如果成立,游戏就结束,并显示提示信息(如:红方获胜),否则继续落子。
5.图形功能函数
(1)PicMap.ForeColor ():定义背景颜色。
(2)PicMap_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single):
用于初始化图形系统,并装入相应的图形驱动器。
该函数把由driver 所指向的图形驱动器代码装入内存。
如果图形驱动器未装入内存,图形函数将不能操作。
图形函数所用的屏显模式由mode所指向的整数值确定。
最后,图形驱动器的路径是由path所指向的字符串给出。
如果没有指定路径,就在当前工作路径上寻找。
(3)rectangle(int left,int top,int right,int bottom):函数rectangle()用当前画出由坐标left,top及right,bottom所定义的矩形。
(4)PicMap.Line ():把当前画线颜色设置为color所指定的颜色。
每个图形显示卡的有效颜色由代码提供。
d)调试分析:
课程设计总结
最大的难题就是关于AI方面的代码,这一段代码我是借用别人的。
通过编制该程序我对以下的知识点进行的巩固和掌握:
1.数组元素为结构体的数组应用。
2.全局变量应用。
3.按键的处理。
4.图形方式等等。
虽然该程序是一个普通而又简单的程序,但是对于VB语言初级阶段的我来说,是一个很好的锻炼甚至可以说是一个很好的提高。
由于个人能力的限制该程序还有许多的缺陷,若要进一步的完善还需要对VB语言更深一步的学习。