#include <iostream>
using namespace std;
int findBestMove(int stones) {
if (stones == 1 || stones == 2)
return stones;
for (int move = 1; move <= 2; move++) {
int remaining = stones - move;
if (remaining % 3 == 0) {
return move;
}
}
return 1;
}
int main() {
int stones;
cout << "Enter the number of stones: ";
cin >> stones;
cout << "Stone Game - You and the AI will take turns.\n";
cout << "You can take 1 or 2 stones each turn.\n";
cout << "Whoever takes the last stone wins!\n";
while (stones > 0) {
cout << "\nStones remaining: " << stones << endl;
int playerMove;
cout << "Your turn! Take 1 or 2 stones: ";
cin >> playerMove;
if ((playerMove != 1 && playerMove != 2) || playerMove > stones) {
cout << "Invalid move. Try again.\n";
continue;
}
stones -= playerMove;
if (stones == 0) {
cout << "You win! Congratulations!\n";
break;
}
int aiMove = findBestMove(stones);
cout << "AI takes " << aiMove << " stones.\n";
stones -= aiMove;
if (stones == 0) {
cout << "AI wins! Better luck next time.\n";
break;
}
}
return 0;
}