-2

What I am trying to do is convert a int value into a string so the output works correctly. The issue I am having is that's obviously giving me an error message because I cannot assign a string to a integer value. So the help I need is how can I create a function that specifically converts them to a string and return them to my print_result function? Thank you!

#include<iostream>
#include<sstream>
#include<string>

using std::cout;
using std::cin;
using std::endl;
using std::stringstream;
using std::string;

int strategy1(int player, int previous_result, int previous_play, int opponent_previous_play);
int strategy2(int player, int previous_result, int previous_play, int opponent_previous_play);
int strategy3(int player, int previous_result, int previous_play, int opponent_previous_play);
int score(int p1, int p2);
void print_result(int round, int p1, int p2, int winner);





int main (){
    int result, p1, new_p1, p2, new_p2, rounds;
    p1 = 1; // start with rock
    p2 = 1; // start with rock
    cout << "How many rounds:";
    cin >> rounds;

    for(int i=0; i<rounds; i++){
        result = score(p1,p2);
        print_result(i+1, p1, p2, result);
        new_p1 = strategy1(1, result, p1, p2);
        new_p2 = strategy3(2, result, p2, p1);
        p1 = new_p1;
        p2 = new_p2;
    }
}

int strategy1(int player, int previous_result, int previous_play, int opponent_previous_play){

    if(previous_play == 1)
        previous_play = 2;
    else if(previous_play == 2)
        previous_play = 3;
    else if(previous_play == 3)
        previous_play = 1;


    return previous_play;


}

int strategy2(int player, int previous_result, int previous_play, int opponent_previous_play){

    if(player == 1){

        if(previous_result == 2)
            previous_play = opponent_previous_play;
        else
            previous_play = previous_play;
    }

    if(player == 2){
        if(previous_result == 1)
            previous_play = opponent_previous_play;
        else
            previous_play = previous_play;

    }
}

int strategy3(int player, int previous_result, int previous_play, int opponent_previous_play){

     if(player == 1){

        if(previous_result == 2){

            if(previous_play == 1 && opponent_previous_play == 2)
                previous_play = 3;
            else if(previous_play == 2 && opponent_previous_play == 3)
                previous_play = 1;
            else
                previous_play = 2;
        }
    }


    if(player == 2){

        if(previous_result == 1){

            if(previous_play == 1 && opponent_previous_play == 2)
                previous_play = 3;
            else if(previous_play == 2 && opponent_previous_play == 3)
                previous_play = 1;
            else
                previous_play = 2;
        }
    }


    return previous_play;
}

int score(int p1, int p2){
    long result = 0;

    if( ((p1 == 1) && (p2 == 1)) || ((p1 == 2) && (p2 == 2)) || ((p1 == 3) && (p2 == 3)) )
        result = 0; 

    else if( ((p1 == 1) && (p2 == 3 )) || ((p1 == 2) && (p2 == 1)) || ((p1 == 3) && (p2 == 2)) )
        result = 1;

    else if(( (p1 == 1) && (p2 == 2) ) || ((p1 == 2) && (p2 == 3)) || ((p1 == 3 ) && (p2 == 1)) ) 
        result = 2;


        return result;
}




void print_result(int round, int p1, int p2, int winner){


    //ERROR WON'T LET ME CHANGE THE INT INTO A STRING
    if(p1 == 1)
        p1 = "rock";
    else if(p1 == 2)
        p1 = "paper";
    else
        p1 = "scissors";

    //ERROR WON'T LET ME CHANGE THE INT INTO A STRING
    if(p2 == 1)
        p2 = "rock";
    else if(p2 == 2)
        p2 = "paper";
    else
        p2 = "scissors";



    if(winner == 0)
        cout << "Round " << round << ":" << " p1=" << p1 << " vs" << " p2=" << p2 << ": tie" << endl;
    else if(winner == 1)
        cout << "Round " << round << ":" << " p1=" << p1 << " vs" << " p2=" << p2 << ": p1" << endl;
    else if(winner == 2)
      cout << "Round " << round << ":" << " p1=" << p1 << " vs" << " p2=" << p2 << ": p2" << endl;

}
Giorgio
  • 35
  • 1
  • 8

2 Answers2

1

You have an int. You need to convert from an integer to a particular string, "mapping" a string to an integer. You can solve this by creating a temporary string, assigning the selection string to the temp string and then displaying the temp string.

string player1choice;
if(p1 == 1)
    player1choice = "rock";
else if(p1 == 2)
    player1choice = "paper";
else
    player1choice = "scissors";

But that's a bit slow.

Instead, try:

static std::string choices[] = 
{
    "Rock",
    "Paper",
    "Scissors"
};

Then you can use choices[p1-1] wherever you need the string. For example,

cout << p1 << " = " << choices[p1-1];

Caveat: Make darn sure you never pass in anything but 1,2, or 3 or you'll wander out of the array's bounds. Look into using an enum to instead of an int for the player's choice. That way the compiler can more easily catch missuse.

enum choicesenum
{
    ROCK = 1,
    PAPER = 2,
    SCISSORS = 3
};

Now rather than writing 1 to mean rock, you can write ROCK. Yeah, you type more characters, but it's a heck of a lot easier to read:

int strategy1(int player, 
              choicesenum previous_result, 
              choicesenum previous_play, 
              choicesenum opponent_previous_play){

    if(previous_play == ROCK)
        previous_play = PAPER;
    else if(previous_play == PAPER)
        previous_play = SCISSORS;
    else if(previous_play == SCISSORS)
        previous_play = ROCK;
    return previous_play;
}

If you try to use numbers not in the enum, it's really easy to catch:

strategy1(1, ROCK, PAPER, HAND_GRENADE); // compiler rejects this 
strategy1(1, ROCK, PAPER, 4); // compiler can warn you 

Also the 4 stands out visually a lot more than in the old case:

strategy1(1, 2, 3, 4);

And now that I think about it

enum choicesenum
{
    ROCK = 0,
    PAPER = 1,
    SCISSORS = 2
};

eliminates the need for the -1 in accessing the array.

cout << p1 << " = " << choices[p1];
cout << choices[ROCK]
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Thank you @user4581301 and I have no idea what an enum because I just started :/ – Giorgio Sep 26 '15 at 21:13
  • An enum is a mapping of a name to a number. For example, you can... Forget this. I'm going to put the example into the answer. Should have done it in the first place – user4581301 Sep 26 '15 at 21:14
0

You can't use the same variable for both a string and an integer. Here's one possible solution:

void print_result(int round, int p1, int p2, int winner){
    std::string play1, play2, win;
    if(p1 == 1)
        play1 = "rock";
    else if(p1 == 2)
        play1 = "paper";
    else
        play1 = "scissors";

    if(p2 == 1)
        play2 = "rock";
    else if(p2 == 2)
        play2 = "paper";
    else
        play2 = "scissors";

    if(winner == 0)
        win = "tie";
    else if(winner == 1)
        win = "p1";
    else
        win = "p2";

    cout << "Round " << round << ":"
         << " p1=" << play1 << " vs p2=" << play2 << ": "
         << win << endl;
}

You should refactor that, of course. You could use a function which looks up a name from an integer; it would probably be clearer if it used a switch statement rather than a series of ifs. Or you could just use a std::vector or names.

And really, you should use enums rather than magic integers.

rici
  • 234,347
  • 28
  • 237
  • 341
  • `switch` instead of `if` may be worth considering – Walter Sep 26 '15 at 20:58
  • @Walter: Yeah, that too. I'm assuming OP is really just starting and hasn't been exposed to much of the language yet. – rici Sep 26 '15 at 21:00
  • I know how switch case statements work but yes, I do not have much experience with them. – Giorgio Sep 26 '15 at 21:01
  • Thank you @rici for the explanation. But was that the only way to do it? My professor talked about making another function and converting them in that function and returning them to the function print_result. Would that have worked but will it be worth the work to do that? – Giorgio Sep 26 '15 at 21:05
  • @Giorgio Function is not necessary. You can use an array. – user4581301 Sep 26 '15 at 21:09
  • @giorgio:yes, a function would be good. I mention that at the end of the answer. – rici Sep 27 '15 at 02:09