lundi 29 avril 2019

Why does my nim game always choose pile A?

I'm trying to make a game of nim, but when the program asks the user which pile they want to choose from, either A, B, or C, the program always says that the user chose pile A and removes the counters from pile A, even if the user chooses pile B or C.

I have looked over my code a lot and see no reason this would happen. All of the indentation and parenthesis check out and look fine. I tried moving the while statement and getting rid of the individual pile functions, but none of that changes anything.

#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;
}

I expect if the user chooses pile B or C, the program says you have chosen pile B or C, and then removes the counters from pile B or C, but the program always chooses pile A and removes counters from pile A.

Aucun commentaire:

Enregistrer un commentaire