Post by imustbeamoron » 17 Oct 2016, 20:39
i made this awhile back.. never did post it.. its for the honeycomb task, feel free to improve or submit it.. I know this can be shortend, but was going for readability.
Code: Select all
CoordMode, mouse, client
t := new honeyComb()
return
GuiClose:
esc::
ExitApp
class Honeycomb{
__new(hxn_Width=80, hxn_Height=60, hxn_Columns=5, hxn_Rows=4){
this.char_init()
this.hxn_init(hxn_Width, hxn_Height, hxn_Columns, hxn_Rows)
this.wndw_init()
this.hFont := this.createFont(round(hxn_Height*.6), round(hxn_Width*.35))
this.selectObject(this.wndw.hDC, this.hFont) ;set the font color to red
DllCall("SetTextColor", "Ptr", this.wndw.hDC, "UInt", 0x0000FF)
for key, val in this.hxn.array ;draw all hexagons
this.hxn_draw(key)
DllCall("SetTextColor", "Ptr", this.wndw.hDC, "UInt", 0x000000) ;set the font color to black
}
char_init(){ ;sets up the char array - used to assign each hxn a random character (letter)
this.char := {}
this.char.selected :=
this.char.array := []
loop 26
this.char.array.push(chr(a_index + 64))
}
hxn_init(hxn_Width, hxn_Height, hxn_Columns, hxn_Rows){ ;sets up the array of hexagons
this.hxn := {} ,this.hxn.array := {}
this.hxn.width := hxn_Width ,this.hxn.height := hxn_Height
this.hxn.rowCount := hxn_Rows ,this.hxn.colCount := hxn_Columns
w := this.hxn.width ,h := this.hxn.height
oX := 10 ,oY := 10
pts := {} ,orig_Y := 10
pts.x := [] ,pts.y := []
pts.x.push(round(w * .25)) ,pts.y.push(0)
pts.x.push(round(w * .75)) ,pts.y.push(0)
pts.x.push(w) ,pts.y.push(round(h * .5))
pts.x.push(round(w *.75)) ,pts.y.push(h)
pts.x.push(round(w *.25)) ,pts.y.push(h)
pts.x.push(0) ,pts.y.push(round(h *.5))
FuncObj := ObjBindMethod(this, "hkey_handler")
loop % this.hxn.colCount
{
if !(a_index & 1)
oY += h/2
loop % this.hxn.rowCount
{
random, charIndex, 1, % this.char.array.maxIndex() ;get rnd char for the new hxn
char := this.char.array[charIndex]
this.hxn.array[char] := {} ;create new hxn object, add it tohxn.array, w/ the key being its rnd char
this.hxn.array[char].char := char
this.hxn.array[char].isFresh := 1
pos := 0
varSetCapacity(hxnPoints, 48, 0) ;build the array of pts structures to be used in the createpolygonrgn call
loop % pts.x.maxIndex()
{
numPut(pts.x[a_index] + oX, hxnPoints, pos + 0, "int")
numPut(pts.y[a_Index] + oY, hxnPoints, pos + 4, "int")
pos += 8
}
this.hxn.array[char].rgn := DllCall("CreatePolygonRgn", "Ptr", &hxnPoints, "Int", 6, "Int", 1)
Hotkey, % char,% FuncObj
this.char.array.Remove(charIndex)
oY += h
}
oY := orig_Y, oX += w * .75
}
FuncObj := ObjBindMethod(this, "mouse_handler")
Hotkey, ~LButton, % FuncObj
}
hxn_draw(key,fillcolor=0x00FFFF){
brush := this.createBrush(fillColor) ;fill the hexagon
this.selectObject(this.wndw.hDC, brush)
DllCall("FillRgn", "Ptr", this.wndw.hDC, "Ptr", this.hxn.array[key].rgn, "Ptr", brush)
this.deleteObject(brush)
brush := this.createBrush(0x000000) ;frame the hexagon
this.selectObject(this.wndw.hDC, brush)
DllCall("FrameRgn", "Ptr", this.wndw.hDC, "Ptr", this.hxn.array[key].rgn, "Ptr", brush, "Int", 1, "Int", 1)
this.deleteObject(brush)
DllCall("SetBkColor", "Ptr", this.wndw.hDC, "UInt", fillColor) ;draw the hexagons char
this.hxn_getCharPos(key, cx, cy)
DllCall("TextOut", "Ptr", this.wndw.hDC, "Int", cX, "Int", cY, "Str", this.hxn.array[key].char, "Int", 1)
}
hxn_getCharPos(key, ByRef x, ByRef y){
;get the char width
VarSetCapacity(tSize, 8, 0)
DllCall("GetTextExtentPoint32", "Ptr", this.wndw.hDC, "Str", key, "Int", StrLen(key), "Ptr", &tSize)
tWidth := NumGet(tSize, 0, "Int")
tHeight := NumGet(tSize, 4, "Int")
;get the bounding box of the hxn rgn
VarSetCapacity(rect, 16, 0)
DllCall("GetRgnBox", "Ptr", this.hxn.array[key].rgn, "Ptr", &rect)
;calc the position of the char within the hxn
x := (this.hxn.width / 2) - (tWidth / 2) + NumGet(rect, 0, "int")
y := (this.hxn.height / 2) - (tHeight / 2) + NumGet(rect, 4, "int")
}
hxn_getRgn(x,y){
for key, value in this.hxn.array
{
if (DllCall("PtInRegion", "Ptr", this.hxn.array[key].rgn, "Int", x, "Int", y)) && this.hxn.array[key].isFresh
{
this.hxn_draw(key, 0xFF00FF)
Hotkey, %key%,, off
this.hxn.array[key].isFresh := 0
this.char.selected .= key "," ;if mouse is used to select a hgn, add it to the list
break
}
}
}
hkey_handler(){
this.hxn_draw(A_ThisHotkey, 0xFF00FF) ;fill the hexagon with the 2nd color..
Hotkey, % A_ThisHotkey, off ;remove this hotkey now that we've already 'used' this hexagon
this.hxn.array[A_ThisHotkey].isFresh := 0
this.char.selected .= A_ThisHotkey "," ;if key is used to select hgn, add it to the list
this.game_getStatus()
}
mouse_handler(){
MouseGetPos, mx, my ;gets the current mouse postion
this.hxn_getRgn(mx,my) ;checks if mouse click was inside a hex rgn/if so, draw it
this.game_getStatus() ;checks if game is over
}
game_getStatus(){ ;checks if game is over
for key, val in this.hxn.array ;if game is over, display list of selected chars
if this.hxn.array[key].isfresh ; and play again dlg box.
return
selected := this.char.selected
selected := SubStr(selected,1, StrLen(selected)-1)
MsgBox, 0x4, Game Over, % "Order of selected hexagons: " selected "`nPlay again?"
IfMsgBox No
ExitApp
Reload
}
wndw_init(){ ;sets up and displays the window
this.wndw := {}
this.wndw.width := round(this.hxn.width * (this.hxn.colCount - 1)) + 20
this.wndw.height := round(this.hxn.height * (this.hxn.rowCount + .5)) + 20
gui, show, % "w" this.wndw.width " h" this.wndw.height, Honeycombs
this.wndw.hWnd := WinExist("A")
this.wndw.hDC := DllCall("GetDC", "Ptr", this.wndw.hWnd)
}
createBrush(color){
return DllCall("CreateSolidBrush", "UInt", color)
}
createFont(height,width,weight=800){
return DllCall("CreateFont"
,"Int", height
,"Int", width
,"Int", 0
,"Int", 0
,"Int", weight
,"UInt", 0
,"UInt", 0
,"UInt", 0
,"UInt", 0
,"UInt", 0
,"UInt", 0
,"UInt", 0
,"UInt", 0
,"Str", 0)
}
selectObject(dc, obj){ ;internal method
return DllCall("SelectObject", "Ptr", dc, "Ptr", obj)
}
deleteObject(obj){ ;used for gdi cleanup
return DllCall("DeleteObject", "Ptr", obj)
}
__Delete(){
DllCall("ReleaseDC", "Ptr", this.wndw.hWnd, "Ptr", this.wndw.hDC) ;release the window DC
this.deleteObject(this.hFont) ;delete the font
for key, val in this.hxn.array ;delete all of the hxn regions
this.deleteObject(this.hxn.array[key].rgn)
}
}
i made this awhile back.. never did post it.. its for the honeycomb task, feel free to improve or submit it.. I know this can be shortend, but was going for readability.
[code]CoordMode, mouse, client
t := new honeyComb()
return
GuiClose:
esc::
ExitApp
class Honeycomb{
__new(hxn_Width=80, hxn_Height=60, hxn_Columns=5, hxn_Rows=4){
this.char_init()
this.hxn_init(hxn_Width, hxn_Height, hxn_Columns, hxn_Rows)
this.wndw_init()
this.hFont := this.createFont(round(hxn_Height*.6), round(hxn_Width*.35))
this.selectObject(this.wndw.hDC, this.hFont) ;set the font color to red
DllCall("SetTextColor", "Ptr", this.wndw.hDC, "UInt", 0x0000FF)
for key, val in this.hxn.array ;draw all hexagons
this.hxn_draw(key)
DllCall("SetTextColor", "Ptr", this.wndw.hDC, "UInt", 0x000000) ;set the font color to black
}
char_init(){ ;sets up the char array - used to assign each hxn a random character (letter)
this.char := {}
this.char.selected :=
this.char.array := []
loop 26
this.char.array.push(chr(a_index + 64))
}
hxn_init(hxn_Width, hxn_Height, hxn_Columns, hxn_Rows){ ;sets up the array of hexagons
this.hxn := {} ,this.hxn.array := {}
this.hxn.width := hxn_Width ,this.hxn.height := hxn_Height
this.hxn.rowCount := hxn_Rows ,this.hxn.colCount := hxn_Columns
w := this.hxn.width ,h := this.hxn.height
oX := 10 ,oY := 10
pts := {} ,orig_Y := 10
pts.x := [] ,pts.y := []
pts.x.push(round(w * .25)) ,pts.y.push(0)
pts.x.push(round(w * .75)) ,pts.y.push(0)
pts.x.push(w) ,pts.y.push(round(h * .5))
pts.x.push(round(w *.75)) ,pts.y.push(h)
pts.x.push(round(w *.25)) ,pts.y.push(h)
pts.x.push(0) ,pts.y.push(round(h *.5))
FuncObj := ObjBindMethod(this, "hkey_handler")
loop % this.hxn.colCount
{
if !(a_index & 1)
oY += h/2
loop % this.hxn.rowCount
{
random, charIndex, 1, % this.char.array.maxIndex() ;get rnd char for the new hxn
char := this.char.array[charIndex]
this.hxn.array[char] := {} ;create new hxn object, add it tohxn.array, w/ the key being its rnd char
this.hxn.array[char].char := char
this.hxn.array[char].isFresh := 1
pos := 0
varSetCapacity(hxnPoints, 48, 0) ;build the array of pts structures to be used in the createpolygonrgn call
loop % pts.x.maxIndex()
{
numPut(pts.x[a_index] + oX, hxnPoints, pos + 0, "int")
numPut(pts.y[a_Index] + oY, hxnPoints, pos + 4, "int")
pos += 8
}
this.hxn.array[char].rgn := DllCall("CreatePolygonRgn", "Ptr", &hxnPoints, "Int", 6, "Int", 1)
Hotkey, % char,% FuncObj
this.char.array.Remove(charIndex)
oY += h
}
oY := orig_Y, oX += w * .75
}
FuncObj := ObjBindMethod(this, "mouse_handler")
Hotkey, ~LButton, % FuncObj
}
hxn_draw(key,fillcolor=0x00FFFF){
brush := this.createBrush(fillColor) ;fill the hexagon
this.selectObject(this.wndw.hDC, brush)
DllCall("FillRgn", "Ptr", this.wndw.hDC, "Ptr", this.hxn.array[key].rgn, "Ptr", brush)
this.deleteObject(brush)
brush := this.createBrush(0x000000) ;frame the hexagon
this.selectObject(this.wndw.hDC, brush)
DllCall("FrameRgn", "Ptr", this.wndw.hDC, "Ptr", this.hxn.array[key].rgn, "Ptr", brush, "Int", 1, "Int", 1)
this.deleteObject(brush)
DllCall("SetBkColor", "Ptr", this.wndw.hDC, "UInt", fillColor) ;draw the hexagons char
this.hxn_getCharPos(key, cx, cy)
DllCall("TextOut", "Ptr", this.wndw.hDC, "Int", cX, "Int", cY, "Str", this.hxn.array[key].char, "Int", 1)
}
hxn_getCharPos(key, ByRef x, ByRef y){
;get the char width
VarSetCapacity(tSize, 8, 0)
DllCall("GetTextExtentPoint32", "Ptr", this.wndw.hDC, "Str", key, "Int", StrLen(key), "Ptr", &tSize)
tWidth := NumGet(tSize, 0, "Int")
tHeight := NumGet(tSize, 4, "Int")
;get the bounding box of the hxn rgn
VarSetCapacity(rect, 16, 0)
DllCall("GetRgnBox", "Ptr", this.hxn.array[key].rgn, "Ptr", &rect)
;calc the position of the char within the hxn
x := (this.hxn.width / 2) - (tWidth / 2) + NumGet(rect, 0, "int")
y := (this.hxn.height / 2) - (tHeight / 2) + NumGet(rect, 4, "int")
}
hxn_getRgn(x,y){
for key, value in this.hxn.array
{
if (DllCall("PtInRegion", "Ptr", this.hxn.array[key].rgn, "Int", x, "Int", y)) && this.hxn.array[key].isFresh
{
this.hxn_draw(key, 0xFF00FF)
Hotkey, %key%,, off
this.hxn.array[key].isFresh := 0
this.char.selected .= key "," ;if mouse is used to select a hgn, add it to the list
break
}
}
}
hkey_handler(){
this.hxn_draw(A_ThisHotkey, 0xFF00FF) ;fill the hexagon with the 2nd color..
Hotkey, % A_ThisHotkey, off ;remove this hotkey now that we've already 'used' this hexagon
this.hxn.array[A_ThisHotkey].isFresh := 0
this.char.selected .= A_ThisHotkey "," ;if key is used to select hgn, add it to the list
this.game_getStatus()
}
mouse_handler(){
MouseGetPos, mx, my ;gets the current mouse postion
this.hxn_getRgn(mx,my) ;checks if mouse click was inside a hex rgn/if so, draw it
this.game_getStatus() ;checks if game is over
}
game_getStatus(){ ;checks if game is over
for key, val in this.hxn.array ;if game is over, display list of selected chars
if this.hxn.array[key].isfresh ; and play again dlg box.
return
selected := this.char.selected
selected := SubStr(selected,1, StrLen(selected)-1)
MsgBox, 0x4, Game Over, % "Order of selected hexagons: " selected "`nPlay again?"
IfMsgBox No
ExitApp
Reload
}
wndw_init(){ ;sets up and displays the window
this.wndw := {}
this.wndw.width := round(this.hxn.width * (this.hxn.colCount - 1)) + 20
this.wndw.height := round(this.hxn.height * (this.hxn.rowCount + .5)) + 20
gui, show, % "w" this.wndw.width " h" this.wndw.height, Honeycombs
this.wndw.hWnd := WinExist("A")
this.wndw.hDC := DllCall("GetDC", "Ptr", this.wndw.hWnd)
}
createBrush(color){
return DllCall("CreateSolidBrush", "UInt", color)
}
createFont(height,width,weight=800){
return DllCall("CreateFont"
,"Int", height
,"Int", width
,"Int", 0
,"Int", 0
,"Int", weight
,"UInt", 0
,"UInt", 0
,"UInt", 0
,"UInt", 0
,"UInt", 0
,"UInt", 0
,"UInt", 0
,"UInt", 0
,"Str", 0)
}
selectObject(dc, obj){ ;internal method
return DllCall("SelectObject", "Ptr", dc, "Ptr", obj)
}
deleteObject(obj){ ;used for gdi cleanup
return DllCall("DeleteObject", "Ptr", obj)
}
__Delete(){
DllCall("ReleaseDC", "Ptr", this.wndw.hWnd, "Ptr", this.wndw.hDC) ;release the window DC
this.deleteObject(this.hFont) ;delete the font
for key, val in this.hxn.array ;delete all of the hxn regions
this.deleteObject(this.hxn.array[key].rgn)
}
}[/code]