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 raya, juego del gato, tatetí, triqui, totito, triqui traka, tres en gallo, michi, ceritos, equis 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:
Tambien conocido como el: tres en línea, tres en raya, juego del gato, tatetí, triqui, totito, triqui traka, tres en gallo, michi, ceritos, equis 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 variablesDim turno As StringDim contX, contY, contTurnos As IntegerPrivate Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Clickturnoo(1, turno)End SubPrivate Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load'iniciando variables y diseño de los labelscontX = 0contY = 0contTurnos = 0Label1.Text = "X: 0"Label2.Text = "O: 0"Button2.Enabled = Falseturno = "x"Label3.Text = "Turno de " + turno'asignando imagen inicial a los PictureBoxsPictureBox1.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.HandEnd Sub'Editando el sub turnoo para actualizar la ventana'segun cada turnoPublic Sub turnoo(img As Integer, turn As String)Dim im As StringIf turn = "x" Thenim = "x.png"turno = "o"Elseim = "o.png"turno = "x"End IfLabel3.Text = "Turno de " + turno'actualizando picturebox segun turno dadoIf img = 1 ThenPictureBox1.ImageLocation = imPictureBox1.Load()PictureBox1.Enabled = FalseElseIf img = 2 ThenPictureBox2.ImageLocation = imPictureBox2.Load()PictureBox2.Enabled = FalseElseIf img = 3 ThenPictureBox3.ImageLocation = imPictureBox3.Load()PictureBox3.Enabled = FalseElseIf img = 4 ThenPictureBox4.ImageLocation = imPictureBox4.Load()PictureBox4.Enabled = FalseElseIf img = 5 ThenPictureBox5.ImageLocation = imPictureBox5.Load()PictureBox5.Enabled = FalseElseIf img = 6 ThenPictureBox6.ImageLocation = imPictureBox6.Load()PictureBox6.Enabled = FalseElseIf img = 7 ThenPictureBox7.ImageLocation = imPictureBox7.Load()PictureBox7.Enabled = FalseElseIf img = 8 ThenPictureBox8.ImageLocation = imPictureBox8.Load()PictureBox8.Enabled = FalseElseIf img = 9 ThenPictureBox9.ImageLocation = imPictureBox9.Load()PictureBox9.Enabled = FalseEnd If'aumentamos el contador de turno para saber cuantos turnos vancontTurnos = contTurnos + 1'comprobando ganador si lo hay aviso si no continuacomprobando()End SubPrivate Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles PictureBox2.Clickturnoo(2, turno)End SubPrivate Sub PictureBox3_Click(sender As Object, e As EventArgs) Handles PictureBox3.Clickturnoo(3, turno)End SubPrivate Sub PictureBox4_Click(sender As Object, e As EventArgs) Handles PictureBox4.Clickturnoo(4, turno)End SubPrivate Sub PictureBox5_Click(sender As Object, e As EventArgs) Handles PictureBox5.Clickturnoo(5, turno)End SubPrivate Sub PictureBox6_Click(sender As Object, e As EventArgs) Handles PictureBox6.Clickturnoo(6, turno)End SubPrivate Sub PictureBox7_Click(sender As Object, e As EventArgs) Handles PictureBox7.Clickturnoo(7, turno)End SubPrivate Sub PictureBox8_Click(sender As Object, e As EventArgs) Handles PictureBox8.Clickturnoo(8, turno)End SubPrivate Sub PictureBox9_Click(sender As Object, e As EventArgs) Handles PictureBox9.Clickturnoo(9, turno)End Sub'Edtando sub comprobando el cual es para comprobar si hay algun'ganadorPublic Sub comprobando()'Posibles convinaciones de turnos:If PictureBox1.ImageLocation = "x.png" And PictureBox2.ImageLocation = "x.png" And PictureBox3.ImageLocation = "x.png" ThencontX = contX + 1Label1.Text = "X: " + CStr(contX)Button2.Enabled = TruecontTurnos = 0stp()End IfIf PictureBox4.ImageLocation = "x.png" And PictureBox5.ImageLocation = "x.png" And PictureBox6.ImageLocation = "x.png" ThencontX = contX + 1Label1.Text = "X: " + CStr(contX)Button2.Enabled = TruecontTurnos = 0stp()End IfIf PictureBox7.ImageLocation = "x.png" And PictureBox8.ImageLocation = "x.png" And PictureBox9.ImageLocation = "x.png" ThencontX = contX + 1Label1.Text = "X: " + CStr(contX)Button2.Enabled = TruecontTurnos = 0stp()End IfIf PictureBox1.ImageLocation = "x.png" And PictureBox5.ImageLocation = "x.png" And PictureBox9.ImageLocation = "x.png" ThencontX = contX + 1Label1.Text = "X: " + CStr(contX)Button2.Enabled = TruecontTurnos = 0stp()End IfIf PictureBox3.ImageLocation = "x.png" And PictureBox5.ImageLocation = "x.png" And PictureBox7.ImageLocation = "x.png" ThencontX = contX + 1Label1.Text = "X: " + CStr(contX)Button2.Enabled = TruecontTurnos = 0stp()End IfIf PictureBox1.ImageLocation = "x.png" And PictureBox4.ImageLocation = "x.png" And PictureBox7.ImageLocation = "x.png" ThencontX = contX + 1Label1.Text = "X: " + CStr(contX)Button2.Enabled = TruecontTurnos = 0stp()End IfIf PictureBox2.ImageLocation = "x.png" And PictureBox5.ImageLocation = "x.png" And PictureBox8.ImageLocation = "x.png" ThencontX = contX + 1Label1.Text = "X: " + CStr(contX)Button2.Enabled = TruecontTurnos = 0stp()End IfIf PictureBox3.ImageLocation = "x.png" And PictureBox6.ImageLocation = "x.png" And PictureBox9.ImageLocation = "x.png" ThencontX = contX + 1Label1.Text = "X: " + CStr(contX)Button2.Enabled = TruecontTurnos = 0stp()End If'--------If PictureBox1.ImageLocation = "o.png" And PictureBox2.ImageLocation = "o.png" And PictureBox3.ImageLocation = "o.png" ThencontY = contY + 1Label2.Text = "O: " + CStr(contY)Button2.Enabled = TruecontTurnos = 0stp()End IfIf PictureBox4.ImageLocation = "o.png" And PictureBox5.ImageLocation = "o.png" And PictureBox6.ImageLocation = "o.png" ThencontY = contY + 1Label2.Text = "O: " + CStr(contY)Button2.Enabled = TruecontTurnos = 0stp()End IfIf PictureBox7.ImageLocation = "o.png" And PictureBox8.ImageLocation = "o.png" And PictureBox9.ImageLocation = "o.png" ThencontY = contY + 1Label2.Text = "O: " + CStr(contY)Button2.Enabled = TruecontTurnos = 0stp()End If'...........If PictureBox1.ImageLocation = "o.png" And PictureBox5.ImageLocation = "o.png" And PictureBox9.ImageLocation = "o.png" ThencontY = contY + 1Label2.Text = "O: " + CStr(contY)Button2.Enabled = TruecontTurnos = 0stp()End IfIf PictureBox3.ImageLocation = "o.png" And PictureBox5.ImageLocation = "o.png" And PictureBox7.ImageLocation = "o.png" ThencontY = contY + 1Label2.Text = "O: " + CStr(contY)Button2.Enabled = Truestp()contTurnos = 0End IfIf PictureBox1.ImageLocation = "o.png" And PictureBox4.ImageLocation = "o.png" And PictureBox7.ImageLocation = "o.png" ThencontY = contY + 1Label2.Text = "O: " + CStr(contY)Button2.Enabled = TruecontTurnos = 0stp()End IfIf PictureBox2.ImageLocation = "o.png" And PictureBox5.ImageLocation = "o.png" And PictureBox8.ImageLocation = "o.png" ThencontY = contY + 1Label2.Text = "O: " + CStr(contY)Button2.Enabled = TruecontTurnos = 0stp()End IfIf PictureBox3.ImageLocation = "o.png" And PictureBox6.ImageLocation = "o.png" And PictureBox9.ImageLocation = "o.png" ThencontY = contY + 1Label2.Text = "O: " + CStr(contY)Button2.Enabled = TruecontTurnos = 0stp()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 partidaIf contTurnos = 9 ThenotraPrtida()End If'-End Sub'el sub stop funcionara para detener los movimientosPublic Sub stp()PictureBox1.Enabled = FalsePictureBox2.Enabled = FalsePictureBox3.Enabled = FalsePictureBox4.Enabled = FalsePictureBox5.Enabled = FalsePictureBox6.Enabled = FalsePictureBox7.Enabled = FalsePictureBox8.Enabled = FalsePictureBox9.Enabled = FalseEnd Sub'el sub otaPrtida iniciara una nueva partidaSub otraPrtida()'turno = "x"' Label3.Text = "Turno de " + turnoPictureBox1.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 = TruePictureBox2.Enabled = TruePictureBox3.Enabled = TruePictureBox4.Enabled = TruePictureBox5.Enabled = TruePictureBox6.Enabled = TruePictureBox7.Enabled = TruePictureBox8.Enabled = TruePictureBox9.Enabled = TrueCursor = Cursors.HandButton2.Enabled = FalsecontTurnos = 0End Sub'boton para utilizar la funcion otraPrtidaPrivate Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.ClickotraPrtida()End Sub'el Button1 lo utilizaremos para terminar y ver si ubo un ganadorPrivate Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.ClickIf contX > contY ThenMsgBox("felicidades x as ganado")ElseIf contX < contY ThenMsgBox("felicidades o as ganado")ElseMsgBox("ubo un empate")End IfcontX = 0contY = 0Label1.Text = "X: 0"Label2.Text = "O: 0"otraPrtida()End SubEnd Class
Comentarios
Publicar un comentario