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