Ludii Forum
Pattern recognition - Printable Version

+- Ludii Forum (https://ludii.games/forums)
+-- Forum: Suggestions (https://ludii.games/forums/forumdisplay.php?fid=10)
+--- Forum: Ludii Features / Services (https://ludii.games/forums/forumdisplay.php?fid=11)
+--- Thread: Pattern recognition (/showthread.php?tid=114)

Pages: 1 2 3


RE: Pattern recognition - AlekErickson - 10-16-2020

Hi, crosscuts are when two opponent pieces are found in the following generic pattern:

OX
XO

however, when I look at the isPattern ludeme, I cannot for the life of me figure out a single example to detect patterns containing more than a single player's pieces.

Could somebody provide an example or just tell me how I would use isPattern to detect the crosscut pattern?


RE: Pattern recognition - Eric Piette - 10-18-2020

Hi Alek,

Yes you can detect that pattern with alternating pieces with our new ludeme (is Pattern ...)

For example let's say you want to detect your square pattern and the 'O' is the "Ball" of P1 and the 'X' is the "Ball" of P2.
You can detect it with
(is Pattern {F R F R F} whats:{(id "Ball" P1) (id "Ball" P2)})

Just be careful, the pattern will start the detection from the last site played and will check for a pattern OXOX here.
so if you want to detect a pattern XOXO, you will have to swap the "whats" parameter like that:
(is Pattern {F R F R F} whats:{(id "Ball" P2) (id "Ball" P1)})

A simple (if ...) on the last move played for example is enough to have the two cases in the same .lud if you need to check that.

Another possibility if each player has only one piece (and the game has only 2 players and you check from the last site played) you can do something like that
(is Pattern {F R F R F} whats:{(mover) (next)}) to avoid the If test and being a little bit more efficient.

Regards,
Eric


RE: Pattern recognition - AlekErickson - 10-20-2020

Thanks, the code magically works , but I still don't understand why it works. 

F R F R F .... I imagine that you start from last to, take a step forward, turn right, take a step forward, turn right, etc until making a square shape. And then I assume it is comparing that against (mover) (next) as a kind of repeating unit?


RE: Pattern recognition - Eric Piette - 10-20-2020

Hi,

Yes the F R F R F is the description of the pattern in using a walk definition based on the turtle graphics (https://docs.python.org/3/library/turtle.html). That's something used often in Ludii to define for example a knight move in Chess or a large piece.

And yes if some "what" are defined we check them as a repeating unit.

So you understood it :)

FYI, we will modify the whats to be what (that's clearer)

And currently that ludeme does not handle empty sites for pattern with possibly some empty sites in it, so we will handle that. Moreover it will be also possible to make pattern with any piece (or none) if the Undefined value is used in the list of "What".

We will also add a list of boolean conditions if the designer prefer to define condition for each site reached in the pattern detection.

Regards,
Eric


RE: Pattern recognition - AlekErickson - 11-25-2020

Would there be a way of using this pattern detection ludeme to determine whether a given piece , such as the last placed piece, is inside a pattern or not?


RE: Pattern recognition - Eric Piette - 11-25-2020

Hi,

Yes, that's pretty much the goal of it.
Look the game "Xanan Zirge" for an example.

Regards,
Eric


RE: Pattern recognition - AlekErickson - 11-25-2020

can it be combined with a while loop to say something like -

while the last piece is contained in the pattern, keep moving this piece.


RE: Pattern recognition - Eric Piette - 11-25-2020

Hi,

Not sure what you mean, I would need a concrete example to be sure of what you mean here.

Regards,
Eric


RE: Pattern recognition - AlekErickson - 11-25-2020

Say the rule is like this:

move add piece to sites empty
then, while it is part of a crosscut
swap the added piece with adjacent enemy neighbor piece


RE: Pattern recognition - AlekErickson - 11-26-2020

I have created a more detailed example that does not have the expected behavior, although from looking at the code you might guess what I want it to do.

Instead of allowing a Swap move upon formation of crosscut, it somehow lets the mover move twice. I will attach the full .lud in a reply to this message.

(define "SwapInDirection"
(forEach Site
(sites Occupied by:Any)
(if
(is Occupied (ahead (site) #1))
(move Swap Pieces
(site)
(ahead (site) #1)
)
)
)
)

(game "SwapOrAdd"
(players 2)
(equipment {
(board <Board> use:Vertex)
(piece "Ball" Each)
(regions P1 { (sites Side N) (sites Side S) } )
(regions P2 { (sites Side W) (sites Side E) } )
})
(rules
(play
(move Add (to (sites Empty))
(then
(while (is Pattern {F R F R F} whats:{(mover) (next)})
(or
{
("SwapInDirection" N)
("SwapInDirection" E)
("SwapInDirection" S)
("SwapInDirection" W)
}
)
)
)
)
)
(end {
(if (is Connected Orthogonal Mover) (result Mover Win))
})
)
)

as I said, here's the lud to my non-functioning (but compiling) example.