查看完整版本: 井字遊戲AI
頁: [1]

zxc693491 發表於 2016-12-1 03:11 PM

井字遊戲AI

本帖最後由 zxc693491 於 2016-12-1 03:42 PM 編輯

自己做的井字遊戲AI  請大家幫我測試看看,希望能找到錯誤,原則上不可能贏他就是了。
另外這是雙人版(做得很難看消磨時間用:
以下提供參考,我用按鈕物件寫的"VB2010"
Public Class 井字遊戲
    Dim time As Integer
    Dim att As Integer           '狀況標記
    Dim cheak As Boolean = False '是否已判斷勝負
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MsgBox("歡迎遊玩井字遊戲,退出請按鍵盤Alt+F4鍵", vbOKOnly, "")
        N()
        S()
        Me.Focus()
        FormBorderStyle = Windows.Forms.FormBorderStyle.None
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button1.Text = "○"
        Button1.Enabled = False
        time = time + 1
        If att = 22 Then
            time = 3
        End If
        AI()
    End Sub '按鈕群
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Button2.Text = "○"
        Button2.Enabled = False
        time = time + 1
        If att = 22 Then
            time = 3
        End If
        AI()
    End Sub '按鈕群
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Button3.Text = "○"
        Button3.Enabled = False
        time = time + 1
        If att = 22 Then
            time = 3
        End If
        AI()
    End Sub '按鈕群
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Button4.Text = "○"
        Button4.Enabled = False
        time = time + 1
        If att = 22 Then
            time = 3
        End If
        AI()
    End Sub '按鈕群
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Button5.Text = "○"
        Button5.Enabled = False
        time = time + 1
        If att = 22 Then
            time = 3
        End If
        AI()
    End Sub '按鈕群
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Button6.Text = "○"
        Button6.Enabled = False
        time = time + 1
        If att = 22 Then
            time = 3
        End If
        AI()
    End Sub '按鈕群
    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Button7.Text = "○"
        Button7.Enabled = False
        time = time + 1
        If att = 22 Then
            time = 3
        End If
        AI()
    End Sub '按鈕群
    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Button8.Text = "○"
        Button8.Enabled = False
        time = time + 1
        If att = 22 Then
            time = 3
        End If
        AI()
    End Sub '按鈕群
    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        Button9.Text = "○"
        Button9.Enabled = False
        time = time + 1
        If att = 22 Then
            time = 3
        End If
        AI()
    End Sub '按鈕群
    '======================================================================================================================
    Sub S()
        If MsgBox("是否先下?", MsgBoxStyle.YesNo, "順序") = MsgBoxResult.No Then
            AIstep1()
        Else
            N()
        End If
        cheak = False
    End Sub        '開始
    Sub N() '復原
        Button1.Enabled = True
        Button2.Enabled = True
        Button3.Enabled = True
        Button4.Enabled = True
        Button5.Enabled = True
        Button6.Enabled = True
        Button7.Enabled = True
        Button8.Enabled = True
        Button9.Enabled = True
        Button1.Text = ""
        Button2.Text = ""
        Button3.Text = ""
        Button4.Text = ""
        Button5.Text = ""
        Button6.Text = ""
        Button7.Text = ""
        Button8.Text = ""
        Button9.Text = ""
        att = 1
        time = 1
    End Sub        '初始化
    Sub AI()
        WinCheak()
        If cheak = True Then
            cheak = False
            Exit Sub
        End If
        If time = 2 Then
            AIstep2()
            Exit Sub
        End If
        If time = 3 Then
            AIstep3()
            Exit Sub
        End If
        If time = 4 Then
            AIstep4()
            Exit Sub
        End If
        If time = 5 Then
            AIstep5()
            Exit Sub
        End If
        If time = 6 Then
            AIstep6()
            Exit Sub
        End If
        If time = 7 Then
            AIstep7()
            Exit Sub
        End If
        If time = 8 Then
            AIstep8()
            Exit Sub
        End If
        If time = 9 Then
            AIstep9()
            Exit Sub
        End If
    End Sub       '步數判斷
    Sub AIstep1() '先攻搶角
        Button3.Text = "X"
        Button3.Enabled = False
        time = 2
        att = 22
    End Sub
    Sub AIstep2()
        If Button1.Text = "○" Then
            Button5.Text = "X"
            Button5.Enabled = False
            att = 1
        ElseIf Button2.Text = "○" Then
            Button5.Text = "X"
            Button5.Enabled = False
            att = 0
        ElseIf Button3.Text = "○" Then
            Button5.Text = "X"
            Button5.Enabled = False
            att = 1
        ElseIf Button4.Text = "○" Then
            Button5.Text = "X"
            Button5.Enabled = False
            att = 0
        ElseIf Button5.Text = "○" Then
            Button3.Text = "X"
            Button3.Enabled = False
            att = 0
        ElseIf Button6.Text = "○" Then
            Button5.Text = "X"
            Button5.Enabled = False
            att = 43
        ElseIf Button7.Text = "○" Then
            Button5.Text = "X"
            Button5.Enabled = False
            att = 1
        ElseIf Button8.Text = "○" Then
            Button5.Text = "X"
            Button5.Enabled = False
            att = 43
        ElseIf Button9.Text = "○" Then
            Button5.Text = "X"
            Button5.Enabled = False
            att = 1
        End If
        time = 3
    End Sub
    Sub AIstep3() '開二路
        If Button1.Text = "○" Then
            Button9.Text = "X"
            Button9.Enabled = False
            att = 1
        End If
        If Button2.Text = "○" Then
            Button9.Text = "X"
            Button9.Enabled = False
            att = 2
        End If
        If Button4.Text = "○" Then
            Button5.Text = "X"
            Button5.Enabled = False
            att = 4
        End If
        If Button5.Text = "○" Then
            Button7.Text = "X"
            Button7.Enabled = False
            att = 5
        End If
        If Button6.Text = "○" Then
            Button1.Text = "X"
            Button1.Enabled = False
            att = 6
        End If
        If Button7.Text = "○" Then
            Button9.Text = "X"
            Button9.Enabled = False
            att = 7
        End If
        If Button8.Text = "○" Then
            Button5.Text = "X"
            Button5.Enabled = False
            att = 8
        End If
        If Button9.Text = "○" Then
            Button1.Text = "X"
            Button1.Enabled = False
            att = 9
        End If
        time = 4
    End Sub
    Sub AIstep4()
        If att = 0 Then
            If (Button2.Enabled = False And Button8.Enabled = False) Or (Button4.Enabled = False And Button6.Enabled = False) Then
                Button1.Text = "X"
                Button1.Enabled = False
                time = 5
                Exit Sub
            End If
            AI2()
        ElseIf att = 1 Then
            If (Button1.Enabled = False And Button9.Enabled = False) Or (Button3.Enabled = False And Button7.Enabled = False) Then
                Button2.Text = "X"
                Button2.Enabled = False
                time = 5
                Exit Sub
            Else
                AI2()
            End If
        ElseIf att = 43 Then
            If Button4.Text = "○" And Button8.Text = "○" Then
                Button7.Text = "X"
                Button7.Enabled = False
            Else
                Button3.Text = "X"
                Button3.Enabled = False
            End If
        End If
        time = 5
    End Sub
    Sub AIstep5()
        If att = 1 Then
            If Button6.Text = "○" Then
                Button7.Text = "X"
                Button7.Enabled = False
            Else
                Button6.Text = "X"
                Button6.Enabled = False
            End If
        ElseIf att = 2 Then
            If Button6.Text = "○" Then
                Button7.Text = "X"
                Button7.Enabled = False
            Else
                Button6.Text = "X"
                Button6.Enabled = False
            End If
        ElseIf att = 4 Then
            If Button7.Text = "○" Then
                Button1.Text = "X"
                Button1.Enabled = False
            Else
                Button7.Text = "X"
                Button7.Enabled = False
            End If
        ElseIf att = 5 Then
            If Button1.Text = "○" Then
                Button9.Text = "X"
                Button9.Enabled = False
            ElseIf Button9.Text = "○" Then
                Button1.Text = "X"
                Button1.Enabled = False
            Else
                If Button2.Text = "○" Then
                    Button8.Text = "X"
                    Button8.Enabled = False
                    att = 9
                ElseIf Button4.Text = "○" Then
                    Button6.Text = "X"
                    Button6.Enabled = False
                    att = 9
                ElseIf Button6.Text = "○" Then
                    Button4.Text = "X"
                    Button4.Enabled = False
                    att = 1
                ElseIf Button8.Text = "○" Then
                    Button2.Text = "X"
                    Button2.Enabled = False
                    att = 1
                End If
            End If
        ElseIf att = 6 Then
            If Button2.Enabled = False Then
                Button7.Text = "X"
                Button7.Enabled = False
            Else
                Button2.Text = "X"
                Button2.Enabled = False
            End If
        ElseIf att = 7 Then
            If Button6.Enabled = False Then
                Button1.Text = "X"
                Button1.Enabled = False
            Else
                Button6.Text = "X"
                Button6.Enabled = False
            End If
        ElseIf att = 8 Then
            If Button7.Enabled = False Then
                Button9.Text = "X"
                Button9.Enabled = False
            Else
                Button7.Text = "X"
                Button7.Enabled = False
            End If
        ElseIf att = 9 Then
            If Button2.Enabled = False Then
                Button7.Text = "X"
                Button7.Enabled = False
            Else
                Button2.Text = "X"
                Button2.Enabled = False
            End If
        End If
        time = 6
        WinCheak()
        If cheak = True Then
            N()
            S()
        End If
    End Sub
    Sub AIstep6()
        AI3()
        If cheak = True Then
            N()
            S()
            Exit Sub
        End If
        AI5()
        time = 7
        If att = 99 Then
            Exit Sub
        End If
        AI2()
    End Sub
    Sub AIstep7()
        AI3()
        If cheak = True Then
            N()
            S()
            Exit Sub
        End If
        AI2()
        time = 8
        WinCheak()
        If cheak = True Then
            N()
            S()
        End If
    End Sub
    Sub AIstep8()
        att = 1
        AI3()
        If cheak = True Then
            N()
            S()
            Exit Sub
        End If
        AI2()
        If att = 96 Then
            time = 9
            WinCheak()
            If cheak = True Then
                N()
                S()
            End If
            Exit Sub
        End If
        If Button2.Enabled = True Then
            Button2.Text = "X"
            Button2.Enabled = False
            att = 97
        ElseIf Button4.Enabled = True Then
            Button4.Text = "X"
            Button4.Enabled = False
            att = 97
        ElseIf Button6.Enabled = True Then
            Button6.Text = "X"
            Button6.Enabled = False
            att = 97
        ElseIf Button8.Enabled = True Then
            Button8.Text = "X"
            Button8.Enabled = False
            att = 97
        End If
        If att = 97 Then
            time = 9
            WinCheak()
            If cheak = True Then
                N()
                S()
            End If
            Exit Sub
        End If
        WinCheak()
        If cheak = True Then
            N()
            S()
        Else
            time = 9
        End If
    End Sub
    Sub AIstep9()
        AI3()
        If cheak = True Then
            N()
            S()
            Exit Sub
        End If
        AI2()
        If att = 96 Then
            time = 10
            WinCheak()
            If cheak = True Then
                N()
                S()
            End If
            Exit Sub
        End If
        If (Button2.Text = "X" Or Button4.Text = "X") And Button1.Enabled = True Then
            Button1.Text = "X"
            Button1.Enabled = False
        ElseIf (Button2.Text = "X" Or Button4.Text = "X") And Button9.Enabled = True Then
            Button9.Text = "X"
            Button9.Enabled = False
        ElseIf Button1.Enabled = False And Button9.Enabled = False Then
            If Button2.Enabled = True Then
                Button2.Text = "X"
                Button2.Enabled = False
            ElseIf Button4.Enabled = True Then
                Button4.Text = "X"
                Button4.Enabled = False
            ElseIf Button6.Enabled = True Then
                Button6.Text = "X"
                Button6.Enabled = False
            ElseIf Button8.Enabled = True Then
                Button8.Text = "X"
                Button8.Enabled = False
            ElseIf Button7.Enabled = True Then
                Button7.Text = "X"
                Button7.Enabled = False
            End If
        Else
            If Button1.Enabled = True Then
                Button1.Text = "X"
                Button1.Enabled = False
            Else
                Button9.Text = "X"
                Button9.Enabled = True
            End If
        End If
        WinCheak()
        If cheak = True Then
            N()
            S()
        Else
            MsgBox("平手囉", MsgBoxStyle.OkOnly, "勝負判斷")
            N()
            S()
        End If
    End Sub
    Sub WinCheak()
        If Button1.Text = Button2.Text And Button1.Text = Button3.Text And Button1.Text <> "" Then
            MsgBox("你輸了", MsgBoxStyle.OkOnly, "勝負判斷")
            cheak = True
        ElseIf Button4.Text = Button5.Text And Button4.Text = Button6.Text And Button4.Text <> "" Then
            MsgBox("你輸了", MsgBoxStyle.OkOnly, "勝負判斷")
            cheak = True
        ElseIf Button7.Text = Button8.Text And Button7.Text = Button9.Text And Button7.Text <> "" Then
            MsgBox("你輸了", MsgBoxStyle.OkOnly, "勝負判斷")
            cheak = True
        ElseIf Button1.Text = Button4.Text And Button1.Text = Button7.Text And Button1.Text <> "" Then
            MsgBox("你輸了", MsgBoxStyle.OkOnly, "勝負判斷")
            cheak = True
        ElseIf Button2.Text = Button5.Text And Button2.Text = Button8.Text And Button2.Text <> "" Then
            MsgBox("你輸了", MsgBoxStyle.OkOnly, "勝負判斷")
            cheak = True
        ElseIf Button3.Text = Button6.Text And Button3.Text = Button9.Text And Button3.Text <> "" Then
            MsgBox("你輸了", MsgBoxStyle.OkOnly, "勝負判斷")
            cheak = True
        ElseIf Button1.Text = Button5.Text And Button1.Text = Button9.Text And Button1.Text <> "" Then
            MsgBox("你輸了", MsgBoxStyle.OkOnly, "勝負判斷")
            cheak = True
        ElseIf Button3.Text = Button5.Text And Button3.Text = Button7.Text And Button3.Text <> "" Then
            MsgBox("你輸了", MsgBoxStyle.OkOnly, "勝負判斷")
            cheak = True
        ElseIf time = 10 Then
            MsgBox("平手囉", MsgBoxStyle.OkOnly, "勝負判斷")
            cheak = True
            N()
            S()
        End If
    End Sub '勝負判斷
    '準則:能贏先下,其次阻止連線,以上沒有問題時尋找最有利位置下。
    Sub AI2()
        If Button1.Text = "○" And Button2.Text = "○" And Button3.Text = "" Then
            Button3.Text = "X"
            Button3.Enabled = False
        ElseIf Button1.Text = "○" And Button3.Text = "○" And Button2.Text = "" Then
            Button2.Text = "X"
            Button2.Enabled = False
        ElseIf Button2.Text = "○" And Button3.Text = "○" And Button1.Text = "" Then
            Button1.Text = "X"
            Button1.Enabled = False
        ElseIf Button4.Text = "○" And Button5.Text = "○" And Button6.Text = "" Then
            Button6.Text = "X"
            Button6.Enabled = False
        ElseIf Button4.Text = "○" And Button6.Text = "○" And Button5.Text = "" Then
            Button5.Text = "X"
            Button5.Enabled = False
        ElseIf Button5.Text = "○" And Button6.Text = "○" And Button4.Text = "" Then
            Button4.Text = "X"
            Button4.Enabled = False
        ElseIf Button7.Text = "○" And Button8.Text = "○" And Button9.Text = "" Then
            Button9.Text = "X"
            Button9.Enabled = False
        ElseIf Button7.Text = "○" And Button9.Text = "○" And Button8.Text = "" Then
            Button8.Text = "X"
            Button8.Enabled = False
        ElseIf Button8.Text = "○" And Button9.Text = "○" And Button7.Text = "" Then
            Button7.Text = "X"
            Button7.Enabled = False
        ElseIf Button1.Text = "○" And Button4.Text = "○" And Button7.Text = "" Then
            Button7.Text = "X"
            Button7.Enabled = False
        ElseIf Button1.Text = "○" And Button7.Text = "○" And Button4.Text = "" Then
            Button4.Text = "X"
            Button4.Enabled = False
        ElseIf Button4.Text = "○" And Button7.Text = "○" And Button1.Text = "" Then
            Button1.Text = "X"
            Button1.Enabled = False
        ElseIf Button2.Text = "○" And Button5.Text = "○" And Button8.Text = "" Then
            Button8.Text = "X"
            Button8.Enabled = False
        ElseIf Button2.Text = "○" And Button8.Text = "○" And Button5.Text = "" Then
            Button5.Text = "X"
            Button5.Enabled = False
        ElseIf Button5.Text = "○" And Button8.Text = "○" And Button2.Text = "" Then
            Button2.Text = "X"
            Button2.Enabled = False
        ElseIf Button3.Text = "○" And Button6.Text = "○" And Button9.Text = "" Then
            Button9.Text = "X"
            Button9.Enabled = False
        ElseIf Button3.Text = "○" And Button9.Text = "○" And Button6.Text = "" Then
            Button6.Text = "X"
            Button6.Enabled = False
        ElseIf Button6.Text = "○" And Button9.Text = "○" And Button3.Text = "" Then
            Button3.Text = "X"
            Button3.Enabled = False
        ElseIf Button1.Text = "○" And Button5.Text = "○" And Button9.Text = "" Then
            Button9.Text = "X"
            Button9.Enabled = False
        ElseIf Button1.Text = "○" And Button9.Text = "○" And Button5.Text = "" Then
            Button5.Text = "X"
            Button5.Enabled = False
        ElseIf Button5.Text = "○" And Button9.Text = "○" And Button1.Text = "" Then
            Button1.Text = "X"
            Button1.Enabled = False
        ElseIf Button3.Text = "○" And Button5.Text = "○" And Button7.Text = "" Then
            Button1.Text = "X"
            Button1.Enabled = False
        ElseIf Button3.Text = "○" And Button7.Text = "○" And Button5.Text = "" Then
            Button5.Text = "X"
            Button5.Enabled = False
        ElseIf Button5.Text = "○" And Button7.Text = "○" And Button3.Text = "" Then
            Button3.Text = "X"
            Button3.Enabled = False
        Else
            AI4() '找不到防禦目標
        End If
        att = 96 'step8.9特殊狀況
    End Sub      '防禦
    Sub AI3()
        If Button1.Text = "X" And Button2.Text = "X" And Button3.Text = "" Then
            Button3.Text = "X"
            Button3.Enabled = False
        ElseIf Button1.Text = "X" And Button3.Text = "X" And Button2.Text = "" Then
            Button2.Text = "X"
            Button2.Enabled = False
        ElseIf Button2.Text = "X" And Button3.Text = "X" And Button1.Text = "" Then
            Button1.Text = "X"
            Button1.Enabled = False
        ElseIf Button4.Text = "X" And Button5.Text = "X" And Button6.Text = "" Then
            Button6.Text = "X"
            Button6.Enabled = False
        ElseIf Button4.Text = "X" And Button6.Text = "X" And Button5.Text = "" Then
            Button5.Text = "X"
            Button5.Enabled = False
        ElseIf Button5.Text = "X" And Button6.Text = "X" And Button4.Text = "" Then
            Button4.Text = "X"
            Button4.Enabled = False
        ElseIf Button7.Text = "X" And Button8.Text = "X" And Button9.Text = "" Then
            Button9.Text = "X"
            Button9.Enabled = False
        ElseIf Button7.Text = "X" And Button9.Text = "X" And Button8.Text = "" Then
            Button8.Text = "X"
            Button8.Enabled = False
        ElseIf Button8.Text = "X" And Button9.Text = "X" And Button7.Text = "" Then
            Button7.Text = "X"
            Button7.Enabled = False
        ElseIf Button1.Text = "X" And Button4.Text = "X" And Button7.Text = "" Then
            Button7.Text = "X"
            Button7.Enabled = False
        ElseIf Button1.Text = "X" And Button7.Text = "X" And Button4.Text = "" Then
            Button4.Text = "X"
            Button4.Enabled = False
        ElseIf Button4.Text = "X" And Button7.Text = "X" And Button1.Text = "" Then
            Button1.Text = "X"
            Button1.Enabled = False
        ElseIf Button2.Text = "X" And Button5.Text = "X" And Button8.Text = "" Then
            Button8.Text = "X"
            Button8.Enabled = False
        ElseIf Button2.Text = "X" And Button8.Text = "X" And Button5.Text = "" Then
            Button5.Text = "X"
            Button5.Enabled = False
        ElseIf Button5.Text = "X" And Button8.Text = "X" And Button2.Text = "" Then
            Button2.Text = "X"
            Button2.Enabled = False
        ElseIf Button3.Text = "X" And Button6.Text = "X" And Button9.Text = "" Then
            Button9.Text = "X"
            Button9.Enabled = False
        ElseIf Button3.Text = "X" And Button9.Text = "X" And Button6.Text = "" Then
            Button6.Text = "X"
            Button6.Enabled = False
        ElseIf Button6.Text = "X" And Button9.Text = "X" And Button3.Text = "" Then
            Button3.Text = "X"
            Button3.Enabled = False
        ElseIf Button1.Text = "X" And Button5.Text = "X" And Button9.Text = "" Then
            Button9.Text = "X"
            Button9.Enabled = False
        ElseIf Button1.Text = "X" And Button9.Text = "X" And Button5.Text = "" Then
            Button5.Text = "X"
            Button5.Enabled = False
        ElseIf Button5.Text = "X" And Button9.Text = "X" And Button1.Text = "" Then
            Button1.Text = "X"
            Button1.Enabled = False
        ElseIf Button3.Text = "X" And Button5.Text = "X" And Button7.Text = "" Then
            Button7.Text = "X"
            Button7.Enabled = False
        ElseIf Button3.Text = "X" And Button7.Text = "X" And Button5.Text = "" Then
            Button5.Text = "X"
            Button5.Enabled = False
        ElseIf Button5.Text = "X" And Button7.Text = "X" And Button3.Text = "" Then
            Button3.Text = "X"
            Button3.Enabled = False
        End If
        WinCheak()
    End Sub      '勝利
    Sub AI4()
        If Button1.Enabled = True Then
            Button1.Text = "X"
            Button1.Enabled = False
        ElseIf Button7.Enabled = True Then
            Button7.Text = "X"
            Button7.Enabled = False
        ElseIf Button9.Enabled = True Then
            Button9.Text = "X"
            Button9.Enabled = False
        ElseIf Button2.Enabled = True Then
            Button2.Text = "X"
            Button2.Enabled = False
        ElseIf Button4.Enabled = True Then
            Button4.Text = "X"
            Button4.Enabled = False
        ElseIf Button6.Enabled = True Then
            Button6.Text = "X"
            Button6.Enabled = False
        ElseIf Button8.Enabled = True Then
            Button8.Text = "X"
            Button8.Enabled = False
        End If
    End Sub      '攻擊
    Sub AI5()
        If Button1.Text = "○" And Button3.Text = "○" And Button2.Text = "X" And Button5.Text = "X" Then
            Button4.Text = "X"
            Button4.Enabled = False
            att = 99
        ElseIf Button3.Text = "○" And Button9.Text = "○" And Button5.Text = "X" And Button6.Text = "X" Then
            Button2.Text = "X"
            Button2.Enabled = False
            att = 99
        ElseIf Button7.Text = "○" And Button9.Text = "○" And Button5.Text = "X" And Button8.Text = "X" Then
            Button6.Text = "X"
            Button6.Enabled = False
            att = 99
        ElseIf Button1.Text = "○" And Button7.Text = "○" And Button4.Text = "X" And Button5.Text = "X" Then
            Button8.Text = "X"
            Button8.Enabled = False
            att = 99
        End If
    End Sub      'Setp6特殊狀況 標記att=99





...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div><div></div>

darkjack 發表於 2016-12-3 11:35 PM

玩了一下 先手跟後手差別
先手 基本上絕對平手

後手 都右上角先跑
這個點可以改成隨機,因為只要擺 中間就可以平手機率很大

zxc693491 發表於 2016-12-5 01:00 AM

darkjack 發表於 2016-12-3 11:35 PM static/image/common/back.gif
玩了一下 先手跟後手差別
先手 基本上絕對平手



原則上是為了簡單化中間下法,因為我已經定義好先手固定走法
如果要改成隨機很簡單,用亂數取值就好了,只是相對的為了讓他無敵
頂多就是改下其他角落,因為我當初設計這個AI就是要讓玩家有跟我對戰的感覺
基本上是不可能贏我的,但是AI可以更準確的下在固定地點,避免人腦失誤的現象
除此之外,在設計的過程常常遇到Bug 例如下了一般人不會走的棋路會造成AI不下的狀況
所以一直在找狀況做標記然後另外判斷處理。
結果做得太高興表單改的怪怪的,就差美工了。
感謝版主的加分以及試玩,希望你能贏過它{:32:}...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div>

darkjack 發表於 2016-12-8 12:13 AM

哈哈...井字遊戲人來玩都只是一個不注意就被贏過去了....

e9906923 發表於 2016-12-21 08:16 PM

還在學習中...
謝謝大大分享資訊
<br><br><br><br><br><div></div>

sango99001 發表於 2019-11-23 11:48 PM

井字遊戲(含AI及雙人版)

雖然這己經是很久的舊文章,小弟試玩樓主的版本後,就想把小弟去年年底自己寫出的井字遊戲分享出來給大家,請不要嫌棄哦,謝謝

zxc693491 發表於 2019-11-25 02:30 PM

sango99001 發表於 2019-11-23 11:48 PM static/image/common/back.gif
井字遊戲(含AI及雙人版)

雖然這己經是很久的舊文章,小弟試玩樓主的版本後,就想把小弟去年年底自己寫出的井 ...

真的好久了…
井字完,要不要嘗試五子,踩地雷

sango99001 發表於 2019-11-27 09:25 AM

zxc693491 發表於 2019-11-25 02:30 PM static/image/common/back.gif
真的好久了…
井字完,要不要嘗試五子,踩地雷

小弟目前對五子棋跟踩地雷的概念還是停留在會玩的階段,完全想不到如何轉換成程式

jerick 發表於 2020-1-8 08:30 PM

其實位置陣列是比較簡單的,難的是電腦端的智力。
踩地雷倒還好,就用亂數產生地雷就好,
而五子棋,你就要讓電腦端能有"推算"的能力,推算自己與玩家後幾步的走法。

star231 發表於 2020-3-4 01:30 PM

這是你學校的作業吧!!{:1:}<br><br><br><br><br><div></div>

star231 發表於 2020-3-11 08:31 AM

網路有很多類似的遊戲程式!去看看寫法!

zaq12345 發表於 2020-4-5 11:42 PM

謝謝大大的分享..大大辛苦了
頁: [1]