📜  棋盘典当-典当游戏

📅  最后修改于: 2021-06-26 22:56:23             🧑  作者: Mango

有一个8 * 8的棋盘和两个棋手,每个棋手都有一个棋子。玩家必须在每一回合中移动其棋子,仅当此举杀死另一只棋子时,才可以向前移动或对角移动一步。无法采取任何行动的玩家输了。
给定白色和黑色典当的行数和列数。任务是在假设双方都发挥出最佳状态的情况下预测谁会获胜。请注意白棋先行,棋子不能移到棋盘外。

例子:

方法:

  • 如果是白色棋子,我们必须检查白色棋子是否在第八行,那么黑色棋将获胜,因为白色棋子没有进一步的移动。如果轮到它的黑色棋子了,那么我们必须检查它是否在第一行,然后白棋获胜,因为黑色棋子没有进一步的移动。
  • 如果轮到白色棋子,而黑色棋子对角线相邻,那么白色棋子将杀死黑色棋子,白色棋子获胜,否则白色棋子将向前移动一步(如果尚未被黑色棋子占据),否则白色棋子将输。
  • 如果轮到黑棋,而白棋对角线相邻,那么黑棋将杀死白棋,黑棋获胜,否则黑棋将向前移动一步(如果尚未被白棋占据),否则黑棋将输。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function that returns true if white wins
bool whiteWins(int rowW, int colW, int rowB, int colB)
{
    int white = 0, black = 0;
  
    while (1) {
  
        // If white can move
        if (rowW != 8) {
  
            // If white pawn can kill black pawn
            // White wins
            if (rowB == rowW + 1
                && (colB == colW - 1 || colB == colW + 1))
                return true;
  
            // Make the move forward
            else
                rowW++;
        }
  
        // White has no moves
        // White loses
        else
            return false;
  
        // If black can move
        if (rowB != 1) {
  
            // If black pawn can kill white pawn
            // White loses
            if (rowB == rowW + 1
                && (colB == colW - 1 || colB == colW + 1))
                return false;
  
            // Make the move forward
            else
                rowB--;
        }
  
        // Black has no moves
        // White wins
        else
            return true;
    }
  
    // If white has got more moves
    if (white > black)
        return true;
  
    return false;
}
  
// Driver code
int main()
{
    int rowW = 2, colW = 2, rowB = 3, colB = 3;
    if (whiteWins(rowW, colW, rowB, colB))
        cout << "White";
    else
        cout << "Black";
    return 0;
}


Java
// Java implementation of the approach
class GFG
{ 
      
// Function that returns true if white wins 
static boolean whiteWins(int rowW, int colW, 
                        int rowB, int colB) 
{ 
    int white = 0, black = 0; 
    boolean flag=true;
  
    while (flag)
    { 
  
        // If white can move 
        if (rowW != 8) 
        { 
  
            // If white pawn can kill black pawn 
            // White wins 
            if (rowB == rowW + 1
                && (colB == colW - 1 || colB == colW + 1)) 
                return true; 
  
            // Make the move forward 
            else
                rowW++; 
        } 
  
        // White has no moves 
        // White loses 
        else
            return false; 
  
        // If black can move 
        if (rowB != 1)
        { 
  
            // If black pawn can kill white pawn 
            // White loses 
            if (rowB == rowW + 1
                && (colB == colW - 1 || colB == colW + 1)) 
                return false; 
  
            // Make the move forward 
            else
                rowB--; 
        } 
  
        // Black has no moves 
        // White wins 
        else
            return true; 
    } 
  
    // If white has got more moves 
    if (white > black) 
        return true; 
  
    return false; 
} 
  
// Driver code 
public static void main(String args[])
{ 
    int rowW = 2, colW = 2, rowB = 3, colB = 3; 
    if (whiteWins(rowW, colW, rowB, colB)) 
        System.out.println("White"); 
    else
        System.out.println("Black"); 
} 
}
  
// This code is contributed by Arnab Kundu


Python3
# Print implementation of the approach
  
# Function that returns true if white wins
def whiteWins(rowW, colW, rowB, colB):
    white = 0;
    black = 0;
  
    while (1):
  
        # If white can move
        if (rowW != 8):
  
            # If white pawn can kill black pawn
            # White wins
            if (rowB == rowW + 1 and 
               (colB == colW - 1 or 
                colB == colW + 1)):
                return True;
  
            # Make the move forward
            else:
                rowW += 1;
  
        # White has no moves
        # White loses
        else:
            return False;
  
        # If black can move
        if (rowB != 1):
  
            # If black pawn can kill white pawn
            # White loses
            if (rowB == rowW + 1 and 
               (colB == colW - 1 or 
                colB == colW + 1)):
                return False;
  
            # Make the move forward
            else:
                rowB -= 1;
  
        # Black has no moves
        # White wins
        else:
            return Frue;
  
    # If white has got more moves
    if (white > black):
        return True;
  
    return False;
  
# Driver code
if __name__ == '__main__':
    rowW, colW = 2, 2;
    rowB, colB = 3, 3;
    if (whiteWins(rowW, colW, rowB, colB)):
        print("White");
    else:
        print("Black");
  
# This code is contributed by Rajput-Ji


C#
// C# implementation of the approach
using System;
public class GFG
{ 
       
// Function that returns true if white wins 
static bool whiteWins(int rowW, int colW, 
                        int rowB, int colB) 
{ 
    int white = 0, black = 0; 
    bool flag=true;
   
    while (flag)
    { 
   
        // If white can move 
        if (rowW != 8) 
        { 
   
            // If white pawn can kill black pawn 
            // White wins 
            if (rowB == rowW + 1
                && (colB == colW - 1 || colB == colW + 1)) 
                return true; 
   
            // Make the move forward 
            else
                rowW++; 
        } 
   
        // White has no moves 
        // White loses 
        else
            return false; 
   
        // If black can move 
        if (rowB != 1)
        { 
   
            // If black pawn can kill white pawn 
            // White loses 
            if (rowB == rowW + 1
                && (colB == colW - 1 || colB == colW + 1)) 
                return false; 
   
            // Make the move forward 
            else
                rowB--; 
        } 
   
        // Black has no moves 
        // White wins 
        else
            return true; 
    } 
   
    // If white has got more moves 
    if (white > black) 
        return true; 
   
    return false; 
} 
   
// Driver code 
public static void Main(String []args)
{ 
    int rowW = 2, colW = 2, rowB = 3, colB = 3; 
    if (whiteWins(rowW, colW, rowB, colB)) 
        Console.WriteLine("White"); 
    else
        Console.WriteLine("Black"); 
} 
}
/* This code contributed by PrinciRaj1992 */


输出:
White

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。