Public Class Form1
'En primer lugar declaramos nuestras variables que utilizaremos
Dim id As String = "X" ' Aqui estamos defiendo quien va primero
Dim counter_jugadas As Integer = 0 'Contador de las jugadas
Dim JX As Integer = 0 ' Contador para saber cuantas veces ha ganado X
Dim JO As Integer = 0 ' Contador para saber cuantas veces ha ganado 0
Dim Igual As Integer = 0 ' Contador para saber los empates
Dim final_juego As Boolean = False
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
reiniciar_botones()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click
Try
'activamos el timer
Timer1.Enabled = True
'iniciamos el timer
Timer1.Start()
If RadioButton1.Checked = True Then
seleccion_de_Juego(sender)
verificar_ganador(id)
switch()
ElseIf RadioButton2.Checked = True Then
If id = "X" Then
seleccion_de_Juego(sender)
verificar_ganador(id)
switch()
End If
If final_juego = False Then
seleccion_maquina()
verificar_ganador(id)
switch()
End If
Else
MsgBox("ERROR : Selecciones Modo De Juego.", MsgBoxStyle.Critical, "Error")
End If
Catch ex As Exception
MsgBox("error inesperado")
End Try
End Sub
Private Sub switch()
If id = "X" Then
id = "O"
Else
id = "X"
End If
Label1.Text = id
End Sub
'METODO PARA SELECCIONAR EL TIPO DE JUEGO
Private Sub seleccion_de_Juego(ByVal sender As Object)
For Each opcion In FlowLayoutPanel1.Controls
If TypeOf opcion Is Button Then
If CType(opcion, Button).Name = CType(sender, Button).Name Then
CType(opcion, Button).Text = id
CType(opcion, Button).Enabled = False
End If
End If
Next
End Sub
'Metodo para ubicar aleatoriamente la 0 si nuestro id es x
Private Sub seleccion()
'Se tomara decisiones completamente al azar
Dim numero As New Random
While True And counter_jugadas < 9
Dim seleccion_maquina As Integer = numero.Next(1, 10)
Dim btns = Me.Controls.Find("button" & seleccion_maquina, True)
If btns.Length > 0 Then
If btns(0).Text = String.Empty Then
btns(0).Text = id
btns(0).Enabled = False
Exit While
End If
End If
End While
End Sub
'Fin de nuestra funcion Seleccion
'Metodo para analizar los bloqueos de nuestro juego
Private Sub seleccion_maquina()
If Bloqueo("O") = True Then
Else
If Bloqueo("X") = True Then
Else
seleccion()
End If
End If
End Sub
Private Function Bloqueo(ByVal opcion As String)
'bloqueo horizontal
Dim counter As Integer 'Conteo de X o 0 Encontrados.
Dim empty As Integer 'Guarda Ultimo Espacio Vacio Encontrado
For i As Integer = 1 To 7 Step 3 'LOOP Monitorea las Filas ... 1 , 4 , 7
counter = 0
For e As Integer = i To i + 2 'LOOP Monitorea los elementos en las filas. 123, 456 , 789
Dim btns = Me.Controls.Find("button" & e, True)
If btns.Length > 0 Then
If btns(0).Text = opcion Then
counter += 1
Else
empty = e
End If
End If
Next
If counter = 2 Then
Dim btns = Me.Controls.Find("button" & empty, True)
If btns.Length > 0 Then
If btns(0).Text = String.Empty Then
btns(0).Text = id
btns(0).Enabled = False
Return True
End If
End If
End If
Next
'Bloqueo Vertical
For i As Integer = 1 To 3 'LOOP Monitorea las Columnas
counter = 0
For e As Integer = i To 9 Step 3 'loop Monitorea los elementos de las columnas.
Dim btns = Me.Controls.Find("button" & e, True)
If btns.Length > 0 Then
If btns(0).Text = opcion Then
counter += 1
Else
empty = e
End If
End If
Next
If counter = 2 Then
Dim btns = Me.Controls.Find("button" & empty, True)
If btns.Length > 0 Then
If btns(0).Text = String.Empty Then
btns(0).Text = id
btns(0).Enabled = False
Return True
End If
End If
End If
Next
'Bloqueo Diagonales
counter = 0
For e As Integer = 1 To 9 Step 4
Dim btns = Me.Controls.Find("button" & e, True)
If btns.Length > 0 Then
If btns(0).Text = opcion Then
counter += 1
Else
empty = e
End If
End If
Next
If counter = 2 Then
Dim btns = Me.Controls.Find("button" & empty, True)
If btns.Length > 0 Then
If btns(0).Text = String.Empty Then
btns(0).Text = id
btns(0).Enabled = False
Return True
End If
End If
End If
counter = 0
For e As Integer = 7 To 3 Step -2
Dim btns = Me.Controls.Find("button" & e, True)
If btns.Length > 0 Then
If btns(0).Text = opcion Then
counter += 1
Else
empty = e
End If
End If
Next
If counter = 2 Then
Dim btns = Me.Controls.Find("button" & empty, True)
If btns.Length > 0 Then
If btns(0).Text = String.Empty Then
btns(0).Text = id
btns(0).Enabled = False
Return True
End If
End If
End If
Return False
End Function
Private Sub verificar_ganador(ByVal x As String)
counter_jugadas += 1
Dim counter As Integer = 0
'Verificar Horizontal
For i As Integer = 1 To 7 Step 3
counter = 0
For e As Integer = i To i + 2
Dim btns = Me.Controls.Find("button" & e, True)
If btns.Length > 0 Then
If btns(0).Text = x Then
btns(0).BackColor = Color.PowderBlue
counter += 1
End If
End If
Next
If counter = 3 Then
mostrar_ganador(x)
Exit Sub
Else
reiniciar_colores()
End If
Next
'Verificar Vertical
For i As Integer = 1 To 3
counter = 0
For e As Integer = i To 9 Step 3
Dim btns = Me.Controls.Find("button" & e, True)
If btns.Length > 0 Then
If btns(0).Text = x Then
btns(0).BackColor = Color.PowderBlue
counter += 1
End If
End If
Next
If counter = 3 Then
mostrar_ganador(x)
Exit Sub
Else
reiniciar_colores()
End If
Next
'Verificar Diagonales
counter = 0
For e As Integer = 1 To 9 Step 4
Dim btns = Me.Controls.Find("button" & e, True)
If btns.Length > 0 Then
If btns(0).Text = x Then
btns(0).BackColor = Color.PowderBlue
counter += 1
End If
End If
Next
If counter = 3 Then
mostrar_ganador(x)
Exit Sub
Else
reiniciar_colores()
End If
counter = 0
For e As Integer = 7 To 3 Step -2
Dim btns = Me.Controls.Find("button" & e, True)
If btns.Length > 0 Then
If btns(0).Text = x Then
btns(0).BackColor = Color.PowderBlue
counter += 1
End If
End If
Next
If counter = 3 Then
mostrar_ganador(x)
Exit Sub
Else
reiniciar_colores()
End If
If counter_jugadas = 9 Then
Igual += 1
Empate1.Text = Igual
MsgBox("Empate", MsgBoxStyle.Information, "INFORMACION")
End If
End Sub
Private Sub mostrar_ganador(ByVal ganador As String)
'detenemos el timer
Timer1.Stop()
enable(False)
final_juego = True
MsgBox("GANO : " & ganador)
If ganador = "X" Then
JX += 1
Else
JO += 1
End If
Labelx.Text = JX
LabelO.Text = JO
End Sub
Private Sub enable(ByVal x As Boolean)
For Each opcion In FlowLayoutPanel1.Controls
If TypeOf opcion Is Button Then
If x = False Then
CType(opcion, Button).Enabled = False
Else
CType(opcion, Button).Enabled = True
End If
End If
Next
End Sub
Private Sub reiniciar_botones()
For Each opcion In FlowLayoutPanel1.Controls
If TypeOf opcion Is Button Then
CType(opcion, Button).Text = String.Empty
End If
Next
reiniciar_colores()
counter_jugadas = 0
Label1.Text = id
End Sub
Private Sub reiniciar_colores()
For Each opcion In FlowLayoutPanel1.Controls
If TypeOf opcion Is Button Then
CType(opcion, Button).BackColor = Color.White
End If
Next
End Sub
Private Sub JuegoNuevo_Click(sender As Object, e As EventArgs) Handles JuegoNuevo.Click
reiniciar_botones()
enable(True)
final_juego = False
If RadioButton2.Checked = True And id <> "X" Then
Button1_Click(Button1, New EventArgs())
End If
Tiempo.Text = "00"
Label5.Text = "00"
End Sub
Private Sub Salir_Click(sender As Object, e As EventArgs) Handles Salir.Click
Me.Close()
End Sub
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked = True Then
enable(True)
reiniciar_botones()
End If
End Sub
Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
If RadioButton2.Checked = True And id <> "X" Then
Button1_Click(Button1, New EventArgs())
End If
End Sub
Private Sub Limpiar_Click(sender As Object, e As EventArgs) Handles Limpiar.Click
JX = 0
JO = 0
Igual = 0
LabelO.Text = JO
Labelx.Text = JX
Empate1.Text = Igual
Tiempo.Text = "00"
Label5.Text = "00"
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Tiempo.Text += 1
If Tiempo.Text = "60" Then
Tiempo.Text += 1
End If
End Sub
End Class
No hay comentarios:
Publicar un comentario