Now, the next part. You want to make the code general, so it can treat
n-dimension.

See, for example:

   b
1 0 0
0 0 0
0 0 0

0 0 0
1 0 1
1 1 0

0 0 1
0 0 1
1 0 0

   hint b
*31
452
342

253
*8*
**3

14*
47*
*63

When it is a 3 dimension field, think about it as a cubic and each 2d
table becomes the horizontal slices(or layers) of it, from the top to
the bottom.

Now look at my code that can treat n-dimension, partially.

board=:   [EMAIL PROTECTED]&8
graphic=: {&'.*'
guard1d=: 0,~0,]
guard2d=: 0,~0,guard1d"1
guard=:   0,~0,guard2d"2
scr=:     ((1 $~ ]) ,: (3 $~ ]))&([EMAIL PROTECTED])  +/@,;._3 guard
hint=:    scr{.@":@[`('*'"_)@.]every]

Now "scr" can handle n-dimension. The other parts don't need any
changes except guard. As you see the tricky part is letting guard
treat n-dimension. However, there definitely is some pattern in guard.

How would you generalize guard code in J style? I believe there is a
simple solution.


2008/6/17 June Kim <[EMAIL PROTECTED]>:
> Problem:
> Given the shape of a mine field, generate the field with the
> probability of 1/8 for mine's presence for each cell. Then display the
> graphical representation and the hint for each cell. Hint for one cell
> is the number of mines in max 8 neighbouring cells, except when there
> is a mine in the cell, in which case the hint is '*' instead.
>
> Example:
> ....*
> .....
> ..*..
> .*...
>
> 0001*
> 01121
> 12*10
> 1*210
>
>
>
> ------------------
> Following is my solution:
>
> board=:0 = [: ? 8 $~ ]
> graphic=: '.*' {~ ]
> guard=: 0 ,.~ 0 ,. 0 ,~ 0 , ]
> hintsc=: (1 1,:3 3)+/@,;._3 guard
> hintscs=:[: ,"2 [: ":&> hintsc
> hint=:[: ,"2 hintsc ":@[`('*'"_)@.] every ]
>
> Usage example:
>
>   (graphic,:hint) b=.board 4 5
>
> Could you suggest any ways to improve the code? I am particulary
> dissatisfied with the use of agenda for substituting '*"s at the
> mine's places.
>
> Of course, different approaches are welcome. I would appreciate it for
> the learning opportunity.
>
> June Kim
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to