Hi, J coders,

I'm writing now in hopes of finding a way to stay (neatly) within tacit
form, and avoid fragile dependencies on state, when I need to "take a
calculated value deep into a train." To show what I mean by "deep", here's
an example.

The following code was written to meet the first four requirements of a TDD
exercise published here:
http://gojko.net/2009/08/02/tdd-as-if-you-meant-it-revisited/

These functions determine whether a tic-tac-toe board situation is an
end-of-game situation.

Vacant=: 0       NB. zero denotes empty (playable) places

gameover =: [: +./ gameover_no_vacancies, gameover_column_won,
gameover_row_won, gameover_diag_won
gameover_no_vacancies =: [: -. [: Vacant& e. ,
gameover_column_won   =: [: +./ *.&(Vacant& ~:)/   *. =/
gameover_row_won      =: [: +./ *.&(Vacant& ~:)/"1 *. =/"1

gameover_diag_won =: verb define
DL =. <./ $ y   NB. dl: diagonal length required for win
diag =. [: +./ |. ,&(((-.@:((,:Vacant)&-:) *. 1=#)@: ~.)/. *. (DL=#)/.) ]
diag y
)

I'd prefer to define 'gameover_diag_won ' tacitly, as I've done with it's
sister verbs. It's almost tacit, as we see from the definition of 'diag'
within it. But 'diag' relies on 'DL', which can't be calculated at the point
in the code where it is used for comparison (because the whole board is not
available "inside" Oblique.)

I'm confident the technique I've used here is just fine, but I'd like to
learn if I'm missing something that allows this sort of thing to be done
without leaving tacit form, AND without complicating the definitions in a
way that seems ugly.

--
Tracy

P.S.  My first version of 'gameover_diag_won' was simpler because I
(intentionally) took the specification of "any diagonal" to mean any
diagonally examined section (a la Oblique) rather than any maximal-length
diagonal. That simpler verb is:

gameover_diag_won =: [: +./ |. ,&(*.&(Vacant&~:)//. *. =//.) ]
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to