Jump to content

Problema no Tic Tac Toe


blck

Recommended Posts

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

Link to comment
Share on other 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

"Nós somos o que fazemos repetidamente, a excelência não é um feito, e sim, um hábito."
Não respondo a questões por PM que possam ser colocadas no fórum!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.