Advertisement
Threed90

PawnWars

Oct 23rd, 2021
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Celebration
  6. {
  7.     public class Program
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             char[,] matrix = new char[8, 8];
  12.  
  13.             int[] white = new int[2];
  14.             int[] black = new int[2];
  15.  
  16.             char[] colName = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };
  17.  
  18.             for (int i = 0; i < matrix.GetLength(0); i++)
  19.             {
  20.                 char[] line = Console.ReadLine().ToCharArray();
  21.  
  22.                 for (int j = 0; j < matrix.GetLength(1); j++)
  23.                 {
  24.                     if (line[j] == 'w')
  25.                     {
  26.                         white[0] = i;
  27.                         white[1] = j;
  28.                     }
  29.  
  30.                     if (line[j] == 'b')
  31.                     {
  32.                         black[0] = i;
  33.                         black[1] = j;
  34.                     }
  35.  
  36.                     matrix[i, j] = line[j];
  37.                 }
  38.             }
  39.  
  40.             ;
  41.             while (true)
  42.             {
  43.                 if (IsValidCoordinate(white[0] - 1, white[1] - 1))
  44.                 {
  45.                     if (matrix[white[0] - 1, white[1] - 1] == 'b')
  46.                     {
  47.                         Console.WriteLine($"Game over! White capture on {colName[white[1] - 1]}{8 - (white[0] - 1)}.");
  48.                         break;
  49.                     }
  50.                 }
  51.  
  52.                 if (IsValidCoordinate(white[0] - 1, white[1] + 1))
  53.                 {
  54.                     if (matrix[white[0] - 1, white[1] + 1] == 'b')
  55.                     {
  56.                         Console.WriteLine($"Game over! White capture on {colName[white[1] + 1]}{8 - (white[0] - 1)}.");
  57.                         break;
  58.                     }
  59.                 }
  60.                 if(white[0] - 1 >= 0)
  61.                 {
  62.                     matrix[white[0], white[1]] = '-';
  63.                     matrix[white[0] - 1, white[1]] = 'w';
  64.                     white[0] -= 1;
  65.  
  66.                     if(white[0] == 0)
  67.                     {
  68.                         Console.WriteLine($"Game over! White pawn is promoted to a queen at {colName[white[1]]}{8 - white[0]}.");
  69.                         break;
  70.                     }
  71.                 }
  72.  
  73.                 if (IsValidCoordinate(black[0] + 1, black[1] - 1))
  74.                 {
  75.                     if (matrix[black[0] + 1, black[1] - 1] == 'w')
  76.                     {
  77.                         Console.WriteLine($"Game over! Black capture on {colName[black[1] - 1]}{8 - (black[0] + 1)}.");
  78.                         break;
  79.                     }
  80.                 }
  81.  
  82.                 if (IsValidCoordinate(black[0] + 1, black[1] + 1))
  83.                 {
  84.                     if (matrix[black[0] + 1, black[1] + 1] == 'w')
  85.                     {
  86.                         Console.WriteLine($"Game over! Black capture on {colName[black[1] + 1]}{8 - (black[0] + 1)}.");
  87.                         break;
  88.                     }
  89.                 }
  90.  
  91.                 if (black[0] + 1 < 8)
  92.                 {
  93.                     matrix[black[0], black[1]] = '-';
  94.                     matrix[black[0] + 1, black[1]] = 'b';
  95.                     black[0] += 1;
  96.  
  97.                     if(black[0] == 7)
  98.                     {
  99.                         Console.WriteLine($"Game over! Black pawn is promoted to a queen at {colName[black[1]]}{8 - black[0]}.");
  100.                         break;
  101.                     }
  102.                 }
  103.             }
  104.         }
  105.  
  106.         private static bool IsValidCoordinate(int row, int col)
  107.         {
  108.             return row >= 0 && col >= 0 && row < 8 && col < 8;
  109.         }
  110.     }
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement