2009年3月13日 星期五

井字遊戲 -- 二種電腦邏輯對戰版

image image

 

 

 

 

Public Class Form1
    Dim but(9) As Button
    Dim i
    Dim sco(9)
    Dim j
    Dim no

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call Hypothesis()
    End Sub

    Sub Hypothesis()

        but(1) = Button1
        but(2) = Button2
        but(3) = Button3
        but(4) = Button4
        but(5) = Button5
        but(6) = Button6
        but(7) = Button7
        but(8) = Button8
        but(9) = Button9

        For i = 1 To 9
            With but(i)
                .Height = but(i).Width
                .Font = New Font("Arial", 50)
                .Text = Mid(but(i).Text, Len(but(i).Text), 1)
                .Text = ""
                .Left = 10 + but(1).Width * ((i - 1) Mod 3)
                .Top = 10 + but(1).Height * ((i - 1) \ 3)
            End With

            sco(i) = 0
        Next

        With Label1
            .Top = but(1).Height * 3 + 20
            Label1.Left = 10
            Label1.Text = ""
            .Font = New Font("標楷體", 15)
        End With

        With Button10
            .Top = but(1).Height * 3 + 15
            .Font = New Font("標楷體", 10)
            .Left = but(1).Width * 2.1
            .Text = " × "
        End With

        With Button11
            .Left = but(1).Width * 1
            .Top = but(1).Height * 3 + 15
            .Font = New Font("標楷體", 10)
            .Text = " ○ "
        End With

    End Sub

    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click
        no = Mid(CType(sender, Button).Name, Len(CType(sender, Button).Name), 1)

    End Sub
    Sub play1()
        j = 2
        While but(j).Text <> ""
            j = Int(Rnd() * 9) + 1
        End While
        but(j).Text = "○"
        sco(j) = 1

    End Sub
    Sub play2()
        j = 1
        While but(j).Text <> ""
            j = Int(Rnd() * 9) + 1
        End While
        but(j).Text = "×"
        sco(j) = 2

    End Sub
    Sub play()

        If sco(1) * sco(2) * sco(3) = 1 Then Label1.Text = "○ 贏了!"
        If sco(4) * sco(5) * sco(6) = 1 Then Label1.Text = "○ 贏了!"
        If sco(7) * sco(8) * sco(9) = 1 Then Label1.Text = "○ 贏了!"
        If sco(1) * sco(4) * sco(7) = 1 Then Label1.Text = "○ 贏了!"
        If sco(2) * sco(5) * sco(8) = 1 Then Label1.Text = "○ 贏了!"
        If sco(3) * sco(6) * sco(9) = 1 Then Label1.Text = "○ 贏了!"
        If sco(1) * sco(5) * sco(9) = 1 Then Label1.Text = "○ 贏了!"
        If sco(3) * sco(5) * sco(7) = 1 Then Label1.Text = "○ 贏了!"

        If sco(1) * sco(2) * sco(3) = 8 Then Label1.Text = "× 贏了!"
        If sco(4) * sco(5) * sco(6) = 8 Then Label1.Text = "× 贏了!"
        If sco(7) * sco(8) * sco(9) = 8 Then Label1.Text = "× 贏了!"
        If sco(1) * sco(4) * sco(7) = 8 Then Label1.Text = "× 贏了!"
        If sco(2) * sco(5) * sco(8) = 8 Then Label1.Text = "× 贏了!"
        If sco(3) * sco(6) * sco(9) = 8 Then Label1.Text = "× 贏了!"
        If sco(1) * sco(5) * sco(9) = 8 Then Label1.Text = "× 贏了!"
        If sco(3) * sco(5) * sco(7) = 8 Then Label1.Text = "× 贏了!"
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        play2()
        play()
    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        play1()
        play()
    End Sub
End Class

沒有留言:

張貼留言