Not to be "that guy", but a way to win every time on every difficulty is to subtract a number so that the AI gets a number congruent to 1 (mod 3). For example, since you start with 21, you need to subtract 2 so that the AI gets 19. As long as you keep following this rule, you are guaranteed to win no matter what the AI does. If the AI went first and was a perfect player, they would also win every time.

@Lumpy_Wiggles There is an error in the code for the level 3 AI. The AI does not play perfectly. I purposely made a mistake in my first move, giving the AI the opportunity to have a winning strategy against me, but the AI also made several errors. You say that the AI want to make "odd" numbers, but that is far from the ideal strategy. Let me outline the actual winning strategy.

You must ensure that the opposing player always ends up with a number that is 1 more than a multiple of 3. If the opposing player always end up with a number that is 1 more than a multiple of 3, the following conditions will happen:

a. The opposing player cannot give you a number that is 1 more than a multiple of 3 because he would have started with one, and he would've had to subtract 3 to make your number also 1 more than a multiple of 3, which is impossible.

b. The opposing player will eventually end up with 1, which is the smallest number that is 1 more than a multiple of 3. This makes them lose.

c. You can always force the opposing player to have a number that is 1 more than a multiple of 3 because if you go first, you subtract 2 to give them 19, which is 1 more than a multiple of 3. If they have a number that is 1 more than a multiple of 3 and they subtract 1, you subtract 2, giving them a number that is 1 more than a multiple of 3. If they subtract 2, you subtract 1, which again gives them a number that is 1 more than a multiple of 3.

Here is a hypothetical scenario that proves that your strategy is not ideal.

Me: -1 (20)

AI: -1 (19)

Me: -1 (18)

AI: -1 (17)

Me: -1 (16)

AI: -1 (15)

Me: -2 (13)

AI: -2 (11)

Me: -1 (10)

AI: -1 (9)

Me: -2 (7)

AI: -2 (5)

Me: -1 (4)

AI: -1 (3)

Me: -2 (1)

AI: -1 (0 and loses!)

As you can see, the AI brought the number to an odd value every time. But this clearly wasn't the ideal strategy! After my second move, I brought the AI to a number that is 1 more than a multiple of 3, which allowed me to win. I purposely made an error on the first move to give the AI the opportunity to win if he played ideal, but clearly he messed up on his second move, allowing me to bounce back despite my errors.