2009年3月11日 星期三

井宇遊戲 - 人與電腦對戰版 2008/03/11

image

'井宇遊戲 - 人與電腦對戰版
' 電腦每次用亂數法,下一個尚未被使用的格子
Public Class Form1
    Dim btn(9) As Button, i As Integer
    Dim oxFlag As Boolean
    Dim sco(9) 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()

        no = Int(Rnd() * 9) + 1
        Call playOne()
    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 = ""
                .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
            .Top = 3 * btn(1).Height + 30
            .Font = New Font("新細明體", 18)
            .Text = ""
            .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 playOne()

        '換電腦玩 - 抓一個空的格子 - 用亂數的方法
        Dim j = Int(Rnd() * 9) + 1
        While sco(j) <> 0
            j = Int(Rnd() * 9) + 1
        End While

        no = j
        Call playOne()
    End Sub

    Sub playOne()
        If oxFlag = True Then
            btn(no).Text = "○"
            sco(no) = 1
        Else
            btn(no).Text = "×"
            sco(no) = 2
        End If
        btn(no).Enabled = False
        oxFlag = Not oxFlag

        '○贏了!
        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
        For i = 1 To 9
            btn(i).Text = ""
            sco(i) = 0
            btn(i).Enabled = True
        Next
        Label1.Text = ""
    End Sub
End Class

沒有留言:

張貼留言