2

I am running stockfish 15 from the command line. I'm wondering if there is an easy way for Stockfish to report the total number of legal moves (safe, or otherwise) for each side after a given move.

For example, after entering the following commands: position startpos moves e2e4 e7e5, I would like stockfish to report to me that white's (or black's) total number of legal moves after e2e4 e7e5 is equal to 29 (there are 29 legal moves available for white or black after e2e4 e7e5).

Is there some way for stockfish to report this to me in the command line, and if not, is there a way to modify the existing code to accomplish this?

1 Answer 1

4

For example, after entering the following commands: position startpos moves e2e4 e7e5, I would like stockfish to report to me that white's (or black's) total mobility after e2e4 e7e5 is equal to 29 (there are 29 legal moves available for white or black after e2e4 e7e5).

Note that your definition of mobility and the definition of mobility used by many chess engines differs. Chess engines generally understand mobility as an evaluation figure that's a good bit more complicated than just the total number of legal moves.

So for future searching, it might be best to use phrasing like calculating the legal move count for a given position, instead of mobility.

Anyhow, what you're asking is possible with Stockfish by using the command go perft 1 which will display the total number of legal moves for the current position:

Stockfish 14.1 by the Stockfish developers (see AUTHORS file)
position startpos moves e2e4 e7e5
go perft 1
a2a3: 1
b2b3: 1
c2c3: 1
d2d3: 1
f2f3: 1
g2g3: 1
h2h3: 1
a2a4: 1
b2b4: 1
c2c4: 1
d2d4: 1
f2f4: 1
g2g4: 1
h2h4: 1
b1a3: 1
b1c3: 1
g1e2: 1
g1f3: 1
g1h3: 1
f1e2: 1
f1d3: 1
f1c4: 1
f1b5: 1
f1a6: 1
d1e2: 1
d1f3: 1
d1g4: 1
d1h5: 1
e1e2: 1

Nodes searched: 29

The go perft command more generally is used to verify that Stockfish's move generation is working correctly, by counting the total number of legal moves up to a certain depth.

In your example you only wanted the number of legal moves at a depth of 1. If for example, you wanted all of the possible legal moves from a position up till depth n, you would type go perft n:

Stockfish 14.1 by the Stockfish developers (see AUTHORS file)
go perft 2
a2a3: 20
b2b3: 20
c2c3: 20
d2d3: 20
e2e3: 20
f2f3: 20
g2g3: 20
h2h3: 20
a2a4: 20
b2b4: 20
c2c4: 20
d2d4: 20
e2e4: 20
f2f4: 20
g2g4: 20
h2h4: 20
b1a3: 20
b1c3: 20
g1f3: 20
g1h3: 20

Nodes searched: 400

Do note that the number of legal moves from a given position increases quite rapidly as the depth grows, so typing in large depth numbers will start taking quite a long time to calculate.

2
  • Thank you very much—that was exactly what I was looking for! Side note: I’ve updated the question title (and body) to say “number of legal moves” instead of “mobility.”
    – stewy0013
    Jun 4, 2022 at 16:14
  • Awesome! @stewy0013 Glad I could help you out! Jun 4, 2022 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.