So I spent way to long trying this, but here's an example bridging off a previous post - where the variable
"letters" is the same as in the previous posts
( requires AHKL ):
Code:
table := Grid( letters )
MsgBox, 0, % "Row, Col", % table.word( "sunset" )
MsgBox, 0, % "Row, Col", % table.word( "DIVERSION" )
Return
Grid( data ) { ; separates data into 2D Array/Object
obj := object( "row", "row"
, "column", "column"
, "diagonal", "diagonal"
, "diagonalcoord", "diagonalcoord"
, "word", "word" )
array := object( "base", obj )
Loop, Parse, data, `n, `r
{
n := A_Index
Loop, Parse, A_LoopField, %A_Space%
array[ n, A_Index ] := A_LoopField
}
array.rows := array._MaxIndex(), array.columns := array[1]._MaxIndex()
array.diagonals := array.rows+array.columns-1
Return, array
}
Row( obj, item, rev=0 ) { ; returns row string
Loop, % obj.columns
string .= rev ? obj[ item, obj.columns-A_Index+1 ] : obj[ item, A_Index ]
Return, string
}
Column( obj, item, rev=0 ) { ; returns columns string
Loop, % obj.rows
string .= rev ? obj[ obj.rows-A_Index+1, item ] : obj[ A_Index, item ]
Return, string
}
Diagonal( obj, item ) { ; gets a diagonals string of letters
If ( item > obj.diagonals )
Return, "ERROR"
Loop, % obj.diagonals
string .= item>0 ? obj[ item-A_Index+1, A_Index ] : obj[ A_Index, obj.columns+item+A_Index ]
Return, string
}
DiagonalCoord( obj, item, pos ) { ; returns the "R,C" position of a position in a diagonal string
If ( item > obj.diagonals )
Return, "ERROR"
If ( item>0 )
If ( item <= obj.rows )
row := item-pos+1, col := pos
Else
row := obj.rows-pos+1, col := pos + ( item-obj.rows )
Else
If ( item >= -1*obj.columns )
row := pos, col := obj.columns+item+pos
Else
row := pos + ( -1*obj.columns-item ), col := pos
Return, row>0 && row<=obj.rows && col>0 && col<=obj.columns
? row "," col : "Invalid Parameters"
}
Word( obj, word ) { ; searches for a word
If ( word = "" )
Return, "No Word"
Loop, % obj.rows ; horizontal search
If pos := InStr( obj.row( A_Index ), word )
Return, A_Index "," pos
Else If pos := InStr( obj.row( A_Index, "Reverse" ), word )
Return, A_Index "," obj.columns-pos+1
Loop, % obj.columns ; vertical search
If pos := InStr( obj.column( A_Index ), word )
Return, pos "," A_Index
Else If pos := InStr( obj.column( A_Index, "Reverse" ), word )
Return, obj.rows-pos+1 "," A_Index
While( !item && A_Index<=obj.diagonals ) { ; diagonal search
string := obj.diagonal( A_Index )
string2 := obj.diagonal( -1 * A_Index )
If pos := InStr( string, word )
item := A_Index
Else If pos := InStr( Flip(string), word )
item := A_Index, pos := StrLen(string)-pos+1
Else If pos := InStr( string2, word )
item := -1 * A_Index
Else If pos := InStr( Flip(string2), word )
item := -1 * A_Index, pos := StrLen(string)-pos+1
}
Return, item ? obj.diagonalCoord( item, pos ) : "N\A"
}
Flip(in) { ; http://www.autohotkey.com/forum/topic46040.html&start=5
VarSetCapacity(out, n:=StrLen(in))
Loop %n%
out .= SubStr(in, n--, 1)
return out
}
Also, if anyone cares, the numbering of the diagonal strings is like this:
Code:
Going up/right Going down/right
1 -4 -3 -2 -1
2 -5
3 -6
4 5 6 7 -7