Let's make it so that at the end of the game, above the table, it is displayed for what minimum number of moves one could win.
If you think carefully about this question, then you can come up with a simple algorithm how to determine the indicated.
Implement the assigned task. This is advanced task - can be skipped :)