投稿者 たかくん  (社会人) 投稿日時 2011/11/22 03:43:01
初心者さん、おはようございます。
僕が初心者の時に作ったプログラムです。
Formデザイナには何もしません。
頑張って自分で読んでみてください。
文法的な事は調べてみてくださいね。
プログラムは2回に分けて掲載します。
文字数制限のため...

Public Class Form1
    Const Cell As Integer = 3
    Const PASS As String = Nothing
    Const FIRST As Integer = 0
    Const PLAYER As String = "○"
    Const COM As String = "×"
    Const REACHI As Integer = 2
    Const BINGO As Integer = 3
    Const Wid As Integer = 64
    Dim Boards(Cell, Cell) As Label
    Dim r As New Random
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim x, y, Cnt, Rd As Integer
        Cnt = 0 : Rd = 0
        For y = 0 To Cell - 1
            For x = 0 To Cell - 1
                Boards(x, y) = New Label
                Cnt = Cnt + 1
                With Boards(x, y)
                    .Tag = Cnt
                    .Size = New Size(Wid, Wid)
                    .Font = New Font("Time New Roman", 36, FontStyle.Bold)
                    .BorderStyle = BorderStyle.FixedSingle
                    .Text = PASS
                    .Location = New Point(x * Wid, y * Wid)
                    Me.Controls.Add(Boards(x, y))
                    AddHandler Boards(x, y).Click, AddressOf Man_Click
                End With
                If y = Cell - 1 And x = Cell - 1 Then Width = (x * Wid) + Wid + 5 : Height = (y * Wid) + Wid + 28
            Next
        Next
        Rd = r.Next(0, 2)   '人間が先手なら抜けるCPUが先手ならComputerAI()を呼ぶ
        If Rd = FIRST Then Exit Sub Else ComputerAI()
    End Sub

    'CPUの戦術
    Private Sub ComputerAI()
        Dim x, y, p, c, i, ComX, ComY As Integer
        For x = 0 To Cell - 1 '縦判定
            p = 0 : c = 0
            For y = 0 To Cell - 1
                If Boards(x, y).Text = PLAYER Then p = p + 1
                If Boards(x, y).Text = COM Then c = c + 1
                If Boards(x, y).Text = PASS And c = 0 Then ComX = x : ComY = y
            Next
            If p = REACHI And c = 0 Then Boards(ComX, ComY).Text = COM : Exit Sub
        Next
        For y = 0 To Cell - 1   '横判定
            p = 0 : c = 0
            For x = 0 To Cell - 1
                If Boards(x, y).Text = PLAYER Then p = p + 1
                If Boards(x, y).Text = COM Then c = c + 1
                If Boards(x, y).Text = PASS And c = 0 Then ComX = x : ComY = y
            Next
            If p = REACHI And c = 0 Then Boards(ComX, ComY).Text = COM : Exit Sub
        Next
        p = 0 : c = 0
        For i = 0 To Cell - 1  '右下斜め判定
            If Boards(i, i).Text = PLAYER Then p = p + 1
            If Boards(i, i).Text = COM Then c = c + 1
            If Boards(i, i).Text = PASS And c = 0 Then ComX = i : ComY = i
        Next
        If p = REACHI And c = 0 Then Boards(ComX, ComY).Text = COM : Exit Sub
        p = 0 : c = 0
        For x = Cell - 1 To 0 Step -1   '左下へ斜め探査
            For y = 0 To Cell - 1
                If x + y = 2 Then
                    If Boards(x, y).Text = PLAYER Then p = p + 1
                    If Boards(x, y).Text = COM Then c = c + 1
                    If Boards(x, y).Text = PASS And c = 0 Then ComX = x : ComY = y
                End If
            Next
        Next
        If p = REACHI And c = 0 Then Boards(ComX, ComY).Text = COM : Exit Sub
        PutCom()
    End Sub