built a paper rock scissor game

Discussion in 'Homework Help' started by kamarul amin, Mar 9, 2015.

  1. kamarul amin

    Thread Starter Member

    Dec 2, 2014
    62
    3
    guys, is there any idea how to built paper rock scissor gmae in C in linux?? hm i just finished loop and ongoing in function then my lecturer ask me to built the game as an assignment :(
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    You should write what the rules are first. From there, you can determine how you will build the game (e.g. user selects rock, paper, or scissors, randomly generate the response/have a second player select their choice, etc.). From there, you should be able to write some code for a specific design goal.
     
    kamarul amin likes this.
  3. kamarul amin

    Thread Starter Member

    Dec 2, 2014
    62
    3
    Wow thats seem tough huhu he gave me only two weeks lol. As i know C in linux is little bit different from in windows. Amci right?? About the library n so on
     
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    C is C, except when it's a little sharp or incrementally different. In other words, it's the same.

    It is not difficult, its just a matter of understanding what is required of you. This is one of the most important parts of programming - defining the requirements. Once you know what is required, you can look into how to implement it. Then, you implement it.

    If you don't understand the problem you hope to solve, you can never solve it.

    I would have given about three days to do this, so be thankful you don't have me as your instructor. ;)
     
    kamarul amin likes this.
  5. GopherT

    AAC Fanatic!

    Nov 23, 2012
    6,043
    3,807
    @kamarul amin
    Just generate random numbers
    For example, symbol = 3 * rand()

    If the result is <1, rock
    >=1 but <2, paper
    >=2 scissor

    You can work from there.

    EDIT: Ok, see correction below - if you build this version, pick "Rock" when you play.
     
    Last edited: Mar 9, 2015
    kamarul amin likes this.
  6. WBahn

    Moderator

    Mar 31, 2012
    17,743
    4,795
    Keep in mind that rand() returns an integer in the range 0 <= rand() <= RAND_MAX, so this code is going to come up scissors a lot!
     
    kamarul amin likes this.
  7. WBahn

    Moderator

    Mar 31, 2012
    17,743
    4,795
    MOD Note: Moved to Homework Help.
     
    kamarul amin likes this.
  8. GopherT

    AAC Fanatic!

    Nov 23, 2012
    6,043
    3,807
    Too many programming languages. I'm sure the OP got the gist of it.

    Some use 0 - 1 for rand(). c uses 0 to ((2^16)-1)
     
    kamarul amin likes this.
  9. WBahn

    Moderator

    Mar 31, 2012
    17,743
    4,795
    No, C uses 0 to RAND_MAX.

    RAND_MAX is an implementation specific value that, on most compilers these days, is [(2^31)-1]. Even back when most compilers used 16-bit ints, RAND_MAX was almost never [(2^16)-1] since that is not compatible with 16-bit singed integers, which is almost always what rand() feeds into. So on those compilers it was usually [(2^15)-1], which is the smallest value of RAND_MAX that is allowed by the language specification. RAND_MAX is not even required to be of the form [(2^n)-1] and on implementations that use a better PRNG, RAND_MAX may be forced to be something else, such as a prime number.
     
    kamarul amin likes this.
  10. GopherT

    AAC Fanatic!

    Nov 23, 2012
    6,043
    3,807
    Hopefully he figures out a module divide and he won't care about the value of RAND_MAX
     
    kamarul amin likes this.
  11. WBahn

    Moderator

    Mar 31, 2012
    17,743
    4,795
    For what he is doing, using the remainder operator (it isn't a modulo operator, but for positive arguments the distinction is moot) will work. In general it introduces a bias into the distribution that favors outcomes less than a certain value over larger ones. Back when RAND_MAX was 32767 on most implementations, that bias started having noticeable effects as soon as the range over which you wanted a random number got to be more than 50 to 100 or so. If you just want one of three values, then the lowest value has about a 0.01% edge over the largest one. For games like this that isn't noticeable, but for anything at all serious that is often big enough to be a problem.
     
    kamarul amin likes this.
  12. kamarul amin

    Thread Starter Member

    Dec 2, 2014
    62
    3
    Haha i will try my best.:);)
     
  13. kamarul amin

    Thread Starter Member

    Dec 2, 2014
    62
    3
    Wow i thought that rand () only can give weird int. Never know that we can range the random number. Nice tip :) ;)
     
  14. kamarul amin

    Thread Starter Member

    Dec 2, 2014
    62
    3
    Haaaaa help me!!!!:(:( my lecturer just only told me about how to display random number by computer timing
     
  15. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Before we get into whether a random number is required, is this supposed to be a single player game? If not, you won't need a random number. If, however, this is supposed to be a single player game, then, yes, a random number is your option.
    Well, you aren't (rather, shouldn't be) using the computer time to create a random number, rather, you should be using srand() to seed the random number generator, basically, you're giving it a starting point from which to generate more numbers that are randomly distributed (almost).

    Do you know the modulo operator (%) ? You can use this to ensure your results are between 0 and a given number-1. See here for more information on rand().

    So far you have not written any code (that we know of), nor have you shown any work whatsoever. Please, show us some work so we can continue.
     
    kamarul amin likes this.
  16. kamarul amin

    Thread Starter Member

    Dec 2, 2014
    62
    3
    i know well about th modulus operator :)
    yeah i'm about to start of but didnt get any idea where to start. when i do the code, i will post it here. he ask to record the win/lose in HTML possible to learn it within a week. thanks for the idea :D
     
  17. kamarul amin

    Thread Starter Member

    Dec 2, 2014
    62
    3
    do u want me to post the question from my lecturer?????
     
  18. kamarul amin

    Thread Starter Member

    Dec 2, 2014
    62
    3
    noo!!!!! why is it the rand()%4 keep producing number 3 ? :'(
     
  19. kamarul amin

    Thread Starter Member

    Dec 2, 2014
    62
    3
    so, this is my new coding. i do find it how to make random int. thanks for your link up there. hehe but still doesn't complete..

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    main()
    {
    char bl;
    int ch1, ch2, x, y, z;

    srand(time(NULL));
    printf("\nThis is the paper rock scissor game");
    printf("\nYou may choose \n'1' for paper\n'2' for rock\n'3' for scissor");
    printf("\nPlayer 1 make a choose :");
    scanf("%d", &ch1);
    ch2=rand()%4;

    switch (ch1)
    {
    case 1: printf("\nThe player 1 has choose paper");
    break;
    case 2: printf("\nThe player 1 has choose rock");
    break;
    case 3: printf("\nThe player 1 has choose scissor");
    break;
    default: printf("\nInvalid input");
    break;

    }

    switch (ch2)
    {
    case 1: printf("\nThe player 2 has choose paper");
    break;
    case 2: printf("\nThe player 2 has choose rock");
    break;
    case 3: printf("\nThe player 2 has choose scissor");
    break;
    default: printf("\nInvalid input");
    break;

    }

    if(ch1==1 && ch2==2)
    printf("\nThe player 2 win the game");
    else if(ch1==1 && ch2==3)
    printf("\nThe player 2 win the game");
    else if(ch1==1 && ch2==1)
    printf("\nThe game tie -,-");
    else if(ch1==2 && ch2==1)
    printf("\nThe player 1 win the game");
    else if(ch1==2 && ch2==2)
    printf("\nThe game tie");
    else if(ch1==2 && ch2==3)
    printf("\nThe player 1 win the game");
    else if(ch1==3 && ch2==1)
    printf("\nThe player 1 win the game");
    else if(ch1==3 && ch2==2)
    printf("\nThe player 1 win the game");
    else if(ch1==3 && ch2==3)
    printf("\nThe game tie");


    }
     
  20. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Two things before I comment on the code:
    1. Saying it doesn't work doesn't help others understand what it is doing, which is often a big indicator as to where to look for a problem. Explain what it does and any errors you encounter.
    2. Please put your code between code tags ([code_][/code_] (without the underscore) - it makes it easier to read without making the page unnecessarily long.

    As for the code, you have
    Which will generate numbers 0-3. You are expecting 1-3.
     
    kamarul amin likes this.
Loading...