Jump to content
Sign in to follow this  
AlmarM

ActionScript 3 to AutoIt v3 functions

Recommended Posts

EDIT:

Ugh, I give up.

Could anyone help me convert this into AU3?

This is what I came up with.

#include <Array.au3>

Dim $aPuzzle[81] = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Solve($aPuzzle)

Func ReturnRow($iCell)
    Return Floor($iCell / 9)
EndFunc

Func ReturnCol($iCell)
    Return Mod($iCell, 9)
EndFunc

Func ReturnBlock($iCell)
    Return Floor(ReturnRow($iCell) / 3) * 3 + Floor(ReturnCol($iCell) / 3)
EndFunc

Func IsPossibleRow($iNumber, $iRow, ByRef $aSudoku)
    Local $bPossible = True
    
    For $i = 0 To 8
        If ($aSudoku[$iRow * 9 + $i] == $iNumber) Then
            $bPossible = False
            ExitLoop
        EndIf
    Next
    
    Return $bPossible
EndFunc

Func IsPossibleCol($iNumber, $iCol, ByRef $aSudoku)
    Local $bPossible = True
    
    For $i = 0 To 8
        If ($aSudoku[$iCol * 9 + $i] == $iNumber) Then
            $bPossible = False
            ExitLoop
        EndIf
    Next
    
    Return $bPossible
EndFunc

Func IsPossibleBlock($iNumber, $iBlock, ByRef $aSudoku)
    Local $bPossible = True
    
    For $i = 0 To 8
        If ($aSudoku[Floor($iBlock / 3) * 27 + Mod($i, 3) + 9 * Floor($i / 3) + 3 * Mod($iBlock, 3) == $iNumber]) Then
            $bPossible = False
            ExitLoop
        EndIf
    Next
    
    Return $bPossible
EndFunc

Func IsPossibleNumber($iCell, $iNumber, ByRef $aSudoku)
    Local $iRow = ReturnRow($iCell)
    Local $iCol = ReturnCol($iCell)
    Local $iBlock = ReturnBlock($iCell)
    
    Return IsPossibleRow($iNumber, $iRow, $aSudoku) And IsPossibleCol($iNumber, $iCol, $aSudoku) And IsPossibleBlock($iNumber, $iBlock, $aSudoku)
EndFunc

Func IsCorrectRow($iRow, ByRef $aSudoku)
    Local $aSequence[9] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    Local $aTempRow[9]
    
    For $i = 0 To 8
        $aTempRow[$i] = $aSudoku[$iRow * 9 + $i]
    Next
    
    _ArraySort($aTempRow)
    $sArray = _ArrayToString($aTempRow, ",")
    $sArray2 = _ArrayToString($aSequence, ",")
    
    Return $sArray == $sArray2
EndFunc

Func IsCorrectCol($iCol, ByRef $aSudoku)
    Local $aSequence[9] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    Local $aTempCol[9]
    
    For $i = 0 To 8
        $aTempCol[$i] = $aSudoku[$iCol + $i * 9]
    Next
    
    _ArraySort($aTempCol)
    $sArray = _ArrayToString($aTempCol, ",")
    $sArray2 = _ArrayToString($aSequence, ",")
    
    Return $sArray == $sArray2
EndFunc

Func IsCorrectBlock($iBlock, ByRef $aSudoku)
    Local $aSequence[9] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    Local $aTempBlock[9]
    
    For $i = 0 To 8
        $aTempBlock[$i] = $aSudoku[Floor($iBlock / 3) * 27 + Mod($i, 3) + 9 * Floor($i / 3) + 3 * Mod($iBlock, 3)]
    Next
    
    _ArraySort($aTempBlock)
    $sArray = _ArrayToString($aTempBlock, ",")
    $sArray2 = _ArrayToString($aSequence, ",")
    
    Return $sArray == $sArray2
EndFunc

Func IsSolved(ByRef $aSudoku)
    For $i = 0 To 8
        If (Not IsCorrectBlock($i, $aSudoku) Or Not IsCorrectRow($i, $aSudoku) Or Not IsCorrectCol($i, $aSudoku)) Then
            Return False
        EndIf
    Next
    
    Return True
EndFunc

Func DeterminePossibleValues($iCell, ByRef $aSudoku)
    Local $aPossible[9]
    
    For $i = 1 To 9
        If (IsPossibleNumber($iCell, $i, $aSudoku)) Then
            _ArrayPush($aPossible, $i, 1)
        EndIf
    Next
    
    Return $aPossible
EndFunc

Func DetermineRandomPossibleValue($aPossible, $iCell)
    Local $iRandomPicked = Floor(Random() * StringLen($aPossible[$iCell])
    
    Return $aPossible[$iCell][$iRandomPicked]
EndFunc

Func ScanForUnique(ByRef $aSudoku)
    Local $aPossible[81][1]
    
    For $i = 0 To 80
        If ($aSudoku[$i] == 0) Then
            $aPossible[$i][0] = DeterminePossibleValues($i, $aSudoku)
            
            If (StringLen($aPossible[$i][0]) == 0) Then
                Return False
            EndIf
        EndIf
    Next
    
    Return $aPossible   
EndFunc

Func RemoveAttempt($aAttempt, $iNumber)
    Local $aTempArray[UBound($aAttempt)]
    
    For $i = 0 To UBound($aAttempt) - 1
        If ($aAttempt[$i] <> $iNumber) Then
            _ArrayPush($aTempArray, $aAttempt[$i], 1)
        EndIf
    Next
    
    Return $aTempArray
EndFunc

Func NextRandom($aPossible)
    Local $iMai = 9
    Local $iMinChoices = 0
    
    For $i = 0 To 80
        If ($aPossible[$i] <> "") Then
            If ((StringLen($aPossible[$i][0]) <= $iMai) And (StringLen($aPossible[$i][0]) > 0) Then
                $iMai = StringLen($aPossible[$i])
                $iMinChoices = $i
            EndIf
        EndIf
    Next
    
    Return $iMinChoices
EndFunc

Func Solve(ByRef $aSudoku)
    Local $aSaved[47]
    Local $aSavedSud[47]
    Local $i = 0
    Local $vNextMove
    Local $iWhatToTry
    Local $aAttempt
    
    While (Not IsSolved($aSudoku))
        $i += 1
        $vNextMove = ScanForUnique($aSudoku)
        
        If ($vNextMove == False) Then
            $vNextMove = _ArrayPop($aSaved)
            $aSudoku = _ArrayPop($aSavedSud)
        EndIf
        
        $iWhatToTry = NextRandom($vNextMove)
        $aAttempt = DetermineRandomPossibleValue($vNextMove, $iWhatToTry)
        
        If (StringLen($vNextMove[$iWhatToTry]) > 1) Then
            $vNextMove[$iWhatToTry] = RemoveAttempt($vNextMove[$iWhatToTry], $aAttempt)
            ;
            ;
        EndIf
        
        $aSudoku[$iWhatToTry] = $aAttempt
    WEnd
    
    _ArrayDisplay($aSudoku)
EndFunc

;)

Edited by AlmarM

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...