Not theoretically optimal solution. I bruteforced only for endgame. Until the end of midgame, I used beam search to enumerate good positions.