Advertisement
Ligh7_of_H3av3n

02. Pawn Wars

Jun 10th, 2024
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.64 KB | None | 0 0
  1. package BonusZadachi2;
  2.  
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class PawnWars {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.  
  11.         int size = 8;
  12.         char[][] board = new char[size][size];
  13.         int whiteRow = 0, whiteCol = 0, blackRow = 0, blackCol = 0;
  14.  
  15.         // Read the board and locate the pawns
  16.         for (int r = 0; r < size; r++) {
  17.             String line = scanner.nextLine();
  18.             for (int c = 0; c < size; c++) {
  19.                 board[r][c] = line.charAt(c);
  20.                 if (board[r][c] == 'w') {
  21.                     whiteRow = r;
  22.                     whiteCol = c;
  23.                 } else if (board[r][c] == 'b') {
  24.                     blackRow = r;
  25.                     blackCol = c;
  26.                 }
  27.             }
  28.         }
  29.  
  30.         while (true) {
  31.             // White pawn move
  32.             if (0 <= whiteRow - 1 && 0 <= whiteCol - 1 && board[whiteRow - 1][whiteCol - 1] == 'b') {
  33.                 System.out.println("Game over! White capture on " + (char) ('a' + blackCol) + (8 - blackRow) + ".");
  34.                 break;
  35.             } else if (0 <= whiteRow - 1 && whiteCol + 1 < size && board[whiteRow - 1][whiteCol + 1] == 'b') {
  36.                 System.out.println("Game over! White capture on " + (char) ('a' + blackCol) + (8 - blackRow) + ".");
  37.                 break;
  38.             } else {
  39.                 board[whiteRow][whiteCol] = '-';
  40.                 whiteRow--;
  41.                 board[whiteRow][whiteCol] = 'w';
  42.                 if (whiteRow == 0) {
  43.                     System.out.println("Game over! White pawn is promoted to a queen at " + (char) ('a' + whiteCol) + "8.");
  44.                     break;
  45.                 }
  46.             }
  47.  
  48.             // Black pawn move
  49.             if (blackRow + 1 < size && 0 <= blackCol - 1 && board[blackRow + 1][blackCol - 1] == 'w') {
  50.                 System.out.println("Game over! Black capture on " + (char) ('a' + whiteCol) + (8 - whiteRow) + ".");
  51.                 break;
  52.             } else if (blackRow + 1 < size && blackCol + 1 < size && board[blackRow + 1][blackCol + 1] == 'w') {
  53.                 System.out.println("Game over! Black capture on " + (char) ('a' + whiteCol) + (8 - whiteRow) + ".");
  54.                 break;
  55.             } else {
  56.                 board[blackRow][blackCol] = '-';
  57.                 blackRow++;
  58.                 board[blackRow][blackCol] = 'b';
  59.                 if (blackRow == 7) {
  60.                     System.out.println("Game over! Black pawn is promoted to a queen at " + (char) ('a' + blackCol) + "1.");
  61.                     break;
  62.                 }
  63.             }
  64.         }
  65.     }
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement