Ludii Forum
Sum of minimal distances between two sites - 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: Sum of minimal distances between two sites (/showthread.php?tid=1635)



Sum of minimal distances between two sites - Alain Busser - 08-04-2023

Hello,

with my pupils we tested the included game, played on a graph with pieces set (at start) to the other player's home. The goal is to be the first one to have one's pieces at home (or to block the other player). In the initial version (created by a 6 years old girl) there were too many draws, so we used armbands to restrict the number of moves: every time one moves, one has to give away one armband, until one can not move anymore. In the included file I managed to emulate this armband trick, with stacks of decreasing lengths.

The game is interesting but the end condition is not good. What I need is to program what we do with the children: once the game is ended, we compute
  • for each piece, the distance to the goal line (0 if the piece is already on the goal line, 1 if it is adjacent to a site on the line, occupied or not, 2 if if it adjacent to a site itself adjacent to the goal line)
  • the sum of the 3 distances (one for each piece), which is an integer between 1 and 6 for each player

Then, the winner is the player who has the smallest sum. If the sums are equal one plays again, until the sums are not equal.

The ludeme

Code:
(count (sites Between from:a to:b))

gives me one unit less than the distance, but I don't know how to apply it to a list of sites, it seems to work only when a and b are sites. So I need
  • a ludeme giving the distance between a site and a group of sites (I mean, the distance between the site and the nearest site in the group)
  • a ludeme giving the total distance between a group of sites and another group of sites (I mean the sum of the values given by the ludeme above)

I see in the language reference that forEach is used either to set an action (like Move), or to filter sites (in the Regions functions) but there is no example of use of forEach to get a list of integers. At least I did not find one.

So how can I compute a sum of distances to get the score?


RE: Sum of minimal distances between two sites - Michael - 08-04-2023

What you want to look up is how to (count Steps).

And to sum the distances you can use (+ (results …)), or just (+ {(count Steps …) … }).


RE: Sum of minimal distances between two sites - Alain Busser - 08-09-2023

Thank you a lot!

But I still don't have it. There's an error with

Code:
(score P1
         (+  (count Steps (site) (sites Top)  (forEach Site (sites Occupied by:P1) ))
         )
)

Even if I switch the "forEach..." and the "count", or if I include the whole ludeme inside brackets.

In the language reference I don't find examples for the use of forEach to retrieve something else than moves or regions.


RE: Sum of minimal distances between two sites - Michael - 08-09-2023

You need to use results, I think. This might be wrong, because I'm typing on my phone without consulting the reference, but something like this:

Code:
(+
    (results
        from:(sites Occupied by:P1)
        to:0
        (count Steps
            (from)
            (sites Top)
        )
    )
)



RE: Sum of minimal distances between two sites - dale walton - 08-26-2023

(08-09-2023, 03:03 AM)Alain Busser Wrote: Thank you a lot!

But I still don't have it. There's an error with

Code:
(score P1
         (+  (count Steps (site) (sites Top)  (forEach Site (sites Occupied by:P1) ))
         )
)

Even if I switch the "forEach..." and the "count", or if I include the whole ludeme inside brackets.

In the language reference I don't find examples for the use of forEach to retrieve something else than moves or regions.


The results method appears to be the simplest. But I can help with some of your other questions:
Your code above fails because "(+" is not an iterative operator for regions or integers. 



It CAN be used to sum arrays such as in the (result) example because  (results ...) is an array.


A more cumbersome solution is to store the values into an array using (remember ...)
Or else you can use addScore.

Code:
(forEach Site
  (sites Occupied by:P1)
  (addScore P1 (count Step (site) (sites Top)))
)

--------------------------
(forEach Site 
  (sites Occupied by:P1)
  (remember Value "P1" (count Step (site) (sites Top))
  (then
    (score P1 (+ (values Remembered "P1")))
) )





The biggest problem with all these is that they can't be used in the (score ) statements in the (byScore ...) ludeme in the (end ...) section) because they are processes (moves) rather than integers. So if you used them you would need to ad code in the Play section to do them just in the last turn.


The (+ (results ...)) can be used in (score ...) because it is an integer, not the result of moves.

Here is how that might look:...


Code:
(end
  (if
    (<endcondition>) //i.e. from your code
    (byScore
      {
        ("ScoreOf" P1 (sites Top))
        ("ScoreOf" P2 (sites Bottom))
      }
      misere:True
) ) )

(define "ScoreOf"
  (score #1
    (+ (results from:(sites Occupied by:#1) to:(from) (count Steps (from) #2)))
) )


--------------------------------------

Another question is do you allow passing (but giving up a ring)
If so you could do it this way, as an example:



Code:
(piece "DiscFlat" Each
  (if
    (> (count Stack at:(from)) 1)
    (or
      (move Select
        (from)
      )
      (move Step
        (to if:(is Empty (to)))
        stack:True
      )
      (then  (remove (last To) ))
) ) )



RE: Sum of minimal distances between two sites - Alain Busser - 09-10-2023

(08-26-2023, 07:27 AM)dale walton Wrote: Another question is do you allow passing (but giving up a ring)
If so you could do it this way, as an example:

Actually the idea came to one of my pupils. We didn't try it yet because I was not convinced that the game would be better. Trying your version with the ludii player shows that it is much better:
  • with the ancient version, the typical end was a draw (total distance 1 for each player at the end),
  • with your proposal, the first player seems to have a winning strategy.

I'll announce that to the pupils tomorrow, thank you a lot, you considerably enhanced the game. I propose it here although it is not the place.