How to resolve the algorithm Honeycombs step by step in the AutoHotkey programming language
How to resolve the algorithm Honeycombs step by step in the AutoHotkey programming language
Table of Contents
Problem Statement
The task is to produce a matrix of 20 hexagon shaped widgets in a honeycomb arrangement. The matrix should be arranged in such a manner that there are five columns of four hexagons. The hexagons in columns one, three and five are aligned horizontally, whereas the hexagons in columns two and four occupy a lower position within the arrangement. Each hexagon should be the same colour, and should display a unique randomly selected single capital letter on the front. The application should now wait for the user to select a hexagon, either by using a pointing device, or by pressing a key that carries a corresponding letter on a hexagon. For platforms that support pointing devices and keyboards, the application should support both methods of selection. A record of the chosen letters should be maintained and the code should be suitably commented, at the point where the the selected letter has been determined. The selected hexagon should now change colour on the display. The cycle repeats until the user has chosen all of the letters. Note that each letter can only be selected once and previously selected hexagons retain their colour after selection. The program terminates when all letters have been chosen. Optionally: output the list of selected letters and show the last selected letter, cater for a different number of columns or a different number of hexagons in each column, cater for two players, (turns alternate and the hexagons change a different colour depending on whether they were selected by player one or player two and records of both players selections are maintained.)
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Honeycombs step by step in the AutoHotkey programming language
Source code in the autohotkey programming language
Columns := 5 ; cater for a different number of columns
hexPerCol := 4 ; cater for a different number of hexagons
size := 40
w := sqrt(3) * size
h := 2 * size
Coord := [], Chosen := [], Seq:= ""
Letters := StrSplit("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
x := A_ScreenWidth/2 - w*(Columns/2) + w/2
y := A_ScreenHeight/2 - h*(hexPerCol/2) + h/2
x1:= x, y1:=y
Gdip()
; Draw High Columns
loop % Ceil(Columns/2)
{
col := A_Index
loop % hexPerCol
{
Random, rnd, 1, % Letters.count()
letter := Letters.RemoveAt(rnd)
Draw_Hexagon(x,y,size,Letter)
y += sqrt(3) * size
}
x:=x1, y:=y1
x+= col*3*size
}
; Draw Low Columns
x:= x1, y:=y1
x+= size*1.5, y += sqrt(3) * size/2
loop % Floor(Columns/2)
{
col := A_Index
loop % hexPerCol
{
Random, rnd, 1, % Letters.count()
letter := Letters.RemoveAt(rnd)
Draw_Hexagon(x,y,size,Letter)
y += sqrt(3) * size
}
x:=x1, y:=y1
x+= size*1.5, y += sqrt(3) * size/2
x+= col*3*size
}
OnExit, Exit
Return
;---------------------------------------------------------------
~LButton::
CoordMode, Mouse, Screen
MouseGetPos, mx, my, mc, mw
if (mc <> hwnd1)
return
minD := []
for L, c in coord
{
x := c.x
y := c.y
D := Sqrt((x-mx)**2 + (y-my)**2)
minD[D] := L
min := A_Index = 1 ? D : min < D ? min : D
}
thisLetter := minD[min]
gosub SelectHex
return;
;---------------------------------------------------------------
KeyPress:
thisLetter := A_ThisHotkey
gosub, SelectHex
return
;---------------------------------------------------------------
SelectHex:
if Chosen[thisLetter]
return
Chosen[thisLetter] := true ; record of chosen letters
Seq .= thisLetter ; record of the chosen letters in order chosed
pBrush := Gdip_BrushCreateSolid(0xFFFF00FF)
x := coord[thisLetter, "x"]
y := coord[thisLetter, "y"]
Draw_Hexagon(x,y,size,thisLetter, "FF000000")
Progress, % "m2 b fs16 zh0 y0 w400 x" A_ScreenWidth/2-200, % Seq,,, Courier New
if (Chosen.Count() = Columns * hexPerCol)
ExitApp
return
;---------------------------------------------------------------
Draw_Hexagon(x,y,size,Letter,TextColor:="FFFF0000"){
global
deg2rad := 3.141592653589793/180
a := x + Size "," y
b := x+ Size*Cos(60*deg2rad) "," y + Size*Sin(60*deg2rad)
c := x+ Size*Cos(120*deg2rad) "," y + Size*Sin(120*deg2rad)
d := x - Size "," y
e := x+ Size*Cos(240*deg2rad) "," y + Size*Sin(240*deg2rad)
f := x+ Size*Cos(300*deg2rad) "," y + Size*Sin(300*deg2rad)
Gdip_FillPolygon(G, pBrush, a "|" b "|" c "|" d "|" e "|" f)
Gdip_DrawLines(G, pPen, a "|" b "|" c "|" d "|" e "|" f "|" a)
Font := "Arial", Gdip_FontFamilyCreate(Font)
Options := "x" x-size/2 " y" y-size/2 "c" TextColor " Bold s" size
Gdip_TextToGraphics(G, Letter, Options, Font)
UpdateLayeredWindow(hwnd1, hdc, 0, 0, Width, Height)
Hotkey, % letter, KeyPress
Coord[letter, "x"] := x
Coord[letter, "y"] := y
}
;---------------------------------------------------------------
gdip(){
global
Gdip_Startup()
Width := A_ScreenWidth, Height := A_ScreenHeight
Gui, 1: -Caption +E0x80000 +LastFound +OwnDialogs +Owner +AlwaysOnTop
Gui, 1: Show, NA
hwnd1 := WinExist()
hbm := CreateDIBSection(Width, Height)
hdc := CreateCompatibleDC()
obm := SelectObject(hdc, hbm)
G := Gdip_GraphicsFromHDC(hdc)
Gdip_SetSmoothingMode(G, 4)
pBrush := Gdip_BrushCreateSolid(0xFFFFFF00)
pPen := Gdip_CreatePen(0xFF000000, 3)
}
;---------------------------------------------------------------
Exit:
Gdip_DeleteBrush(pBrush)
Gdip_DeletePen(pPen)
SelectObject(hdc, obm)
DeleteObject(hbm)
DeleteDC(hdc)
Gdip_DeleteGraphics(G)
Gdip_Shutdown(pToken)
ExitApp
Return
;---------------------------------------------------------------
You may also check:How to resolve the algorithm String length step by step in the AArch64 Assembly programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the Racket programming language
You may also check:How to resolve the algorithm Create a file step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the Quackery programming language
You may also check:How to resolve the algorithm Combinations with repetitions step by step in the Maple programming language