Honeycombs: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
(11 intermediate revisions by 7 users not shown)
Line 14:
 
Honeycomb class:
<syntaxhighlight lang="actionscript3">
<lang ActionScript3>
package {
Line 229:
 
}
</syntaxhighlight>
</lang>
 
Document (main) class:
<syntaxhighlight lang="actionscript3">
<lang ActionScript3>
package {
Line 399:
 
}
</syntaxhighlight>
</lang>
 
=={{header|Ada}}==
{{libheader|SDLAda}}
<langsyntaxhighlight Adalang="ada">with Ada.Numerics.Elementary_Functions;
with Ada.Numerics.Discrete_Random;
 
Line 607:
Window.Finalize;
SDL.Finalise;
end Honeycombs;</langsyntaxhighlight>
=={{header|AutoHotkey}}==
Requires [https://www.autohotkey.com/boards/viewtopic.php?f=6&t=6517&start=320 Gdip Library]
<langsyntaxhighlight AutoHotkeylang="autohotkey">Columns := 5 ; cater for a different number of columns
hexPerCol := 4 ; cater for a different number of hexagons
size := 40
Line 741:
ExitApp
Return
;---------------------------------------------------------------</langsyntaxhighlight>
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> ALTERNATE = 1
VDU 23,22,252;252;8,16,16,128
*FONT Arial,24,B
Line 827:
SYS "TextOut", @memhdc%, x%-size.dx%/2, y%-size.dy%/2, text$, LEN(text$)
SYS "CreatePolygonRgn", ^pt%(0,0), 6, ALTERNATE TO hrgn%
= hrgn%</langsyntaxhighlight>
[[File:honeycombs_bbc.gif]]
 
=={{header|C}}==
 
<syntaxhighlight lang="c">
<lang C>
 
/* Program for gtk3 */
Line 1,102:
return EXIT_SUCCESS;
}
</syntaxhighlight>
</lang>
 
=={{header|C sharp}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 1,302:
}
}
}</langsyntaxhighlight>
XAML:
<langsyntaxhighlight lang="xml"><Window x:Class="Honeycombs.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Line 1,316:
<Canvas x:Name="HoneycombCanvas" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Window></langsyntaxhighlight>
[[File:CSharpHoneycomb.jpg]]
 
 
 
=={{header|C++}}==
{{libheader|Qt}}
<langsyntaxhighlight lang="cpp">//
// honeycombwidget.h
//
Line 1,350 ⟶ 1,352:
};
 
#endif // HONEYCOMBWIDGET_H</langsyntaxhighlight>
<langsyntaxhighlight lang="cpp">//
// honeycombwidget.cpp
//
Line 1,451 ⟶ 1,453:
update(cell->polygon.boundingRect());
}
}</langsyntaxhighlight>
<langsyntaxhighlight lang="cpp">//
// main.cpp
//
Line 1,463 ⟶ 1,465:
w.show();
return a.exec();
}</langsyntaxhighlight>
 
{{out}}
[[Media:Honeycombs_qt.png]]
Screenshot: [https://slack-files.com/T0CNUL56D-F0173FUB31P-857ea545b3 honeycombs_qt.png] (offsite PNG image)
 
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
[[File:DelphiHoneycomb.png|frame|none]]
 
You can click on any hexagon in the honeycomb and it will be highlighted purple.
 
<syntaxhighlight lang="Delphi">
 
 
{Structure containing information for one hexagon}
 
type THexagon = record
Points: array [0..6-1] of TPoint;
Center: TPoint;
Letter: Char;
Selected: boolean;
end;
 
{Array of hexagons}
 
var Hexagons: array of array of THexagon;
 
 
function PointInPolygon(Point: TPoint; const Polygon: array of TPoint): Boolean;
{Test if point is in the polygon}
var Rgn: HRGN;
begin
Rgn := CreatePolygonRgn(Polygon[0], Length(Polygon), WINDING);
Result := PtInRegion(rgn, Point.X, Point.Y);
DeleteObject(rgn);
end;
 
 
function HitTest(X,Y: integer; var Col,Row: integer): boolean;
{Find hexagon that X,Y may be in}
{Return a hexagon found, Hexagon is in specified by Col, Row}
var C,R: integer;
begin
Result:=True;
for R:=0 to High(Hexagons[0]) do
for C:=0 to High(Hexagons) do
if PointInPolygon(Point(X,Y),Hexagons[C,R].Points) then
begin
Col:=C; Row:=R;
exit;
end;
Result:=False;
end;
 
 
 
procedure BuildHoneyComb(Pos: TPoint; Radius: integer);
{Build honeycombo from hexagons}
var XStep,YStep: integer;
var Off: TPoint;
var Col,Row: integer;
var Cnt: integer;
 
procedure SetHexagon(var Hex: THexagon; Pos: TPoint);
{Set the points for one hexagon}
begin
Hex.Center:=Pos;
Hex.Points[0]:=Point(Pos.X-Radius,Pos.Y);
Hex.Points[1]:=Point(Pos.X-XStep,Pos.Y-YStep);
Hex.Points[2]:=Point(Pos.X+XStep,Pos.Y-YStep);
Hex.Points[3]:=Point(Pos.X+Radius,Pos.Y);
Hex.Points[4]:=Point(Pos.X+XStep,Pos.Y+YStep);
Hex.Points[5]:=Point(Pos.X-XStep,Pos.Y+YStep);
{Assign one char to hexagon, A..Z in order created}
Hex.Letter:=Char(Cnt+$41);
{Deselect hexagon}
Hex.Selected:=False;
Inc(Cnt);
end;
 
procedure RandomizeChars;
{Randomize the characters}
var X1,Y1,X2,Y2: integer;
var C: char;
begin
for X1:=0 to High(Hexagons) do
for Y1:=0 to High(Hexagons[0]) do
begin
X2:=Random(Length(Hexagons));
Y2:=Random(Length(Hexagons[0]));
C:=Hexagons[X1,Y1].Letter;
Hexagons[X1,Y1].Letter:=Hexagons[X2,Y2].Letter;
Hexagons[X2,Y2].Letter:=C;
end;
end;
 
 
begin
Cnt:=0;
{Set number of hexagons in honey comb}
SetLength(Hexagons,5,4);
{Values to set the corners of the hexagon}
XStep:=Round(Radius / 2);
YStep:=Round(Radius * 0.866025403784438646);
for Col:=0 to High(Hexagons) do
for Row:=0 to High(Hexagons[0]) do
begin
{Calculate the position of hexagon in honeycomb}
Off.X:=Pos.X+(Radius+XStep) * Col;
Off.Y:=Pos.Y+YStep*Row*2;
if (Col and 1)=1 then Off.Y:=Off.Y + YStep;
{Set hexagon in honeycomb}
SetHexagon(Hexagons[Col,Row],Off);
end;
RandomizeChars;
end;
 
 
procedure DrawHoneyComb(Canvas: TCanvas);
{Draw polygons describing honeycomb}
var Col,Row: integer;
var Hex: THexagon;
var FS: TSize;
begin
Canvas.Pen.Width:=4;
Canvas.Font.Size:=20;
Canvas.Font.Style:=[fsBold];
Canvas.Font.Name:='Arial';
FS:=Canvas.TextExtent('M');
for Col:=0 to High(Hexagons) do
for Row:=0 to High(Hexagons[0]) do
begin
Hex:=Hexagons[Col,Row];
if Hex.Selected then Canvas.Brush.Color:=clFuchsia
else Canvas.Brush.Color:=clYellow;
Canvas.Polygon(Hex.Points);
Canvas.TextOut(Hex.Center.X-FS.CX div 2,Hex.Center.Y-FS.CY div 2,Hex.Letter);
end;
end;
 
 
 
procedure ShowHoneycomb(Image: TImage);
var MW: TMouseWaiter;
var MI: TMouseData;
var Row,Col: integer;
begin
MW:=TMouseWaiter.Create(TWinControl(Image));
Image.Canvas.Pen.Width:=3;
BuildHoneyComb(Point(140,90),40);
DrawHoneyComb(Image.Canvas);
Image.Canvas.Brush.Color:=clWhite;
Image.Canvas.TextOut(10,10,'Click outside honeycomb to terminate');
Image.Invalidate;
while true do
begin
MI:=MW.WaitForMouse;
if HitTest(MI.X,MI.Y,Col,Row) then
begin
Hexagons[Col,Row].Selected:=True;
DrawHoneyComb(Image.Canvas);
Image.Invalidate;
end
else break;
if Application.Terminated then break;
end;
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
 
</pre>
 
=={{header|Go}}==
{{libheader|raylib-go}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,593 ⟶ 1,767:
 
rl.CloseWindow()
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
{{libheader|gloss}}
{{libheader|random-shuffle}}
<langsyntaxhighlight Haskelllang="haskell">import Data.Char (toUpper)
import Data.Function (on)
import Data.List (zipWith4)
Line 1,738 ⟶ 1,912:
(return . drawWorld)
handleInput
(\_ x -> return x)</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
Line 1,749 ⟶ 1,923:
 
Label selection is straight forward. Mouse selection first determines if x,y is within the widgets rectangular outer bounds. The x,y point is then reflected into the north west quadrant of the cell and the helper data is used to calculate an abbreviated cross-product (x and y will always be 0). The direction of the resultant z indicates if the point is inside or outside of the widgets inner bounds.
<langsyntaxhighlight Iconlang="icon">link printf
 
procedure main(A)
Line 1,856 ⟶ 2,030:
return W
}
end</langsyntaxhighlight>
 
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/printf.icn printf.icn provides formatting]
 
=={{header|J}}==
 
<syntaxhighlight lang="j">require'ide/qt/gl2'
coinsert'jgl2'
 
NB. corners of a unit hexagon
ucorn=: +.^j.2p1*(%~i.)6
 
NB. center to center offset for columns and rows
uof=: %:2.3 3
 
shape=: 4 5
scale=: 50
scorn=: scale*ucorn
centers=: scale*(((%:0 0.75)*/~2|{:"1)+uof*"1 1:+|."1)shape#:i.shape
 
honeycomb=: {{)n
pc honeycomb nosize closeok;
minwh 460 460;
cc hex isidraw flush;
pshow;
}}{{
sel=: 0"0 grid=: shape$u:65+(*/shape)?26
txt=:''
wd m
draw y
}}
 
draw=: {{
glfill 0 0 0 255
hex"0 i.$grid
gltextcolor glrgb 0 0 255
glfont 'courier 24'
gltextxy 60 10
glpaint gltext txt
}}
 
hex=: {{
coord=. <shape#:y
center=. coord{centers
glbrush glrgb (-coord{sel)|.255 255 0
glrgb 255 255 255
glpen 3,PS_SOLID
glpolygon,<.center+"1 scorn
gltextcolor glrgb 255 0 0
glfont 'courier 24 bold'
gltextxy <.center-10
gltext coord{grid
}}
 
select_character=: {{
if. -. y{,sel do. NB. the selected character has been determined
txt=:txt, y{,grid
end.
draw sel=: 1 (y"_)} sel
}}
 
honeycomb_hex_char=: {{
ch=. toupper {.sysdata
if. ch e. ,grid do. select_character (,grid)i.ch end.
}}
 
honeycomb_hex_mbldown=:{{
xy=. 2{.".sysdata
k=. (i.<./)dist=.,+/&.:*:"1 centers-"1 xy
if. scale > k{dist do.select_character k end.
}}
 
honeycomb''</syntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
Line 1,992 ⟶ 2,236:
g.drawString(s, x, y);
}
}</langsyntaxhighlight>
 
=={{header|Julia}}==
Uses Cairo and Gtk for graphics. Tasks done include the optional one of recording and then displaying letters as chosen with mouse or keyboard on exit once all letters are chosen.
<langsyntaxhighlight lang="julia">using Gtk.ShortNames, GtkReactive, Graphics, Cairo, Colors, Random
 
mutable struct Hexagon
Line 2,105 ⟶ 2,349:
exit()
 
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
This is a translation of the Java entry except that code has been added to end the program automatically when all the hexagons have been selected.
<langsyntaxhighlight lang="scala">// version 1.1.4
 
import java.awt.BasicStroke
Line 2,255 ⟶ 2,499:
}
}
}</langsyntaxhighlight>
 
=={{header|Liberty BASIC}}==
By Andy Amaya, Sept. 24, 2015 -- with thanks from the Liberty BASIC Community.
<syntaxhighlight lang="lb">
<lang lb>
NoMainWin
Dim hxc(20,2), ltr(26)
Line 2,397 ⟶ 2,641:
End If
End Function
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Two players, 5 by 4.
<syntaxhighlight lang="mathematica">hexagon[{x_, y_}] :=
Polygon[Transpose[{{1/2, 1/4, -1/4, -1/2, -1/4, 1/4} +
x, {0, Sqrt[3]/4, Sqrt[3]/4, 0, -Sqrt[3]/4, -Sqrt[3]/4} + y}]];
off = Transpose[{ConstantArray[0, 20], {0, 0, 0, 0, Sqrt[3]/4,
Sqrt[3]/4, Sqrt[3]/4, Sqrt[3]/4, 0, 0, 0, 0, Sqrt[3]/4,
Sqrt[3]/4, Sqrt[3]/4, Sqrt[3]/4, 0, 0, 0, 0}}];
DynamicModule[{letters = RandomSample[CharacterRange["A", "Z"], 20],
blue = False, cols = {},
locs = Tuples[{Range[1, 4, 3/4],
Range[1, 1 + (3 Sqrt[3])/2, Sqrt[3]/2]}] - off},
EventHandler[
Dynamic[Graphics[{EdgeForm[{Thick, Black}], LightGray,
hexagon /@ locs, {#[[1]], hexagon[#[[2]]]} & /@ cols, Black,
MapThread[
Text, {Style[#, FontSize -> Large] & /@ letters, locs}], Red,
Text[Style[
StringJoin[
letters[[FirstPosition[locs, #[[2]]][[1]]]] & /@
Cases[cols, {Red, _}][[All, 2]]],
FontSize -> 40], {5/2, -1/2}, {Right, Center}], Blue,
Text[Style[
StringJoin[
letters[[FirstPosition[locs, #[[2]]][[1]]]] & /@
Cases[cols, {Blue, _}][[All, 2]]],
FontSize -> 40], {5/2, -1/2}, {Left, Center}]},
PlotRange -> {{-1, 6}, Automatic},
ImageSize -> Large]], {"MouseClicked" :>
If[! MemberQ[cols[[All, 2]],
Nearest[locs, MousePosition["Graphics"]][[1]]],
AppendTo[
cols, {If[blue, Blue, Red],
Nearest[locs, MousePosition["Graphics"]][[1]]}];
blue = ! blue]}]]</syntaxhighlight>
 
=={{header|MATLAB}}==
Simple version
*Click-play, no entering letters
*One-player
*Prints choices list to figure title, no output
*Easy to change number of rows or columns
*No support for different numbers of hexagons in different columns
*Works for up to 26 hexagons before using lowercase labels, 52 hexagons before repeating letters
*Works off of mouse-click events, so program doesn't need to "quit" as it is not really running constantly
<syntaxhighlight lang="matlab">function Honeycombs
nRows = 4; % Number of rows
nCols = 5; % Number of columns
nHexs = nRows*nCols; % Number of hexagons
rOuter = 1; % Circumradius
startX = 0; % x-coordinate of upper left hexagon
startY = 0; % y-coordinate of upper left hexagon
delX = rOuter*1.5; % Horizontal distance between hexagons
delY = rOuter*sqrt(3); % Vertical distance between hexagons
offY = delY/2; % Vertical offset between columns
genHexX = rOuter.*cos(2.*pi.*(0:5).'./6); % x-coords of general hexagon
genHexY = rOuter.*sin(2.*pi.*(0:5).'./6); % y-coords of general hexagon
centX = zeros(1, nHexs); % x-coords of hexagon centers
centY = zeros(1, nHexs); % y-coords of hexagon centers
for c = 1:nCols
idxs = (c-1)*nRows+1:c*nRows; % Indeces of hexagons in that column
if mod(c, 2) % Odd numbered column - higher y-values
centY(idxs) = startY:-delY:startY-delY*(nRows-1);
else % Even numbered column - lower y-values
centY(idxs) = startY-offY:-delY:startY-offY-delY*(nRows-1);
end
centX(idxs) = (startX+(c-1)*delX).*ones(1, nRows);
end
[MCentX, MGenHexX] = meshgrid(centX, genHexX);
[MCentY, MGenHexY] = meshgrid(centY, genHexY);
HexX = MCentX+MGenHexX; % x-coords of hexagon vertices
HexY = MCentY+MGenHexY; % y-coords of hexagon vertices
figure
hold on
letters = char([65:90 97:122]);
randIdxs = randperm(26);
letters = [letters(randIdxs) letters(26+randIdxs)];
hexH = zeros(1, nHexs);
for k = 1:nHexs % Create patches individually
hexH(k) = patch(HexX(:, k), HexY(:, k), [1 1 0]);
textH = text(centX(k), centY(k), letters(mod(k, length(letters))), ...
'HorizontalAlignment', 'center', 'FontSize', 14, ...
'FontWeight', 'bold', 'Color', [1 0 0], 'HitTest', 'off');
set(hexH(k), 'UserData', textH) % Save to object for easy access
end
axis equal
axis off
set(gca, 'UserData', '') % List of clicked patch labels
set(hexH, 'ButtonDownFcn', @onClick)
end
 
function onClick(obj, event)
axesH = get(obj, 'Parent');
textH = get(obj, 'UserData');
set(obj, 'FaceColor', [1 0 1]) % Change color
set(textH, 'Color', [0 0 0]) % Change label color
set(obj, 'HitTest', 'off') % Ignore future clicks
currList = get(axesH, 'UserData'); % Hexs already clicked
newList = [currList get(textH, 'String')]; % Update list
set(axesH, 'UserData', newList)
title(newList)
end</syntaxhighlight>
 
=={{header|Nim}}==
Line 2,403 ⟶ 2,750:
This program is largely inspired by the C version, but this is not a direct translation. We have reused many parts of the C version, for instance the computations and the way to draw the cells. But we have also introduced a “honeycomb” object, improved somewhat the drawing of the labels and taken advantage of the high level binding to Gtk3 provided by “gintro”.
 
<langsyntaxhighlight Nimlang="nim">import lenientops, math, random, sequtils, strutils, tables
 
import gintro/[gobject, gdk, gtk, gio, cairo]
Line 2,591 ⟶ 2,938:
let app = newApplication(Application, "Rosetta.honeycombs")
discard app.connect("activate", activate)
discard app.run()</langsyntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Two players, 5 by 4.
<lang Mathematica>hexagon[{x_, y_}] :=
Polygon[Transpose[{{1/2, 1/4, -1/4, -1/2, -1/4, 1/4} +
x, {0, Sqrt[3]/4, Sqrt[3]/4, 0, -Sqrt[3]/4, -Sqrt[3]/4} + y}]];
off = Transpose[{ConstantArray[0, 20], {0, 0, 0, 0, Sqrt[3]/4,
Sqrt[3]/4, Sqrt[3]/4, Sqrt[3]/4, 0, 0, 0, 0, Sqrt[3]/4,
Sqrt[3]/4, Sqrt[3]/4, Sqrt[3]/4, 0, 0, 0, 0}}];
DynamicModule[{letters = RandomSample[CharacterRange["A", "Z"], 20],
blue = False, cols = {},
locs = Tuples[{Range[1, 4, 3/4],
Range[1, 1 + (3 Sqrt[3])/2, Sqrt[3]/2]}] - off},
EventHandler[
Dynamic[Graphics[{EdgeForm[{Thick, Black}], LightGray,
hexagon /@ locs, {#[[1]], hexagon[#[[2]]]} & /@ cols, Black,
MapThread[
Text, {Style[#, FontSize -> Large] & /@ letters, locs}], Red,
Text[Style[
StringJoin[
letters[[FirstPosition[locs, #[[2]]][[1]]]] & /@
Cases[cols, {Red, _}][[All, 2]]],
FontSize -> 40], {5/2, -1/2}, {Right, Center}], Blue,
Text[Style[
StringJoin[
letters[[FirstPosition[locs, #[[2]]][[1]]]] & /@
Cases[cols, {Blue, _}][[All, 2]]],
FontSize -> 40], {5/2, -1/2}, {Left, Center}]},
PlotRange -> {{-1, 6}, Automatic},
ImageSize -> Large]], {"MouseClicked" :>
If[! MemberQ[cols[[All, 2]],
Nearest[locs, MousePosition["Graphics"]][[1]]],
AppendTo[
cols, {If[blue, Blue, Red],
Nearest[locs, MousePosition["Graphics"]][[1]]}];
blue = ! blue]}]]</lang>
 
=={{header|MATLAB}}==
Simple version
*Click-play, no entering letters
*One-player
*Prints choices list to figure title, no output
*Easy to change number of rows or columns
*No support for different numbers of hexagons in different columns
*Works for up to 26 hexagons before using lowercase labels, 52 hexagons before repeating letters
*Works off of mouse-click events, so program doesn't need to "quit" as it is not really running constantly
<lang MATLAB>function Honeycombs
nRows = 4; % Number of rows
nCols = 5; % Number of columns
nHexs = nRows*nCols; % Number of hexagons
rOuter = 1; % Circumradius
startX = 0; % x-coordinate of upper left hexagon
startY = 0; % y-coordinate of upper left hexagon
delX = rOuter*1.5; % Horizontal distance between hexagons
delY = rOuter*sqrt(3); % Vertical distance between hexagons
offY = delY/2; % Vertical offset between columns
genHexX = rOuter.*cos(2.*pi.*(0:5).'./6); % x-coords of general hexagon
genHexY = rOuter.*sin(2.*pi.*(0:5).'./6); % y-coords of general hexagon
centX = zeros(1, nHexs); % x-coords of hexagon centers
centY = zeros(1, nHexs); % y-coords of hexagon centers
for c = 1:nCols
idxs = (c-1)*nRows+1:c*nRows; % Indeces of hexagons in that column
if mod(c, 2) % Odd numbered column - higher y-values
centY(idxs) = startY:-delY:startY-delY*(nRows-1);
else % Even numbered column - lower y-values
centY(idxs) = startY-offY:-delY:startY-offY-delY*(nRows-1);
end
centX(idxs) = (startX+(c-1)*delX).*ones(1, nRows);
end
[MCentX, MGenHexX] = meshgrid(centX, genHexX);
[MCentY, MGenHexY] = meshgrid(centY, genHexY);
HexX = MCentX+MGenHexX; % x-coords of hexagon vertices
HexY = MCentY+MGenHexY; % y-coords of hexagon vertices
figure
hold on
letters = char([65:90 97:122]);
randIdxs = randperm(26);
letters = [letters(randIdxs) letters(26+randIdxs)];
hexH = zeros(1, nHexs);
for k = 1:nHexs % Create patches individually
hexH(k) = patch(HexX(:, k), HexY(:, k), [1 1 0]);
textH = text(centX(k), centY(k), letters(mod(k, length(letters))), ...
'HorizontalAlignment', 'center', 'FontSize', 14, ...
'FontWeight', 'bold', 'Color', [1 0 0], 'HitTest', 'off');
set(hexH(k), 'UserData', textH) % Save to object for easy access
end
axis equal
axis off
set(gca, 'UserData', '') % List of clicked patch labels
set(hexH, 'ButtonDownFcn', @onClick)
end
 
function onClick(obj, event)
axesH = get(obj, 'Parent');
textH = get(obj, 'UserData');
set(obj, 'FaceColor', [1 0 1]) % Change color
set(textH, 'Color', [0 0 0]) % Change label color
set(obj, 'HitTest', 'off') % Ignore future clicks
currList = get(axesH, 'UserData'); % Hexs already clicked
newList = [currList get(textH, 'String')]; % Update list
set(axesH, 'UserData', newList)
title(newList)
end</lang>
 
=={{header|Perl}}==
Line 2,700 ⟶ 2,945:
The programme uses the ''Tk'' GUI toolkit.
 
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
use warnings;
use strict;
Line 2,789 ⟶ 3,034:
)->pack;
$mw->bind('<Alt-q>', sub { $btn->invoke });
MainLoop();</langsyntaxhighlight>
 
=={{header|Phix}}==
Line 2,795 ⟶ 3,040:
In the 2 player game, the string chosen contains the selections of both players: odd chars = player 1, even chars = player 2.<br>
Included in the distribution as demo\rosetta\honeycomb.exw
<langsyntaxhighlight Phixlang="phix">include ..\arwen\arwen.ew
include ..\arwen\axtra.ew
 
Line 2,985 ⟶ 3,230:
setHandler(main,routine_id("mainHandler"))
 
WinMain(main, SW_NORMAL)</langsyntaxhighlight>
 
=={{header|Prolog}}==
Works with SWI-Prolog and XPCE.
<langsyntaxhighlight Prologlang="prolog">honeycomb :-
new(W, window('Honeycomb')),
new(Counter, counter(20)),
Line 3,204 ⟶ 3,449:
:- pce_end_class(cell).
 
</syntaxhighlight>
</lang>
[[File:Prolog_honeycombs.png|400px]]
 
=={{header|PureBasic}}==
Requires PureBasic v4.60. Screen controls in PureBasic are referred to as 'gadgets'.
<langsyntaxhighlight PureBasiclang="purebasic">Structure hexGadget
text.s
Status.i ;nonselected = 0, selected = 1
Line 3,411 ⟶ 3,656:
FreeGadget(honeycomb\gadgetID)
CloseWindow(0)
EndIf</langsyntaxhighlight>
[[File:PureBasic_Honeycomb.png]]
 
Line 3,418 ⟶ 3,663:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
 
(struct Hex (x y letter clicked?) #:mutable #:transparent)
Line 3,478 ⟶ 3,723:
 
(displayln "The letters were chosen in the order:")
(for-each display (add-between (reverse letters-chosen) " "))</langsyntaxhighlight>
 
=={{header|Ruby}}==
{{libheader|Shoes}}
<langsyntaxhighlight lang="ruby">Shoes.app(title: "Honeycombs", height: 700, width: 700) do
C = Math::cos(Math::PI/3)
S = Math::sin(Math::PI/3)
Line 3,616 ⟶ 3,861:
end
end
end</langsyntaxhighlight>
 
=={{header|Scala}}==
Line 3,622 ⟶ 3,867:
{{libheader|Scala Java Swing interoperability}}
{{works with|Scala|2.13}}
<langsyntaxhighlight Scalalang="scala">import java.awt.{BasicStroke, BorderLayout, Color, Dimension,
Font, FontMetrics, Graphics, Graphics2D, Point, Polygon, RenderingHints}
import java.awt.event.{KeyAdapter, KeyEvent, MouseAdapter, MouseEvent}
Line 3,712 ⟶ 3,957:
}
 
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Perl}}
[[File:Honeycombs_sidef.png|250px|thumb|right]]
<langsyntaxhighlight lang="ruby">require('Tk')
 
class Honeycombs(
Line 3,800 ⟶ 4,045:
}
 
Honeycombs().display(title: 'Honeycombs')</langsyntaxhighlight>
 
=={{header|Tcl}}==
{{libheader|Tk}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
package require Tk
 
Line 3,890 ⟶ 4,135:
tkwait window .c
puts "overall list of characters: $chosen"
exit</langsyntaxhighlight>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|DOME}}
{{libheader|Wren-polygon}}
Unlike the Kotlin entry, the 20 capital letters are now chosen at random from the available 26.
 
The following script uses a font called ''memory.ttf''. If this is not present in your DOME distribution, it can be downloaded from [https://github.com/domeengine/dome/raw/main/examples/fonts/memory.ttf here] and should be placed in the same directory as the script itself.
<syntaxhighlight lang="wren">import "graphics" for Canvas, Color
import "dome" for Window, Process
import "math" for Math
import "font" for Font
import "input" for Mouse, Keyboard
import "random" for Random
import "./polygon" for Polygon
 
var Rand = Random.new()
 
class Hexagon is Polygon {
static baseColor { Color.yellow }
static selectedColor { Color.pink }
 
construct new(x, y, halfWidth, letter) {
_x = x
_y = y
_letter = letter
var vertices = List.filled(6, null)
for (i in 0..5) {
var vx = x + halfWidth * Math.cos(i * Num.pi / 3)
var vy = y + halfWidth * Math.sin(i * Num.pi / 3)
vertices[i] = [vx, vy]
}
super(vertices, "")
_selected = false
}
 
letter { _letter }
selected { _selected }
selected=(v) { _selected = v }
 
draw() {
var col = selected ? Hexagon.selectedColor : Hexagon.baseColor
drawfill(col)
super.draw(Color.black)
col = selected ? Color.black : Color.red
Canvas.print(_letter, _x - 8, _y - 8, col)
}
}
 
class Honeycombs {
construct new(width, height) {
Window.title = "Honeycombs"
Window.resize(width, height)
Canvas.resize(width, height)
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toList
Rand.shuffle(letters)
_letters = letters[0..19]
_x1 = 150
_y1 = 100
_x2 = 225
_y2 = 143
_w = 150
_h = 87
_hs = null
_comb = List.filled(20, null)
Font.load("memory", "memory.ttf", 48)
Font["memory"].antialias = true
Canvas.font = "memory"
}
 
drawHexagons() {
for (i in 0..._comb.count) {
var x
var y
if (i < 12) {
x = _x1 + (i % 3) * _w
y = _y1 + (i / 3).floor * _h
} else {
x = _x2 + (i % 2) * _w
y = _y2 + ((i - 12) / 2).floor * _h
}
_comb[i] = Hexagon.new(x, y, (_w / 3).floor, _letters[i])
_comb[i].draw()
}
}
 
allSelected() { _comb.all { |h| h.selected } }
 
init() {
drawHexagons()
}
 
update() {
_hs = null
if (Mouse.isButtonPressed("left")) {
for (h in _comb) {
if (h.contains(Mouse.position.x, Mouse.position.y)) {
_hs = h
break
}
}
} else if (Keyboard.allPressed.count > 0) {
for (h in _comb) {
if (Keyboard.isKeyDown(h.letter)) {
_hs = h
break
}
}
}
}
 
draw(alpha) {
if (_hs) {
_hs.selected = true
_hs.draw()
if (allSelected()) Process.exit(0)
}
}
}
 
var Game = Honeycombs.new(600, 500)</syntaxhighlight>
 
=={{header|XPL0}}==
[[File:HoneyXPL0.gif|right]]
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\stdlib; \(color definitions, etc.)
 
proc DrawHexagon(X0, Y0, Side, Color); \Draw a filled hexagon centered at X0,Y0
Line 3,935 ⟶ 4,301:
until Counter >= Cols*Rows;
SetVid($03); \restore normal text display
]</langsyntaxhighlight>
 
{{omit from|ACL2}}
9,477

edits