Jump to content
Sign in to follow this  
DHPW

passing an array with more dimensions to a function

Recommended Posts

DHPW

So I am trying to make a Tic Tac Toe Game, but I kind of got stuck at passing an array with 2-dimensions.

#include
#include
#include
#include

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Tic Tac Toe", 186, 201, 192, 124)
$A11 = GUICtrlCreateButton("", 16, 24, 25, 25)
$A12 = GUICtrlCreateButton("", 61, 24, 25, 25)
$A13 = GUICtrlCreateButton("", 100, 24, 25, 25)
$A21 = GUICtrlCreateButton("", 16, 66, 25, 25)
$A22 = GUICtrlCreateButton("", 61, 66, 25, 25)
$A23 = GUICtrlCreateButton("", 100, 66, 25, 25)
$A31 = GUICtrlCreateButton("", 16, 118, 25, 25)
$A32 = GUICtrlCreateButton("", 61, 118, 25, 25)
$A33 = GUICtrlCreateButton("", 100, 118, 25, 25)
$Reset = GUICtrlCreateButton("Reset", 16, 168, 75, 25)
$Label1 = GUICtrlCreateLabel("Score:", 144, 8, 35, 17)
$Label2 = GUICtrlCreateLabel("X:", 152, 24, 14, 17)
$Label3 = GUICtrlCreateLabel("O:", 152, 40, 15, 17)
$Xscore = GUICtrlCreateLabel("0", 168, 24, 10, 17)
$Oscore = GUICtrlCreateLabel("0", 168, 40, 10, 17)
$NewGame = GUICtrlCreateButton("New Game", 96, 168, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

$Turn = 1

#region Button Array
Global $ButtonArray[4][4]
;TOP row
$ButtonArray[1][1] = $A11
$ButtonArray[1][2] = $A12
$ButtonArray[1][3] = $A13
;MIDDLE row
$ButtonArray[2][1] = $A21
$ButtonArray[2][2] = $A22
$ButtonArray[2][3] = $A23
;BOTTOM row
$ButtonArray[3][1] = $A31
$ButtonArray[3][2] = $A32
$ButtonArray[3][3] = $A33
#endregion Button Array

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $A11
            XorO($Turn,$ButtonArray[1][1])
    EndSwitch
WEnd

Func XorO($Turn,ByRef $Btn)
    Local $array[$x][$y] = $Btn
    Switch $x
        Case 1
            $XorO_X = 16
        Case 2
            $XorO_X = 61
        Case 3
            $XorO_X = 100
    EndSwitch
    Switch $y
        Case 1
            $XorO_Y = 24
        Case 2
            $XorO_Y = 66
        Case 3
            $XorO_Y = 118
    EndSwitch

    If Mod($Turn,2) = 1 Then
        GUICtrlDelete($Btn)
        GUICtrlCreateLabel("X", $XorO_X,$XorO_Y)
    Else
        GUICtrlDelete($Btn)
        GUICtrlCreateLabel("O", $XorO_X,$XorO_Y)
    EndIf
    $Turn += 1
EndFunc

Basically I want to use the location of the $ButtonArray to use it in the array which would then specify the Location of the "X" or "O".

It would be great if some one can explain how to send an Array with more dimensions into a function. And how to declare a function with an Array of multiply dimensions.

Edited by DHPW

Share this post


Link to post
Share on other sites
jdelaney

Here ya go...works for the label/button replace...:

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Tic Tac Toe", 186, 201, 192, 124)
$A11 = GUICtrlCreateButton("", 16, 24, 25, 25)
$A12 = GUICtrlCreateButton("", 61, 24, 25, 25)
$A13 = GUICtrlCreateButton("", 100, 24, 25, 25)
$A21 = GUICtrlCreateButton("", 16, 66, 25, 25)
$A22 = GUICtrlCreateButton("", 61, 66, 25, 25)
$A23 = GUICtrlCreateButton("", 100, 66, 25, 25)
$A31 = GUICtrlCreateButton("", 16, 118, 25, 25)
$A32 = GUICtrlCreateButton("", 61, 118, 25, 25)
$A33 = GUICtrlCreateButton("", 100, 118, 25, 25)
$Reset = GUICtrlCreateButton("Reset", 16, 168, 75, 25)
$Label1 = GUICtrlCreateLabel("Score:", 144, 8, 35, 17)
$Label2 = GUICtrlCreateLabel("X:", 152, 24, 14, 17)
$Label3 = GUICtrlCreateLabel("O:", 152, 40, 15, 17)
$Xscore = GUICtrlCreateLabel("0", 168, 24, 10, 17)
$Oscore = GUICtrlCreateLabel("0", 168, 40, 10, 17)
$NewGame = GUICtrlCreateButton("New Game", 96, 168, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $Turn = 1
#region Button Array
Global $ButtonArray[4][4]
;TOP row
$ButtonArray[1][1] = $A11
$ButtonArray[1][2] = $A12
$ButtonArray[1][3] = $A13
;MIDDLE row
$ButtonArray[2][1] = $A21
$ButtonArray[2][2] = $A22
$ButtonArray[2][3] = $A23
;BOTTOM row
$ButtonArray[3][1] = $A31
$ButtonArray[3][2] = $A32
$ButtonArray[3][3] = $A33
#endregion Button Array
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
     Case $GUI_EVENT_CLOSE
         Exit
     Case $A11,$A12,$A13,$A21,$A22,$A23,$A31,$A32,$A33
         $Turn = XorO($Turn,$nMsg)
EndSwitch
WEnd
Func XorO($Turn,ByRef $Btn)
For $y = 1 To UBound($ButtonArray) - 1
For $x = 1 To UBound($ButtonArray,2) - 1
If $ButtonArray[$y][$x] = $Btn Then ExitLoop 2
Next
Next
Switch $x
     Case 1
         $XorO_X = 16
     Case 2
         $XorO_X = 61
     Case 3
         $XorO_X = 100
EndSwitch
Switch $y
     Case 1
         $XorO_Y = 24
     Case 2
         $XorO_Y = 66
     Case 3
         $XorO_Y = 118
EndSwitch
If Mod($Turn,2) = 1 Then
     GUICtrlDelete($Btn)
     $ButtonArray[$y][$x] = GUICtrlCreateLabel("X", $XorO_X,$XorO_Y)
Else
     GUICtrlDelete($Btn)
     $ButtonArray[$y][$x] = GUICtrlCreateLabel("O", $XorO_X,$XorO_Y)
EndIf
$Turn += 1
Return $Turn
EndFunc

took the liberty to make some changes...i like these kinds of challenges...now just need to automate the game play, and teach a computer that war is winless:

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Tic Tac Toe", 186, 201, 192, 124)
Global $A11 = GUICtrlCreateButton("", 16, 24, 25, 25)
Global $A12 = GUICtrlCreateButton("", 61, 24, 25, 25)
Global $A13 = GUICtrlCreateButton("", 106, 24, 25, 25)
Global $A21 = GUICtrlCreateButton("", 16, 66, 25, 25)
Global $A22 = GUICtrlCreateButton("", 61, 66, 25, 25)
Global $A23 = GUICtrlCreateButton("", 106, 66, 25, 25)
Global $A31 = GUICtrlCreateButton("", 16, 110, 25, 25)
Global $A32 = GUICtrlCreateButton("", 61, 110, 25, 25)
Global $A33 = GUICtrlCreateButton("", 106, 110, 25, 25)
Global $B11 = GUICtrlCreateLabel("", 16, 30, 25, 25, $SS_CENTER)
GUICtrlSetState(-1, $GUI_HIDE)
Global $B12 = GUICtrlCreateLabel("", 61, 30, 25, 25, $SS_CENTER)
GUICtrlSetState(-1, $GUI_HIDE)
Global $B13 = GUICtrlCreateLabel("", 106, 30, 25, 25, $SS_CENTER)
GUICtrlSetState(-1, $GUI_HIDE)
Global $B21 = GUICtrlCreateLabel("", 16, 72, 25, 25, $SS_CENTER)
GUICtrlSetState(-1, $GUI_HIDE)
Global $B22 = GUICtrlCreateLabel("", 61, 72, 25, 25, $SS_CENTER)
GUICtrlSetState(-1, $GUI_HIDE)
Global $B23 = GUICtrlCreateLabel("", 106, 72, 25, 25, $SS_CENTER)
GUICtrlSetState(-1, $GUI_HIDE)
Global $B31 = GUICtrlCreateLabel("", 16, 116, 25, 25, $SS_CENTER)
GUICtrlSetState(-1, $GUI_HIDE)
Global $B32 = GUICtrlCreateLabel("", 61, 116, 25, 25, $SS_CENTER)
GUICtrlSetState(-1, $GUI_HIDE)
Global $B33 = GUICtrlCreateLabel("", 106, 116, 25, 25, $SS_CENTER)
GUICtrlSetState(-1, $GUI_HIDE)
Global $Reset = GUICtrlCreateButton("Reset", 16, 168, 75, 25)
Global $Label1 = GUICtrlCreateLabel("Score:", 144, 8, 35, 17)
Global $Label2 = GUICtrlCreateLabel("X:", 152, 24, 14, 17)
Global $Label3 = GUICtrlCreateLabel("O:", 152, 40, 15, 17)
Global $Xscore = GUICtrlCreateLabel("0", 168, 24, 10, 17)
Global $Oscore = GUICtrlCreateLabel("0", 168, 40, 10, 17)
Global $Label4 = GUICtrlCreateLabel("T:", 152, 56, 15, 17)
Global $TIEscore = GUICtrlCreateLabel("0", 168, 56, 10, 17)
Global $TIEscore = GUICtrlCreateLabel("0", 168, 56, 10, 17)
Global $NewGame = GUICtrlCreateButton("New Game", 96, 168, 75, 25)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###
Global $Turn = 0
#region Button Array
Global $ButtonArray[4][4][2]
;TOP row
Global Enum $iButton, $iLabel
$ButtonArray[1][1][$iButton] = $A11
$ButtonArray[1][2][$iButton] = $A12
$ButtonArray[1][3][$iButton] = $A13
;MIDDLE row
$ButtonArray[2][1][$iButton] = $A21
$ButtonArray[2][2][$iButton] = $A22
$ButtonArray[2][3][$iButton] = $A23
;BOTTOM row
$ButtonArray[3][1][$iButton] = $A31
$ButtonArray[3][2][$iButton] = $A32
$ButtonArray[3][3][$iButton] = $A33
$ButtonArray[1][1][$iLabel] = $B11
$ButtonArray[1][2][$iLabel] = $B12
$ButtonArray[1][3][$iLabel] = $B13
;MIDDLE row
$ButtonArray[2][1][$iLabel] = $B21
$ButtonArray[2][2][$iLabel] = $B22
$ButtonArray[2][3][$iLabel] = $B23
;BOTTOM row
$ButtonArray[3][1][$iLabel] = $B31
$ButtonArray[3][2][$iLabel] = $B32
$ButtonArray[3][3][$iLabel] = $B33
#endregion Button Array
#endregion ### START Koda GUI section ### Form=
While 1
 $nMsg = GUIGetMsg()
 $bCheckWinner = False
 Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $A11, $A12, $A13, $A21, $A22, $A23, $A31, $A32, $A33
   $Turn = XorO($Turn, $nMsg)
   $bCheckWinner = True
  Case $Reset
   ResetBoard(True)
  Case $NewGame
   ResetBoard(False)
 EndSwitch
 If $Turn > 4 And $bCheckWinner Then
  $sWinner = CheckBoardForWinner()
  If StringLen($sWinner)>0 Then
   ResetBoard(False)
   AddPoint($sWinner)
   $Turn = 0
  EndIf
 EndIf
 If $Turn = 9 Then
  ResetBoard(False)
  AddPoint("")
  $Turn = 0
 EndIf
WEnd
Func XorO($Turn, ByRef $Btn)
 For $y = 1 To UBound($ButtonArray) - 1
  For $x = 1 To UBound($ButtonArray, 2) - 1
   If $ButtonArray[$y][$x][$iButton] = $Btn Then ExitLoop 2
  Next
 Next
 GUICtrlSetState($ButtonArray[$y][$x][$iButton], $GUI_HIDE)
 If Mod($Turn, 2) = 1 Then
  $Player = "O"
 Else
  $Player = "X"
 EndIf
 GUICtrlSetData($ButtonArray[$y][$x][$iLabel], $Player)
 GUICtrlSetState($ButtonArray[$y][$x][$iLabel], $GUI_SHOW)
 $Turn += 1
 Return $Turn
EndFunc   ;==>XorO
Func ResetBoard($bCallersResetScores)
 For $i = 0 To UBound($ButtonArray) - 1
  For $j = 0 To UBound($ButtonArray, 2) - 1
   GUICtrlSetState($ButtonArray[$i][$j][$iButton], $GUI_SHOW)
   GUICtrlSetState($ButtonArray[$i][$j][$iLabel], $GUI_HIDE)
   GUICtrlSetData($ButtonArray[$i][$j][$iLabel], "")
  Next
 Next
 If $bCallersResetScores Then
  GUICtrlSetData($Xscore,0)
  GUICtrlSetData($Oscore,0)
  GUICtrlSetData($TIEscore,0)
 EndIf
EndFunc   ;==>ResetBoard
Func CheckBoardForWinner()
 ; Check Horiz
 For $y = 1 To 3
  $iRead1 = GUICtrlRead($ButtonArray[$y][1][$iLabel])
  $iRead2 = GUICtrlRead($ButtonArray[$y][2][$iLabel])
  $iRead3 = GUICtrlRead($ButtonArray[$y][3][$iLabel])
  If Not IsInt($iRead1) And $iRead1 = $iRead2 And $iRead1 = $iRead3 Then Return $iRead1
 Next
 ; Check Vert
 For $x = 1 To 3
  $iRead1 = GUICtrlRead($ButtonArray[1][$x][$iLabel])
  $iRead2 = GUICtrlRead($ButtonArray[2][$x][$iLabel])
  $iRead3 = GUICtrlRead($ButtonArray[3][$x][$iLabel])
  If Not IsInt($iRead1) And $iRead1 = $iRead2 And $iRead1 = $iRead3 Then Return $iRead1
 Next
 ; Check Diag
 $iRead1 = GUICtrlRead($ButtonArray[1][1][$iLabel])
 $iRead2 = GUICtrlRead($ButtonArray[2][2][$iLabel])
 $iRead3 = GUICtrlRead($ButtonArray[3][3][$iLabel])
 $iRead4 = GUICtrlRead($ButtonArray[1][3][$iLabel])
 $iRead5 = GUICtrlRead($ButtonArray[3][1][$iLabel])
 If Not IsInt($iRead1) And $iRead1 = $iRead2 And $iRead1 = $iRead3 Then Return $iRead1
 If Not IsInt($iRead4) And $iRead4 = $iRead2 And $iRead4 = $iRead5 Then Return $iRead1
 Return ""
EndFunc
Func AddPoint($sCallersWinner)
 If $sCallersWinner = "X" Then
  $iControl = $Xscore
 ElseIf $sCallersWinner = "Y" Then
  $iControl = $Oscore
 Else
  $iControl = $TIEscore
 EndIf
 $iScore = GuiCtrlRead($iControl)
 GUICtrlSetData($iControl,$iScore+1)
EndFunc
Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
kylomas

DHPW,

There are several attempts at this game including one networked, multiplayer game. If you get stuck on something these thread may help with ideas for technique.

Good Luck,

kylomas

edit: spelling

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
DHPW

Thanks for your help and thanks for the tip!

I took the liberty to try out your program and it looks like there are some bugs, for example "O" never gets points, meaning if "O" wins then T gets a point... That's an easy fix!

Creating a computer opponent wouldnt be that hard, because there are only 5 ways to win and they are always pretty easy to block off. So if it's the human's goal to win, he will barely achieve that^^

Share this post


Link to post
Share on other sites
kylomas

DHPW,

I took the liberty to try out your program

That is NOT my program, but I get your points,

Good Luck,

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
jdelaney

Oops, mixed up my X O's with X Y's in the AddPoint function:

Func AddPoint($sCallersWinner)
 If $sCallersWinner = "X" Then
  $iControl = $Xscore
 ElseIf $sCallersWinner = "O" Then
  $iControl = $Oscore
 Else
  $iControl = $TIEscore
 EndIf

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
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
Sign in to follow this  

×