📜  使用位操作的“剪刀石头布”游戏中的获胜者

📅  最后修改于: 2021-05-25 10:29:52             🧑  作者: Mango

两名球员正在玩剪刀石头布的一系列游戏。总共有N个游戏演奏的由阵列ARR [] [],其中ARR [I] [0]是播放机之一,ARR [I] [1]是玩家2的在i移动的移动表示集合{‘R’,’P’,’S’}中的游戏。任务是找到每个游戏的赢家。请注意,如果两个玩家都选择相同的项目,则该游戏为平局。

例子:

方法:假设播放器1由位1表示,播放器2由0表示。此外,让Rock表示为00 (十进制为0),Paper表示为01 (十进制为1),剪刀为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的倍数,则玩家赢2次;如果差异为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


输出:
A
B
A
Draw