rock, paper, scissor help C programming

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jmf777
    New Member
    • Aug 2008
    • 19

    rock, paper, scissor help C programming

    Hi here is my problem I want to have the outcome of my rock paper scissors game to print outcomes like: You chose paper and I chose rock. You win. The value for player_choice and machine_choice is declared in select.cpp (listed below).

    My question is how would I go about telling, the report.cpp code below to check the select.cpp code for the variable to print?

    #include "p_r_s.h"

    void report(outcome result, int *win_cnt_ptr,
    int *lose_cnt_ptr, int *tie_cnt_ptr)

    {

    switch(result) {
    case win:
    ++*win_cnt_ptr;
    printf("%27s I chose");
    printf("%c", player_choice);
    printf("%27s you chose");
    printf("%c", machine_choice) ;
    printf("%27s You win.\n", "\"");
    break;
    case lose:
    ++*lose_cnt_ptr ;
    printf("%27s i chose");
    printf("%c", player_choice);
    printf("%27s you chose");
    printf("%c", machine_choice) ;
    printf("%27s You lose.\n", "");
    break;
    case tie:
    ++*tie_cnt_ptr;
    printf("%27s A tie.\n", "");
    break;
    default:
    printf("PROGRAM MER ERROR: Unexpected result!\n\n");
    exit(1);
    }
    }

    #include "p_r_s.h"

    p_r_s selection_by_ma chine(void)
    {
    return ((p_r_s) (rand() % 3));
    }
    p_r_s selection_by_pl ayer(char x)
    {
    char c;

    p_r_s player_choice;

    printf("Input p, r, or s: ");
    scanf("%c", &c);
    switch (c) {
    case 'p':
    player_choice = paper;
    break;
    case 'r':
    player_choice = rock;
    break;
    case 's':
    player_choice = scissors;
    break;
    case 'g':
    player_choice = game;
    break;
    case 'i':
    player_choice = instructions;
    break;
    case 'q':
    player_choice = quit;
    break;
    default:
    player_choice = help;
    break;
    }
    return player_choice;
    }
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    R u saking how should you call the corresponding function in select.cpp or something else..?
    can you explain it further as ia m not able to understand ...

    Raghu

    Comment

    • jmf777
      New Member
      • Aug 2008
      • 19

      #3
      yeah thats basically it. In select.cpp the player and the machine makes a choice of rock paper or scissor. In report.cpp I want to call the selection that the player and the machine made in select.cpp to report.cpp and print that selection.

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Originally posted by jmf777
        yeah thats basically it. In select.cpp the player and the machine makes a choice of rock paper or scissor. In report.cpp I want to call the selection that the player and the machine made in select.cpp to report.cpp and print that selection.

        I think you have to call the report() function in a loop and from the report function you have to call the function in select().


        NOTE:Please use code tags while posting the code.


        Raghu

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by jmf777
          [code=c] printf("%27s I chose");[/code]
          You do that all over the place; check your manuals because it is not correct.

          kind regards,

          Jos

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            You use a storage class specifier to tell the compiler the variable exists but it's not in this file:

            [code=c]
            /*report.cpp*/
            extern p_r_s player_choice;
            extern p_r_s machine_choice;
            [/code]

            Then just define the variables in another file:
            [code=c]
            /*select.cpp*/
            p_r_s player_choice;
            p_r_s machine_choice;
            [/code]
            The linker will hook the variable in one file to the code in the other file.

            Comment

            • jmf777
              New Member
              • Aug 2008
              • 19

              #7
              I've got the program to at least build and executed it by adding the underlined code.

              Code:
              void report(outcome result, int *win_cnt_ptr,
              			int *lose_cnt_ptr, int *tie_cnt_ptr, [U]p_r_s player_choice, p_r_s machine_choice[/U])
              to my header and my report function but now when I win. It prints funny characters like an up and down character before "you chose" and a smiley face box after "you chose." I'm now lost can someone please point me in the right direction. I'm assuming its because my outcome function is only returning a win or lose to main.

              Code:
              #include "p_r_s.h"
              
              outcome compare(p_r_s player_choice, p_r_s machine_choice)
              {
              	outcome result;
              	
              	if (player_choice == machine_choice)
              		return tie;
              	switch (player_choice) {
              case paper:
              	result = (machine_choice == rock) ? win : lose;
              	break;
              case rock:
              	result = (machine_choice == scissors) ? win : lose;
              	break;
              case scissors:
              	result = (machine_choice == paper) ? win : lose;
              	break;
              default:
              	printf("PROGRAMMER ERROR: Unexpected choice!\n\n");
              	exit(1);
              	}
              	return result;
              }

              Comment

              • jmf777
                New Member
                • Aug 2008
                • 19

                #8
                Whoops I just figured it out but thanks to everyone that responded and tried to help

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  Originally posted by jmf777
                  Whoops I just figured it out
                  That's what we like to see.

                  Comment

                  Working...