Juego de peón-peón de tablero de ajedrez

Hay un tablero de ajedrez de 8×8 y dos jugadores de ajedrez que tienen un solo peón cada uno. Un jugador tiene que mover su peón en cada turno, ya sea un paso hacia adelante o un paso en diagonal solo cuando este movimiento mata al otro peón. El jugador que no puede hacer ningún movimiento pierde. 
Dados los números de fila y columna de peones blancos y negros. La tarea es predecir quién ganaría suponiendo que ambas jugadas sean óptimas. Tenga en cuenta que las blancas juegan primero y un peón no puede moverse fuera del tablero de ajedrez.
 

Ejemplos: 
 

Entrada: filaW = 2, colW = 2, filaB = 3, colB = 3 
Salida: Blanco
Entrada: filaW = 2, colW = 2, filaB = 3, colB = 3 
Salida: Blanco 
 

Acercarse: 
 

  • Si es el turno del peón blanco, tenemos que verificar si el peón blanco está en la octava fila, entonces las negras ganan porque el peón blanco no tiene más movimiento. Si es el turno del peón negro, tenemos que comprobar si está en la primera fila, entonces las blancas ganan porque el peón negro no tiene más movimiento.
  • Si es el turno del peón blanco y el peón negro está adyacente en diagonal, el peón blanco matará al peón negro y el peón blanco gana; de lo contrario, el peón blanco avanzará un paso (si aún no está ocupado por el peón negro), de lo contrario, el blanco perderá.
  • Si es el turno del peón negro y el peón blanco está adyacente en diagonal, entonces el peón negro matará al peón blanco y el peón negro gana; de lo contrario, el peón negro avanzará un paso (si no está ocupado por el peón blanco), de lo contrario, las negras perderán.

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
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

<script>
 
// JavaScript implementation of the approach
 
// Function that returns true if white wins
    function whiteWins(rowW , colW , rowB , colB) {
        var white = 0, black = 0;
        var 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
     
        var rowW = 2, colW = 2, rowB = 3, colB = 3;
        if (whiteWins(rowW, colW, rowB, colB))
            document.write("White");
        else
            document.write("Black");
 
// This code contributed by aashish1995
 
</script>
Producción: 

White

 

Publicación traducida automáticamente

Artículo escrito por pp_pankaj y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *