Ludii Forum
forEach Group if: - Printable Version

+- Ludii Forum (https://ludii.games/forums)
+-- Forum: Questions (https://ludii.games/forums/forumdisplay.php?fid=13)
+--- Forum: About the Ludii Grammar (https://ludii.games/forums/forumdisplay.php?fid=15)
+--- Thread: forEach Group if: (/showthread.php?tid=438)



forEach Group if: - slimy_asparagus - 02-14-2021

According to the language reference "forEach Group" has an "if:" parameter. I am not clear how to use it. Say if I want to iterate over all Groups of "Yellow1" pieces and apply moves XXX to each, can I do something like this. I have the feeling this is not correct, but I don't know what it would be.

Code:
(forEach
        Group
        Cell
        Adjacent
        if:(= (id "Yellow" P1) (what at:(sites))
                XXX
    )
I wonder if the documentation could be improved to clarify this.


RE: forEach Group if: - Eric Piette - 02-22-2021

The condition on the if is a condition to include or not some elements of each group found. Each site can be iterated in it with (to). 
Then in your example with the XXX you can apply whatever the move you want but with (sites) you can iterate all the sites of each group.

For example in the following example, you get all the group composed of Yellow pieces owned by P1, then on each element of these groups you set the state of these sites to 2.


Code:
(forEach
        Group
        Cell
        Adjacent
        if:(= (id "Yellow" P1) (what at:(to))
        (forEach Site (sites) (set State at:(site) 2))
)

Regards,
Eric


RE: forEach Group if: - slimy_asparagus - 03-12-2021

Eric,

My latest work on Blooms incorporated your advice.

Code:
// #1 - A/B
(define "MarkSurroundedPiecesById"
    (forEach
        Group
        Cell
        Adjacent
        if:(= (id #1 Next) (what at:(to)))
        (if
            (= 0 (count Liberties if:(= (id #1 Next) (what at:(to)))))
            (forEach
                Site
                (sites)
                (and
                    (set State at:(site) ("Captured"))
                    (addScore Mover 1)
                )
            )
        )
    )
)


However when you try to place the second stone (first move for P2), Ludii puts it back. From playing around it seems that the problem is the (count Liberties ...) bit.

I attach a .lud file. As it happens I have been using 1.1.15, as I am playing a game under that version and have no specific reason to upgrade yet. However I believe this also happened under 1.1.16.


RE: forEach Group if: - slimy_asparagus - 06-23-2021

Okay I have picked this up again. I enclose a .lud file and a trial.


[Image: pic6244030.png]

I am hoping I can get at whether I am using "forEach Group" correctly by using corresponding syntax for "count Groups". So in the Test Ludeme Dialog I am testing the following code:

Code:
(count
    Groups
    Cell
    Adjacent
    if:(= (id #1 #2) (what at:(to)))
)

#1 - "A" or "B"
#2 - P1 or P2


So for A, P1 (Yellow) I would expect 1.
For B, P1 (Red) I would expect 1.
For A, P2 (Teal) I would expect 2.
For B, P2 (Black) I would expect 0.

However for all of these I get 0. Also when I do this, my Ludii writes out to the console: "** Could not compile game.util.math.Count."

However if just ask for the Test Ludeme Dialog to calculate
Code:
(count
    Groups
    Cell
    Adjacent
)
it returns 2, which is what I would expect. However it still gives the error message: "** Could not compile game.util.math.Count."


RE: forEach Group if: - Eric Piette - 07-01-2021

Hi,

Just a short answer to tell you that I saw your message and I will check all of that at some point. I did the ludemes about groups a long time ago and I suppose I can improve them now but I need time to look at them. if their eval methods have problems and also if I can improve them in term of efficiency I will. I come back to you when I looked at that.

Regards,
Eric