[Jprogramming] primitive for 2048-like-game

Luca Masini lmasini at web.de
Wed May 22 22:41:48 UTC 2019


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.



More information about the Programming mailing list