Jump to content

Directional _ArraySearch()


Recommended Posts

Hi all,

I was wandering if anyone knew a way of using something similar to _ArraySearch() to be able to search diagonally through an array.

As you probably know in Tic-Tac-Toe you can win by row, column or diagonal. I have rows all sorted but column and diagonal do not work as using _ArraySearch() means I have to check for a blank cell throughout my grid.

Here is the code I have, it checks both AI winning moves and player blocking however, it will not work correctly as it does not place the marker in the right places.

Func PossibleWin()
   ; Rows
   ; CPU Win
    If $intBoard[0] * $intBoard[1] * $intBoard[2] = 50 Then
        $r1Win = _ArraySearch($intBoard, 2, 1, 3)
        Go($r1Win)
    ElseIf $intBoard[3] * $intBoard[4] * $intBoard[5] = 50 Then
        $r2Win = _ArraySearch($intBoard, 2, 4, 6)
        Go($r2Win)
    ElseIf $intBoard[6] * $intBoard[7] * $intBoard[8] = 50 Then
        $r3Win = _ArraySearch($intBoard, 2, 7, 9)
        Go($r3Win)
   ; Player Block
    ElseIf $intBoard[0] * $intBoard[1] * $intBoard[2] = 18 Then
        $r12Win = _ArraySearch($intBoard, 2, 1, 3)
        Go($r12Win)
    ElseIf $intBoard[3] * $intBoard[4] * $intBoard[5] = 18 Then
        $r22Win = _ArraySearch($intBoard, 2, 4, 6)
        Go($r22Win)
    ElseIf $intBoard[6] * $intBoard[7] * $intBoard[8] = 18 Then
        $r32Win = _ArraySearch($intBoard, 2, 7, 9)
        Go($r32Win)
   ; Columns
   ; CPU Win
    ElseIf $intBoard[0] * $intBoard[3] * $intBoard[6] = 50 Then
        $c1Win = _ArraySearch($intBoard, 2, 1, 7)
        Go($c1Win)
    ElseIf $intBoard[1] * $intBoard[4] * $intBoard[7] = 50 Then
        $c2Win = _ArraySearch($intBoard, 2, 2, 8)
        Go($c2Win)
    ElseIf $intBoard[2] * $intBoard[5] * $intBoard[8] = 50 Then
        $c3Win = _ArraySearch($intBoard, 2, 2, 8)
        Go($c3Win)
   ; Player Block
    ElseIf $intBoard[0] * $intBoard[3] * $intBoard[6] = 18 Then
        $c11Win = _ArraySearch($intBoard, 2, 1, 7)
        Go($c11Win)
    ElseIf $intBoard[1] * $intBoard[4] * $intBoard[7] = 18 Then
        $c21Win = _ArraySearch($intBoard, 2, 2, 8)
        Go($c21Win)
    ElseIf $intBoard[2] * $intBoard[5] * $intBoard[8] = 18 Then
        $c31Win = _ArraySearch($intBoard, 2, 2, 8)
        Go($c31Win)
   ; Diagonal
   ; CPU Win
    ElseIf $intBoard[0] * $intBoard[4] * $intBoard[8] = 50 Then
        $d1Win = _ArraySearch($intBoard, 2)
        Go($d1Win)
    ElseIf $intBoard[2] * $intBoard[4] * $intBoard[6] = 50 Then
        $d2Win = _ArraySearch($intBoard, 2, 3, 9)
        Go($d2Win)
   ; Player Block
    ElseIf $intBoard[0] * $intBoard[4] * $intBoard[8] = 18 Then
        $d11Win = _ArraySearch($intBoard, 2)
        Go($d11Win)
    ElseIf $intBoard[2] * $intBoard[4] * $intBoard[6] = 18 Then
        $d21Win = _ArraySearch($intBoard, 2, 3, 9)
        Go($d21Win)
   ; No other moves
    Else
        NextMove()
    EndIf
EndFunc

As you can see, in the column and diagonal is where I am now stuck. You can also see that in the parameters for _ArraySearch() is where the problem is as it checks out all of the board and not the actual area it should be in.

I know this is probably very tricky but any help is much appreciated.

Thankyou,

James

Link to comment
Share on other sites

Hi all,

I was wandering if anyone knew a way of using something similar to _ArraySearch() to be able to search diagonally through an array.

As you probably know in Tic-Tac-Toe you can win by row, column or diagonal. I have rows all sorted but column and diagonal do not work as using _ArraySearch() means I have to check for a blank cell throughout my grid.

Here is the code I have, it checks both AI winning moves and player blocking however, it will not work correctly as it does not place the marker in the right places.

Func PossibleWin()
  ; Rows
  ; CPU Win
    If $intBoard[0] * $intBoard[1] * $intBoard[2] = 50 Then
        $r1Win = _ArraySearch($intBoard, 2, 1, 3)
        Go($r1Win)
    ElseIf $intBoard[3] * $intBoard[4] * $intBoard[5] = 50 Then
        $r2Win = _ArraySearch($intBoard, 2, 4, 6)
        Go($r2Win)
    ElseIf $intBoard[6] * $intBoard[7] * $intBoard[8] = 50 Then
        $r3Win = _ArraySearch($intBoard, 2, 7, 9)
        Go($r3Win)
  ; Player Block
    ElseIf $intBoard[0] * $intBoard[1] * $intBoard[2] = 18 Then
        $r12Win = _ArraySearch($intBoard, 2, 1, 3)
        Go($r12Win)
    ElseIf $intBoard[3] * $intBoard[4] * $intBoard[5] = 18 Then
        $r22Win = _ArraySearch($intBoard, 2, 4, 6)
        Go($r22Win)
    ElseIf $intBoard[6] * $intBoard[7] * $intBoard[8] = 18 Then
        $r32Win = _ArraySearch($intBoard, 2, 7, 9)
        Go($r32Win)
  ; Columns
  ; CPU Win
    ElseIf $intBoard[0] * $intBoard[3] * $intBoard[6] = 50 Then
        $c1Win = _ArraySearch($intBoard, 2, 1, 7)
        Go($c1Win)
    ElseIf $intBoard[1] * $intBoard[4] * $intBoard[7] = 50 Then
        $c2Win = _ArraySearch($intBoard, 2, 2, 8)
        Go($c2Win)
    ElseIf $intBoard[2] * $intBoard[5] * $intBoard[8] = 50 Then
        $c3Win = _ArraySearch($intBoard, 2, 2, 8)
        Go($c3Win)
  ; Player Block
    ElseIf $intBoard[0] * $intBoard[3] * $intBoard[6] = 18 Then
        $c11Win = _ArraySearch($intBoard, 2, 1, 7)
        Go($c11Win)
    ElseIf $intBoard[1] * $intBoard[4] * $intBoard[7] = 18 Then
        $c21Win = _ArraySearch($intBoard, 2, 2, 8)
        Go($c21Win)
    ElseIf $intBoard[2] * $intBoard[5] * $intBoard[8] = 18 Then
        $c31Win = _ArraySearch($intBoard, 2, 2, 8)
        Go($c31Win)
  ; Diagonal
  ; CPU Win
    ElseIf $intBoard[0] * $intBoard[4] * $intBoard[8] = 50 Then
        $d1Win = _ArraySearch($intBoard, 2)
        Go($d1Win)
    ElseIf $intBoard[2] * $intBoard[4] * $intBoard[6] = 50 Then
        $d2Win = _ArraySearch($intBoard, 2, 3, 9)
        Go($d2Win)
  ; Player Block
    ElseIf $intBoard[0] * $intBoard[4] * $intBoard[8] = 18 Then
        $d11Win = _ArraySearch($intBoard, 2)
        Go($d11Win)
    ElseIf $intBoard[2] * $intBoard[4] * $intBoard[6] = 18 Then
        $d21Win = _ArraySearch($intBoard, 2, 3, 9)
        Go($d21Win)
  ; No other moves
    Else
        NextMove()
    EndIf
EndFunc

As you can see, in the column and diagonal is where I am now stuck. You can also see that in the parameters for _ArraySearch() is where the problem is as it checks out all of the board and not the actual area it should be in.

I know this is probably very tricky but any help is much appreciated.

Thankyou,

James

Without digging into it, if you have the Beta then you can probably do something with _ArrayFindAll().

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

I shall try the beta now, thank you George!

NP

If it helps then just put me in your will for the Mercedes.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

It sounds to me like your game script may be overly complex. I wrote a version of Tic-Tac-Toe which you may be able to get some tips from. Here is the function which checks for a win. If this is helpful I can post more.

Some of the global vars

;Number of cells across + down
 Global $boardSize = 3
 
;Margin around entire board (pixels)
 Global $boardMargin = 20
 
;3-Dimensional array to store board player positions
;[Y][X][0] = Button control id
;[Y][X][1] = Button status 0: Empty, 1: Player 1, 2: Player 2
 Global $boardArray[$boardSize][$boardSize][2]

;Check for win (only need to check current player)
Func CheckWin()

    ;Search for complete rows first
    ;Rows
    For $Y = 0 to $boardSize - 1
        $count = 0
        
        ;Columns
        For $X = 0 to $boardSize - 1
            If $boardArray[$Y][$X][1] = $currentPlayer Then $count += 1
        Next
        
        If $count = $boardSize Then
            DeclareWin($Y, 0)
            Return
        EndIf
    Next
    
    ;Next search for complete columns
    ;Columns
    For $X = 0 to $boardSize - 1
        $count = 0
        
        ;Rows
        For $Y = 0 to $boardSize - 1
            If $boardArray[$Y][$X][1] = $currentPlayer Then $count += 1
        Next
        
        If $count = $boardSize Then
            DeclareWin($X, 1)
            Return
        EndIf
    Next
    
    ;Finally search for diagonals
    ;Top/Left->Bottom/Right
    $count = 0
    For $Z = 0 to $boardSize - 1
        If $boardArray[$Z][$Z][1] = $currentPlayer Then $count += 1        
    Next
    If $count = $boardSize Then
        DeclareWin(0,2)
        Return
    EndIf
    
    ;Top/right->Bottom/Left
    $count = 0
    For $Z = 0 to $boardSize - 1
        If $boardArray[$Z][$boardSize-1-$Z][1] = $currentPlayer Then $count += 1        
    Next
    If $count = $boardSize Then
        DeclareWin(1,2)
        Return
    EndIf
EndFunc

;Location is zero indexed row,column, or diagonal
;Type 0: Row, 1: Column, 2: Diagonal
Func DeclareWin($location, $type)
    Switch $type
        Case 0
            ;Highlight row using given zero indexed row number
            For $Z = 0 to $boardSize - 1
                Highlight($boardArray[$location][$Z][0])
            Next
        Case 1
            ;Highlight column using given zero indexed column number
            For $Z = 0 to $boardSize - 1
                Highlight($boardArray[$Z][$location][0])
            Next
        Case 2
            ;Highlight diagonal Top/Left->Bottom/Right
            If $location = 0 Then
                For $Z = 0 to $boardSize - 1
                    Highlight($boardArray[$Z][$Z][0])
                Next
            ;Highlight diagonal Top/right->Bottom/Left
            Else
                For $Z = 0 to $boardSize - 1
                    Highlight($boardArray[$Z][$boardSize-1-$Z][0])
                Next
            EndIf
    EndSwitch
    ;MsgBox(0,$type,"Player " & $currentPlayer & " is the champion" & @CRLF & "Location: " & $location & @CRLF & "Type: " & $type)
EndFunc

Func Highlight($control)
    GuiCtrlSetColor($control, 0xFF0000)
EndFunc
Edited by weaponx
Link to comment
Share on other sites

Hmm, there is a way I just thought of, may be easier than _ArrayFindAll().

Basically my grid is 3x3. If I wanted to go diagonally from left to right then I would start at boardID 1 then +3 to the ID to find the middle number and +3 again to find the next number. If I wanted to go down then I would +2 to the ID. Only thing is I would have to find away of directing it.

I shall take a look into it..

Link to comment
Share on other sites

Thanks for the link weaponx but I cannot make it work with my script :D

I could just go through a lot of If statements to check if it has been marked otherwise I have no idea :/

If you want to PM me your script I can try to clean it up.

Link to comment
Share on other sites

I have gone through most of your script and have come to a conclusion. I can't do much with it due to the brute-force nature of it. Everything is hardcoded for your win checking and this mean a lot of redundant code. Almost every board game requires a 2-dimensional array so you can easily locate a grid location given X,Y coordinates.

If I were to complete these changes the only thing that would be left of your original script is the gui design.

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...