Advertisement
why_where_what

01.DangerousFloor

Feb 4th, 2018
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.64 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _01.DangerousFloor
  5. {
  6.     class Program
  7.     {
  8.         private static char[,] matrix = new char[8, 8];
  9.         private static char[] pieces = new char[] { 'K', 'R', 'B', 'Q', 'P' };
  10.         static void Main()
  11.         {
  12.             for (int row = 0; row < 8; row++)
  13.             {
  14.                 char[] pieces = Console.ReadLine()
  15.                     .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
  16.                     .Select(char.Parse).ToArray();
  17.                 for (int col = 0; col < pieces.Length; col++)
  18.                 {
  19.                     matrix[row, col] = pieces[col];
  20.                 }
  21.             }
  22.  
  23.             string input = Console.ReadLine();
  24.  
  25.             while (input != "END")
  26.             {
  27.                 string[] tokens = input
  28.                     .Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
  29.                 string first = tokens[0];
  30.                 string second = tokens[1];
  31.  
  32.                 char figure = first[0];
  33.                 int currentRow = (int)char.GetNumericValue(first[1]);
  34.                 int currentCol = (int)char.GetNumericValue(first[2]);
  35.  
  36.                 int finalRow = (int)char.GetNumericValue(second[0]);
  37.                 int finalCol = (int)char.GetNumericValue(second[1]);
  38.  
  39.                 IsMovePossible(figure, currentRow, currentCol, finalRow, finalCol);
  40.  
  41.                 input = Console.ReadLine();
  42.             }
  43.         }
  44.  
  45.         static void IsMovePossible(char figure, int currentRow, int currentCol, int finalRow, int finalCol)
  46.         {
  47.             bool isPieceAllowed = IsThereSuchPiece(figure, currentRow, currentCol);
  48.             if (!isPieceAllowed)
  49.             {
  50.                 Console.WriteLine($"There is no such a piece!");
  51.                 return;
  52.             }
  53.             switch (figure)
  54.             {
  55.                 case 'K':
  56.                     bool isKingsMoveValid = CheckKingsMove(currentRow, currentCol, finalRow, finalCol);
  57.                     if (!isKingsMoveValid)
  58.                     {
  59.                         PrintInvalidMove();
  60.                     }
  61.                     else
  62.                     {
  63.                         try
  64.                         {
  65.                             matrix[currentRow, currentCol] = 'x';
  66.                         }
  67.                         catch (Exception)
  68.                         {
  69.                             PrintMoveOutOfBoard();
  70.                         }
  71.                         try
  72.                         {
  73.                             matrix[finalRow, finalCol] = 'K';
  74.                         }
  75.                         catch (Exception)
  76.                         {
  77.                             PrintMoveOutOfBoard();
  78.                             matrix[currentRow, currentCol] = 'K';
  79.                         }
  80.                     }
  81.                     break;
  82.                 case 'R':
  83.                     bool isRooksMoveValid = CheckRooksMove(currentRow, currentCol, finalRow, finalCol);
  84.                     if (!isRooksMoveValid)
  85.                     {
  86.                         PrintInvalidMove();
  87.                     }
  88.                     else
  89.                     {
  90.                         try
  91.                         {
  92.                             matrix[currentRow, currentCol] = 'x';
  93.                         }
  94.                         catch (Exception)
  95.                         {
  96.                             PrintMoveOutOfBoard();
  97.                         }
  98.                         try
  99.                         {
  100.                             matrix[finalRow, finalCol] = 'R';
  101.                         }
  102.                         catch (Exception)
  103.                         {
  104.                             PrintMoveOutOfBoard();
  105.                             matrix[currentRow, currentCol] = 'R';
  106.                         }
  107.                     }
  108.                     break;
  109.                 case 'B':
  110.                     bool isBishopsMoveValid = CheckBishopsMove(currentRow, currentCol, finalRow, finalCol);
  111.  
  112.                     if (!isBishopsMoveValid)
  113.                     {
  114.                         PrintInvalidMove();
  115.                     }
  116.                     else
  117.                     {
  118.                         try
  119.                         {
  120.                             matrix[currentRow, currentCol] = 'x';
  121.                         }
  122.                         catch (Exception)
  123.                         {
  124.                             PrintMoveOutOfBoard();
  125.                         }
  126.                         try
  127.                         {
  128.                             matrix[finalRow, finalCol] = 'B';
  129.                         }
  130.                         catch (Exception)
  131.                         {
  132.                             PrintMoveOutOfBoard();
  133.                             matrix[currentRow, currentCol] = 'B';
  134.                         }
  135.                     }
  136.                     break;
  137.                 case 'Q':
  138.                     bool isQueensMoveValid = CheckQueensMove(currentRow, currentCol, finalRow, finalCol);
  139.                     if (!isQueensMoveValid)
  140.                     {
  141.                         PrintInvalidMove();
  142.                     }
  143.                     else
  144.                     {
  145.                         try
  146.                         {
  147.                             matrix[currentRow, currentCol] = 'x';
  148.                         }
  149.                         catch (Exception)
  150.                         {
  151.                             PrintMoveOutOfBoard();
  152.                         }
  153.                         try
  154.                         {
  155.                             matrix[finalRow, finalCol] = 'Q';
  156.                         }
  157.                         catch (Exception)
  158.                         {
  159.                             PrintMoveOutOfBoard();
  160.                             matrix[currentRow, currentCol] = 'Q';
  161.                         }
  162.                     }
  163.                     break;
  164.                 case 'P':
  165.                     bool isPawnsMoveValid = CheckPawnsMove(currentRow, currentCol, finalRow, finalCol);
  166.                     if (!isPawnsMoveValid)
  167.                     {
  168.                         PrintInvalidMove();
  169.                     }
  170.                     else
  171.                     {
  172.                         try
  173.                         {
  174.                             matrix[currentRow, currentCol] = 'x';
  175.                         }
  176.                         catch (Exception)
  177.                         {
  178.                             PrintMoveOutOfBoard();
  179.                         }
  180.                         try
  181.                         {
  182.                             matrix[finalRow, finalCol] = 'P';
  183.                         }
  184.                         catch (Exception)
  185.                         {
  186.                             PrintMoveOutOfBoard();
  187.                             matrix[currentRow, currentCol] = 'P';
  188.                         }
  189.                     }
  190.                     break;
  191.             }
  192.         }
  193.  
  194.         static bool CheckPawnsMove(int currentRow, int currentCol, int finalRow, int finalCol)
  195.         {
  196.             int rowDiff = currentRow - finalRow;
  197.             if (rowDiff == 1 && currentCol == finalCol)
  198.             {
  199.                 return true;
  200.             }
  201.             return false;
  202.         }
  203.  
  204.         static bool CheckQueensMove(int currentRow, int currentCol, int finalRow, int finalCol)
  205.         {
  206.             if (CheckRooksMove(currentRow, currentCol, finalRow, finalCol) || CheckBishopsMove(currentRow, currentCol, finalRow, finalCol))
  207.             {
  208.                 return true;
  209.             }
  210.             return false;
  211.         }
  212.  
  213.         static bool CheckBishopsMove(int currentRow, int currentCol, int finalRow, int finalCol)
  214.         {
  215.             int leftDiff = Math.Abs(currentRow - finalRow);
  216.             int rightDiff = Math.Abs(currentCol - finalCol);
  217.  
  218.             bool leftUpRow = finalRow < currentRow;
  219.             bool leftUpCol = finalCol < currentCol;
  220.  
  221.             bool rightUpRow = finalRow < currentRow;
  222.             bool rightUpCol = finalCol > currentCol;
  223.  
  224.             bool rightDownRow = finalRow > currentRow;
  225.             bool rightDownCol = finalCol > currentCol;
  226.  
  227.             bool leftDownRow = finalRow > currentRow;
  228.             bool leftDownCol = finalCol < currentCol;
  229.  
  230.             if (leftDiff == rightDiff)
  231.             {
  232.                 if ((leftUpRow && leftUpCol) || (rightUpRow && rightUpCol) || (rightDownRow && rightDownCol) || (leftDownRow && leftDownCol))
  233.                 {
  234.                     return true;
  235.                 }
  236.             }
  237.             return false;
  238.         }
  239.  
  240.         static bool CheckRooksMove(int currentRow, int currentCol, int finalRow, int finalCol)
  241.         {
  242.             if ((currentRow == finalRow && currentCol != finalCol) || (currentRow != finalRow && currentCol == finalCol))
  243.             {
  244.                 return true;
  245.             }
  246.             return false;
  247.         }
  248.  
  249.         static bool CheckKingsMove(int currentRow, int currentCol, int finalRow, int finalCol)
  250.         {
  251.             int rowDiff = Math.Abs(currentRow - finalRow);
  252.             int colDiff = Math.Abs(currentCol - finalCol);
  253.             if (rowDiff > 1 || colDiff > 1)
  254.             {
  255.                 return false;
  256.             }
  257.             return true;
  258.         }
  259.  
  260.         static void PrintMoveOutOfBoard()
  261.         {
  262.             Console.WriteLine($"Move go out of board!");
  263.         }
  264.  
  265.         static void PrintInvalidMove()
  266.         {
  267.             Console.WriteLine($"Invalid move!");
  268.         }
  269.  
  270.         static bool IsThereSuchPiece(char figure, int currentRow, int currentCol)
  271.         {
  272.             return matrix[currentRow, currentCol] == figure;
  273.         }
  274.     }
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement