2009年3月10日 星期二

井字遊戲--人機互動版-20090310版

'井字遊戲--人機互動版-20090310版

image

Public Class Form1
    Dim btn(9) As Button, i As Integer
    Dim oxFlag As Boolean
    Dim sco(9) As Integer
    Dim j As Integer
    Dim no As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call prepare()
        cplay()
    End Sub
    Sub cplay()
        Call getOneN()
        Call proc()
    End Sub


    Sub getOneN()  '取得一個未有人下過的空格
        j = Int(Rnd() * 9) + 1
        While sco(j) <> 0
            j = Int(Rnd() * 9) + 1
        End While
        no = j
    End Sub
    Sub prepare()
        Randomize()
        oxFlag = True
        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)
                '.Text = Mid((btn(i).Name), Len((btn(i).Name)), 1)
                .Text = ""
                .Height = .Width
                .Font = New Font("Arial", 50)
                .Left = 10 + .Width * ((i - 1) Mod 3)
                .Top = 10 + .Height * ((i - 1) \ 3)
            End With
            sco(i) = 0
        Next

        Me.Width = 3 * btn(1).Width + 30
        With Label1
            .Text = ""
            .Top = 3 * btn(1).Height + 30
            .Font = New Font("新細明體", 16)
            .ForeColor = Color.Red
        End With

        Button10.Top = 3 * btn(1).Height + 30

        Me.Height = 3 * btn(1).Height + 100
        Button10.Left = Me.Width - Button10.Width - 20
        Button10.Text = "重新開始"
    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)
        Call proc()
        Call cplay()
    End Sub
    Sub proc()
        If oxFlag = True Then
            btn(no).Text = "○"
            sco(no) = 1
        Else
            btn(no).Text = "×"
            sco(no) = 2
        End If
        btn(no).Enabled = False

        '○
        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 = "×贏了!"

        oxFlag = Not oxFlag
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        For i = 1 To 9
            btn(i).Enabled = True
            btn(i).Text = ""
            sco(i) = 0
        Next
        Label1.Text = ""
        Call cplay()
    End Sub
End Class

沒有留言:

張貼留言