vendredi 25 novembre 2016

what is wrong with this code? it is a game

Why it is repeating the last step?!

The game is: Write a program that plays an ancient Chinese game. Actually, this is a simplified version of the game. Each game starts with a user-specified number of stones in a pile. The user and the computer take turns removing either one or two stones from the pile. The player who takes the last stone loses. program should have the computer use the optimal playing strategy. The optimal strategy is as follows: Divide the remaining number of stones by three. If the remainder is zero, then two stones are removed, or else one stone is removed. For example, if the remaining number of stones is nine or fifteen, then two stones are removed: if the remaining number of stones is eight or ten, then one stone is removed Your program should allow the user to play additional games of Game as long as he/she enters a "y" or "yes" (lowercase or uppercase) in response to a "Do you want to play again?"

#include <iostream>
#include <string>
#include <ctime>
#include <cmath>
using namespace std;

int user_play(int &pile);
int computer_play(int &pile);
int toRemove1 = 1;
int toRemove2 = 2;

int pile;

int main()
{
// what is wrong with this code 
// it is repeating the last step 

string ans1;
string ans2;
    do{
       cout << "\n\n=========================Welcome to the Game=========================" << endl << endl   //welcome message and displays rule for user//

                    << "The one who takes the last marble loses. " << endl << endl
                    << "Enjoy and Best of luck ^_^\n\n";
                cout << "  |||||\n 0 . . 0\n0   ^   0\n0  \\_/  0\n 0     0\n  00000\n   \n\n";


                cout << "Please enter the number of stones you wish you begin with: ";
                cin >> pile;


                cout << "would you like to go first: ";
                cin >> ans1;
                if (ans1 == "y" || ans1 == "yes"){


                    user_play(pile);
                }
                else{
                    computer_play(pile);
                }
                cout << "\nDo you want to play again? ";  //asks the player if he wishes to play the game again//
                cin >> ans1;
            } while (ans1 == "yes" || ans1 == "y");
            system("pause > null");
            return 0;
        }
        int user_play(int &pile)
        {
            int stones1;
            while(pile != 1 ) {
                cout << "how many would you like remove: 1 or 2? ";
                cin >> stones1;
                pile = pile - stones1;
                computer_play(pile);
            }
            cout << "The number of stones left is 0 \n ";
            cout << "The computer wins\n ";
            return 0;
        }
        int computer_play(int &pile)
        {
            int stones2 = pile % 3;
            while (pile != 1){
                if (stones2 = 0){
                    pile = pile - toRemove2;
                    cout << "The number of stones left is " << pile << " the computer remove 2 stones \n";
                    user_play(pile);
                }
                else
                {
                    pile = pile - toRemove1;
                    cout << "The number of stones left is " << pile << " the computer remove 1 stone \n";
                    user_play(pile);
                }
            }
            cout << "The number of stones left is 0 \n ";
            cout << "You win;\n";
            return 0;
        }
  // what is wrong with this code 
  // it is repeating the last step 

Aucun commentaire:

Enregistrer un commentaire