#include <iostream>
#include <string>
#include <limits>

bool inputNumber(int &n)
{
    std::cin >> n;

	bool result = std::cin.good();
	if (!result) {
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}
	return result;
}

int main(void)
{
	const int numPlayer = 2;
	const int numInitialStone = 15;

	std::string playerName[numPlayer];

	// 同じ名前が入力されることは考慮していない
	for (int i = 0; i < numPlayer; ++i) {
		std::cout << "プレイヤー" << (i + 1) << "の名前を入力：";
		std::cin >> playerName[i];
		std::cout << "[LOG]playerName[" << i << "] = " << playerName[i] << std::endl;
	}

	int turn = 0;
	int numStone = numInitialStone;

	while (numStone != 0) {
		int numTakingStone;
		std::cout << playerName[turn] << "さん残りは" << numStone << "個、何個とりますか：";
		if (!inputNumber(numTakingStone)) {
			continue;
		}
		if (numTakingStone < 1 || 3 < numTakingStone) {
			continue;
		}
		if (numTakingStone > numStone) {
			continue;
		}
		std::cout << "[LOG]numTakingStone = " << numTakingStone << std::endl;

		numStone -= numTakingStone;
		if (numStone == 0) {
			std::cout << playerName[turn] << "さん、あなたの負けです。" << std::endl;
		}

		turn = (turn + 1) % numPlayer;
	}

	return 0;
}
