Need Help!

Forum Listing » Coding Forum » View Topic

  • This is a simple Tic-Tac-Toe game. I have it to where the player can place down X's, but for some reason the computer O won't go. Do any of you see the problem?


    GraphicsWindow.Hide()

    gw=350

    gh=350

    gridX=25

    gridY=25

    cellSize=100

    GraphicsWindow.CanResize="False"

    GraphicsWindow.Width=gw

    GraphicsWindow.Height=gh

    GraphicsWindow.Top=(Desktop.Height-gh)/2

    GraphicsWindow.Left=(Desktop.Width-gw)/2

    GraphicsWindow.Title="TK Games - Tic-Tac-Toe"

    GraphicsWindow.Show()

    playerTurn="True"

    GraphicsWindow.MouseMove=OnMouseMove

    GraphicsWindow.MouseDown=OnMouseDown

    SetupBoard()

    gameLoop:

    CheckGameOver()

    If gameOver="True" Then

      Goto endGame

    EndIf

    If playerTurn="True" Then

      Program.Delay(1000)

      Goto gameLoop

    EndIf

    TryWin()

    If compRow<>-1 And compCol<>-1 Then

      playComputerTurn()

      Goto gameCheck

    EndIf

    TryBlock()

    If compRow<>-1 And compCol<>-1 Then

      PlayComputerTurn()

      Goto gameCheck

    EndIf

    If cells[1][1]="" Then

      compRow=1

      compCol=1

      PlayComputerTurn()

      Goto gameCheck

    EndIf

    PickCorner()

    If compRow<>-1 And compCol<>-1 Then

      PlayComputerTurn()

      Goto gameCheck

    EndIf

    PickAny()

    If compRow<>-1 And compCol<>-1 Then

      Goto gameCheck

    EndIf

    gameCheck:

    CheckGameOver()

    If gameOver<>"True" Then

      Goto gameLoop

    EndIf

    endGame:

    If playerWins="True" Then

      GraphicsWindow.ShowMessage("You Win!","Game Over")

      Program.End()

    ElseIf computerWins="True" Then

      GraphicsWindow.ShowMessage("Computer Wins!","Game Over")

      Program.End()

    Else

      GraphicsWindow.ShowMessage("Tie Game!","Game Over")

      Program.End()

    EndIf

    Sub TryWin

      compRow=-1

      compCol=-1

      For i=0 to 2

        If cells[i][0]="O" And cells[i]="O" And cells[i][2]="" Then

          compRow=i

          compCol=2

        ElseIf cells[i][0]="0" And cells[i][1]="" And cells[i][2]="O" Then

          compRow=i

          compCol=1

        ElseIf cells[i][0]="" And cells[i][1]="O" And cells[i][2]="O" Then

          compRow=i

          compCol=0

        ElseIf cells[0][i]="O" And cells[1][i]="O" And cells[2][i]="" Then

          compCol=i

          compRow=2

        ElseIf cells[0][i]="O" And cells[1][i]="" And cells[2][i]="O" Then

          compCol=i

          compRow=1

        ElseIf cells[0][i]="" And cells[1][i]="O" And cells[2][i]="O" Then

          compCol=i

          compRow=0

        EndIf

      EndFor

      If cells[0][0]="O" And cells[1][1]="O" And cells[2][2]="" Then

        compCol=2

        compRow=2

      ElseIf cells[0][0]="O" And cells[1][1]="" And cells[2][2]="O" Then

        compCol=1

        compRow=1

      ElseIf cells[0][0]="" And cells[1][1]="O" And cells[2][2]="O" Then

        compCol=0

        compRow=0

      EndIf

    EndSub

    Sub TryBlock

      compRow=-1

      compCol=-1

      For i=0 to 2

        If cells[i][0]="X" And cells[i][1]="X" And cells[i][2]="" Then

          compRow=i

          compCol=2

        ElseIf cells[i][0]="X" And cells[i][1]="" And cells[i][2]="X" Then

          compRow=i

          compCol=1

        ElseIf cells[i][0]="" And cells[i][1]="X" And cells[i][2]="X" Then

          compRow=i

          compCol=0

        ElseIf cells[0][i]="X" And cells[1][i]="X" And cells[2][i]="" Then

          compCol=i

          compRow=2

        ElseIf cells[0][i]="X" And cells[1][i]="" And cells[2][i]="X" Then

          compCol=i

          compRow=1

        ElseIf cells[0][i]="" And cells[1][i]="X" And cells[2][i]="X" Then

          compCol=i

          compRow=0

        EndIf

      EndFor

      If cells[0][0]="X" And cells[1][1]="X" And cells[2][2]="" Then

        compCol=2

        compRow=2

      ElseIf cells[0][0]="X" And cells[1][1]="" And cells[2][2]="X" Then

        compCol=1

        compRow=1

      ElseIf cells[0][0]="" And cells[1][1]="X" And cells[2][2]="X" Then

        compCol=0

        compRow=0

      EndIf

    EndSub

    Sub CheckGameOver

      For i=0 to 2

        If cells[i][0]="X" And cells[i][1]="X" And cells[i][2]="X" Then

          playerWins="True"

          gameOver="True"

        ElseIf cells[i][0]="O" And cells[i][1]="O" And cells[i][2]="O" Then

          computerWins="True"

          gameOver="True"

        ElseIf cells[0][i]="X" And cells[1][i]="X" And cells[2][i]="X" Then

          playerWins="True"

          gameOver="True"

        ElseIf cells[0][i]="O" And cells[1][i]="O" And cells[2][i]="O" Then

          computerWins="True"

          gameOver="True"

        EndIf

      EndFor

      If cells[0][0]="X" And cells[1][1]="X" And cells[2][2]="X" Then

        playerWins="True"

        gameOver="True"

      ElseIf cells[0][0]="O" And cells[1][1]="O" And cells[2][2]="O" Then

        computerWins="True"

        gameOver="True"

      ElseIf cells[0][2]="O" And cells[1][1]="O" And cells[2][0]="O" Then

        computerWins="True"

        gameOver="True"

      ElseIf cells[0][2]="X" And cells [1][1]="X" And cells[2][0]="X" Then

        playerWins="True"

        gameOver="True"

      EndIf

      If gameOver<>"True" Then

        gameOver="True"

        For i=0 to 2

          For j=0 To 2

            If cells[i][j]="" Then

              gameOver="False"

            EndIf

          EndFor

        EndFor

      EndIf

    EndSub

    Sub PickCorner

      If cells[0][0]="X" And cells[2][2]="" Then

        compRow=2

        compCol=2

      ElseIf cells[0][2]="X" And cells[2][0]="" Then

        compRow=2

        compCol=0

      ElseIf cells[2][0]="X" And cells[0][2]="" Then

        compRow=0

        compCol=2

      ElseIf cells[2][2]="X" And cells[0][0]="" Then

        compRow=2

        compCol=2

      ElseIf cells[0][0]="" Then

        compRow=0

        compCol=0

      ElseIf cells[0][2]="" Then

        compRow=0

        compCol=2

      ElseIf cells[2][0]="" Then

        compRow=2

        compCol=0

      ElseIf cells[2][2]="" Then

        compRow=2

        compCol=2

      EndIf

    EndSub

    Sub PickAny

      For i=0 to 2

        For j=0 To 2

          If cells[i][j]="" Then

            compRow=i

            compCol=j

            Goto endPickAny

          EndIf

        EndFor

      EndFor

    endPickAny:

    EndSub

    Sub PlayComputerTurn

      r=compRow

      c=compCol

      cells[r][c]="O"

      GraphicsWindow.BrushColor="Black"

      GraphicsWindow.FontSize=40

      GraphicsWindow.FontBold="False"

      cellText=Shapes.AddText("O")

      Shapes.Move(cellText,c*cellSize+gridX+40,r*cellSize+gridY+26)

      playerTurn="True"

    EndSub

    Sub OnMouseDown

      If playerTurn="True" Then

        mx=GraphicsWindow.MouseX

        my=GraphicsWindow.MouseY

        If mx>gridX And my>gridY And mx<(gridX+3*cellSize) And my<(gridY+3*cellSize) Then

          c=Math.Floor((mx-gridX)/cellSize)

          r=Math.Floor((my-gridY)/cellSize)

          If cells[r][c]="" Then

            cells[r][c]="X"

            GraphicsWindow.BrushColor="Black"

            GraphicsWindow.FontSize=40

            GraphicsWindow.FontBold="False"

            cellText=Shapes.AddText("X")

            Shapes.Move(cellText,c*cellSize+gridX+40,r*cellSize+gridY+26)

          EndIf

        EndIf

      EndIf

    EndSub

    Sub OnMouseMove

      If playerTurn="True" Then

        hover="False"

        DrawCell()

        mx=GraphicsWindow.MouseX

        my=GraphicsWindow.MouseY

        If mx>gridX And my>gridY And mx<(gridX+3*cellSize) And my<(gridY+3*cellSize) Then

          col=Math.Floor((mx-gridX)/cellSize)

          row=Math.Floor((my-gridY)/cellSize)

          hover="True"

          DrawCell()

        EndIf

      EndIf

    EndSub

    Sub SetupBoard

      hover="False"

      For col=0 to 2

        For row=0 To 2

          DrawCell()

        EndFor

      EndFor

      row=-1

      col=-1

    EndSub

    Sub DrawCell

      If row>=0 And col>=0 And row<=2 And col<=2 Then

        x=col*cellSize+gridX

        y=row*cellSize+gridY

        If hover="True" Then

          GraphicsWindow.BrushColor="Azure"

        Else

          GraphicsWindow.BrushColor="White"

        EndIf

        GraphicsWindow.FillRectangle(x,y,cellSize,cellSize)

        GraphicsWindow.PenColor="Black"

        GraphicsWindow.DrawRectangle(x,y,cellSize,cellSize)

      EndIf

    EndSub   

  • It doesn't look like you ever set it to not do the player's turn in the gameLoop code, which is reiterated each time the player takes their turn. Thus, nothing else happens, if I'm reading your code correctly.

  • OMG THANK YOU! I was so frustrated last night lol. Worked :)

  • Whoa, nice catch, Link!

  • glad I could help!

  • didn't actually read through it myself, my eyes are bothering me, but glad that link was able to help you DBGOD

People Online