📜  Chessboard Pawn-Pawn 游戏

📅  最后修改于: 2021-09-24 03:19:21             🧑  作者: Mango

有一个8*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 */


Javascript


输出:
White