Yin and yang: Difference between revisions
Content added Content deleted
(More specific links.) |
m (→{{header|D}}: reimplemented with hexagonal board) |
||
Line 5: | Line 5: | ||
=={{header|D}}== |
=={{header|D}}== |
||
⚫ | |||
{{incorrect|D|The output does not sufficiently resemble a Yin-yang symbol.}} |
|||
⚫ | |||
enum { Void = 0, Yan = 1, Yin = 2, I = 3} ; |
enum { Void = 0, Yan = 1, Yin = 2, I = 3} ; |
||
enum Tiles = [" |
enum Tiles = [" ",".","#","?"] ; // full-width unicode char |
||
⚫ | |||
struct Hex { int x, y ; } ; |
|||
struct Coord { real x, y ; } ; |
|||
alias int delegate(Hex) Draw ; |
|||
struct Board { // a square board |
struct Board { // a square board |
||
immutable int scale ; |
immutable int scale, size ; |
||
Coord org ; // origin point coordinate |
|||
immutable int size ; |
|||
int[][] |
int[][] hex ; |
||
this(int s) { |
this(int s) { |
||
scale = s ; |
scale = s ; |
||
size = s*12 ; |
size = s*12 ; |
||
hex = new int[][](size + 3,size + 3) ; // make the board bigger |
|||
org = hex2Coord( Hex(1 + size/2 , 1 + size/2)) ; |
|||
} |
|||
static Coord hex2Coord(Hex h) { |
|||
real shiftx = (h.y % 2)* 0.5L + h.x ; |
|||
return Coord(shiftx, -h.y) ; |
|||
} |
} |
||
alias pix this ; |
|||
static string str(int v) { return Tiles[(v % 4)] ; } |
static string str(int v) { return Tiles[(v % 4)] ; } |
||
Line 28: | Line 36: | ||
string toString() { |
string toString() { |
||
string[] s ; |
string[] s ; |
||
foreach( r ; |
foreach( i, r ; hex) |
||
s ~= reduce!"a~b"(map!str(r)) ; |
s ~= (i % 2 == 1 ? Shift : "") ~ reduce!"a~b"(map!str(r)) ; |
||
return s.join("\n") ; |
return s.join("\n") ; |
||
} |
} |
||
void |
void circle(Coord center, real cr, Draw action) { |
||
auto rr = (cr*scale)^^2 ; |
auto rr = (cr*scale)^^2 ; |
||
foreach(y, ref r ; |
foreach(y, ref r ; hex) |
||
foreach( x , ref v ; r) { |
|||
auto |
auto here = hex2Coord(Hex(x, y)) ; |
||
auto |
auto dx = here.x - org.x - center.x*scale ; |
||
auto dy = here.y - org.y - center.y*scale ; |
|||
if(dx^^2 + dy^^2 <= rr) |
if(dx^^2 + dy^^2 <= rr) |
||
v = action(x) ; |
v = action(Hex(x,y)) ; |
||
} |
} |
||
} |
} |
||
Board |
Board yinYan() { |
||
foreach(r ; |
foreach(r ; hex) // clear |
||
r[] = |
r[] = Void ; |
||
circle(Coord( 0, 0),6, |
|||
⚫ | |||
(Hex h) { return (hex2Coord(h).x <= org.x) ? Yan : Yin ; } |
|||
) ; |
|||
circle(Coord( 0, 3),3, (Hex h) { return Yan ; } ) ; |
|||
circle(Coord( 0,-3),3, (Hex h) { return Yin ; } ) ; |
|||
⚫ | |||
circle(Coord( 0, 3),1, (Hex h) { return Yin ; } ) ; |
|||
return this ; |
return this ; |
||
} |
} |
||
Line 57: | Line 68: | ||
void main(string[] args) { |
void main(string[] args) { |
||
writeln(Board(2). |
writeln(Board(2).yinYan) ; |
||
writeln(Board(1). |
writeln(Board(1).yinYan) ; |
||
}</lang> |
}</lang> |
||
Output: |
Output: |
||
<pre style="line-height: |
<pre style="line-height:0.99em; font-family:Courier New;"> . |
||
........## |
|||
········# |
|||
...........## |
|||
···········## |
|||
.............### |
|||
·············## |
|||
........#.....### |
|||
········#·····### |
|||
........####....#### |
|||
········###····#### |
|||
........#####....#### |
|||
········#####····#### |
|||
.........####....##### |
|||
·········###····##### |
|||
...........#.....###### |
|||
···········#·····###### |
|||
.................####### |
|||
·················###### |
|||
................####### |
|||
················####### |
|||
...............######### |
|||
···············######## |
|||
............############# |
|||
············############# |
|||
.........############### |
|||
········############### |
|||
.......################ |
|||
·······################ |
|||
.......################# |
|||
······################# |
|||
......#####.########### |
|||
······#####·########### |
|||
.....####....######### |
|||
·····####···######### |
|||
....####.....######## |
|||
····####·····######## |
|||
....####....######## |
|||
····####···######## |
|||
...#####.######## |
|||
···#####·######## |
|||
...############# |
|||
··############# |
|||
..########### |
|||
··########### |
|||
..######## |
|||
·######## |
|||
# |
|||
⚫ | |||
· |
|||
······# |
|||
. |
|||
····#··## |
|||
.....# |
|||
····###··## |
|||
....#..## |
|||
·····#··### |
|||
....##..## |
|||
········### |
|||
.....#..### |
|||
······####### |
|||
........#### |
|||
···######## |
|||
......####### |
|||
···##·##### |
|||
....######## |
|||
··##···#### |
|||
...##.##### |
|||
··##·#### |
|||
..##..#### |
|||
·###### |
|||
..##.#### |
|||
# </pre> |
|||
.##### |
|||
#</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |