Option Explicit On 
Option Strict On

Public Class frmTicTacToe
  Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

  Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call

  End Sub

  'Form overrides dispose to clean up the component list.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
      If Not (components Is Nothing) Then
        components.Dispose()
      End If
    End If
    MyBase.Dispose(disposing)
  End Sub
  Friend WithEvents picTurn As System.Windows.Forms.PictureBox
  Friend WithEvents picMark1 As System.Windows.Forms.PictureBox
  Friend WithEvents picMark2 As System.Windows.Forms.PictureBox
  Friend WithEvents picMark5 As System.Windows.Forms.PictureBox
  Friend WithEvents picMark3 As System.Windows.Forms.PictureBox
  Friend WithEvents picMark4 As System.Windows.Forms.PictureBox
  Friend WithEvents picMark8 As System.Windows.Forms.PictureBox
  Friend WithEvents picMark6 As System.Windows.Forms.PictureBox
  Friend WithEvents picMark7 As System.Windows.Forms.PictureBox
  Friend WithEvents btnReset As System.Windows.Forms.Button
  Friend WithEvents Label2 As System.Windows.Forms.Label
  Friend WithEvents Label3 As System.Windows.Forms.Label
  Friend WithEvents Label4 As System.Windows.Forms.Label
  Friend WithEvents Label5 As System.Windows.Forms.Label
  Friend WithEvents lblTurn As System.Windows.Forms.Label
  Friend WithEvents picMark9 As System.Windows.Forms.PictureBox

  'Required by the Windows Form Designer
  Private components As System.ComponentModel.Container

  'NOTE: The following procedure is required by the Windows Form Designer
  'It can be modified using the Windows Form Designer.  
  'Do not modify it using the code editor.
  Friend WithEvents picEarth As System.Windows.Forms.PictureBox
  Friend WithEvents picSun As System.Windows.Forms.PictureBox
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmTicTacToe))
    Me.Label3 = New System.Windows.Forms.Label()
    Me.Label2 = New System.Windows.Forms.Label()
    Me.picTurn = New System.Windows.Forms.PictureBox()
    Me.picMark5 = New System.Windows.Forms.PictureBox()
    Me.picMark4 = New System.Windows.Forms.PictureBox()
    Me.picMark7 = New System.Windows.Forms.PictureBox()
    Me.picMark6 = New System.Windows.Forms.PictureBox()
    Me.picMark1 = New System.Windows.Forms.PictureBox()
    Me.picMark3 = New System.Windows.Forms.PictureBox()
    Me.picMark2 = New System.Windows.Forms.PictureBox()
    Me.btnReset = New System.Windows.Forms.Button()
    Me.picMark9 = New System.Windows.Forms.PictureBox()
    Me.picMark8 = New System.Windows.Forms.PictureBox()
    Me.Label5 = New System.Windows.Forms.Label()
    Me.Label4 = New System.Windows.Forms.Label()
    Me.lblTurn = New System.Windows.Forms.Label()
    Me.picEarth = New System.Windows.Forms.PictureBox()
    Me.picSun = New System.Windows.Forms.PictureBox()
    Me.SuspendLayout()
    '
    'Label3
    '
    Me.Label3.BackColor = System.Drawing.Color.Blue
    Me.Label3.Location = New System.Drawing.Point(16, 240)
    Me.Label3.Name = "Label3"
    Me.Label3.Size = New System.Drawing.Size(256, 8)
    Me.Label3.TabIndex = 4
    '
    'Label2
    '
    Me.Label2.BackColor = System.Drawing.Color.Blue
    Me.Label2.Location = New System.Drawing.Point(16, 152)
    Me.Label2.Name = "Label2"
    Me.Label2.Size = New System.Drawing.Size(256, 8)
    Me.Label2.TabIndex = 4
    '
    'picTurn
    '
    Me.picTurn.Location = New System.Drawing.Point(72, 0)
    Me.picTurn.Name = "picTurn"
    Me.picTurn.Size = New System.Drawing.Size(56, 56)
    Me.picTurn.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
    Me.picTurn.TabIndex = 1
    Me.picTurn.TabStop = False
    '
    'picMark5
    '
    Me.picMark5.Location = New System.Drawing.Point(120, 176)
    Me.picMark5.Name = "picMark5"
    Me.picMark5.Size = New System.Drawing.Size(56, 56)
    Me.picMark5.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
    Me.picMark5.TabIndex = 1
    Me.picMark5.TabStop = False
    '
    'picMark4
    '
    Me.picMark4.Location = New System.Drawing.Point(32, 176)
    Me.picMark4.Name = "picMark4"
    Me.picMark4.Size = New System.Drawing.Size(56, 56)
    Me.picMark4.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
    Me.picMark4.TabIndex = 1
    Me.picMark4.TabStop = False
    '
    'picMark7
    '
    Me.picMark7.Location = New System.Drawing.Point(32, 256)
    Me.picMark7.Name = "picMark7"
    Me.picMark7.Size = New System.Drawing.Size(56, 56)
    Me.picMark7.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
    Me.picMark7.TabIndex = 1
    Me.picMark7.TabStop = False
    '
    'picMark6
    '
    Me.picMark6.Location = New System.Drawing.Point(208, 176)
    Me.picMark6.Name = "picMark6"
    Me.picMark6.Size = New System.Drawing.Size(56, 56)
    Me.picMark6.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
    Me.picMark6.TabIndex = 1
    Me.picMark6.TabStop = False
    '
    'picMark1
    '
    Me.picMark1.Location = New System.Drawing.Point(32, 88)
    Me.picMark1.Name = "picMark1"
    Me.picMark1.Size = New System.Drawing.Size(56, 56)
    Me.picMark1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
    Me.picMark1.TabIndex = 1
    Me.picMark1.TabStop = False
    '
    'picMark3
    '
    Me.picMark3.Location = New System.Drawing.Point(208, 88)
    Me.picMark3.Name = "picMark3"
    Me.picMark3.Size = New System.Drawing.Size(56, 56)
    Me.picMark3.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
    Me.picMark3.TabIndex = 1
    Me.picMark3.TabStop = False
    '
    'picMark2
    '
    Me.picMark2.Location = New System.Drawing.Point(120, 88)
    Me.picMark2.Name = "picMark2"
    Me.picMark2.Size = New System.Drawing.Size(56, 56)
    Me.picMark2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
    Me.picMark2.TabIndex = 1
    Me.picMark2.TabStop = False
    '
    'btnReset
    '
    Me.btnReset.Location = New System.Drawing.Point(88, 344)
    Me.btnReset.Name = "btnReset"
    Me.btnReset.Size = New System.Drawing.Size(112, 23)
    Me.btnReset.TabIndex = 2
    Me.btnReset.Text = "&Reset Game"
    '
    'picMark9
    '
    Me.picMark9.Location = New System.Drawing.Point(208, 256)
    Me.picMark9.Name = "picMark9"
    Me.picMark9.Size = New System.Drawing.Size(56, 56)
    Me.picMark9.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
    Me.picMark9.TabIndex = 1
    Me.picMark9.TabStop = False
    '
    'picMark8
    '
    Me.picMark8.Location = New System.Drawing.Point(120, 256)
    Me.picMark8.Name = "picMark8"
    Me.picMark8.Size = New System.Drawing.Size(56, 56)
    Me.picMark8.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
    Me.picMark8.TabIndex = 1
    Me.picMark8.TabStop = False
    '
    'Label5
    '
    Me.Label5.BackColor = System.Drawing.Color.Blue
    Me.Label5.Location = New System.Drawing.Point(184, 72)
    Me.Label5.Name = "Label5"
    Me.Label5.Size = New System.Drawing.Size(8, 256)
    Me.Label5.TabIndex = 5
    '
    'Label4
    '
    Me.Label4.BackColor = System.Drawing.Color.Blue
    Me.Label4.Location = New System.Drawing.Point(96, 72)
    Me.Label4.Name = "Label4"
    Me.Label4.Size = New System.Drawing.Size(8, 256)
    Me.Label4.TabIndex = 5
    '
    'lblTurn
    '
    Me.lblTurn.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.lblTurn.Location = New System.Drawing.Point(128, 16)
    Me.lblTurn.Name = "lblTurn"
    Me.lblTurn.Size = New System.Drawing.Size(112, 32)
    Me.lblTurn.TabIndex = 0
    Me.lblTurn.Text = "Turn"
    Me.lblTurn.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
    '
    'picEarth
    '
    Me.picEarth.Image = CType(resources.GetObject("picEarth.Image"), System.Drawing.Bitmap)
    Me.picEarth.Location = New System.Drawing.Point(16, 336)
    Me.picEarth.Name = "picEarth"
    Me.picEarth.Size = New System.Drawing.Size(48, 40)
    Me.picEarth.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
    Me.picEarth.TabIndex = 6
    Me.picEarth.TabStop = False
    Me.picEarth.Visible = False
    '
    'picSun
    '
    Me.picSun.Image = CType(resources.GetObject("picSun.Image"), System.Drawing.Bitmap)
    Me.picSun.Location = New System.Drawing.Point(232, 336)
    Me.picSun.Name = "picSun"
    Me.picSun.Size = New System.Drawing.Size(48, 40)
    Me.picSun.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
    Me.picSun.TabIndex = 7
    Me.picSun.TabStop = False
    Me.picSun.Visible = False
    '
    'frmTicTacToe
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(300, 385)
    Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.picSun, Me.picEarth, Me.btnReset, Me.picMark8, Me.picMark6, Me.picMark7, Me.picMark5, Me.picMark3, Me.picMark4, Me.picMark2, Me.picMark1, Me.picMark9, Me.picTurn, Me.lblTurn, Me.Label5, Me.Label4, Me.Label3, Me.Label2})
    Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
    Me.Name = "frmTicTacToe"
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
    Me.Text = "Tic-Tac-Toe Problem"
    Me.ResumeLayout(False)

  End Sub

#End Region

  Dim EarthTurn As Boolean ' if True, it's Earth's turn
  Dim CanClick As Boolean ' if True, can click grid
  Dim NumberClicks As Integer 'Number of grid clicks
  Dim Marker(9) As Integer 'what marker is in what location (1-Earth,2-Sun)

  Private Sub frmTicTacToe_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    btnReset.PerformClick()
  End Sub

  Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
    Dim I As Integer
    'Clear board
    picMark1.Image = Nothing
    picMark2.Image = Nothing
    picMark3.Image = Nothing
    picMark4.Image = Nothing
    picMark5.Image = Nothing
    picMark6.Image = Nothing
    picMark7.Image = Nothing
    picMark8.Image = Nothing
    picMark9.Image = Nothing
    For I = 1 To 9
      Marker(I) = 0
    Next I
    'Make it Earth's turn
    picTurn.Image = picEarth.Image
    lblTurn.Text = "Turn"
    EarthTurn = True
    CanClick = True
    NumberClicks = 0
  End Sub

  Private Sub picMark_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picMark1.Click, picMark2.Click, picMark3.Click, picMark4.Click, picMark5.Click, picMark6.Click, picMark7.Click, picMark8.Click, picMark9.Click
    'Decide which picture was clicked (1-9)
    Dim picClicked As PictureBox, Index As Integer
    Dim Win As Boolean
    picClicked = CType(sender, PictureBox)
    'Index is last digit of control name
    Index = CInt(Val(Mid(picClicked.Name, Len(picClicked.Name), 1)))
    'If can't click or marker already there exit sub
    If Not (CanClick) Or Marker(Index) <> 0 Then
      Exit Sub
    End If
    'Increment click then mark
    NumberClicks = NumberClicks + 1
    If EarthTurn Then
      Marker(Index) = 1
      picClicked.Image = picEarth.Image
    Else
      Marker(Index) = 2
      picClicked.Image = picSun.Image
    End If
    Win = False
    'Check for win
    If Marker(1) = Marker(Index) And Marker(2) = Marker(Index) And Marker(3) = Marker(Index) Then
      'Top horizontal win
      Win = True
    ElseIf Marker(4) = Marker(Index) And Marker(5) = Marker(Index) And Marker(6) = Marker(Index) Then
      'Middle horizontal win
      Win = True
    ElseIf Marker(7) = Marker(Index) And Marker(8) = Marker(Index) And Marker(9) = Marker(Index) Then
      'Bottom horizontal win
      Win = True
    ElseIf Marker(1) = Marker(Index) And Marker(4) = Marker(Index) And Marker(7) = Marker(Index) Then
      'Left vertical win
      Win = True
    ElseIf Marker(2) = Marker(Index) And Marker(5) = Marker(Index) And Marker(8) = Marker(Index) Then
      'Middle vertical win
      Win = True
    ElseIf Marker(3) = Marker(Index) And Marker(6) = Marker(Index) And Marker(9) = Marker(Index) Then
      'Right vertical win
      Win = True
    ElseIf Marker(1) = Marker(Index) And Marker(5) = Marker(Index) And Marker(9) = Marker(Index) Then
      'Top, left down diagonal win
      Win = True
    ElseIf Marker(7) = Marker(Index) And Marker(5) = Marker(Index) And Marker(3) = Marker(Index) Then
      'Bottom, left up diagonal win
      Win = True
    End If
    If Win Then
      'There's a win
      lblTurn.Text = "Win!!"
      CanClick = False
    Else
      'If we got this far, this was no win
      If NumberClicks < 9 Then
        EarthTurn = Not (EarthTurn)
        If EarthTurn Then
          picTurn.Image = picEarth.Image
        Else
          picTurn.Image = picSun.Image
        End If
      Else
        'If there's nine clicks and no win, it's a tie
        picTurn.Image = Nothing
        lblTurn.Text = "A Tie ..."
        CanClick = False
      End If
    End If
  End Sub
End Class
