Ludii Forum
How can I get the history moves of the game - Printable Version

+- Ludii Forum (https://ludii.games/forums)
+-- Forum: General (https://ludii.games/forums/forumdisplay.php?fid=1)
+--- Forum: Discussion (https://ludii.games/forums/forumdisplay.php?fid=4)
+--- Thread: How can I get the history moves of the game (/showthread.php?tid=1013)

Pages: 1 2


How can I get the history moves of the game - FirstAI - 06-14-2022

Hi! I am writing a ludii AI, but I have a problem. How can I get the history moves of the game? I can't get the correct history moves with the following code:

@Override
    public Move selectAction
    (
        final Game game,
        final Context context,
        final double maxSeconds,
        final int maxIterations,
        final int maxDepth
    )
    {
        final Trial trial = context.trial();
        System.out.println("trail move number = " + trial.moveNumber());

        State state = context.state();
        System.out.println("trail mover = " + state.mover());

        System.out.println("Moves = ");
        for(int i = 0; i < trial.moveNumber(); i++)
        {
            Move move = trial.getMove(i);
            int from = trial.getMove(i).from();
            int to = trial.getMove(i).to();

            System.out.println("(" + from + "." + to + ")");
        }
     
     
        FastArrayList<Move> legalMoves = game.moves(context).moves();
        //System.out.println("Legal Moves = " + legalMoves);
     
        // If we're playing a simultaneous-move game, some of the legal moves may be
        // for different players. Extract only the ones that we can choose.
        if (!game.isAlternatingMoveGame())
            legalMoves = AIUtils.extractMovesForMover(legalMoves, player);
     
        final int r = ThreadLocalRandom.current().nextInt(legalMoves.size());
        return legalMoves.get®;
    }


the from and to is always -1, strange.


RE: How can I get the history moves of the game - Eric Piette - 06-14-2022

Code:
for (int i = trial.numInitialPlacementMoves(); i < trial.numMoves(); i++)
{
     Move move = trial.getMove(i);
     system.out.println(move);
}


Should print all the moves after the moves used to setup the game.

Then, if you want to print the from and to of your move, this depends on what you are looking for.

If you look for, the from/to of the first decision action in the move, you have to use .from() and .to(). However, even if some actions have a value for the from and the to, this can be -1 (undefined) for each of them if the first decision action in the list of actions composing the move is not related to the board (for example if your decision action is to pass).

An alternative is to use .fromNonDecision() and .toNonDecision() if you want the from and to values associated with the move despite the first decision action in the list of actions.

To understand more about decision moves, I invite you to look at 4.3.1 in the Ludii Game Logic Guide.

Eric


RE: How can I get the history moves of the game - FirstAI - 06-14-2022

In fact, I want to extract information from ludii's game and context structures, and translate it into chess's UCI protocol. UCI usually consists of two parts, the first part is the piece location information, and the second part is the history moves. Can this be achieved? How should the code be organized?


RE: How can I get the history moves of the game - Eric Piette - 06-15-2022

Hi,

Yes, this is possible in doing what I described before and then convert it in the structure needed by UCI (which I do not know).

Eric.


RE: How can I get the history moves of the game - FirstAI - 06-16-2022

The problem is that, in the code below the "from" and "to" of the move is the same (bug?), I can't get the right value of "from".
for (int i = trial.numInitialPlacementMoves(); i < trial.numMoves(); i++)
{
Move move = trial.getMove(i);
system.out.println(move);
}

But when I access the trial.lastMove(), the "from" and "to" value are okay!


RE: How can I get the history moves of the game - Eric Piette - 06-17-2022

Did you try what I said here: "An alternative is to use .fromNonDecision() and .toNonDecision() ..."?


RE: How can I get the history moves of the game - FirstAI - 06-19-2022

yes I have tried .fromNonDecision() and .toNonDecision(), the result is the same, "from" value is equal to "to" value each move.

the code I used is as below:
        for (int i = trial.numInitialPlacementMoves(); i < trial.numMoves(); i++)

         {
            Move move = trial.getMove(i);
            int from = move.fromNonDecision();
            int to = move.toNonDecision();

            System.out.println("(" + from + "," + to + ")");
        }


RE: How can I get the history moves of the game - Eric Piette - 06-22-2022

Hi,

That's still for Chess?

Because that's def possible that from and to are equal for some games (like placement games e.g. TTT).
But for Chess, 99% of the time that should not happen (and did not happen for our code). Could you share with us something that we can run ourselves to try?

Eric


RE: How can I get the history moves of the game - FirstAI - 06-22-2022

yes, I tested it for chess! I just add some code to RandomAI.java to show the chess moves, as below:

// RandomAI.java ---------------------------------------------------------------------------------

package random;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

import javax.swing.plaf.synth.Region;

import game.Game;
import main.collections.FastArrayList;
import other.AI;
import other.context.Context;
import other.location.Location;
import other.move.Move;
import other.move.MoveSequence;
import other.state.State;
import other.state.container.ContainerState;
import other.state.owned.Owned;
import other.trial.Trial;
import utils.AIUtils;

/**
* Example third-party implementation of a random AI for Ludii
*
* @author Dennis Soemers
*/
public class RandomAI extends AI
{

//-------------------------------------------------------------------------

/** Our player index */
protected int player = -1;

//-------------------------------------------------------------------------

/**
* Constructor
*/
public RandomAI()
{
this.friendlyName = "Random AI";
}

//-------------------------------------------------------------------------

@Override
public Move selectAction
(
final Game game,
final Context context,
final double maxSeconds,
final int maxIterations,
final int maxDepth
)
{
System.out.println("maxSeconds = " + maxSeconds
+ ", maxIterations = " + maxIterations + ", maxDepth = " + maxDepth);

final Trial trial = context.trial();
System.out.println("trail move number = " + trial.moveNumber());

State state = context.state();
System.out.println("trail mover = " + state.mover());

for(int i = 0; i < trial.moveNumber(); i++)
{
Move move = trial.getMove(i);
int from = move.fromNonDecision();
int to = move.toNonDecision();

System.out.println("(" + from + "," + to + ")");
}

Move move = trial.lastMove();
int from = move.fromNonDecision();
int to = move.toNonDecision();
System.out.println("lastMove (" + from + "," + to + ")");

FastArrayList<Move> legalMoves = game.moves(context).moves();
//System.out.println("Legal Moves = " + legalMoves);

final int r = ThreadLocalRandom.current().nextInt(legalMoves.size());
return legalMoves.get®;
}

@Override
public void initAI(final Game game, final int playerID)
{
this.player = playerID;
}

//-------------------------------------------------------------------------

}


RE: How can I get the history moves of the game - Eric Piette - 06-23-2022

Hi,

I just look your code, and you are not running the correct code I sent you.

The loop should be

for (int i = trial.numInitialPlacementMoves(); i < trial.numMoves(); i++)

And you are looking from the move 0 (so including the setup) + you are using moveNumber() and not numMoves() for the end of the loop.

Eric