Rock, Paper, Scissors game help

This is a discussion on Rock, Paper, Scissors game help within the Game Programming forums, part of the General Programming Boards category; Heres my code, and all comments are what the errors are, i just don't understand what they're saying... --------------------------- #include ...

  1. #1
    MillaTime
    Guest

    Rock, Paper, Scissors game help

    Heres my code, and all comments are what the errors are, i just don't understand what they're saying...
    ---------------------------
    #include <iostream.h>
    #include <string.h>
    #include <stdlib.h>

    char computerchoice(int y, char playerchoice);
    void who_wins(int y, char playerchoice);

    int main()
    {
    int x;
    char playerchoice[9];
    cout<<"Welcome to my first game!"<<endl;
    for(x=0;x<500;x++)
    {
    cout<<"Please type in rock, paper, or scissors"<<endl<<"and then press enter"<<endl;
    cin.getline(playerchoice, '/n');
    cout<<"I choose "<<computerchoice<<endl;
    void who_wins();
    }
    return 0;
    }
    char computerchoice()
    {
    char compchoice[9];
    int y=rand()%4;

    switch (y)
    {
    case 1:
    compchoice = "rock "; //'=' : left operand must be l-value
    break;

    case 2:
    compchoice = "paper "; //'=' : left operand must be l-value
    break;

    case 3:
    compchoice = "scissors"; //'=' : left operand must be l-value
    break;
    }
    return compchoice[9];
    }

    char who_wins(char compchoice, char playerchoice)
    {
    if (!strcmp(compchoice, "rock ")) //'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'
    {
    if (strcmp("rock", playerchoice)<0) //'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'
    {
    cout<<endl<<"HAHA, I win!"<<endl;
    }
    else if(strcmp("rock", playerchoice)>0) //'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'
    {
    cout<<endl<<"You got lucky, you lucky bastard..."<<endl;
    }
    else
    {
    cout<<endl<<"Let's try this again!"<<endl;
    }
    }
    if (!strcmp("paper", compchoice)) //'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'
    {
    if (strcmp("paper ", playerchoice)<0) //'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'
    {
    cout<<endl<<"Don't you wish you had mind-reading capabilities like mine???"<<endl;
    }
    else if (strcmp("paper", playerchoice)>0) //'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'
    {
    cout<<endl<<"Ya, I thought I'd throw ya' a bone"<<endl;
    }
    else
    {
    cout<<endl<<"This time, just go ahead and try to beat me..."<<endl;
    }
    }
    if (!strcmp("scissors", compchoice)) //'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'
    {
    if (strcmp("scissors", playerchoice)<0) //'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'
    {
    cout<<endl<<"Man, I ROCK!...heh heh heh...get it? rock...heh heh heh..."<<endl;
    }
    else if (strcmp("scissors", playerchoice)>0) //'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'
    {
    cout<<endl<<"You keep this up and I'm gonna' send an electric shock through the keyboard."<<endl;
    }
    else
    {
    cout<<endl<<"Hey, good job, this is an improvement from your constant losing!"<<endl;
    }
    }
    }
    -----------------------
    anybody know what's goin on?

  2. #2
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Lightbulb Well, try this...

    In order to use '=' on a char, it has to be a pointer. The other way is like this:

    strcpy(myChar, "My String");

    In computerchoice(int y, char choice), change 'char choice' to 'char *choice'. I hope this helps a little. Also, in computerchoice(...) try changing 'return compchoice[9];' to 'return *compchoice'.
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: trifaze_mattu@lycos.com

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  3. #3
    Unreg23432
    Guest
    Hmm, I'm just gonna post some rewritten code...have tested it under linux, so no guarantees that it work on windows...(the "man I rock" string seems a little misplaced though...

    It's often better to use integers when comparing things and switch/case statements gives better structure to the code than lots of if's.

    Good luck with your coding...

    /Fredde

    ------------------------------


    #include <iostream.h>
    #include <string.h>
    #include <stdlib.h>

    #define ROCK (0)
    #define PAPER (1)
    #define SCISSORS (2)

    int convert_choice(const char *choice)
    {
    if (!strcasecmp(choice,"rock")) return ROCK;
    if (!strcasecmp(choice,"paper")) return PAPER;
    if (!strcasecmp(choice,"scissors")) return SCISSORS;
    return -1;
    }

    const char *print_choice(int choice)
    {
    static char rock[] = "rock", paper[] = "paper", scissors[] = "scissors", unknown[] = "unknown";
    switch (choice){
    case 0: return rock;
    case 1: return paper;
    case 2: return scissors;
    default: return unknown;
    }
    }

    int computerchoice(void)
    {
    return rand()%3; // gives value of either 0,1 or 2
    }

    void who_wins(int compchoice, int playerchoice)
    {
    switch (compchoice){
    case ROCK:
    switch (playerchoice){
    case SCISSORS:
    cout<<endl<<"HAHA, I win!"<<endl;
    break;
    case PAPER:
    cout<<endl<<"You got lucky, you lucky bastard..."<<endl;
    break;
    case ROCK:
    cout<<endl<<"Let's try this again!"<<endl;
    break;
    }
    break;
    case PAPER:
    switch (playerchoice){
    case ROCK:
    cout<<endl<<"Don't you wish you had mind-reading capabilities like mine???"<<endl;
    break;
    case SCISSORS:
    cout<<endl<<"Ya, I thought I'd throw ya' a bone"<<endl;
    break;
    case PAPER:
    cout<<endl<<"This time, just go ahead and try to beat me..."<<endl;
    break;
    }
    break;
    case SCISSORS:
    switch (playerchoice){
    case ROCK:
    cout<<endl<<"Man, I ROCK!...heh heh heh...get it? rock...heh heh heh..."<<endl;
    break;
    case PAPER:
    cout<<endl<<"You keep this up and I'm gonna' send an electric shock through the keyboard."<<endl;
    break;
    case SCISSORS:
    cout<<endl<<"Hey, good job, this is an improvement from your constant losing!"<<endl;
    break;
    }
    break;
    }
    }

    int main()
    {
    char playerchoice[9];
    int compchoice;
    cout<<"Welcome to my first game!"<<endl;
    for(int x=0;x<500;x++){
    cout<<"Please type in rock, paper, or scissors"<<endl<<"and then press enter"<<endl;
    cin.getline(playerchoice, 9, '\n');
    if (convert_choice(playerchoice) != -1){
    compchoice = computerchoice();
    cout<<"I choose "<< print_choice(compchoice)<<endl;
    who_wins(compchoice,convert_choice(playerchoice));
    } else {
    cout << "What did you say?" << endl;
    }
    }
    return 0;
    }

  4. #4
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    sorry to bust your bubble but please use the code tags or include your source in a seperate file. Thanks.

  5. #5
    MillaTime
    Guest
    Alright, i fixed everything down to one error. I used the pointer idea rather than the strcpy(...) thing, cuz i thought it would be more educating. I got rid of the "I ROCK" thing, cuz ya, it was out of place. So, here's my new code, and what the hell is that error saying!?
    Oh, and I don't know how to include files in my posts, so sry droid, I guess you'll just have to scroll a little... (;

    ------------------------------

    #include <iostream.h>
    #include <string.h>
    #include <stdlib.h>

    char computerchoice(char *compchoice);
    char who_wins(char *compchoice, char *playerchoice);

    int main()
    {
    int x;
    char *playerchoice[9];
    cout<<"Welcome to my first game!"<<endl;
    for(x=0;x<500;x++)
    {
    cout<<"Please type in rock, paper, or scissors"<<endl<<"and then press enter"<<endl;
    cin.getline(&playerchoice, 9, '/n'); // 'class istream &__thiscall istream::getline(char *,int,char)' : cannot convert parameter 1 from 'char (*)[9]' to 'char *'
    cout<<"I choose "<<computerchoice<<endl;
    void who_wins();
    }
    return 0;
    }
    char computerchoice(char *compchoice)
    {
    int y=rand()%4;

    switch (y)
    {
    case 1:
    compchoice = &"rock ";
    break;

    case 2:
    compchoice = &"paper ";
    break;

    case 3:
    compchoice = &"scissors";
    break;
    }
    return *compchoice;
    }

    char who_wins(char *compchoice, char *playerchoice)
    {
    if (!strcmp(compchoice, "rock "))
    {
    if (strcmp("rock", playerchoice)<0)
    {
    cout<<endl<<"HAHA, I win!"<<endl;
    }
    else if(strcmp("rock", playerchoice)>0)
    {
    cout<<endl<<"You got lucky, you lucky bastard..."<<endl;
    }
    else
    {
    cout<<endl<<"Let's try this again!"<<endl;
    }
    }
    if (!strcmp("paper", compchoice))
    {
    if (strcmp("paper ", playerchoice)<0)
    {
    cout<<endl<<"Don't you wish you had mind-reading capabilities like mine???"<<endl;
    }
    else if (strcmp("paper", playerchoice)>0)
    {
    cout<<endl<<"Ya, I thought I'd throw ya' a bone"<<endl;
    }
    else
    {
    cout<<endl<<"This time, just go ahead and try to beat me..."<<endl;
    }
    }
    if (!strcmp("scissors", compchoice))
    {
    if (strcmp("scissors", playerchoice)<0)
    {
    cout<<endl<<"Good ol' rock, nothing beats it..."<<endl;
    }
    else if (strcmp("scissors", playerchoice)>0)
    {
    cout<<endl<<"You keep this up and I'm gonna' send an electric shock through the keyboard."<<endl;
    }
    else
    {
    cout<<endl<<"Hey, good job, this is an improvement from your constant losing!"<<endl;
    }
    }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. paper rock scissors
    By Amyaayaa in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 09:59 AM
  2. my upcoming UNO card game :)
    By Hussain Hani in forum Game Programming
    Replies: 5
    Last Post: 01-24-2008, 12:19 AM
  3. Please comment on my c++ game
    By MegaManZZ in forum Game Programming
    Replies: 10
    Last Post: 01-22-2008, 10:03 AM
  4. Replies: 8
    Last Post: 10-24-2007, 05:46 PM
  5. need help with rock paper scissors program
    By Mshock in forum C Programming
    Replies: 3
    Last Post: 04-22-2006, 07:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21