Hi Luca
Here is a link to a previous forum thread on this topic:
http://www.jsoftware.com/pipermail/programming/2016-June/045277.html

The thread is worth reading as it shows works through the various
issues that arose and explains the final version from Marshall
Lochbaum

mergerow=: ((* >:) #~ _1 |. -.@]) [: >/\.&.|. 2 =/\ ,&_1

If you are unaware, there is a 2048 addon available in JAL which uses
the techniques above to implement the merges in all 4 directions.

https://github.com/jsoftware/games_2048/blob/master/engine.ijs

Cheers,



On Thu, May 23, 2019 at 10:41 AM Luca Masini <lmas...@web.de> wrote:

> Hi,
>
> I'm trying to come out with a few primitives for a 2048-like-game
> operations.
>
> I did the following
>
>     first =: {.
>     rest =. }.
>     f =: ((dyad def 'x,y')"(0 1)`(dyad def '(x + first y) , rest y'"(0
> 1))@.=)
>
> In words f should do the following
>    - if x = first y the reduce it to x+y
>    - if x not = first y then give back the sequence x,y
>
>
> The intended use would be, for ex.
>
>    f/ 4 4 4
> 4 8
>
>    f/ 4 4 4 4
> 8 8
>
> In the first case it works.
>
>
> In the second case the result is the following:
>
>     f/ 4 4 4 4
> 8 0
> 4 8
>
> My idea was
>
>    f/ x x x x
>
> is equivalent to
>    4 f 4 f 4 f 4
>
> then, working right-to-left, 4 = first 4, so it simplify to
>    4 f 4 f 8
>
> now, 4 is not = first 8, so it should simplify to
>    4 f 4 8
>
> now, 4 is = first 4 8, so it should simplify to
>    8 8
>
>
> What could be a way to implement the behavior that I described?
>
> MfG
>
> Luca.
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to