• Revista PROGRAMAR: Já está disponível a edição #53 da revista programar. Faz já o download aqui!

blck

Problema no Tic Tac Toe

4 mensagens neste tópico

Estou a fazer um jogo do galo isto não está nada de especial mas estou a usar imagens para X e o O e um método chamado GetCurrentDirectory()

Public Class Form1
    Dim TTT As String(,)
    Dim counter As Integer
    Dim xTurn As Boolean
    Dim Xpic As String
    Dim Opic As String
    Dim xWinCount As Integer = 0
    Dim oWinCount As Integer = 0
    Dim xWinLabel As String = "X"
    Dim oWInLabel As String = "O"



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TTT = New String(,) {{"a", "b", "c"}, {"d", "e", "f"}, {"g", "h", "i"}}
        xTurn = True
        Xpic = "\x.jpg"
        Opic = "\o.jpg"
        MsgBox("o = " & oWinCount)

    End Sub

    Private Sub pic02_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pic02.Click
        setPicture(pic02, 0, 2)
    End Sub

    Private Sub RestartToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Restart.Click
        Reset()
        xWinLabel = "x"
        oWInLabel = "o"
        Xpic = "\x.jpg"
        Opic = "\o.jpg"
    End Sub
    Private Sub checkForWinner()

        If TTT(0, 0) = TTT(0, 1) And TTT(0, 1) = TTT(0, 2) Then
            Winner(TTT(0, 0))
            Exit Sub
        End If


        If TTT(1, 0) = TTT(1, 1) And TTT(1, 1) = TTT(1, 2) Then
            Winner(TTT(1, 0))
            Exit Sub
        End If


        If TTT(2, 0) = TTT(2, 1) And TTT(2, 1) = TTT(2, 2) Then
            Winner(TTT(2, 0))
            Exit Sub
        End If


        If TTT(0, 0) = TTT(1, 0) And TTT(1, 0) = TTT(2, 0) Then
            Winner(TTT(0, 0))
            Exit Sub
        End If


        If TTT(0, 1) = TTT(1, 1) And TTT(1, 1) = TTT(2, 1) Then
            Winner(TTT(0, 1))
            Exit Sub
        End If


        If TTT(0, 2) = TTT(1, 2) And TTT(1, 2) = TTT(2, 2) Then
            Winner(TTT(0, 2))
            Exit Sub
        End If


        If TTT(0, 0) = TTT(1, 1) And TTT(1, 1) = TTT(2, 2) Then
            Winner(TTT(0, 0))
            Exit Sub
        End If


        If TTT(2, 0) = TTT(1, 1) And TTT(1, 1) = TTT(0, 2) Then
            Winner(TTT(2, 0))
        End If


        If counter >= 9 Then
            MsgBox("Nobody Wins")
        End If
    End Sub

    Private Sub NewGameToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewGameToolStripMenuItem.Click
        Reset()
    End Sub



    Private Sub setPicture(ByVal picbox As PictureBox, ByVal r As Integer, ByVal c As Integer)
        Dim returnValue As String
        If xTurn = True Then

            picbox.Image = Image.FromFile( _
          returnValue = directory.GetCurrentDirectory & Xpic)
            TTT(r, c) = "x"
        Else
            picbox.Image = Image.FromFile( _
              returnValue = directory.GetCurrentDirectory & Opic)
            TTT(r, c) = "o"
        End If


        xTurn = Not xTurn
        picbox.Enabled = False
        counter += 1
        checkForWinner()
    End Sub

    Private Sub pic00_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pic00.Click
        setPicture(pic00, 0, 0)
    End Sub


    Private Sub pic01_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pic01.Click
        setPicture(pic01, 0, 1)
    End Sub

    Private Sub pic10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pic10.Click
        setPicture(pic10, 1, 0)
    End Sub

    Private Sub pic11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pic11.Click
        setPicture(pic11, 1, 1)
    End Sub

    Private Sub pic12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pic12.Click
        setPicture(pic12, 1, 2)
    End Sub

    Private Sub pic20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pic20.Click
        setPicture(pic20, 2, 0)
    End Sub

    Private Sub pic21_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pic21.Click
        setPicture(pic21, 2, 1)
    End Sub

    Private Sub pic22_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pic22.Click
        setPicture(pic22, 2, 2)
    End Sub

    Private Sub FoodToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FoodToolStripMenuItem.Click

        Xpic = "\x.jpg"
        Opic = "\o.jpg"
        xWinLabel = "O"
        oWInLabel = "X"
    End Sub
    Private Sub StopGame()
        pic00.Enabled = False
        pic01.Enabled = False
        pic02.Enabled = False
        pic10.Enabled = False
        pic11.Enabled = False
        pic12.Enabled = False
        pic20.Enabled = False
        pic21.Enabled = False
        pic22.Enabled = False

    End Sub
    Private Sub Winner(ByVal value As String)
        If value = "x" Then
            MsgBox(xWinLabel & " is the Winner")
            xWinCount += 1
            lblXwins.Text = "x Wins: " & xWinCount
        Else
            MsgBox(oWInLabel & " is the Winner")
            oWinCount += 1
            lblOWins.Text = "O Wins: " & oWinCount
        End If
        StopGame()
    End Sub
    Private Sub Reset()
        counter = 0
        pic00.Image = Nothing
        pic01.Image = Nothing
        pic02.Image = Nothing
        pic10.Image = Nothing
        pic11.Image = Nothing
        pic12.Image = Nothing
        pic20.Image = Nothing
        pic21.Image = Nothing
        pic22.Image = Nothing
        pic00.Enabled = True
        pic01.Enabled = True
        pic02.Enabled = True
        pic10.Enabled = True
        pic11.Enabled = True
        pic12.Enabled = True
        pic20.Enabled = True
        pic21.Enabled = True
        pic22.Enabled = True

        TTT = New String(,) {{"a", "b", "c"}, {"d", "e", "f"}, {"g", "h", "i"}}
        xTurn = True
        counter = 0
    End Sub
End Class

Ele dá o seguinte problema: Name 'directory' is not declared.

Não percebo o porque deste erro :S.

Desde ja obrigado

0

Partilhar esta mensagem


Link para a mensagem
Partilhar noutros sites

Boas.

Não tenho a certeza.. mas acho que usando esse método é preciso fazeres import...

Experimenta fazeres : "Imports System.IO", antes da Public Class

0

Partilhar esta mensagem


Link para a mensagem
Partilhar noutros sites

Experimenta antes assim:

Private Sub setPicture(ByVal picbox As PictureBox, ByVal r As Integer, ByVal c As Integer)
        Dim returnValue As String
        If xTurn = True Then

            picbox.Image = Image.FromFile( _
          returnValue = Application.StartupPath() &  Xpic)
            TTT(r, c) = "x"
        Else
            picbox.Image = Image.FromFile( _
              returnValue = Application.StartupPath() & Opic)
            TTT(r, c) = "o"
        End If


        xTurn = Not xTurn
        picbox.Enabled = False
        counter += 1
        checkForWinner()
    End Sub

0

Partilhar esta mensagem


Link para a mensagem
Partilhar noutros sites

Crie uma conta ou ligue-se para comentar

Só membros podem comentar

Criar nova conta

Registe para ter uma conta na nossa comunidade. É fácil!


Registar nova conta

Entra

Já tem conta? Inicie sessão aqui.


Entrar Agora