Rock, Paper, And Scissors Tournament


General Statement: A tournament is being held for champion players of the game Rock, Paper, and Scissors. For Player A and Player B, determine who wins each game and who wins the overall tournament.

Input: The first line in the data set is an integer that represents the number of datapairs that follow. The data begins on the second line. R represents rock, P represents paper, and S represents scissors.

Output: The output is to be formatted exactly like that for the sample output given below.

Assumptions: The only letters in the input will be upper case R, P, and S. The first letter in the pair is the choice for player A and the second letter is the choice for player B.

Discussion: The game is a draw if both players choose the same item. Paper wins over rock because paper covers rock. Scissors wins over paper because scissors cuts paper. Rock wins over scissors because rock breaks scissors.

Sample Input:
7
RRRSSRSPPPPSRP

Sample Output:
DRAW
A WINS
B WINS
A WINS
DRAW
B WINS
B WINS
B WINS TOURNAMENT

Solution :

#include <stdio.h>
#include <string.h>

int who_wins (char a, char b)
{
    if ( a == 'P' && b == 'R' )
        return 1;
    if ( a == 'P' && b == 'S' )
        return 2;

    if ( a == 'R' && b == 'P' )
        return 2;
    if ( a == 'R' && b == 'S' )
        return 1;

    if ( a == 'S' && b == 'R' )
        return 2;
    if ( a == 'S' && b == 'P' )
        return 1;

    return 0;
}

int main ()
{
    int data_pair;
    scanf ("%d", &data_pair);
    getchar ();

    int count_A = 0;
    int count_B = 0;

    while ( data_pair-- ) {

        char a;
        char b;
        scanf ("%c %c", &a, &b);

        if ( who_wins (a, b) == 1 ) {
            printf ("A WINS\n");
            count_A++;
        }

        else if ( who_wins (a, b) == 2 ) {
            printf ("B WINS\n");
            count_B++;
        }

        else
            printf ("DRAW\n");
    }

    if ( count_A > count_B )
        printf ("A WINS TOURNAMENT\n");
    else
        printf ("B WINS TOURNAMENT\n");

    return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s