Honeycombs: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
(6 intermediate revisions by 4 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}}
Line 1,863 ⟶ 2,037:
=={{header|J}}==
 
<langsyntaxhighlight Jlang="j">require'ide/qt/gl2'
coinsert'jgl2'
 
Line 1,929 ⟶ 2,103:
}}
 
honeycomb''</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
Line 2,062 ⟶ 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,175 ⟶ 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,325 ⟶ 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,467 ⟶ 2,641:
End If
End Function
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Two players, 5 by 4.
<langsyntaxhighlight Mathematicalang="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}]];
Line 2,503 ⟶ 2,677:
cols, {If[blue, Blue, Red],
Nearest[locs, MousePosition["Graphics"]][[1]]}];
blue = ! blue]}]]</langsyntaxhighlight>
 
=={{header|MATLAB}}==
Line 2,514 ⟶ 2,688:
*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
<langsyntaxhighlight MATLABlang="matlab">function Honeycombs
nRows = 4; % Number of rows
nCols = 5; % Number of columns
Line 2,570 ⟶ 2,744:
set(axesH, 'UserData', newList)
title(newList)
end</langsyntaxhighlight>
 
=={{header|Nim}}==
Line 2,576 ⟶ 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,764 ⟶ 2,938:
let app = newApplication(Application, "Rosetta.honeycombs")
discard app.connect("activate", activate)
discard app.run()</langsyntaxhighlight>
 
 
Line 2,771 ⟶ 2,945:
The programme uses the ''Tk'' GUI toolkit.
 
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
use warnings;
use strict;
Line 2,860 ⟶ 3,034:
)->pack;
$mw->bind('<Alt-q>', sub { $btn->invoke });
MainLoop();</langsyntaxhighlight>
 
=={{header|Phix}}==
Line 2,866 ⟶ 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 3,056 ⟶ 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,275 ⟶ 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,482 ⟶ 3,656:
FreeGadget(honeycomb\gadgetID)
CloseWindow(0)
EndIf</langsyntaxhighlight>
[[File:PureBasic_Honeycomb.png]]
 
Line 3,489 ⟶ 3,663:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
 
(struct Hex (x y letter clicked?) #:mutable #:transparent)
Line 3,549 ⟶ 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,687 ⟶ 3,861:
end
end
end</langsyntaxhighlight>
 
=={{header|Scala}}==
Line 3,693 ⟶ 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,783 ⟶ 3,957:
}
 
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Perl}}
[[File:Honeycombs_sidef.png|250px|thumb|right]]
<langsyntaxhighlight lang="ruby">require('Tk')
 
class Honeycombs(
Line 3,871 ⟶ 4,045:
}
 
Honeycombs().display(title: 'Honeycombs')</langsyntaxhighlight>
 
=={{header|Tcl}}==
{{libheader|Tk}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
package require Tk
 
Line 3,961 ⟶ 4,135:
tkwait window .c
puts "overall list of characters: $chosen"
exit</langsyntaxhighlight>
 
=={{header|Wren}}==
Line 3,970 ⟶ 4,144:
 
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.
<langsyntaxhighlight ecmascriptlang="wren">import "graphics" for Canvas, Color
import "dome" for Window, Process
import "math" for Math
Line 4,082 ⟶ 4,256:
}
 
var Game = Honeycombs.new(600, 500)</langsyntaxhighlight>
 
=={{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 4,127 ⟶ 4,301:
until Counter >= Cols*Rows;
SetVid($03); \restore normal text display
]</langsyntaxhighlight>
 
{{omit from|ACL2}}
9,486

edits