Jogo da Velha [VB.net]

5 respostas
L

Olá pessoal...

Minha professora de VB.net passou para fazermos ou um jogo da velha ou um jogo da forca.

Bem, ja consegui fazer o jogo da forca, mas agora à nivel de aprendizagem, gostaria de fazer o jogo da velha.

O propósito do jogo é usar array.

Um dos problemas que encontrei logo de cara, é que não posso fazer isso no VB.net

For i = i To 9

        Label(i).Caption = " "

    Next i

Pois no VB.net ele não reconhece essa classe "Label".

O que eu poderia fazer para resolver essa situação?

Por que de inicial eu faria isso:

dim player_1_turn as boolean

 

Private Sub Command1_Click()

    For i = i To 9

        Label(i).Caption = " "

    Next i

End Sub

Private Sub Label_Click(Index As Integer)

    Label(Index).caption = IIf(player_1_turn = True, "X", "O"): player_1_turn = Not player_1_turn

     

If Label(1).caption = Label(2).caption = Label(3).caption Or Label(4).caption = Label(5).caption = Label(6).caption Or _

    Label(7).caption = Label(8).caption = Label(9).caption Or Label(1).caption = Label(4).caption = Label(7).caption Or _

    Label(2).caption = Label(5).caption = Label(8).caption Or Label(3).caption = Label(6).caption = Label(9).caption Or _

    Label(1).caption = Label(5).caption = Label(9).caption Or Label(7).caption = Label(5).caption = Label(3).caption Then

    MsgBox ("Game over." & Chr(13) & "Player " & IIf(player_1_turn = True, 2, 1) & " é o vencedor !")

End If

End Sub

5 Respostas

I

Vê se isto te ajuda: http://www.macoratti.net/10/07/vbn_jv.htm

L

Já dei uma olhada nesse projeto, só que ele não utiliza arrays.

Esse é o problema…

I

É eu imaginei isto :oops:

W

Soh uma observacao.

Isso nao vai funcionar porque vc nao pode definir uma variavel usando uma palavra reservada da linguagem, no caso Label.

For i = i To 9
    Label(i).Caption = " "
Next i

Vc disse que ele nao reconhece a classe Label. Reconhece sim, mas como eu disse Label eh um web server control da API, vc nao pode usar o mesmo nome
para uma variavel de instancia.

Eu programo em C#, mas ate onde eu sei, em VB.NET, suponha que vc queira criar um array com 6 objetos Label, vc faria algo assim:

Dim LabelArray(5) As Label

For i = 0 To 5
     LabelArray(i) = New Label()
     LabelArray(i).Text = "Label : " & i.ToString
Next

Outra coisa, faz alguma coisa a respeito desse IF aqui:

If Label(1).caption = Label(2).caption = Label(3).caption Or Label(4).caption = Label(5).caption = Label(6).caption Or _    
    Label(7).caption = Label(8).caption = Label(9).caption Or Label(1).caption = Label(4).caption = Label(7).caption Or _ 
    Label(2).caption = Label(5).caption = Label(8).caption Or Label(3).caption = Label(6).caption = Label(9).caption Or _
    Label(1).caption = Label(5).caption = Label(9).caption Or Label(7).caption = Label(5).caption = Label(3).caption Then  
  
    MsgBox ("Game over." & Chr(13) & "Player " & IIf(player_1_turn = True, 2, 1) & " é o vencedor !")  
  
End If

Nao da pra ficar assim!!! Da pra vc fazer as regras de termino do jogo bem mais simples que isso.

//Daniel

L

windsofhell:
Soh uma observacao.

Isso nao vai funcionar porque vc nao pode definir uma variavel usando uma palavra reservada da linguagem, no caso Label.

For i = i To 9
    Label(i).Caption = " "
Next i

Vc disse que ele nao reconhece a classe Label. Reconhece sim, mas como eu disse Label eh um web server control da API, vc nao pode usar o mesmo nome
para uma variavel de instancia.

Eu programo em C#, mas ate onde eu sei, em VB.NET, suponha que vc queira criar um array com 6 objetos Label, vc faria algo assim:

Dim LabelArray(5) As Label

For i = 0 To 5
     LabelArray(i) = New Label()
     LabelArray(i).Text = "Label : " & i.ToString
Next

Outra coisa, faz alguma coisa a respeito desse IF aqui:

If Label(1).caption = Label(2).caption = Label(3).caption Or Label(4).caption = Label(5).caption = Label(6).caption Or _    
    Label(7).caption = Label(8).caption = Label(9).caption Or Label(1).caption = Label(4).caption = Label(7).caption Or _ 
    Label(2).caption = Label(5).caption = Label(8).caption Or Label(3).caption = Label(6).caption = Label(9).caption Or _
    Label(1).caption = Label(5).caption = Label(9).caption Or Label(7).caption = Label(5).caption = Label(3).caption Then  
  
    MsgBox ("Game over." & Chr(13) & "Player " & IIf(player_1_turn = True, 2, 1) & " é o vencedor !")  
  
End If

Nao da pra ficar assim!!! Da pra vc fazer as regras de termino do jogo bem mais simples que isso.

//Daniel

Valeu pela idéia, não sabia que tinha como fazer dessa forma.

Consegui fazer desse modo:

Public Class Form3 Dim aux, aux1 As String Dim i, y, k, c1, c2 As Integer Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click aux = sender.text Lbl_c1.Text = c1 Lbl_c2.Text = c2 i = i + 1 k = k + 1 Select Case aux Case " " y = i Mod 2 If y = 0 Then Button1.Text = "X" Lbl_indic1.Visible = True Lbl_indic2.Visible = False Else Button1.Text = "O" Lbl_indic1.Visible = False Lbl_indic2.Visible = True End If Case " " y = i Mod 2 If y = 0 Then Button2.Text = "X" Lbl_indic1.Visible = True Lbl_indic2.Visible = False Else Button2.Text = "O" Lbl_indic1.Visible = False Lbl_indic2.Visible = True End If Case " " y = i Mod 2 If y = 0 Then Button3.Text = "X" Lbl_indic1.Visible = True Lbl_indic2.Visible = False Else Button3.Text = "O" Lbl_indic1.Visible = False Lbl_indic2.Visible = True End If Case " " y = i Mod 2 If y = 0 Then Button4.Text = "X" Lbl_indic1.Visible = True Lbl_indic2.Visible = False Else Button4.Text = "O" Lbl_indic1.Visible = False Lbl_indic2.Visible = True End If Case " " y = i Mod 2 If y = 0 Then Button5.Text = "X" Lbl_indic1.Visible = True Lbl_indic2.Visible = False Else Button5.Text = "O" Lbl_indic1.Visible = False Lbl_indic2.Visible = True End If Case " " y = i Mod 2 If y = 0 Then Button6.Text = "X" Lbl_indic1.Visible = True Lbl_indic2.Visible = False Else Button6.Text = "O" Lbl_indic1.Visible = False Lbl_indic2.Visible = True End If Case " " y = i Mod 2 If y = 0 Then Button7.Text = "X" Lbl_indic1.Visible = True Lbl_indic2.Visible = False Else Button7.Text = "O" Lbl_indic1.Visible = False Lbl_indic2.Visible = True End If Case " " y = i Mod 2 If y = 0 Then Button8.Text = "X" Lbl_indic1.Visible = True Lbl_indic2.Visible = False Else Button8.Text = "O" Lbl_indic1.Visible = False Lbl_indic2.Visible = True End If Case " " y = i Mod 2 If y = 0 Then Button9.Text = "X" Lbl_indic1.Visible = True Lbl_indic2.Visible = False Else Button9.Text = "O" Lbl_indic1.Visible = False Lbl_indic2.Visible = True End If End Select If Button1.Text = Button2.Text And Button2.Text = Button3.Text Then If Button1.Text = "O" Then aux1 = Lbl_nome1.Text c1 = c1 + 1 Else aux1 = Lbl_nome2.Text c2 = c2 + 1 End If MsgBox("Jogador " & aux1 & " venceu!") Lbl_indic1.Visible = False Lbl_indic2.Visible = False Button1.Text = " " Button2.Text = " " Button3.Text = " " Button4.Text = " " Button5.Text = " " Button6.Text = " " Button7.Text = " " Button8.Text = " " Button9.Text = " " k = 0 ElseIf Button4.Text = Button5.Text And Button5.Text = Button6.Text Then If Button4.Text = "O" Then aux1 = Lbl_nome1.Text c1 = c1 + 1 Else aux1 = Lbl_nome2.Text c2 = c2 + 1 End If MsgBox("Jogador " & aux1 & " venceu") Lbl_indic1.Visible = False Lbl_indic2.Visible = False Button1.Text = " " Button2.Text = " " Button3.Text = " " Button4.Text = " " Button5.Text = " " Button6.Text = " " Button7.Text = " " Button8.Text = " " Button9.Text = " " k = 0 ElseIf Button7.Text = Button8.Text And Button8.Text = Button9.Text Then If Button7.Text = "O" Then aux1 = Lbl_nome1.Text c1 = c1 + 1 Else aux1 = Lbl_nome2.Text c2 = c2 + 1 End If MsgBox("Jogador " & aux1 & " venceu") Lbl_indic1.Visible = False Lbl_indic2.Visible = False Button1.Text = " " Button2.Text = " " Button3.Text = " " Button4.Text = " " Button5.Text = " " Button6.Text = " " Button7.Text = " " Button8.Text = " " Button9.Text = " " k = 0 ElseIf Button1.Text = Button4.Text And Button4.Text = Button7.Text Then If Button1.Text = "O" Then aux1 = Lbl_nome1.Text c1 = c1 + 1 Else aux1 = Lbl_nome2.Text c2 = c2 + 1 End If MsgBox("Jogador " & aux1 & " venceu") Lbl_indic1.Visible = False Lbl_indic2.Visible = False Button1.Text = " " Button2.Text = " " Button3.Text = " " Button4.Text = " " Button5.Text = " " Button6.Text = " " Button7.Text = " " Button8.Text = " " Button9.Text = " " k = 0 ElseIf Button2.Text = Button5.Text And Button5.Text = Button8.Text Then If Button2.Text = "O" Then aux1 = Lbl_nome1.Text c1 = c1 + 1 Else aux1 = Lbl_nome2.Text c2 = c2 + 1 End If MsgBox("Jogador " & aux1 & " venceu") Lbl_indic1.Visible = False Lbl_indic2.Visible = False Button1.Text = " " Button2.Text = " " Button3.Text = " " Button4.Text = " " Button5.Text = " " Button6.Text = " " Button7.Text = " " Button8.Text = " " Button9.Text = " " k = 0 ElseIf Button3.Text = Button6.Text And Button6.Text = Button9.Text Then If Button3.Text = "O" Then aux1 = Lbl_nome1.Text c1 = c1 + 1 Else aux1 = Lbl_nome2.Text c2 = c2 + 1 End If MsgBox("Jogador " & aux1 & " venceu") Lbl_indic1.Visible = False Lbl_indic2.Visible = False Button1.Text = " " Button2.Text = " " Button3.Text = " " Button4.Text = " " Button5.Text = " " Button6.Text = " " Button7.Text = " " Button8.Text = " " Button9.Text = " " k = 0 ElseIf Button1.Text = Button5.Text And Button5.Text = Button9.Text Then If Button1.Text = "O" Then aux1 = Lbl_nome1.Text c1 = c1 + 1 Else aux1 = Lbl_nome2.Text c2 = c2 + 1 End If MsgBox("Jogador " & aux1 & " venceu") Lbl_indic1.Visible = False Lbl_indic2.Visible = False Button1.Text = " " Button2.Text = " " Button3.Text = " " Button4.Text = " " Button5.Text = " " Button6.Text = " " Button7.Text = " " Button8.Text = " " Button9.Text = " " k = 0 ElseIf Button3.Text = Button5.Text And Button5.Text = Button7.Text Then If Button3.Text = "O" Then aux1 = Lbl_nome1.Text c1 = c1 + 1 Else aux1 = Lbl_nome2.Text c2 = c2 + 1 End If MsgBox("Jogador " & aux1 & " venceu") Lbl_indic1.Visible =true Lbl_indic2.Visible = False Button1.Text = " " Button2.Text = " " Button3.Text = " " Button4.Text = " " Button5.Text = " " Button6.Text = " " Button7.Text = " " Button8.Text = " " Button9.Text = " " k = 0 ElseIf k = 9 Then MsgBox("Deu velha!") Lbl_indic1.Visible = True Lbl_indic2.Visible = False Button1.Text = " " Button2.Text = " " Button3.Text = " " Button4.Text = " " Button5.Text = " " Button6.Text = " " Button7.Text = " " Button8.Text = " " Button9.Text = " " k = 0 End If End Sub

Criado 29 de novembro de 2011
Ultima resposta 30 de nov. de 2011
Respostas 5
Participantes 3