2009年3月13日 星期五

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

image

image

Public Class Form1
    Dim btn(9) As Button
    Dim i
    Dim j
    Dim sco(9)
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Randomize()
        btn(1) = Button1
        btn(2) = Button2
        btn(3) = Button3
        btn(4) = Button4
        btn(5) = Button5
        btn(6) = Button6
        btn(7) = Button7
        btn(8) = Button8
        btn(9) = Button9
        For i = 1 To 9
            With btn(i)
                .Height = btn(i).Width
                .Font = New Font("Arial", 50)
                ' .Text = Mid(btn(i).Text, Len(btn(i).Text), 1)
                .Text = ""
                .Left = 10 + btn(1).Width * ((i - 1) Mod 3)
                .Top = 10 + btn(1).Height * ((i - 1) \ 3)
            End With
            sco(i) = 0
        Next
        Label1.Top = btn(1).Height * 3 + 20
        Label1.Text = ""
        Button10.Top = btn(1).Height * 3 + 20
        Button11.Top = btn(1).Height * 3 + 20
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        j = 1
        While btn(j).Text <> ""
            j = j + 1
        End While
        btn(j).Text = "○"
        sco(j) = 1
        Call checkwin()
    End Sub
    Sub checkwin()
        '○贏了
        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 Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        j = Int(Rnd() * 9) + 1
        While btn(j).Text <> ""
            j = Int(Rnd() * 9) + 1
        End While
        btn(j).Text = "×"
        sco(j) = 2

        Call checkwin()

    End Sub
End Class

沒有留言:

張貼留言