Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*coded by Roman R*/


    char P1_Name[20], P2_Name[20];
    char rock, paper, scissors;
    char P1Choice, P2Choice, Greaterchoice;

    char rock_paper_scissors(P1_Name, P2_Name) {
        char Greaterchoice;
    if (P1Choice==rock && P2Choice==scissors || P1Choice==scissors && P2Choice==paper || P1Choice==paper && P2Choice==rock) {
        Greaterchoice = 0;
    } else if (P1Choice==rock && P2Choice==paper || P1Choice==paper && P2Choice==scissors || P1Choice==scissors && P2Choice==rock) {
        Greaterchoice = 1;
    } else {
        Greaterchoice = 2;
    }
   
    printf("Player1 Enter your name.:");
        scanf ("%s", P1_Name);
    printf("Player2 Enter your name.:");
        scanf ("%s", P2_Name);
       
char get_p1_input() {
    char P1Choice
    printf("Enter rock paper or scissors:\n")
    scanf("%c", &P1Choice);
    return P1Choice;
    }
char get_p2_input() {
    char P2Choice
    printf("Enter rock paper or scissors:\n")
    scanf("%c", &P2Choice);
    return P2Choice;
    }
           
int main() {
        if (Greaterchoice==0) {
        printf("%s", Won, P1_Name);
        }
       
        if (Greaterchoice==1) {
        printf("%s", won, P2_Name)
        }
       
        if (Greaterchoice==2) {
        printf("It,s a draw");
        }
    return 0;
}


What I have tried:

I am a beginner and would please like some advice how to complete this 2 player rock paper scissors game. I am studying but I am having a hard time to find the right answers. Please come at this assuming i have little knowledge on the terminology. Thank you!
Posted
Updated 4-Dec-20 5:46am
v2
Comments
Rick York 3-Dec-20 16:17pm    
You should indent your code better. It is much easier to follow and understand when you do.
W Balboos, GHB 4-Dec-20 7:43am    
You say you had a "hard time to find the right answer". I am hoping you're trying to learn to program. You shouldn't be looking for "the right answer". You should be looking on how to create an answer. How to do some specific thing - which is learning how to do something you can use again and again.

I'm giving you a working version, albeit minimalistic, of your code. Note there aren't global variables (they are evil).
C
#include <stdio.h>

const char Rock = 'r';
const char Scissors = 's';
const char Paper = 'p';

char get_input(int player_no)
{
  while (1)
  {
    printf("Player %d, please enter Rock (r) Paper (p) or Scissors (s):\n", player_no);
    char choice = getchar();
    getchar(); // rempve the newline in the input buffer
    if ( (choice == Rock) || (choice == Scissors) || (choice == Paper))
      return choice;
  }
}

int winner_number( char p1_choice, char p2_choice)
{
  if ( (p1_choice==Rock && p2_choice==Scissors) || (p1_choice==Scissors && p2_choice==Paper) || (p1_choice==Paper && p2_choice==Rock))
  {
    return 1;
  }
  else if ( (p1_choice==Rock && p2_choice==Paper) || (p1_choice==Paper && p2_choice==Scissors) || (p1_choice==Scissors && p2_choice==Rock) )
  {
    return 2;
  }
  return 0;
}

int main()
{
  char answer;
  do
  {
    char p1_choice = get_input(1);
    char p2_choice = get_input(2);
    int winner = winner_number( p1_choice, p2_choice);
    if ( winner == 0)
      printf("it is a draw\n");
    else
      printf("the winner is player %d\n", winner);

    printf("do you wish to continue?\n");
    answer = getchar();
    getchar(); // remove the newline in the input buffer
  } while ( answer == 'y');
  return 0;
}
 
Share this answer
 
v2
Comments
Member 14999245 8-Dec-20 2:09am    
Hello CPalini could you possibly walk me through the logic of the code please>?
CPallini 8-Dec-20 4:58am    
The logic is simple and mimics your original one. There is a loop in the main function, that user may exit on request. The loop first calls twice the get_input function in order to collect both player 1 and player 2 input and eventually calls the winner_number function to obtain the winner (or the draw).
If you have specific question on those points, then don't esistate to ask.
That will not compile.

You cannot execute code unless it is inside a function in C - the system wouldn't know what code you want to starts with!
So you need a main function so that the system knows where to start executing code - it calls that when your app starts, and it can call any other functions you need to.
 
Share this answer
 
Quote:
I am studying but I am having a hard time to find the right answers.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Wow Thank you for the code. It helps me to understand how code works. I am going to see if I can modify it. I will post back with what I have.
 
Share this answer
 
Comments
Richard MacCutchan 4-Dec-20 11:10am    
This is not a solution. You should use the Have a Question or Comment? link below CPallini's message, so he gets notified of your comment.
Can i ask questions CPallini? If there is a better way you would prefer please let me know. So on line 9 you state that while parameter is 1 printf. What is 1 referring to? I assume while 1 means while true. True to what or in what aspect?
 
Share this answer
 
Comments
CHill60 4-Dec-20 12:35pm    
If you want to comment on a solution use the "Have a Question or Comment?" link next to it. Do not post comments as solutions - the member will not be notified of your response.
You have already been told this!
CPallini 4-Dec-20 14:32pm    
Yes you may well ask. It would have been better asking directly (At the bottom of my solution). Anyway, according to C programming language:
https://en.cppreference.com/w/c/language/while
So, yes, 1 (like any non-zero value) is interpreted as 'true' that is the loop is executed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900