Results 1 to 12 of 12

Thread: stupid array question

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Posts
    537
    hello
    I am making a tic tac toe game and am trying to streamline
    my code by using an array.
    I have 9 cmdButtons and am trying to run a sub that checks
    to see (across the top row)
    ---------------------------
    Private Sub Command1_Click(Index As Integer)
    cmdButton(index).caption="X"
    if cmdButton(1)+cmdButton(2)+cmdButton(3)=6
    then msgbox"you win
    endif
    end sub
    ---------------------------
    I know that is the wrong syntax but any suggestions would be great.
    I actually got the tic tac toe game to work but it is
    a million lines of if then statements,checking for every
    possible solution.
    thanks
    pnj

  2. #2
    Guest
    Code:
    If cmdButton(1).Caption & cmdButton(2).Caption & cmdButton(3).Caption = "XXX" Then MsgBox "You Win"

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Posts
    537

    thanks megatron

    is there a way to set up some type of loop/while statement
    so I can stream line my code?
    right now I have about 100 lines of if/then statements.
    I want to call a sub to run through the rows/colums to check for a winner.
    thanks.
    let me know if you need more info on what I am trying to do.
    thanks again.
    pnj

  4. #4
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Optimised Version

    You can probably get it smaller, but as a demo this is probably sufficient.

    To run this, you will need to post the entire contents of the code below into a new text file. Then save the file as form1.frm. Then you may open this in Vb and run it.

    Good Luck

    Code:
    VERSION 5.00
    Begin VB.Form Form1 
       Caption         =   "Form1"
       ClientHeight    =   5475
       ClientLeft      =   60
       ClientTop       =   345
       ClientWidth     =   7245
       LinkTopic       =   "Form1"
       ScaleHeight     =   5475
       ScaleWidth      =   7245
       StartUpPosition =   3  'Windows Default
       Begin VB.CommandButton cmdReset 
          Caption         =   "Reset Game"
          Height          =   375
          Left            =   480
          TabIndex        =   10
          Top             =   2880
          Width           =   1935
       End
       Begin VB.CommandButton cmdSquare 
          Caption         =   "3"
          Height          =   495
          Index           =   3
          Left            =   360
          TabIndex        =   8
          TabStop         =   0   'False
          Top             =   840
          Width           =   615
       End
       Begin VB.CommandButton cmdSquare 
          Caption         =   "6"
          Height          =   495
          Index           =   6
          Left            =   360
          TabIndex        =   7
          TabStop         =   0   'False
          Top             =   1440
          Width           =   615
       End
       Begin VB.CommandButton cmdSquare 
          Caption         =   "1"
          Height          =   495
          Index           =   1
          Left            =   1080
          TabIndex        =   6
          TabStop         =   0   'False
          Top             =   240
          Width           =   615
       End
       Begin VB.CommandButton cmdSquare 
          Caption         =   "4"
          Height          =   495
          Index           =   4
          Left            =   1080
          TabIndex        =   5
          TabStop         =   0   'False
          Top             =   840
          Width           =   615
       End
       Begin VB.CommandButton cmdSquare 
          Caption         =   "7"
          Height          =   495
          Index           =   7
          Left            =   1080
          TabIndex        =   4
          TabStop         =   0   'False
          Top             =   1440
          Width           =   615
       End
       Begin VB.CommandButton cmdSquare 
          Caption         =   "2"
          Height          =   495
          Index           =   2
          Left            =   1800
          TabIndex        =   3
          TabStop         =   0   'False
          Top             =   240
          Width           =   615
       End
       Begin VB.CommandButton cmdSquare 
          Caption         =   "5"
          Height          =   495
          Index           =   5
          Left            =   1800
          TabIndex        =   2
          TabStop         =   0   'False
          Top             =   840
          Width           =   615
       End
       Begin VB.CommandButton cmdSquare 
          Caption         =   "8"
          Height          =   495
          Index           =   8
          Left            =   1800
          TabIndex        =   1
          TabStop         =   0   'False
          Top             =   1440
          Width           =   615
       End
       Begin VB.CommandButton cmdSquare 
          Caption         =   "0"
          Height          =   495
          Index           =   0
          Left            =   360
          TabIndex        =   0
          TabStop         =   0   'False
          Top             =   240
          Width           =   615
       End
       Begin VB.Label lblTurn 
          Caption         =   "Move"
          Height          =   375
          Left            =   360
          TabIndex        =   9
          Top             =   2160
          Width           =   2055
       End
    End
    Attribute VB_Name = "Form1"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    Option Explicit
    Dim mTurn As String
    Dim mO As Integer ' score for O
    Dim mX As Integer ' score for X
    Dim mWinningScores(1 To 8) As Integer
    
    Private Enum WinningScores
      TopRow = 1 + 2 + 4
      MiddleRow = 8 + 16 + 32
      BottomRow = 64 + 128 + 256
      LeftColumn = 1 + 8 + 64
      MiddleColumn = 2 + 16 + 128
      RightColumn = 4 + 32 + 256
      TopLeftDiagonal = 1 + 16 + 256
      TopRightDiagonal = 4 + 16 + 64
    End Enum
    
    Private Sub Form_Load()
      ResetBoard
      
      ' set up the array of winning scores
      mWinningScores(1) = WinningScores.TopRow
      mWinningScores(2) = WinningScores.MiddleRow
      mWinningScores(3) = WinningScores.BottomRow
      mWinningScores(4) = WinningScores.LeftColumn
      mWinningScores(5) = WinningScores.MiddleColumn
      mWinningScores(6) = WinningScores.RightColumn
      mWinningScores(7) = WinningScores.TopLeftDiagonal
      mWinningScores(8) = WinningScores.TopRightDiagonal
    End Sub
    Private Sub cmdReset_Click()
      ResetBoard
    End Sub
    
    Private Sub cmdSquare_Click(Index As Integer)
      cmdSquare(Index).Caption = mTurn
      cmdSquare(Index).Enabled = False
      
      ' add the score to the player total and switch the turn
      If mTurn = "O" Then
        mO = mO + 2 ^ Index
        mTurn = "X"
      Else
        mX = mX + 2 ^ Index
        mTurn = "O"
      End If
      
      AdviseTurn
      CheckForWin
      CheckForGameOver
    End Sub
    
    
    Private Sub CheckForWin()
      'check to see if the last player to move just won
      If mTurn = "X" Then
        ' It is X's turn, so check O for a win
        If WinningScore(mO) Then
          MsgBox "O has won"
          GameOver
        End If
      Else
        ' It is O's turn, so check X for a win
        If WinningScore(mX) Then
          MsgBox "X has won"
          GameOver
        End If
      
      End If
    End Sub
    Private Function WinningScore(aScore As Integer) As Boolean
      ' there are more efficient way to do this, but for demo purposes I hope
      ' this is easier to follow
      Dim c As Integer
      For c = 1 To 8
        If (aScore And mWinningScores(c)) = mWinningScores(c) Then
          WinningScore = True
          Exit Function
        End If
      Next
    End Function
    Private Sub CheckForGameOver()
      If mO + mX = 511 Then
        MsgBox "There are no more moves possible"
      End If
    End Sub
    Private Sub ResetBoard()
      Dim c As Integer
      For c = 0 To 8
        cmdSquare(c).Enabled = True
        cmdSquare(c).Caption = ""
      Next
      
      ' reset turn and scores
      mTurn = "O"
      mO = 0
      mX = 0
      
      AdviseTurn
    End Sub
    Private Sub AdviseTurn()
      lblTurn = "It is " & mTurn & "'s turn"
    End Sub
    Private Sub GameOver()
      Dim c As Integer
      For c = 0 To 8
        cmdSquare(c).Enabled = False
      Next
    End Sub
    Paul Lewis

  5. #5
    Lively Member
    Join Date
    Sep 2000
    Location
    NC, USA
    Posts
    102

    Talking hmmmm

    I am quite sure this could be shortened even further but here is another way.

    Code:
    Dim XArray(2, 2) As String * 1
    
    Private Sub Command1_Click(Index As Integer)
        Command1(Index).Caption = "X"
        If Index = 0 Then XArray(0, 0) = "X"
        If Index = 1 Then XArray(0, 1) = "X"
        If Index = 2 Then XArray(0, 2) = "X"
        If Index = 3 Then XArray(1, 0) = "X"
        If Index = 4 Then XArray(1, 1) = "X"
        If Index = 5 Then XArray(1, 2) = "X"
        If Index = 6 Then XArray(2, 0) = "X"
        If Index = 7 Then XArray(2, 1) = "X"
        If Index = 8 Then XArray(2, 2) = "X"
        Dim iCntHor As Integer, iCntVer As Integer
        Dim Test As String
        For iCntHor = 0 To 2
            For iCntVer = 0 To 2
                Test = Test & XArray(iCntHor, iCntVer)
            Next
            If Test = "XXX" Then
                MsgBox "You Win"
                Unload Me
                Exit Sub
            End If
            Test = ""
        Next
        For iCntVer = 0 To 2
            For iCntHor = 0 To 2
                Test = Test & XArray(iCntHor, iCntVer)
            Next
            If Test = "XXX" Then
                MsgBox "You Win"
                Unload Me
                Exit Sub
            End If
            Test = ""
        Next
        If XArray(0, 0) & XArray(1, 1) & XArray(2, 2) = "XXX" Then
            MsgBox "You Win"
            Unload Me
            Exit Sub
        ElseIf XArray(0, 2) & XArray(1, 1) & XArray(2, 0) = "XXX" Then
            MsgBox "You Win"
            Unload Me
            Exit Sub
        End If
    End Sub
    Hope that helps,
    KillemAll

  6. #6
    Lively Member
    Join Date
    Sep 2000
    Location
    NC, USA
    Posts
    102

    Lightbulb

    That would have been much easier if I could have made a multidimisional array of command buttons. Does anyone know if there is some way to do that?

  7. #7
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    You have to do it manually

    This code will create and position the command buttons for you. What you want though is to have the Index Property of the command button to support multi-dimensions which I do not think you will be able to do.

    Code:
    ' form has one command button (command1) which has the 
    ' visible property = false and index=0
    
    Option Explicit
    Dim mMultiArray(1 To 3, 1 To 3) As CommandButton
    Private Sub Form_Load()
      ScaleMode = 3
      Dim x As Integer
      Dim y As Integer
      Dim myButton As CommandButton
      
      For y = 1 To 3
        For x = 1 To 3
          Load Command1((y - 1) * 3 + x)
          Set mMultiArray(x, y) = Command1((y - 1) * 3 + x)
          mMultiArray(x, y).Visible = True
          mMultiArray(x, y).Left = x * 50
          mMultiArray(x, y).Top = y * 50
          
        Next x
      Next y
      
    End Sub
    ' now you can loop through your buttons at will.
    Paul Lewis

  8. #8
    Lively Member
    Join Date
    Sep 2000
    Location
    NC, USA
    Posts
    102

    Thumbs down hehe, it should be so easy :D

    You have to dim withevents and it will not let you dim withevents a multidimitional array. Unless I am overlooking something. If anyone knows how you could have a multidimitional array of controls. please tell us.

    Thanks,
    KillemAll

  9. #9
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    There is only 8 ways to win, so this could work, every time your press a button
    Code:
    'in a button
    call checkForWinner
    
    Public Sub checkForWinner()
       If cmd(1).caption = cmd(2).caption and cmd(2).caption = cmd(3).caption then msgbox "You Win!" 
    
    'that checks one way, so you only need 8 lines to check for a winner
    'unless you have an extended if...
    End Sub

  10. #10
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Not sure I follow...

    KillemAll,

    Not sure I follow you... Why do you think you must decalare the array of Controls (it's not a Control Array remember) with the WithEvents keyword?

    What is it you are missing out on that you need?

    Perhaps if you add the following to the sample I posted earlier, you will see that I still recieve events on the click event.

    Code:
    Private Sub Command1_Click(Index As Integer)
    
      Dim x As Integer
      Dim y As Integer
      x = (Index - 1) Mod 3 + 1
      y = Int((Index - 1) / 3) + 1
      MsgBox "You clicked button " & Index & vbCrLf & "Which has a coordinate of (" & x & "," & y & ")"
      
    End Sub
    What benefit are you after (if it were possible to have a multi-dimensional Control Array)

    Regards
    Paul Lewis

  11. #11
    Lively Member
    Join Date
    Sep 2000
    Location
    NC, USA
    Posts
    102

    Red face hehehe, I humbly beg your forgiveness...

    Points all well taken there Paul. Had a brain siezure or something. I didn't see that you have basically created a multi dimintional array of controls with your code. I will have to file that away for future use. The only advantage I was looking for was to loop through the columns and rows of buttons. I tried to do the same thing you did but I did not have the Load and Set statements. So I figured you could not use the events of them. I figured you had to dim them "WithEvents". Thanks again for straightening me out.

    here is my revised attempt based on your code.

    Code:
    Option Explicit
    Dim mMultiArray(1 To 3, 1 To 3) As CommandButton
    Private Sub Form_Load()
      ScaleMode = 3
      Dim x As Integer
      Dim y As Integer
      Dim myButton As CommandButton
      
      For y = 1 To 3
        For x = 1 To 3
          Load Command1((y - 1) * 3 + x)
          Set mMultiArray(x, y) = Command1((y - 1) * 3 + x)
          mMultiArray(x, y).Visible = True
          mMultiArray(x, y).Left = x * 50
          mMultiArray(x, y).Top = y * 50
          mMultiArray(x, y).Caption = "-"
        Next x
      Next y
      
    End Sub
    Private Sub Command1_Click(Index As Integer)
      Dim x As Integer
      Dim y As Integer
      x = (Index - 1) Mod 3 + 1
      y = Int((Index - 1) / 3) + 1
      Command1(Index).Caption = "X"
      Dim iCntHor As Integer, iCntVer As Integer
      Dim Test As String
        For iCntHor = 1 To 3
            For iCntVer = 1 To 3
                Test = Test & mMultiArray(iCntHor, iCntVer).Caption
            Next
            If Test = "XXX" Then
                MsgBox "You Win"
                Unload Me
                Exit Sub
            End If
            Test = ""
        Next
        For iCntVer = 1 To 3
            For iCntHor = 1 To 3
                Test = Test & mMultiArray(iCntHor, iCntVer).Caption
            Next
            If Test = "XXX" Then
                MsgBox "You Win"
                Unload Me
                Exit Sub
            End If
            Test = ""
        Next
        If mMultiArray(1, 1).Caption & mMultiArray(2, 2).Caption & mMultiArray(3, 3).Caption = "XXX" Then
            MsgBox "You Win"
            Unload Me
            Exit Sub
        ElseIf mMultiArray(1, 3).Caption & mMultiArray(2, 2).Caption & mMultiArray(3, 1).Caption = "XXX" Then
            MsgBox "You Win"
            Unload Me
            Exit Sub
        End If
    End Sub

  12. #12
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Wink No problem

    I thought you might have been having another seizure but I had to be sure it wasn't me instead

    Cheers
    Paul Lewis

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width