[Duvida] VB 2005 - Jogo do Galo

ShaoZ

Power Member
Bem tive uma nova duvida e para não estar a abrir outro tópico decidi criar neste.

Eu estou a fazer um jogo do galo, o jogo já faz tudo menos mostrar o vencedor ou se há empate.

Eu criei uma matriz -----> Dim tabuleiro(2, 2) As String

E criei 9 botões e 9 labels, ao carregar no botão ele desaparece e aparece a label com o valor X ou O

Eu para o vencedor pus isto(1 exemplo) :

Código:
If (Label1.Text = Label5.Text And Label1.Text = Label9.Text And Label5.Text = Label9.Text) Then
            MsgBox("VENCEDOR")
End If
E no empate pus(1 exemplo):

Código:
If tabuleiro(0, 0) = tabuleiro(0, 1) And tabuleiro(0, 1) = tabuleiro(0, 2) Then
            MsgBox("Empate!", MsgBoxStyle.Information, "Jogo do Galo")
End If
Mas nenhum deles funciona, quer dizer aparece logo a msgbox ao abrir o form :-D
 
Sim porque, quando começas o jogo, as labels já têm todas o mesmo texto: "" (vazio). Acrescenta esta expressão à linha:

Código:
And Label1.Text <> ""
Depois tens que fazer o mesmo para as outras situações.

Podes também retirar a expressão inútil "Label5.Text = Label9.Text", pois se o 1 = 5 e o 1 = 9, então automaticamente o 5 = 9.

Quanto ao empate, não percebi porque fizeste dessa forma. Parece que isso apenas verifica se a primeira coluna/linha está toda igual...
 
codigo de um jogo em vb 2005 do galo

Código:
Public Class Form1

    Dim valor As Char = "O"


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.lbl_jog_X.Text = ""
        Me.lbl_jog_O.Text = ""
        Me.lbl_jog.Text = ""
        Me.btn_b00.Enabled = False
        Me.btn_b01.Enabled = False
        Me.btn_b02.Enabled = False
        Me.btn_b10.Enabled = False
        Me.btn_b11.Enabled = False
        Me.btn_b12.Enabled = False
        Me.btn_b20.Enabled = False
        Me.btn_b21.Enabled = False
        Me.btn_b22.Enabled = False

    End Sub

    Private Sub btn_sair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_sair.Click
        Close()
    End Sub

    Private Sub inverter(ByVal sender As System.Object)

        ' mudar o x par a O
        If valor = "X" Then
            valor = "O"
            sender.forecolor = Color.Black
            Me.lbl_jog.Text = "É o " & Me.txt_jogx.Text & "  A jogar"
        Else

            valor = "X"
            sender.forecolor = Color.Blue
            Me.lbl_jog.Text = "É o " & Me.txt_jogo.Text & "  A jogar"
        End If


    End Sub

    Private Sub verif()
        ' verificação de hipoteses possiveis de vitoria

        ' primeira linha

        If Me.btn_b00.Text = "X" And Me.btn_b01.Text = "X" And Me.btn_b02.Text = "X" Then

            ponto_X()
            limpar()

        End If

        If Me.btn_b00.Text = "O" And Me.btn_b01.Text = "O" And Me.btn_b02.Text = "O" Then

            ponto_O()
            limpar()

        End If

        ' segunda linha

        If Me.btn_b10.Text = "X" And Me.btn_b11.Text = "X" And Me.btn_b12.Text = "X" Then
            ponto_X()
            limpar()

        End If

        If Me.btn_b10.Text = "O" And Me.btn_b11.Text = "O" And Me.btn_b12.Text = "O" Then
            ponto_O()
            limpar()

        End If

        ' terceira linha

        If Me.btn_b20.Text = "X" And Me.btn_b21.Text = "X" And Me.btn_b22.Text = "X" Then

            ponto_X()
            limpar()

        End If

        If Me.btn_b20.Text = "O" And Me.btn_b21.Text = "O" And Me.btn_b22.Text = "O" Then


            ponto_O()
            limpar()

        End If

        ' primeira coluna

        If Me.btn_b00.Text = "X" And Me.btn_b10.Text = "X" And Me.btn_b20.Text = "X" Then

            ponto_X()
            limpar()

        End If

        If Me.btn_b00.Text = "O" And Me.btn_b10.Text = "O" And Me.btn_b20.Text = "O" Then
            ponto_O()
            limpar()
        End If

        ' segunda coluna

        If Me.btn_b01.Text = "X" And Me.btn_b11.Text = "X" And Me.btn_b21.Text = "X" Then

            ponto_X()
            limpar()

        End If

        If Me.btn_b01.Text = "O" And Me.btn_b11.Text = "O" And Me.btn_b21.Text = "O" Then
            ponto_O()
            limpar()
        End If

        ' treceira coluna

        If Me.btn_b02.Text = "X" And Me.btn_b12.Text = "X" And Me.btn_b22.Text = "X" Then

            ponto_X()
            limpar()
        End If

        If Me.btn_b02.Text = "O" And Me.btn_b12.Text = "O" And Me.btn_b22.Text = "O" Then
            ponto_O()
            limpar()
        End If

        ' diagonal 1

        If Me.btn_b00.Text = "X" And Me.btn_b11.Text = "X" And Me.btn_b22.Text = "X" Then

            ponto_X()
            limpar()

        End If

        If Me.btn_b00.Text = "O" And Me.btn_b11.Text = "O" And Me.btn_b22.Text = "O" Then

            ponto_O()
            limpar()

        End If


        ' diagonal 2

        If Me.btn_b02.Text = "X" And Me.btn_b11.Text = "X" And Me.btn_b20.Text = "X" Then

            ponto_X()
            limpar()


        End If

        If Me.btn_b02.Text = "O" And Me.btn_b11.Text = "O" And Me.btn_b20.Text = "O" Then
            ponto_O()
            limpar()

        End If
        empatar()

      


    End Sub

    Private Sub empatar()

        ' caso de ampate


        If Me.btn_b00.Text <> "" And Me.btn_b01.Text <> "" And Me.btn_b02.Text <> "" And _
           Me.btn_b10.Text <> "" And Me.btn_b11.Text <> "" And Me.btn_b12.Text <> "" And _
           Me.btn_b20.Text <> "" And Me.btn_b21.Text <> "" And Me.btn_b22.Text <> "" Then

            MsgBox(" Ninguem ganhou  nem o    " & Me.txt_jogx.Text & "   nem o   " & Me.txt_jogo.Text)
            limpar()

        End If
    End Sub
    Private Sub limpar()

        'limpa os botões

        '   Me.btn_b00.Text = ""
        '   Me.btn_b01.Text = ""
        '   Me.btn_b02.Text = ""
        '   Me.btn_b10.Text = ""
        '   Me.btn_b11.Text = ""
        '   Me.btn_b12.Text = ""
        '   Me.btn_b20.Text = ""
        '   Me.btn_b21.Text = ""
        '   Me.btn_b22.Text = ""

        For Each objecto As Control In Me.Controls

            If (objecto.GetType.Name.ToString = "Button") And objecto.Name <> "btn_sair" Then

                objecto.Text = ""

            End If

        Next

    

    End Sub


    Private Sub ponto_X()
        Me.lbl_jog_X.Text = Val(Me.lbl_jog_X.Text) + 1

        MsgBox(" o Jogador " & Me.txt_jogx.Text & "Ganhou")
    End Sub
    Private Sub ponto_O()
        Me.lbl_jog_O.Text = Val(Me.lbl_jog_O.Text) + 1

        MsgBox(" o Jogador " & Me.txt_jogo.Text & "Ganhou")
    End Sub
    Private Sub btn_b00_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_b00.Click, btn_b01.Click, btn_b02.Click, btn_b10.Click, btn_b11.Click _
    , btn_b12.Click, btn_b20.Click, btn_b21.Click, btn_b22.Click


        ' não deixa jogar enquanto não escrfever o nome dos jogadores 

        If Me.txt_jogo.Text = "" And Me.txt_jogx.Text = "" Then


        ElseIf sender.text = "" Then

            inverter(sender)
            sender.text = valor
            verif()
        End If

        Me.txt_jogo.Enabled = False
        Me.txt_jogx.Enabled = False

    


    End Sub


    'Private Sub btn_b01_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_b01.Click
    '    inverter(sender)
    '    Me.btn_b01.Text = valor
    '    Me.btn_b01.Enabled = False
    '    verif()

    'End Sub

    'Private Sub btn_b02_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_b02.Click
    '   inverter(sender)
    '  Me.btn_b02.Text = valor
    ' Me.btn_b02.Enabled = False
    'verif()

    'End Sub


    'Private Sub btn_b10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_b10.Click
    '   inverter(sender)
    '  Me.btn_b10.Text = valor
    ' Me.btn_b10.Enabled = False
    'verif()
    'End Sub

    'Private Sub btn_b11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_b11.Click
    '   inverter(sender)
    '  Me.btn_b11.Text = valor
    ' Me.btn_b11.Enabled = False
    'verif()
    'End Sub

    'Private Sub btn_b12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_b12.Click
    '   inverter(sender)
    '  Me.btn_b12.Text = valor
    ' Me.btn_b12.Enabled = False
    ' verif()
    ' End Sub

    'Private Sub btn_b20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_b20.Click
    '   inverter(sender)
    '  Me.btn_b20.Text = valor
    ' Me.btn_b20.Enabled = False
    'verif()
    ' End Sub

    'Private Sub btn_b21_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_b21.Click
    '   inverter(sender)
    '  Me.btn_b21.Text = valor
    ' Me.btn_b21.Enabled = False
    'verif()
    'End Sub

    'Private Sub btn_b22_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_b22.Click
    '   inverter(sender)
    '  Me.btn_b22.Text = valor
    ' Me.btn_b22.Enabled = False
    '  verif()
    'End Sub




    Private Sub txt_jogx_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_jogx.TextChanged, txt_jogo.TextChanged

        ' poe logo o nome escrito na caixa de texto

        If valor = "X" Then
            Me.lbl_jog.Text = " é o   " & Me.txt_jogo.Text & "   a Jogar"
        Else
            Me.lbl_jog.Text = " é o   " & Me.txt_jogx.Text & "   a Jogar"
        End If


        ' põe os botões a verdade caso as caixas de texto sejam diferentes de vazio
        If Me.txt_jogo.Text <> "" And Me.txt_jogx.Text <> "" Then

            Me.btn_b00.Enabled = True
            Me.btn_b01.Enabled = True
            Me.btn_b02.Enabled = True
            Me.btn_b10.Enabled = True
            Me.btn_b11.Enabled = True
            Me.btn_b12.Enabled = True
            Me.btn_b20.Enabled = True
            Me.btn_b21.Enabled = True
            Me.btn_b22.Enabled = True

        End If

    End Sub
End Class

____________________________________________________________________________

____________________________________________________________________________
descrição dos objectos do form


Código:
possição dos botões no form igual a posisção dos nomes
dos mesmos vista a baixo:

btn_b00 , btn_b01 ,btn_b02
btn_b10 , btn_b11 ,btn_b12
btn_b20 , btn_b21 ,btn_b22


[B]jogador x[/B]

txt_jogx - txt onde se se escreve o nome jogador x

lbl_jog_X - marca a pontuação desse jogador( 1pt por jogo)
so marca caso seja >0
[B]
jogador 0[/B]

txt_jog0 - txt onde se se escreve o nome jogador x

lbl_jog_0 - marca a pontuação desse jogador( 1pt por jogo) 
so marca caso seja >0


lbl_jog - diz qual é o jogador a jogar (durante o jogo)

aqui tens o link do design do form

[img=http://img178.imageshack.us/img178/4269/galoio4.th.jpg]

como não percebi bem o teu prob

mas aqui esta um jogo do galo feito em vb2005 para 2 jogadores

espero que ajude
 
Boas!

Uma sugestão, em vez de teres labels e botões à mistura, só te baralhas, até mesmo com o desenho enquanto programas..., usa apenas botões ou labels, não ambos.

No caso dos botões, basta alterares o caption do mesmo quando carregas nele, colocas o mesmo disabled e depois fazes a verificação se ganhou ou não.

No caso das labels, usa o evento on_click, alteras o text da label e fazes o mesmo com a verificação.

Terás de usar uma variável global para saberes se é o jogador X ou O que está a carregar no botão/label.

Mas não faças uma misturada das duas..., só te baralhas e a probabilidade de dar porcaria é maior.

Espero que ajude ;)

abraços, HecKel
 
Tal como o HecKel disse eu pus só com botões mas depois se o jogador 1 tiver carregado no botão 1 (X) e se fizer duplo clique no botão ele altera o valor para (O)

Será que há algum comando ou protecção para o utilizador só poder clicar uma vez no botão??
 
bem quando chegar a casa ja vou fazer isso e o jogo ja vai ficar completo, mas depois vou aplicar uns extras no jogo :-D

Depois se tiver duvidas dos extras que vou por eu pergunto aqui :lol:

Fikem bem e obrigado pela ajuda :)
 
Back
Topo