Scripts/Turnstile Rotary Enigma

From J Wiki
Jump to navigation Jump to search

The Turnstile puzzle consists of two overlapping circular disks. Each disk has 12 pieces. There are 5 bright colours used for the corners, a pair of each colour. There is one edge of each of those colours, and the remaining 6 edges are grey. The aim is to put together all pieces of the each colour.

Enigma.png

J Old Enigma

Originally appeared in earlier J JForum:general/2007-October/031026. Due to changes in gl2 was removed. Here is the old version: File:Enigma old.ijs

J Updated Enigma

Here's the new version for j601 and above. It uses a software algorithm for flood with a slightly changed interface, optimized for multiple floods at the same time. [{{#file: "enigma.ijs"}} Download script: enigma.ijs ]

NB. Enigma
NB.
NB. In this game, the wheels can be spun and the task is
NB. to return them to the original starting position.

require 'gl2'
coinsert 'jgl2'

The original contiguous algorithm was proposed by R.E. Boss. [{{#file: "enigma.ijs"}} Download script: enigma.ijs ]

NB. ---------------------------------------------------------
NB. glflood x y color - contiguous area at xy set to 16bRRGBB
NB. originally media/paint addon by Oleg Kobchneko
NB. algorithm by R.E. Boss
NB. http://www.jsoftware.com/pipermail/general/2005-August/023910.html

glflood_jgl2_=: 3 : 0
  wh=. glqwh''
  z=. (|.wh)$glqpixels 0 0,wh
  w=. contig z
  for_i. ,:^:(2>#@$) <.y do.
    q=. w=(<|.}:i){w
    z=.    (q*{:i)+(-.q)*z
  end.
  glpixels 0 0,wh,,z
)

eq=. [:(+{:)/\.&.(|.@|:) [:+/\1:,}:~:}. NB. equals
h_v=. eq&.|: ,:&, eq NB. horizontals en verticals in 2 lists

NB. a replace table is a 2 item noun of which the first is the lookup and the second the replacement
repl=. (i.~{.){ {:@] NB. replaces x. by applying replace table y. assumes that x. is contained in y. or ($0)-: x.-.y.

vth=. {.,: {: repl [:|.@|:@:> {:<@(<./)/. |: NB. replaces raveled verticals to smallest horizontals

mc=. |:@({.(<./)/.|:) NB. minimal connections
sc=. [:({.,: {: repl ])^:_ mc NB. shortcuts chaines

contig=. $$ [:{. [:(] ([:\:"1~&.|: repl) sc)^:_ [: vth h_v
contig_jgl2_=: contig f.

Coordinates are adjusted to compensate for the old 1000x1000 space.

Controls are placed at the bottom for better usability and to match the change in orientation. [{{#file: "enigma.ijs"}} Download script: enigma.ijs ]

NB. =========================================================
P=: 320 240%1000
adjx=: 1 : '([: , (,.2 2)&(m&*;._3))"1'
adj=: P adjx

showlines=: gllines
showpaint=: glflood@:,.

ENIGMA=: 0 : 0
pc enigma nosize;pn "Enigma";
xywh 2 124 36 12;cc b0 button rightscale;cn "<<";
xywh 40 124 36 12;cc b1 button leftscale rightscale;cn ">>";
xywh 84 124 36 12;cc b2 button leftscale rightscale;cn "<<";
xywh 122 124 36 12;cc b3 button leftscale rightmove;cn ">>";
xywh 0 0 160 120;cc g0 isigraph rightmove bottommove;
pas 0 2;pcenter;
rem form end;
)

enigma_run=: 3 : 0
  wd ENIGMA
  wd 'pshow'
)

enigma_close=: 3 : 0
  try. wd 'psel enigma;pclose;' catch. end.
  try. wd 'psel isview;ptop' catch. end.
)

enigma_contents_button=: wdinfo bind 'contents'

enigma_g0_paint=: 3 : 0
  glclear''
  glpen 1 0 [ glrgb 0 0 0
  glbrushnull''

  showlines   adj L1
  showlines   adj L2
  showlines"1 adj L3

  glellipse adj CRC
  glarc adj ARCS

  (adj FIXED) showpaint FDEX { COLORS
  (adj PTS)   showpaint NDX  { COLORS
)

enigma_b0_button=: glpaint@lmove0
enigma_b1_button=: glpaint@lmove1
enigma_b2_button=: glpaint@rmove0
enigma_b3_button=: glpaint@rmove1

Different paint update approach is used to match later J gl2. [{{#file: "enigma.ijs"}} Download script: enigma.ijs ]

NB. =========================================================
lmove0=: 3 : 0
  ind=. IL0{NDX
  NDX=: ind JL0 } NDX
  (adj JL0{PTS) showpaint ind{COLORS
)

lmove1=: 3 : 0
  ind=. JL1{NDX
  NDX=: ind IL1 } NDX
  (adj IL1{PTS) showpaint ind{COLORS
)

rmove0=: 3 : 0
  ind=. IR0{NDX
  NDX=: ind JR0 } NDX
  (adj JR0{PTS) showpaint ind{COLORS
)

rmove1=: 3 : 0
  ind=. JR1{NDX
  NDX=: ind IR1 } NDX
  (adj IR1{PTS) showpaint ind{COLORS
)

Constants are reformatted to be aligned and more compact. [{{#file: "enigma.ijs"}} Download script: enigma.ijs ]

NB. =========================================================
IL0=: 1 2 3 4 5 6 7 8 9 10 11 12 38 39 36 37 35 41 40 44 42 43 46 45 49 48 47 50 51 0
JL0=: 6 7 8 9 11 12 10 39 38 35 36 37 40 41 44 42 43 46 45 49 47 48 50 51 0 2 1 3 4 5
IL1=: _5|.|.IL0
JL1=: _5|.|.JL0

IR0=: 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 10
JR0=: 16 17 18 19 21 22 20 24 23 25 26 27 28 29 31 32 30 33 34 35 36 37 39 38 11 12 10 13 14 15
IR1=: _5|.|.IR0
JR1=: _5|.|.JR0

t=. ' 000000 0000ff 00ff00 ff0000 00ffff ff00ff ffff00 ffffff'
t=. 0".'16b',"1];._1 t
COLORS=: 2 3 5 6 1 4{t

L1=:    500 650 500 350 320 240 140 350 140 650 320 760
L1=: L1,500 650 680 760 860 650 860 350 680 240 500 350

L2=:    10 280 10 730 320 950 500 840 680 950 990 730 990 280
L2=: L2, 680 50 500 160 320 50 10 280

L3=: 0&".;._2 (0 : 0)
 10 280 140 350
 10 730 140 650
320  50 320 240
320 760 320 950
990 280 860 350
990 730 860 650
680  50 680 240
680 760 680 950
)

CRC=:      45  133 550 733
CRC=: CRC,:415 133 550 733

Arc segments had to be swapped to match change in orientation. [{{#file: "enigma.ijs"}} Download script: enigma.ijs ]

ARCS=: 0&".;._2 (0 : 0)
_315  133 550 733 230 925 230  75
 765  133 550 733 765  55 765 935
 225 _305 550 733 300 125 700 125
 225  571 550 733 700 875 300 872
_135 _305 550 733  68 340 940 340
_135  571 550 733 940 670  65 650
 585 _305 550 733  60 350 940 350
 585  571 550 733 940 650  65 650
)

PTS=: 0&".;._2 (0 : 0)
130 350 140 340 150 350 230 280 230 310
310 240 330 240 320 250 410 280 410 310
490 350 500 340 510 350 590 280 590 310
670 240 690 240 680 250 770 280 770 310
850 350 860 340 870 350 850 500 870 500
850 650 870 650 860 660 770 690 770 720
670 770 680 750 690 770 590 690 590 720
490 650 510 650 500 670 490 500 510 500
410 690 410 720 310 760 320 750 330 760
230 690 230 720 130 650 150 650 140 660
)
PTS=: _2 [\ (,PTS),     130 500 150 500

NDX=:     0 1 4 1 4 1 2 4 2 4 4 2 5 2 5 2 3 5 3 5 5 3 0 5 0
NDX=: NDX,5 0 1 5 1 2 5 1 5 2 4 5 2 4 5 4 2 3 4 2 4 3 0 4 3 0 4

FIXED=: 25 300 25 280 330 70 965 270 965 300 965 730
FIXED=: _2[\FIXED,    670 910 25 730 320 500 680 500

FDEX=: 0 1 2 3 0 1 2 3 4 5

enigma_run''

See Also


CategoryGames CategoryLiterate[[Category:Games R.2.1.10]