1

hi i'm working on an assignment and having trouble with this piece of the program.

The game of “23” is a two-player game that begins with a pile of 23 tooth- picks. Players take turns, withdrawing either 1, 2, or 3 toothpicks at a time. The player to withdraw the last toothpick loses the game. Write a human vs. computer program that plays “23”. The human should always move first. When it is the computer’s turn, it should play according to the following rules:

  • withdraw 4 – X toothpicks, where X is the number of toothpicks the human withdrew on the previous turn.

  • if there are 2 to 4 toothpicks left, then the computer should withdraw enough toothpicks to leave 1.

  • if there is one toothpick left, then the computer has to take it and it loses.

When the human player enters the number of toothpicks to withdraw, the program should perform input validation. Make sure that the entered number is between 1 and 3 and that the player is not trying to withdraw more toothpicks than exist in the pile.

here is my code. any help will be appreciated.

#include <iostream>
using namespace std;

void compAlgorithm(int totalTp, int compTp, int userTp, int countr) {
do {
    if (totalTp > 4) {
        compTp = 4 - userTp;
        totalTp += totalTp - compTp;
        countr--;
    }
    if (totalTp >= 2 && totalTp <= 4) {
        switch (totalTp) {
            case 2:
                totalTp += totalTp - 1;
                countr--;
                break;
            case 3:
                totalTp += totalTp - 2;
                countr--;
                break;
            case 4:
                totalTp += totalTp - 3;
                countr--;
                break;
            }
        }
    } while (countr == 1);
}

int main() {
    int userTp = 0;
    int compTp = 0;
    int totalTp = 23;
    int countr = 0;

    do {
        if (countr == 0) {
            cout << "please enter a vale of toothpics between 1 and 3" << endl;
            cin >> userTp;
            totalTp += totalTp - userTp;
            countr++;
        }
        else if (countr == 1) {
            compAlgorithm;
        }
    } while (totalTp >= 2);

    if (totalTp == 1 && countr == 1) {
        cout << "you win" << endl;
    }
    else if (totalTp > 0 && totalTp < 2 && countr == 0) {
        cout << "please enter a vale of toothpics between 1 and 2" << endl;
        cin >> userTp;
        totalTp = totalTp - userTp;
        switch (totalTp) {
        case 1:
            cout << "you win" << endl;
            break;
        case 2:
            cout << "you loose" << endl;
            break;
        }
    }
    system("pause");
    return 0;
}

thanks in advance.

2
  • 3
    It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: How to debug small programs Commented Oct 21, 2016 at 20:10
  • 1
    Spelling: change "you loose" to "you lose". Commented Oct 21, 2016 at 20:15

2 Answers 2

3

This is wrong:

else if (countr == 1) {
    compAlgorithm;
}

you must pass arguments to this function call, if that is your intent. Currently this line compAlgorithm; does nothing and possibly causes infinite loop.

Sign up to request clarification or add additional context in comments.

Comments

0

I think this might be because C++ pass the arguments through functions as a value if you don't specify anything. In function compAlgorithm you must use a reference in totalTp and countr, because you expect that they change outside the scope. I leave you this link to see more about this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.