Я пытаюсь сделать игру nim, но когда программа спрашивает пользователя, какую кучу он хочет выбрать, A, B или C, программа всегда говорит, что пользователь выбрал кучу A, и удаляет счетчики из стопки A, даже если пользователь выбирает стопку B или C.
Я много раз просматривал свой код и не вижу причин, по которым это могло бы произойти. Все отступы и скобки проверены и выглядят хорошо. Я попытался переместить оператор while и избавиться от отдельных функций стопки, но ничего из этого не изменило.
#include <iostream>
#include <string>
using namespace std;
string player1() {
string player1;
cout << "Player one, what is your name?" << endl;
cin >> player1;
return player1;
}
string player2() {
string player2;
cout << "Player two, what is your name?" << endl;
cin >> player2;
return player2;
}
int main() {
string name1 = player1();
string name2 = player2();
int pile1 = 8;
int pile2 = 10;
int pile3 = 7;
// while (pile1 >= 0 && pile2 >= 0 && pile3 >= 0) {
char which_pile;
int counters;
cout << name1 << ", what pile do you want to choose from?" << endl;
cin >> which_pile;
while (pile1 >= 0 && pile2 >= 0 && pile3 >= 0) {
if(which_pile == 'A' || 'a') {
cout << "You have chosen pile A. How many counters do you want to take?" << endl;
cin >> counters;
pile1 -= counters;
cout << "\n" << pile1 << endl;
cout << pile2 << endl;
cout << pile3 << "\n" << endl;
} else if(which_pile == 'B' || 'b') {
cout << "You have chosen pile B. How many counters do you want to take?" << endl;
cin >> counters;
pile2 -= counters;
cout << "\n" << pile1 << endl;
cout << pile2 << endl;
cout << pile3 << "\n" << endl;
} else if(which_pile == 'C' || 'c') {
cout << "You have chosen pile C. How many counters do you want to take?" << endl;
cin >> counters;
pile3 -= counters;
cout << "\n" << pile1 << endl;
cout << pile2 << endl;
cout << pile3 << "\n" << endl;
} else {
cout << "Bad input." << endl;
}
cout << name2 << ", what pile do you want to choose from?" << endl;
cin >> which_pile;
if(which_pile == 'A' || 'a') {
cout << "You have chosen pile A. How many counters do you want to take?" << endl;
cin >> counters;
pile1 -= counters;
cout << "\n" << pile1 << endl;
cout << pile2 << endl;
cout << pile3 << "\n" << endl;
} else if(which_pile == 'B' || 'b') {
cout << "You have chosen pile B. How many counters do you want to take?" << endl;
cin >> counters;
pile2 -= counters;
cout << "\n" << pile1 << endl;
cout << pile2 << endl;
cout << pile3 << "\n" << endl;
} else if(which_pile == 'C' || 'c') {
cout << "You have chosen pile C. How many counters do you want to take?" << endl;
cin >> counters;
pile3 -= counters;
cout << "\n" << pile1 << endl;
cout << pile2 << endl;
cout << pile3 << "\n" << endl;
} else {
cout << "Bad input." << endl;
}
}
return 0;
}
Я ожидаю, что если пользователь выберет стопку B или C, программа сообщит, что вы выбрали стопку B или C, а затем удалит счетчики из стопки B или C, но программа всегда выбирает стопку A и удаляет счетчики из стопки A.