Juego de x0 vb.net

Hola en este ejemplo de visual studio crearemos  el juego de xo.



Tambien conocido como el: tres en línea,  tres en rayajuego del gatotatetítriquitotitotriqui trakatres en gallomichiceritosequis cero o la vieja, es un juego de lápiz y papel entre dos jugadores: O y X, que marcan los espacios de un tablero de 3×3 alternadamente. Un jugador gana si consigue tener una línea de tres de sus símbolos: la línea puede ser horizontal, vertical o diagonal.
Mas info: wiki

aqui un video en el cual lo crean y juegan:


Básicamente consiste en intentar poner 3 x seguidas o 3 ceros seguidos vertical u horizontalmente.
Nota en el vídeo lo juegan dos dos 0 de diferente color aqui lo aremos con una x y un 0


Una vez entendiendo como jugarlo pasemos al diseño


Como puede observarce se nesecitara de 3 labels( label3 sin texto) 9 PictureBox los cuales en la imagen se muestra el orden como deben ir osea PictureBox1, PictureBox2, PictureBox3 asi sucesivamente hasta el 9 y dos botones

Una vez colocado el diseño ocuparemos las siguientes imágenes para los PictureBox:



Ya con la adquisición de las imágenes pasamos al código.

Aquí el código:

Public Class Form1
'Definiendo variables
Dim turno As String
Dim contX, contY, contTurnos As Integer
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
turnoo(1, turno)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'iniciando variables y diseño de los labels
contX = 0
contY = 0
contTurnos = 0
Label1.Text = "X: 0"
Label2.Text = "O: 0"
Button2.Enabled = False
turno = "x"
Label3.Text = "Turno de " + turno
'asignando imagen inicial a los PictureBoxs
PictureBox1.ImageLocation = "appletenhtml.png"
PictureBox1.Load()
PictureBox2.ImageLocation = "appletenhtml.png"
PictureBox2.Load()
PictureBox3.ImageLocation = "appletenhtml.png"
PictureBox3.Load()
PictureBox4.ImageLocation = "appletenhtml.png"
PictureBox4.Load()
PictureBox5.ImageLocation = "appletenhtml.png"
PictureBox5.Load()
PictureBox6.ImageLocation = "appletenhtml.png"
PictureBox6.Load()
PictureBox7.ImageLocation = "appletenhtml.png"
PictureBox7.Load()
PictureBox8.ImageLocation = "appletenhtml.png"
PictureBox8.Load()
PictureBox9.ImageLocation = "appletenhtml.png"
PictureBox9.Load()
Cursor = Cursors.Hand
End Sub
'Editando el sub turnoo para actualizar la ventana
'segun cada turno
Public Sub turnoo(img As Integer, turn As String)
Dim im As String
If turn = "x" Then
im = "x.png"
turno = "o"
Else
im = "o.png"
turno = "x"
End If
Label3.Text = "Turno de " + turno
'actualizando picturebox segun turno dado
If img = 1 Then
PictureBox1.ImageLocation = im
PictureBox1.Load()
PictureBox1.Enabled = False
ElseIf img = 2 Then
PictureBox2.ImageLocation = im
PictureBox2.Load()
PictureBox2.Enabled = False
ElseIf img = 3 Then
PictureBox3.ImageLocation = im
PictureBox3.Load()
PictureBox3.Enabled = False
ElseIf img = 4 Then
PictureBox4.ImageLocation = im
PictureBox4.Load()
PictureBox4.Enabled = False
ElseIf img = 5 Then
PictureBox5.ImageLocation = im
PictureBox5.Load()
PictureBox5.Enabled = False
ElseIf img = 6 Then
PictureBox6.ImageLocation = im
PictureBox6.Load()
PictureBox6.Enabled = False
ElseIf img = 7 Then
PictureBox7.ImageLocation = im
PictureBox7.Load()
PictureBox7.Enabled = False
ElseIf img = 8 Then
PictureBox8.ImageLocation = im
PictureBox8.Load()
PictureBox8.Enabled = False
ElseIf img = 9 Then
PictureBox9.ImageLocation = im
PictureBox9.Load()
PictureBox9.Enabled = False
End If
'aumentamos el contador de turno para saber cuantos turnos van
contTurnos = contTurnos + 1
'comprobando ganador si lo hay aviso si no continua
comprobando()
End Sub
Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles PictureBox2.Click
turnoo(2, turno)
End Sub
Private Sub PictureBox3_Click(sender As Object, e As EventArgs) Handles PictureBox3.Click
turnoo(3, turno)
End Sub
Private Sub PictureBox4_Click(sender As Object, e As EventArgs) Handles PictureBox4.Click
turnoo(4, turno)
End Sub
Private Sub PictureBox5_Click(sender As Object, e As EventArgs) Handles PictureBox5.Click
turnoo(5, turno)
End Sub
Private Sub PictureBox6_Click(sender As Object, e As EventArgs) Handles PictureBox6.Click
turnoo(6, turno)
End Sub
Private Sub PictureBox7_Click(sender As Object, e As EventArgs) Handles PictureBox7.Click
turnoo(7, turno)
End Sub
Private Sub PictureBox8_Click(sender As Object, e As EventArgs) Handles PictureBox8.Click
turnoo(8, turno)
End Sub
Private Sub PictureBox9_Click(sender As Object, e As EventArgs) Handles PictureBox9.Click
turnoo(9, turno)
End Sub
'Edtando sub comprobando el cual es para comprobar si hay algun
'ganador
Public Sub comprobando()
'Posibles convinaciones de turnos:
If PictureBox1.ImageLocation = "x.png" And PictureBox2.ImageLocation = "x.png" And PictureBox3.ImageLocation = "x.png" Then
contX = contX + 1
Label1.Text = "X: " + CStr(contX)
Button2.Enabled = True
contTurnos = 0
stp()
End If
If PictureBox4.ImageLocation = "x.png" And PictureBox5.ImageLocation = "x.png" And PictureBox6.ImageLocation = "x.png" Then
contX = contX + 1
Label1.Text = "X: " + CStr(contX)
Button2.Enabled = True
contTurnos = 0
stp()
End If
If PictureBox7.ImageLocation = "x.png" And PictureBox8.ImageLocation = "x.png" And PictureBox9.ImageLocation = "x.png" Then
contX = contX + 1
Label1.Text = "X: " + CStr(contX)
Button2.Enabled = True
contTurnos = 0
stp()
End If
If PictureBox1.ImageLocation = "x.png" And PictureBox5.ImageLocation = "x.png" And PictureBox9.ImageLocation = "x.png" Then
contX = contX + 1
Label1.Text = "X: " + CStr(contX)
Button2.Enabled = True
contTurnos = 0
stp()
End If
If PictureBox3.ImageLocation = "x.png" And PictureBox5.ImageLocation = "x.png" And PictureBox7.ImageLocation = "x.png" Then
contX = contX + 1
Label1.Text = "X: " + CStr(contX)
Button2.Enabled = True
contTurnos = 0
stp()
End If
If PictureBox1.ImageLocation = "x.png" And PictureBox4.ImageLocation = "x.png" And PictureBox7.ImageLocation = "x.png" Then
contX = contX + 1
Label1.Text = "X: " + CStr(contX)
Button2.Enabled = True
contTurnos = 0
stp()
End If
If PictureBox2.ImageLocation = "x.png" And PictureBox5.ImageLocation = "x.png" And PictureBox8.ImageLocation = "x.png" Then
contX = contX + 1
Label1.Text = "X: " + CStr(contX)
Button2.Enabled = True
contTurnos = 0
stp()
End If
If PictureBox3.ImageLocation = "x.png" And PictureBox6.ImageLocation = "x.png" And PictureBox9.ImageLocation = "x.png" Then
contX = contX + 1
Label1.Text = "X: " + CStr(contX)
Button2.Enabled = True
contTurnos = 0
stp()
End If
'--------
If PictureBox1.ImageLocation = "o.png" And PictureBox2.ImageLocation = "o.png" And PictureBox3.ImageLocation = "o.png" Then
contY = contY + 1
Label2.Text = "O: " + CStr(contY)
Button2.Enabled = True
contTurnos = 0
stp()
End If
If PictureBox4.ImageLocation = "o.png" And PictureBox5.ImageLocation = "o.png" And PictureBox6.ImageLocation = "o.png" Then
contY = contY + 1
Label2.Text = "O: " + CStr(contY)
Button2.Enabled = True
contTurnos = 0
stp()
End If
If PictureBox7.ImageLocation = "o.png" And PictureBox8.ImageLocation = "o.png" And PictureBox9.ImageLocation = "o.png" Then
contY = contY + 1
Label2.Text = "O: " + CStr(contY)
Button2.Enabled = True
contTurnos = 0
stp()
End If
'...........
If PictureBox1.ImageLocation = "o.png" And PictureBox5.ImageLocation = "o.png" And PictureBox9.ImageLocation = "o.png" Then
contY = contY + 1
Label2.Text = "O: " + CStr(contY)
Button2.Enabled = True
contTurnos = 0
stp()
End If
If PictureBox3.ImageLocation = "o.png" And PictureBox5.ImageLocation = "o.png" And PictureBox7.ImageLocation = "o.png" Then
contY = contY + 1
Label2.Text = "O: " + CStr(contY)
Button2.Enabled = True
stp()
contTurnos = 0
End If
If PictureBox1.ImageLocation = "o.png" And PictureBox4.ImageLocation = "o.png" And PictureBox7.ImageLocation = "o.png" Then
contY = contY + 1
Label2.Text = "O: " + CStr(contY)
Button2.Enabled = True
contTurnos = 0
stp()
End If
If PictureBox2.ImageLocation = "o.png" And PictureBox5.ImageLocation = "o.png" And PictureBox8.ImageLocation = "o.png" Then
contY = contY + 1
Label2.Text = "O: " + CStr(contY)
Button2.Enabled = True
contTurnos = 0
stp()
End If
If PictureBox3.ImageLocation = "o.png" And PictureBox6.ImageLocation = "o.png" And PictureBox9.ImageLocation = "o.png" Then
contY = contY + 1
Label2.Text = "O: " + CStr(contY)
Button2.Enabled = True
contTurnos = 0
stp()
End If
'en caso no alla ninguna convincion de triunfo si el contador
'llega a 9 es porque no ubo ganador por lo que pasara a
'cargar otra partida
If contTurnos = 9 Then
otraPrtida()
End If
'-
End Sub
'el sub stop funcionara para detener los movimientos
Public Sub stp()
PictureBox1.Enabled = False
PictureBox2.Enabled = False
PictureBox3.Enabled = False
PictureBox4.Enabled = False
PictureBox5.Enabled = False
PictureBox6.Enabled = False
PictureBox7.Enabled = False
PictureBox8.Enabled = False
PictureBox9.Enabled = False
End Sub
'el sub otaPrtida iniciara una nueva partida
Sub otraPrtida()
'turno = "x"
' Label3.Text = "Turno de " + turno
PictureBox1.ImageLocation = "appletenhtml.png"
PictureBox1.Load()
PictureBox2.ImageLocation = "appletenhtml.png"
PictureBox2.Load()
PictureBox3.ImageLocation = "appletenhtml.png"
PictureBox3.Load()
PictureBox4.ImageLocation = "appletenhtml.png"
PictureBox4.Load()
PictureBox5.ImageLocation = "appletenhtml.png"
PictureBox5.Load()
PictureBox6.ImageLocation = "appletenhtml.png"
PictureBox6.Load()
PictureBox7.ImageLocation = "appletenhtml.png"
PictureBox7.Load()
PictureBox8.ImageLocation = "appletenhtml.png"
PictureBox8.Load()
PictureBox9.ImageLocation = "appletenhtml.png"
PictureBox9.Load()
PictureBox1.Enabled = True
PictureBox2.Enabled = True
PictureBox3.Enabled = True
PictureBox4.Enabled = True
PictureBox5.Enabled = True
PictureBox6.Enabled = True
PictureBox7.Enabled = True
PictureBox8.Enabled = True
PictureBox9.Enabled = True
Cursor = Cursors.Hand
Button2.Enabled = False
contTurnos = 0
End Sub
'boton para utilizar la funcion otraPrtida
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
otraPrtida()
End Sub
'el Button1 lo utilizaremos para terminar y ver si ubo un ganador
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If contX > contY Then
MsgBox("felicidades x as ganado")
ElseIf contX < contY Then
MsgBox("felicidades o as ganado")
Else
MsgBox("ubo un empate")
End If
contX = 0
contY = 0
Label1.Text = "X: 0"
Label2.Text = "O: 0"
otraPrtida()
End Sub
End Class






Comentarios

Entradas más populares de este blog

Ejemplo de suma Pascal

Solucion al error collation desconocida: 'utf8mb4_0900_ai_ci'