#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;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8bGltaXRzPgoKYm9vbCBpbnB1dE51bWJlcihpbnQgJm4pCnsKICAgIHN0ZDo6Y2luID4+IG47CgoJYm9vbCByZXN1bHQgPSBzdGQ6OmNpbi5nb29kKCk7CglpZiAoIXJlc3VsdCkgewoJCXN0ZDo6Y2luLmNsZWFyKCk7CgkJc3RkOjpjaW4uaWdub3JlKHN0ZDo6bnVtZXJpY19saW1pdHM8c3RkOjpzdHJlYW1zaXplPjo6bWF4KCksICdcbicpOwoJfQoJcmV0dXJuIHJlc3VsdDsKfQoKaW50IG1haW4odm9pZCkKewoJY29uc3QgaW50IG51bVBsYXllciA9IDI7Cgljb25zdCBpbnQgbnVtSW5pdGlhbFN0b25lID0gMTU7CgoJc3RkOjpzdHJpbmcgcGxheWVyTmFtZVtudW1Q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