Open In App

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: 



DRAW
Input: arr[] = {“SS”, “RP”, “PS”} 
Output: 
Draw 


 



 

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




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




# 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




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




<script>
// Javascript implementation of the approach
 
// Function to return the
// winner of the game
function winner(moves)
{
    let data = new Map();
    data.set('R', 0);
    data.set('P', 1);
    data.set('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.get(moves[0]) | 1 << (2)) -
         (data.get(moves[1]) | 0 << (2))) % 3 != 0)
    {
        return "A";
    }
   
    return "B";
}
 
// Function to perform the queries   
function performQueries(arr,n)
{
    for (let i = 0; i < n; i++)
        document.write(winner(arr[i]) + "<br>");
}
 
// Driver code
let arr=["RS", "SR", "SP", "PP" ];
let  n = arr.length;
performQueries(arr, n);
 
// This code is contributed by patel2127
</script>

Output: 
A
B
A
Draw

 

Time Complexity: O(n)

Auxiliary Space: O(1)


Article Tags :