ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 9687|回复: 38

[讨论]三子棋之人工智能的实现(已完工)

[复制链接]

TA的精华主题

TA的得分主题

发表于 2007-11-15 16:47 | 显示全部楼层 |阅读模式

当我还是小屁孩(穿开档裤)的时候就会下棋了,就今天我要介绍给大家的三子棋.棋盘为3*3的格子,最先连成斜三点一线的一方胜.由于走法简单现拿出来给大家分享一下,同时也希望各位能动动脑子,想想该如何实现人工智能?


WmBEk5AW.rar (56.35 KB, 下载次数: 275)


[此贴子已经被作者于2007-11-28 16:14:40编辑过]
[ 本帖最后由 彭希仁 于 2009-7-12 12:48 编辑 ]

TA的精华主题

TA的得分主题

 楼主| 发表于 2007-11-15 16:49 | 显示全部楼层

基本思路是这样的.

第一步:选让棋子动起来,即每个棋子都有一个对应的坐标(棋盘),

第二步:评分机制,分为三种1(胜),0(平),-1(输)

第三步,关键算法

         即:穷举第一种走法之后,将用调用递归轮到对方走棋,以对方最好的一步棋,取反做为本次走法的得分.(即对方走胜了,也就是本方输了)

            穷举第二种走法之后,将用调用递归轮到对方走棋,以对方最好的一步棋,取反做为本次走法的得分.

            穷举N种走法之后,找出这些最高评分的一次走法.

如此反复的递归,就可实现人工智能.递归的深度越深智能越高,当然这也要付出相就的代价,即时间越长,所以得选择合适的递归深度.

[此贴子已经被作者于2007-11-16 8:08:31编辑过]

TA的精华主题

TA的得分主题

发表于 2007-11-15 22:24 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2007-11-15 23:16 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2007-11-15 23:36 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2007-11-16 00:49 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
对于人工智能我一直很有兴趣,但却一直不得其门而入。

TA的精华主题

TA的得分主题

发表于 2007-11-16 09:22 | 显示全部楼层
有趣,现在开始研究,希望能出成品

TA的精华主题

TA的得分主题

 楼主| 发表于 2007-11-16 09:28 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
QUOTE:
晚上有空点写了一点点,明天继续
QUOTE:
 第一步,初始化工作,可选择并移动蓝色棋子
QUOTE:
Public arr, a
Private Sub Image8_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    For i = 1 To 3
        For ii = 1 To 3
            If X > arr(i, ii, 2) And X < arr(i, ii, 2) + 100 And Y > arr(i, ii, 3) And Y < arr(i, ii, 3) + 100 Then
                If arr(i, ii, 4) = -1 Then
                    If a <> 0 Then
                        Me.Controls(a).SpecialEffect = fmSpecialEffectFlat   '重选棋子
                        a = arr(i, ii, 1)
                        Me.Controls(a).SpecialEffect = fmSpecialEffectBump
                    Else
                        a = arr(i, ii, 1)
                        Me.Controls(a).SpecialEffect = fmSpecialEffectBump

                    End If
                End If
                If arr(i, ii, 4) = 0 And a <> 0 Then
                       For iii = 1 To 3
                            For iiii = 1 To 3
                                If arr(iii, iiii, 1) = a Then
                                    X = iii
                                    Y = iiii
                                End If
                            Next iiii
                        Next iii
                        If X = i And Y = ii Then Exit Sub
                        If Abs(X - i) > 1 Or Abs(Y - ii) > 1 Then Exit Sub
                        Call 移动棋子(X, Y, i, ii)
                End If
            End If
        Next ii
    Next i
End Sub

Private Sub UserForm_Activate()
    Call 初始化_Click
End Sub

Private Sub 初始化_Click()
    Dim i As Long
    Dim ii As Long
    行宽 = 122
    列宽 = 122
    ReDim arr(0 To 4, 0 To 4, 1 To 4)    '分别记录棋子图片控件名称,位置
    For i = 1 To 3                '位置
        For ii = 1 To 3
            arr(i, ii, 2) = 20 + (ii - 1) * 行宽
            arr(i, ii, 3) = 25 + (i - 1) * 列宽
        Next ii
    Next i
    '将棋子图片名称载入数组
    arr(1, 1, 1) = "Image2"
    arr(1, 2, 1) = "Image3"
    arr(1, 3, 1) = "Image4"
    arr(3, 1, 1) = "Image5"
    arr(3, 2, 1) = "Image6"
    arr(3, 3, 1) = "Image7"
    '1代表蓝色棋子,-1代表黑化棋子
    arr(1, 1, 4) = 1
    arr(1, 2, 4) = 1
    arr(1, 3, 4) = 1
    arr(3, 1, 4) = -1
    arr(3, 2, 4) = -1
    arr(3, 3, 4) = -1
    For i = 1 To 3
        For ii = 1 To 3
            If arr(i, ii, 1) <> "" Then
                Me.Controls(arr(i, ii, 1)).Left = arr(i, ii, 2)
                Me.Controls(arr(i, ii, 1)).Top = arr(i, ii, 3)
                Me.Controls(arr(i, ii, 1)).SpecialEffect = fmSpecialEffectFlat
            End If
        Next ii
    Next i
End Sub
Sub 移动棋子(X, Y, xx, yy)
    a = arr(X, Y, 1)
    arr(xx, yy, 1) = arr(X, Y, 1)
    arr(xx, yy, 4) = arr(X, Y, 4)
    Me.Controls(a).Left = arr(xx, yy, 2)
    Me.Controls(a).Top = arr(xx, yy, 3)
    arr(X, Y, 1) = 0
    arr(X, Y, 4) = 0
    Me.Controls(a).SpecialEffect = fmSpecialEffectFlat
    a = 0
End Sub

1vSrjcI6.rar (50.4 KB, 下载次数: 51)
[此贴子已经被作者于2007-11-27 23:22:59编辑过]

TA的精华主题

TA的得分主题

发表于 2007-11-16 09:43 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册

彭兄的逻辑思维能力令我叹服!相信你一定能成功!

论坛上高手如云!但彭兄绝对是高手中的高手!

关注此帖!

TA的精华主题

TA的得分主题

 楼主| 发表于 2007-11-16 09:58 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册

 

'已完工,以下就是电脑思考部分的代码

Public arr, a
Sub 电脑走棋()
    Dim arr1
    ReDim arr1(1 To 3, 1 To 3)
    Dim z
    For i = 1 To 3
        For j = 1 To 3
            arr1(i, j) = arr(i, j, 4)
        Next j
    Next i
    If (arr1(1, 1) = -1 And arr1(2, 2) = -1 And arr1(3, 3) = -1) Or (arr1(1, 3) = -1 And arr1(2, 2) = -1 And arr1(3, 1) = -1) Then
        MsgBox "认输": Exit Sub
    End If
    Call 人工智能(arr1, z, 1)

    For i = 1 To 3
        For j = 1 To 3
            If arr1(i, j) = 0 And arr(i, j, 4) = 1 Then
                X = i
                Y = j
                UserForm1.Controls(arr(i, j, 1)).SpecialEffect = fmSpecialEffectFlat
            End If
            If arr1(i, j) = 1 And arr(i, j, 4) = 0 Then
                xx = i
                yy = j
            End If
        Next j
    Next i
    If (X = i And Y = ii) Or z < 0 Then MsgBox "认输": Exit Sub
    If X = 0 Or Y = 0 Or xx = 0 Or yy = 0 Then Exit Sub
    a = arr(X, Y, 1)
    arr(xx, yy, 1) = arr(X, Y, 1)
    arr(xx, yy, 4) = arr(X, Y, 4)
    UserForm1.Controls(a).Left = arr(xx, yy, 2)
    UserForm1.Controls(a).Top = arr(xx, yy, 3)
    arr(X, Y, 1) = 0
    arr(X, Y, 4) = 0
    UserForm1.Controls(a).SpecialEffect = fmSpecialEffectFlat
    a = 0
    If (arr1(1, 1) = 1 And arr1(2, 2) = 1 And arr1(3, 3) = 1) Or (arr1(1, 3) = 1 And arr1(2, 2) = 1 And arr1(3, 1) = 1) Then
        MsgBox "电脑胜"
    End If
End Sub

Sub 人工智能(arr1, z, s)
    If s = 5 Then
        z = 0
        Exit Sub
    End If
    arr3 = arr1
    Dim aar2
    Dim zz
    z = -1

    For i = 1 To 3
        For j = 1 To 3
            For ii = 1 To 3
                For jj = 1 To 3
                    If arr1(i, j) = 1 And arr1(ii, jj) = 0 Then
                        If Abs(ii - i) < 2 And Abs(jj - j) < 2 Then
                            If Abs(ii - i) * Abs(jj - j) = 0 Or (i = 2 And j = 2) Or (ii = 2 And jj = 2) Then
                                arr2 = arr1
                                arr2(ii, jj) = arr2(i, j)
                                arr2(i, j) = 0
                                '判断是否三子连线
                                If (arr2(1, 1) = 1 And arr2(2, 2) = 1 And arr2(3, 3) = 1) Or (arr2(1, 3) = 1 And arr2(2, 2) = 1 And arr2(3, 1) = 1) Then
                                    arr1 = arr2
                                    z = 1              '胜了,记录走法并返回
                                    Exit Sub
                                End If

                                For iii = 1 To 3                 '交换角色,换对方走棋
                                    For jjj = 1 To 3
                                        arr2(iii, jjj) = arr2(iii, jjj) * -1
                                    Next jjj
                                Next iii
                                Call 人工智能(arr2, zz, s + 1)
                            If zz * -1 > z Or (zz * -1 = z And Rnd > 0.3) 'ZZ返回的是对方最好的走法的得分,*-1之后就可以做得这次走法的得分,即对方胜了,就是自已输了.
                                    z = zz * -1                 '将最高得分记录下来
                                    arr3 = arr1                 '将最高得分的走法记录下来
                                    arr3(ii, jj) = arr3(i, j)
                                    arr3(i, j) = 0
                                End If
                            End If
                        End If
                    End If
                Next jj
            Next ii
        Next j
    Next i
    arr1 = arr3
End Sub


[此贴子已经被作者于2007-11-28 16:15:55编辑过]

pFU3W4lq.rar

54.7 KB, 下载次数: 42

6DWQgUYp.rar

55.89 KB, 下载次数: 38

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-22 07:47 , Processed in 0.050390 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表