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 **i ^{th}** 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 dofference 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++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the ` `// winner of the game ` `string winner(string moves) ` `{ ` ` ` `map<` `char` `, ` `int` `> data; ` ` ` `data[` `'R'` `] = 0; ` ` ` `data[` `'P'` `] = 1; ` ` ` `data[` `'S'` `] = 2; ` ` ` ` ` `// Both the players chose to ` ` ` `// play the same move ` ` ` `if` `(moves[0] == moves[1]) { ` ` ` `return` `"Draw"` `; ` ` ` `} ` ` ` ` ` `// Player A wins the game ` ` ` `if` `(((data[moves[0]] | 1 << (2)) ` ` ` `- (data[moves[1]] | 0 << (2))) ` ` ` `% 3) { ` ` ` `return` `"A"` `; ` ` ` `} ` ` ` ` ` `return` `"B"` `; ` `} ` ` ` `// Function to perform the queries ` `void` `performQueries(string arr[], ` `int` `n) ` `{ ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `cout << winner(arr[i]) << endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string arr[] = { ` `"RS"` `, ` `"SR"` `, ` `"SP"` `, ` `"PP"` `}; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(string); ` ` ` ` ` `performQueries(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the ` `// winner of the game ` `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` `); ` ` ` ` ` `// Both the players chose to ` ` ` `// play the same move ` ` ` `if` `(moves.charAt(` `0` `) == moves.charAt(` `1` `)) ` ` ` `{ ` ` ` `return` `"Draw"` `; ` ` ` `} ` ` ` ` ` `// Player A wins the game ` ` ` `if` `(((data.get(moves.charAt(` `0` `)) | ` `1` `<< (` `2` `)) - ` ` ` `(data.get(moves.charAt(` `1` `)) | ` `0` `<< (` `2` `))) % ` `3` `!= ` `0` `) ` ` ` `{ ` ` ` `return` `"A"` `; ` ` ` `} ` ` ` ` ` `return` `"B"` `; ` `} ` ` ` `// Function to perform the queries ` `static` `void` `performQueries(String arr[], ` `int` `n) ` `{ ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `System.out.print(winner(arr[i]) + ` `"\n"` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `String arr[] = { ` `"RS"` `, ` `"SR"` `, ` `"SP"` `, ` `"PP"` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `performQueries(arr, n); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the ` `# winner of the game ` `def` `winner(moves): ` ` ` `data ` `=` `dict` `() ` ` ` `data[` `'R'` `] ` `=` `0` ` ` `data[` `'P'` `] ` `=` `1` ` ` `data[` `'S'` `] ` `=` `2` ` ` ` ` `# Both the players chose to ` ` ` `# play the same move ` ` ` `if` `(moves[` `0` `] ` `=` `=` `moves[` `1` `]): ` ` ` `return` `"Draw"` ` ` ` ` `# Player A wins the game ` ` ` `if` `(((data[moves[` `0` `]] | ` `1` `<< (` `2` `)) ` `-` ` ` `(data[moves[` `1` `]] | ` `0` `<< (` `2` `))) ` `%` `3` `): ` ` ` `return` `"A"` ` ` ` ` `return` `"B"` ` ` `# Function to perform the queries ` `def` `performQueries(arr,n): ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `print` `(winner(arr[i])) ` ` ` `# Driver code ` `arr ` `=` `[` `"RS"` `, ` `"SR"` `, ` `"SP"` `, ` `"PP"` `] ` `n ` `=` `len` `(arr) ` ` ` `performQueries(arr, n) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the ` `// winner of the game ` `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); ` ` ` ` ` `// Both the players chose to ` ` ` `// play the same move ` ` ` `if` `(moves[0] == moves[1]) ` ` ` `{ ` ` ` `return` `"Draw"` `; ` ` ` `} ` ` ` ` ` `// Player A wins the game ` ` ` `if` `(((data[moves[0]] | 1 << (2)) - ` ` ` `(data[moves[1]] | 0 << (2))) % 3 != 0) ` ` ` `{ ` ` ` `return` `"A"` `; ` ` ` `} ` ` ` ` ` `return` `"B"` `; ` `} ` ` ` `// Function to perform the queries ` `static` `void` `performQueries(String []arr, ` `int` `n) ` `{ ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `Console.Write(winner(arr[i]) + ` `"\n"` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `String []arr = { ` `"RS"` `, ` `"SR"` `, ` `"SP"` `, ` `"PP"` `}; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `performQueries(arr, n); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

A B A Draw

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Fast Exponention using Bit Manipulation
- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
- Game Theory (Normal-form Game) | Set 6 (Graphical Method [2 X N] Game)
- Game Theory (Normal-form Game) | Set 7 (Graphical Method [M X 2] Game)
- Bit manipulation | Swap Endianness of a number
- Maximize the Expression | Bit Manipulation
- Maximize the expression (A AND X) * (B AND X) | Bit Manipulation
- Find the winner in nim-game
- Find the winner of the Game to Win by erasing any two consecutive similar alphabets
- Find the winner of the game with N piles of boxes
- Predict the winner of the game | Sprague-Grundy
- Predict the winner in Coin Game
- Predict the winner of the game on the basis of absolute difference of sum by selecting numbers
- Find the winner of the Game
- Determine winner of the Game by arranging balls in a row
- Find the winner of a game of removing any number of stones from the least indexed non-empty pile from given N piles
- Determine the winner of a game of deleting Characters from a String
- Predict the winner of a card game of removing K cards in each turn such that Bitwise AND of K and size of pile is 0
- Bits manipulation (Important tactics)
- Combinatorial Game Theory | Set 2 (Game of Nim)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.