Two players are playing a series of games of Rock–paper–scissors. There are a total of N games played represented by an array arr[][] where arr[i][0] is the move of player one and arr[i][1] is the move of the player two in the ith game from the set {‘R’, ‘P’, ‘S’}. The task is to find the winner of each of the game. Note that the game is a draw if both players choose the same item.
Examples:
Input: arr[] = {“RS”, “SR”, “SP”, “PP”}
Output:
A
B
A
DRAW
Input: arr[] = {“SS”, “RP”, “PS”}
Output:
Draw
B
B
Approach: Suppose player one is represented by bit 1 and player two is represented by 0. Moreover, let Rock be represented by 00 (0 in decimal), Paper by 01 (1 in decimal) and Scissors with 10 (2 in decimal).
If player one chooses rock, it will be represented by 100,
Similarly, 101 means Paper is chosen by player one.
The first bit indicates the player number and the next two bits for their choice.
Pattern:
100 (4 in decimal) (player 1, rock), 001 (1 in decimal) (player 2, paper) -> player 2 won (4-1 = 3)
101 (5 in decimal) (player 1, paper), 010 (2 in decimal) (player 2, scissors) -> player 2 won (5-2 = 3)
110 (6 in decimal) (player 1, scissors), 000 (0 in decimal) (player 2, rock) -> player 2 won (6-0 = 6)
101 (5 in decimal) (player 1, paper), 000 (0 in decimal) (player 2, rock) -> player 1 won (5-0 = 5)
110 (6 in decimal) (player 1, scissors), 001 (1 in decimal) (player 2, paper) -> player 1 won (6-1 = 5)
100 (4 in decimal) (player 1, rock), 010 (2 in decimal) (player 2, scissors) -> player 1 won (4-2 = 2)
According to the pattern, if the difference is a multiple of 3 then player two wins or if the difference is 4 then the game is a draw. In the rest of the cases, player one wins the game.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string winner(string moves)
{
map< char , int > data;
data[ 'R' ] = 0;
data[ 'P' ] = 1;
data[ 'S' ] = 2;
if (moves[0] == moves[1]) {
return "Draw" ;
}
if (((data[moves[0]] | 1 << (2))
- (data[moves[1]] | 0 << (2)))
% 3) {
return "A" ;
}
return "B" ;
}
void performQueries(string arr[], int n)
{
for ( int i = 0; i < n; i++)
cout << winner(arr[i]) << endl;
}
int main()
{
string arr[] = { "RS" , "SR" , "SP" , "PP" };
int n = sizeof (arr) / sizeof (string);
performQueries(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static String winner(String moves)
{
HashMap<Character,
Integer> data = new HashMap<Character,
Integer>();
data.put( 'R' , 0 );
data.put( 'P' , 1 );
data.put( 'S' , 2 );
if (moves.charAt( 0 ) == moves.charAt( 1 ))
{
return "Draw" ;
}
if (((data.get(moves.charAt( 0 )) | 1 << ( 2 )) -
(data.get(moves.charAt( 1 )) | 0 << ( 2 ))) % 3 != 0 )
{
return "A" ;
}
return "B" ;
}
static void performQueries(String arr[], int n)
{
for ( int i = 0 ; i < n; i++)
System.out.print(winner(arr[i]) + "\n" );
}
public static void main(String[] args)
{
String arr[] = { "RS" , "SR" , "SP" , "PP" };
int n = arr.length;
performQueries(arr, n);
}
}
|
Python3
def winner(moves):
data = dict ()
data[ 'R' ] = 0
data[ 'P' ] = 1
data[ 'S' ] = 2
if (moves[ 0 ] = = moves[ 1 ]):
return "Draw"
if (((data[moves[ 0 ]] | 1 << ( 2 )) -
(data[moves[ 1 ]] | 0 << ( 2 ))) % 3 ):
return "A"
return "B"
def performQueries(arr,n):
for i in range (n):
print (winner(arr[i]))
arr = [ "RS" , "SR" , "SP" , "PP" ]
n = len (arr)
performQueries(arr, n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static String winner(String moves)
{
Dictionary< char ,
int > data = new Dictionary< char ,
int >();
data.Add( 'R' , 0);
data.Add( 'P' , 1);
data.Add( 'S' , 2);
if (moves[0] == moves[1])
{
return "Draw" ;
}
if (((data[moves[0]] | 1 << (2)) -
(data[moves[1]] | 0 << (2))) % 3 != 0)
{
return "A" ;
}
return "B" ;
}
static void performQueries(String []arr, int n)
{
for ( int i = 0; i < n; i++)
Console.Write(winner(arr[i]) + "\n" );
}
public static void Main(String[] args)
{
String []arr = { "RS" , "SR" , "SP" , "PP" };
int n = arr.Length;
performQueries(arr, n);
}
}
|
Javascript
<script>
function winner(moves)
{
let data = new Map();
data.set( 'R' , 0);
data.set( 'P' , 1);
data.set( 'S' , 2);
if (moves[0] == moves[1])
{
return "Draw" ;
}
if (((data.get(moves[0]) | 1 << (2)) -
(data.get(moves[1]) | 0 << (2))) % 3 != 0)
{
return "A" ;
}
return "B" ;
}
function performQueries(arr,n)
{
for (let i = 0; i < n; i++)
document.write(winner(arr[i]) + "<br>" );
}
let arr=[ "RS" , "SR" , "SP" , "PP" ];
let n = arr.length;
performQueries(arr, n);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)