User:Brian Schott/code/SherlockEinsteinSandbox

From J Wiki
Jump to: navigation, search

Sherlock Einstein Sandbox

Grid.jpg

The Sherlock Puzzle was introduced to the JChat forum by Bjorn and he later explained that it is often called the Einstein Puzzle. I have not found a Wikipedia entry for the puzzle, however. I found a very playable version for the Mac and have gotten addicted.

Windows: http://www.jsoftware.com/pipermail/chat/2008-January/000901.html
Mac: http://games.flowix.com/en/index.html

The Mac version of the game is very playable, but I wanted a digital sandbox where I can try play options. The resulting script creates a J grid rendering of the Einstein board which reacts only to left and right mouse clicks -- no direct cell entry is possible. The idea is the following.

  • a left click selects a marker/symbol if the cell is not blank, but toggles to the original marker/symbol if the cell is blank
  • a right click blanks out a marker/symbol in a cell or restores the original marker/symbol to a blank cell.

A slick feature of the original grid class is that multiple independent grids can be created at once, so that multiple conflicting trials cases can be explored.

One unfortunate feature for my Mac is that a right click must be done from the mouse and not from a Command+Click as other applications allow. I have not noticed this limitation for other (non-J) applications.

Warning: The unicode symbol for a pentagon is supposed to be 16b2b20, but does not seem to work for me so I have used logical and, 16b2227; and I have used 16b2228, logical or, for the inverted pentagon in the noun poly0.

grid CELLDATA[CELLHIGH_jzgrid_ =: 12 18$0

After running the script below, type in the line above for each additional grid you want.

NB. einstein.ijs
NB. 2/23/8

require 'grid'

t      =: 4&u:  NB. unicode from hex
box    =: <"0
copies =: $,:
prevoke =: ".@(''''&,)@(,&'''~')
prevoke =: ''''".@,'''~',~]
shape  =: 2 3&$
seq6   =: +&0 1 2 3 4 5

roman0 =: 16b2160
dice0  =: 16b2680
NB. a pentagon should be 16b2b20, but is black square in J
NB. so 16b2227 is used in its place and 2228 is a sub too
poly0  =: 16b2206 16b2207 16b25fb 16b25c7 16b2227 16b2228
math0  =: 16b002b 16b2212 16b00f7 16b00d7 16b003d 16b221a

number =: shape '123456'
alpha  =: shape 'ABCDEF'
roman  =: t shape seq6 roman0
dice   =: t shape seq6 dice0
poly   =: t shape      poly0
math   =: t shape      math0

icons  =: ;:'number alpha roman dice poly math'
column =: <.@(%&3)

NB. grid CELLDATA =: ,./6 copies ,/box&> prevoke each  icons

CELLCOLOR_jzgrid_ =: 3# 6$0 1
CELLHIGH_jzgrid_ =: 12 18$0
Blank_jzgrid_   =: <' '
rows_jzgrid_    =: ,]+1 _1{~2&|
match_jzgrid_   =: -:|.
group_jzgrid_   =: 0 1 2+3*<.@(%&3)
mbldown_jzgrid_ =: monad define
  mouseset''
  'Px Py Row Col Ctrl Shift'=: mousepos''

  if. Blank -: (<Row,Col) { CELLDATA
  do. CELLDATA=: ((<Row,Col) { CELLDATA__) (<Row,Col) } CELLDATA
  else.
    dat =. (<Row,Col) { CELLDATA
    orig=. (<Row,Col) { CELLDATA__
    if. dat -: orig
    do.
      cols =. (3|Col) + 3* i. 6
      ccells =. ;/Row,.cols
      tgcells =. ,"1/@{(rows Row);group"0 cols
      ccells =. ccells #~ -.match"1 tgcells {"1 _ CELLDATA
      CELLDATA=: Blank ccells} CELLDATA
      gcells =. ,@{(rows Row);group Col
      CELLDATA=: dat gcells } CELLDATA
      CELLHIGH=: 1 gcells } CELLHIGH
    end.
  end.
  showit 4
)

mbrdown_jzgrid_ =: monad define
  mouseset''
  'Px Py Row Col Ctrl Shift'=: mousepos''
  gcells =. ,@{(rows Row);group Col
  if. match gcells { CELLDATA
  do.
    CELLDATA=: (,gcells { CELLDATA__) gcells } CELLDATA
    CELLHIGH=: 0 gcells } CELLHIGH
  else.
    dat =. (<Row,Col) { CELLDATA
    if. Blank -: dat
    do.
      dat =. (<Row,Col) { CELLDATA__
      CELLDATA=: dat (<Row,Col)} CELLDATA
      smoutput 'You may want to right-click other cells, too'
    else.
      CELLDATA=: Blank (<Row,Col)} CELLDATA
      cols =. (3|Col) + 3* i. 6
      T =: ccells =. ;/Row,.cols
      if. 1=+/Blank~:ccells { CELLDATA
      do.
        Row,Col =. 1 {;ccells #~ Blank~:ccells { CELLDATA
        dat =. (<Row,Col) { CELLDATA
        orig=. (<Row,Col) { CELLDATA__
        if. dat -: orig
        do.
          cols =. (3|Col) + 3* i. 6
          ccells =. ;/Row,.cols
          CELLDATA=: Blank ccells} CELLDATA
          gcells =. ,@{(rows Row);group Col
          CELLDATA=: dat gcells } CELLDATA
        end.
      end.
    end.
  end.
  showit 4
)
CELLDATA =: ,./6 copies ,/box&> prevoke each  icons
smoutput 'grid CELLDATA'
grid CELLDATA