📜  使用 Bit 操作在石头剪刀布游戏中获胜

📅  最后修改于: 2021-09-24 05:04:25             🧑  作者: Mango

两名玩家正在玩一系列的石头剪刀布游戏。共有N场比赛,由数组arr[][] 表示,其中arr[i][0]是第i 个玩家的走法, arr[i][1]是第i玩家的走法来自集合{‘R’, ‘P’, ‘S’} 的游戏。任务是找到每场比赛的赢家。请注意,如果两个玩家都选择相同的项目,则游戏为平局。
例子:

方法:假设玩家一由位1表示,玩家二由0表示。此外,让 Rock 用00 (十进制 0)表示,Paper 用01 (十进制 1)表示,Scissors 用10 (十进制 2)表示。
如果玩家一选择摇滚,它将用100表示,
类似地, 101表示纸被玩家一选择。
第一位表示玩家编号和接下来的两位供他们选择。
图案:
100(十进制 4)(玩家 1,摇滚),001(十进制 1)(玩家 2,纸)-> 玩家 2 获胜(4-1 = 3)
101(十进制 5)(玩家 1,纸),010(十进制 2)(玩家 2,剪刀)-> 玩家 2 获胜(5-2 = 3)
110(十进制 6)(玩家 1,剪刀),000(十进制的 0)(玩家 2,岩石)-> 玩家 2 获胜(6-0 = 6)
101(十进制 5)(玩家 1,纸),000(十进制的 0)(玩家 2,摇滚)-> 玩家 1 获胜(5-0 = 5)
110(十进制 6)(玩家 1,剪刀),001(十进制 1)(玩家 2,纸)-> 玩家 1 获胜(6-1 = 5)
100(十进制 4)(玩家 1,岩石),010(十进制 2)(玩家 2,剪刀)-> 玩家 1 获胜(4-2 = 2)
根据该模式,如果差是 3 的倍数,则玩家两个获胜,或者如果差为 4,则游戏为平局。在其余情况下,玩家一赢得比赛。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the
// winner of the game
string winner(string moves)
{
    map 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
// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function to return the
// winner of the game
static String winner(String moves)
{
    HashMap data = new HashMap();
    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
# 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#
// 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 data = new Dictionary();
    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


Javascript


输出:
A
B
A
Draw

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程