Jsoftware
High-Performance Development Platform

N-Dimensional Mine Sweeper (was Re: Mine Sweeper)

View: New views
5 Messages — Rating Filter:   Alert me  

N-Dimensional Mine Sweeper (was Re: Mine Sweeper)

by juneaftn :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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=:   0=?@$&8
graphic=: {&'.*'
guard1d=: 0,~0,]
guard2d=: 0,~0,guard1d"1
guard=:   0,~0,guard2d"2
scr=:     ((1 $~ ]) ,: (3 $~ ]))&(#@$)  +/@,;._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 <juneaftn@...>:

> 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

Re: N-Dimensional Mine Sweeper (was Re: Mine Sweeper)

by Raul Miller-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 6/17/08, June Kim <juneaftn@...> wrote:
> Now, the next part. You want to make the code general, so it can treat
> n-dimension.
...
> How would you generalize guard code in J style? I believe there is a
> simple solution.

guard=: ({.~ _1 - $)@({.~ 1 + $)
   guard 1 2 3
0 1 2 3 0
   guard 1 2 3,:4 5 6
0 0 0 0 0
0 1 2 3 0
0 4 5 6 0
0 0 0 0 0

--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: N-Dimensional Mine Sweeper (was Re: Mine Sweeper)

by juneaftn :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2008/6/17 Raul Miller <rauldmiller@...>:
> On 6/17/08, June Kim <juneaftn@...> wrote:
>> Now, the next part. You want to make the code general, so it can treat
>> n-dimension.
> ...
>> How would you generalize guard code in J style? I believe there is a
>> simple solution.
>
> guard=: ({.~ _1 - $)@({.~ 1 + $)

Great. I always tend to underestimate the power and usefulness of "take".


>   guard 1 2 3
> 0 1 2 3 0
>   guard 1 2 3,:4 5 6
> 0 0 0 0 0
> 0 1 2 3 0
> 0 4 5 6 0
> 0 0 0 0 0
>
> --
> Raul
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: N-Dimensional Mine Sweeper (was Re: Mine Sweeper)

by juneaftn :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Just for the record, I include my solution for guard in the following.
I wanted to find a purely tacit version of guard but I couldn't. You
can see the struggle; it's complex. As you might notice, I tried to
follow the repetitive pattern shown in my original 3d version. Of
course the final code is more complex than Raul's guard, which takes
use of "take."

enc=: 0,~0,]
er=: enc"
erstep=: 3 : '(>:r) ; (r=.>{.y) er >{:y'
guard=:[: >@{: (erstep@]^:(#@$@[) (1 ; ]))

   guard >:i. 2
0 1 2 0
   guard >:i. 2 3
0 0 0 0 0
0 1 2 3 0
0 4 5 6 0
0 0 0 0 0
   guard >:i. 2 3 4
0  0  0  0  0 0
0  0  0  0  0 0
0  0  0  0  0 0
0  0  0  0  0 0
0  0  0  0  0 0

0  0  0  0  0 0
0  1  2  3  4 0
0  5  6  7  8 0
0  9 10 11 12 0
0  0  0  0  0 0

0  0  0  0  0 0
0 13 14 15 16 0
0 17 18 19 20 0
0 21 22 23 24 0
0  0  0  0  0 0

0  0  0  0  0 0
0  0  0  0  0 0
0  0  0  0  0 0
0  0  0  0  0 0
0  0  0  0  0 0


2008/6/17 June Kim <juneaftn@...>:

> 2008/6/17 Raul Miller <rauldmiller@...>:
>> On 6/17/08, June Kim <juneaftn@...> wrote:
>>> Now, the next part. You want to make the code general, so it can treat
>>> n-dimension.
>> ...
>>> How would you generalize guard code in J style? I believe there is a
>>> simple solution.
>>
>> guard=: ({.~ _1 - $)@({.~ 1 + $)
>
> Great. I always tend to underestimate the power and usefulness of "take".
>
>
>>   guard 1 2 3
>> 0 1 2 3 0
>>   guard 1 2 3,:4 5 6
>> 0 0 0 0 0
>> 0 1 2 3 0
>> 0 4 5 6 0
>> 0 0 0 0 0
>>
>> --
>> Raul
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
>>
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

RE: N-Dimensional Mine Sweeper (was Re: Mine Sweeper)

by Dan Bron :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Raul wrote:

>   guard=: ({.~ _1 - $)@({.~ 1 + $)

Or:

       ( |.~ _1 #~ #@:$ ) @: ( {.~  2+$)

Rotate, like take, accepts a vector on the left (each scalar corresponding to an axis in the right).

-Dan

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm