Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.4 kB
2
Indexable
Never
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        char[][] board = new char[8][8];

        System.out.println("Enter the initial board configuration:");
        for (int i = 0; i < 8; i++) {
            String line = scanner.nextLine();
            for (int j = 0; j < 8; j++) {
                board[i][j] = line.charAt(j);
            }
        }

        playPawnWars(board);

        scanner.close();
    }

    public static void playPawnWars(char[][] board) {
        boolean whiteTurn = true;
        int whiteRow = -1, whiteCol = -1, blackRow = -1, blackCol = -1;

        // Finding initial positions of white and black pawns
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (board[i][j] == 'w') {
                    whiteRow = i;
                    whiteCol = j;
                } else if (board[i][j] == 'b') {
                    blackRow = i;
                    blackCol = j;
                }
            }
        }

        while (true) {
            if (whiteTurn) {
                int newRow = whiteRow - 1;
                int newLeftCol = whiteCol - 1;
                int newRightCol = whiteCol + 1;

                if (isValidMove(newRow, newLeftCol) && board[newRow][newLeftCol] == 'b') {
                    System.out.println("Game over! White capture on " + convertToChessCoord(newRow, newLeftCol) + ".");
                    break;
                } else if (isValidMove(newRow, newRightCol) && board[newRow][newRightCol] == 'b') {
                    System.out.println("Game over! White capture on " + convertToChessCoord(newRow, newRightCol) + ".");
                    break;
                } else if (isValidMove(newRow, whiteCol) && board[newRow][whiteCol] == '-') {
                    board[newRow][whiteCol] = 'w';
                    board[whiteRow][whiteCol] = '-';
                    whiteRow = newRow;
                    if (whiteRow == 7) {
                        System.out.println("Game over! White pawn is promoted to a queen at " + convertToChessCoord(newRow, whiteCol) + ".");
                        break;
                    }
                }
            } else {
                int newRow = blackRow + 1;
                int newCol = blackCol;

                if (isValidMove(newRow, newCol) && board[newRow][newCol] == '-') {
                    board[newRow][newCol] = 'b';
                    board[blackRow][blackCol] = '-';
                    blackRow = newRow;
                    blackCol = newCol;
                    if (blackRow == 0) {
                        System.out.println("Game over! Black pawn is promoted to a queen at " + convertToChessCoord(newRow, newCol) + ".");
                        break;
                    }
                }
            }

            whiteTurn = !whiteTurn;
        }
    }

    public static boolean isValidMove(int row, int col) {
        return row >= 0 && row < 8 && col >= 0 && col < 8;
    }

    public static String convertToChessCoord(int row, int col) {
        char file = (char) ('a' + col);
        char rank = (char) ('8' - row);
        return String.valueOf(file) + String.valueOf(rank);
    }
}
Leave a Comment