Winner in the Rock-Paper-Scissor game using Bit manipulation

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 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.