Ludii Forum
Nullreference exception with "enclose" - Printable Version

+- Ludii Forum (https://ludii.games/forums)
+-- Forum: Problems (https://ludii.games/forums/forumdisplay.php?fid=5)
+--- Forum: Grammar Problems (https://ludii.games/forums/forumdisplay.php?fid=24)
+--- Thread: Nullreference exception with "enclose" (/showthread.php?tid=245)



Nullreference exception with "enclose" - Castux - 11-09-2020

Code:
(define "CaptureSurrounded"
    (enclose
        (from (difference (sites Board) (sites Empty)))
        Adjacent
        (between
            if:(is Enemy (who at:(between)))
            (apply
                (and
                    (addScore Mover 1)
                    (remove (between))
                )
            )
        )
    )
)


Causes java.lang.NullPointerException when loading. Same with "(from (sites Board))".

Full file:

Code:
(define "CaptureSurrounded"
    (enclose
        (from (difference (sites Board) (sites Empty)))
        Adjacent
        (between
            if:(is Enemy (who at:(between)))
            (apply
                (and
                    (addScore Mover 1)
                    (remove (between))
                )
            )
        )
    )
)

(define "Refill"
    (and {
        (remove (sites hand Mover))
        (add (piece (id "HexA" Mover)) (to (handSite Mover 0)))
        (add (piece (id "HexB" Mover)) (to (handSite Mover 1)))
        ("CaptureSurrounded")
    })
)

(game "Blooms"
    (players 2)
    (equipment {
        (board (hex 5))
        (piece "HexA" Each)
        (piece "HexB" Each)
        (hand Each size:2)
    })
    (rules

        (start {
            (place "HexA1" (handSite P1 0))
            (place "HexB1" (handSite P1 1))
            (place "HexA2" (handSite P2 0))
            (place "HexB2" (handSite P2 1))
        })
        (play
            (or
                (move
                    (from (sites Hand Mover))
                    (to (sites Empty))
                    (then
                        (if
                            (and
                                (not ("HandEmpty" Mover))
                                (> (count Moves) 1)
                            )
                            (moveAgain)
                            "Refill"
                        )
                    )
                )
                (if ("SameTurn")
                    (move Pass (then "Refill"))
                )
            )
        )
        (end
            (if
                (>= (score Mover) 15)
                (result Mover Win)
            )
        )
    )
)

(metadata
    (graphics {
        (player Colour P1 (colour Blue))
        (player Colour P2 (colour Red))
        (piece Colour P1 "HexB" fillColour:(colour LightBlue) )
        (piece Colour P2 "HexB" fillColour:(colour LightRed) )
    })
)

FYI, I have implemented the same thing using:

Code:
(define "CaptureSurrounded"
    (forEach Group if:
        (and
            (is Enemy (who at:(to)))
            (= 0 (count Liberties at:(to)))
        )
        (and
            (addScore Mover (count Sites in:(sites)))
            (remove (sites))
        )
    )
)

and it doesn't crash. The initial problem remains, though.


RE: Nullreference exception with "enclose" - Eric Piette - 11-10-2020

Hi,

Can you share a full .lud with the problem in it? That's easier for me to look and potentially fix a problem. Thank you.

Regards,
Eric


RE: Nullreference exception with "enclose" - Castux - 11-10-2020

I had pasted it in the second "code" section here. Do you normally prefer attached files?

Here it is :)


RE: Nullreference exception with "enclose" - Eric Piette - 11-10-2020

Hi,

Sorry I did not see the full .lud was in your "code" section. Yes I am more usual with an attachment ;)

Concerning the bug, I fixed the issue in our code (dev version) to avoid the code to break. This is caused because you use in the (from ...) ludeme a RegionFunction rather than an IntFunction.

However, (enclose ...) is working only for a single site. Here an example of its use:


Code:
(move Add
    (to (sites Empty))
    (then
        (enclose
            (from (last To))
            Orthogonal
            (between
                if:(is Enemy (who at:(between)))
                (apply (remove (between)))
            )
        )
    )
)

If you need to run the enclose capture to all the sites of a region you have to use a (forEach Site ....) ludeme to then call it inside of it. Here a different example concerning the use of (forEach Site ...)


Code:
(forEach Site
    (sites Occupied by:Mover)
    (set State at:(site) 1)
)


Regards,
Eric