Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.81 KB | None | 0 0
  1. namespace DangerousFloor
  2. {
  3.     using System;
  4.     using System.Linq;
  5.     using System.Collections.Generic;
  6.     public class DangerousFloor
  7.     {
  8.         static void Main()
  9.         {
  10.             char[][] matrix = new char[8][];
  11.             FillingMatrix(matrix);
  12.             while (true)
  13.             {
  14.                 string commandLine = Console.ReadLine();
  15.                 if (commandLine == "END")
  16.                 {
  17.                     break;
  18.                 }
  19.                 char[] inputData = commandLine.ToCharArray();
  20.                 char currentPiece = inputData[0];
  21.                 int startRow = (int)Char.GetNumericValue(inputData[1]);
  22.                 int startCol = (int)Char.GetNumericValue(inputData[2]);
  23.                 int targetRow = (int)Char.GetNumericValue(inputData[4]);
  24.                 int targetCol = (int)Char.GetNumericValue(inputData[5]);
  25.                 if (!PieceAtLocation(startRow, startCol, matrix, currentPiece))
  26.                 {
  27.                     PrintInvalidPiece();
  28.                 }
  29.                 else
  30.                 {
  31.                     switch (currentPiece)
  32.                     {
  33.                         case 'K':
  34.                             if (!IsInRange(targetRow,targetCol,matrix))
  35.                             {
  36.                                 PrintOutOfBounds();
  37.                             }
  38.                             else
  39.                             {
  40.                                 if (!MoveKing(startRow,startCol,targetRow,targetCol,matrix))
  41.                                 {
  42.                                     PrintInvalidMove();
  43.                                 }
  44.                                 else
  45.                                 {
  46.                                     MovePiece(currentPiece, startRow, startCol, targetRow, targetCol, matrix);
  47.                                 }
  48.                             }
  49.                             break;
  50.                         case 'R':
  51.                             if (!IsInRange(targetRow,targetCol,matrix))
  52.                             {
  53.                                 PrintOutOfBounds();
  54.                             }
  55.                             else
  56.                             {
  57.                                 if (!MoveRook(startRow,startCol,targetRow,targetCol,matrix))
  58.                                 {
  59.                                     PrintInvalidMove();
  60.                                 }
  61.                                 else
  62.                                 {
  63.                                     MovePiece(currentPiece, startRow, startCol, targetRow, targetCol, matrix);
  64.                                 }
  65.                             }
  66.                             break;
  67.                         case 'B':
  68.                             if (!IsInRange(targetRow,targetCol,matrix))
  69.                             {
  70.                                 PrintOutOfBounds();
  71.                             }
  72.                             else
  73.                             {
  74.                                 if (!MoveBishop(startRow,startCol,targetRow,targetCol,matrix))
  75.                                 {
  76.                                     PrintInvalidMove();
  77.                                 }
  78.                                 else
  79.                                 {
  80.                                     MovePiece(currentPiece, startRow, startCol, targetRow, targetCol, matrix);
  81.                                 }
  82.                             }
  83.                             break;
  84.                         case 'P':
  85.                             if (!IsInRange(targetRow,targetCol,matrix))
  86.                             {
  87.                                 PrintOutOfBounds();
  88.                             }
  89.                             else
  90.                             {
  91.                                 if (!MovePawn(startRow,startCol,targetRow,targetCol,matrix))
  92.                                 {
  93.                                     PrintInvalidMove();
  94.                                 }
  95.                                 else
  96.                                 {
  97.                                     MovePiece(currentPiece, startRow, startCol, targetRow,targetCol,matrix);
  98.                                 }
  99.                             }
  100.                             break;
  101.                         case 'Q':
  102.                             if (!IsInRange(targetRow,targetCol,matrix))
  103.                             {
  104.                                 PrintOutOfBounds();
  105.                             }
  106.                             else
  107.                             {
  108.                                 if (!MoveQueen(startRow,startCol,targetRow,targetCol,matrix))
  109.                                 {
  110.                                     PrintInvalidMove();
  111.                                 }
  112.                                 else
  113.                                 {
  114.                                     MovePiece(currentPiece, startRow, startCol, targetRow, targetCol, matrix);
  115.                                 }
  116.                             }
  117.                             break;
  118.                         default:
  119.                             break;
  120.                     }
  121.                 }
  122.  
  123.  
  124.             }
  125.         }
  126.  
  127.         private static bool MoveQueen(int startRow,int startCol,int targetRow,int targetCol,char[][] matrix)
  128.         {
  129.             int rowDifference = Math.Abs(targetRow - startRow);
  130.             int colDifference = Math.Abs(targetCol - startCol);
  131.             if (rowDifference == colDifference || startRow == targetRow || startCol == targetCol)
  132.             {
  133.                 return true;
  134.             }
  135.             return false;
  136.         }
  137.  
  138.         private static bool MovePawn(int startRow, int startCol, int targetRow, int targetCol, char[][] matrix)
  139.         {
  140.             int colDifference = Math.Abs(targetCol - startCol);
  141.             if (targetRow != startRow + 1 && colDifference < 2)
  142.             {
  143.                 return true;
  144.             }
  145.             return false;
  146.         }
  147.  
  148.         private static bool MoveBishop(int startRow, int startCol, int targetRow, int targetCol, char[][] matrix)
  149.         {
  150.             int rowDifference = Math.Abs(targetRow - startRow);
  151.             int colDifference = Math.Abs(targetCol - startCol);
  152.             if (rowDifference == colDifference)
  153.             {
  154.                 return true;
  155.             }
  156.             return false;
  157.         }
  158.  
  159.         private static bool MoveRook(int startRow, int startCol, int targetRow, int targetCol, char[][] matrix)
  160.         {
  161.             if (targetRow == startRow || targetCol == startCol)
  162.             {
  163.                 return true;
  164.             }
  165.             return false;
  166.         }
  167.  
  168.         private static bool MoveKing(int startRow, int startCol, int targetRow, int targetCol, char[][] matrix)
  169.         {
  170.  
  171.             if (IsInRange(targetRow,targetCol,matrix))
  172.             {
  173.                 int rowDifference = Math.Abs(targetRow - startRow);
  174.                 int colDifference = Math.Abs(targetCol - startCol);
  175.                 if (rowDifference <= 1 && colDifference <= 1)
  176.                 {
  177.                     return true;
  178.                 }
  179.             }
  180.             else
  181.             {
  182.                 PrintOutOfBounds();
  183.                 return false;
  184.             }
  185.            
  186.             return false;
  187.         }
  188.         static void MovePiece(char currentPiece, int startRow,int startCol, int targetRow,int targetCol,char[][] matrix)
  189.         {
  190.             matrix[startRow][startCol] = 'x';
  191.             matrix[targetRow][targetCol] = currentPiece;
  192.         }
  193.         static void PrintOutOfBounds()
  194.         {
  195.             Console.WriteLine($"Move go out of board!");
  196.         }
  197.         static void PrintInvalidMove()
  198.         {
  199.             Console.WriteLine($"Invalid move!");
  200.         }
  201.  
  202.         static void PrintInvalidPiece()
  203.         {
  204.             Console.WriteLine($"There is no such a piece!");
  205.         }
  206.         static bool PieceAtLocation(int startRow,int startCol,char[][] matrix,char currentPiece)
  207.         {
  208.             return matrix[startRow][startCol] == currentPiece;
  209.         }
  210.  
  211.         private static void FillingMatrix(char[][] matrix)
  212.         {
  213.             for (int row = 0; row < matrix.Length; row++)
  214.             {
  215.                 char[] inputData = Console.ReadLine()
  216.                     .Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)
  217.                     .Select(char.Parse)
  218.                     .ToArray();
  219.                 matrix[row] = new char[inputData.Length];
  220.                 for (int col = 0; col < matrix[row].Length; col++)
  221.                 {
  222.                     matrix[row][col] = inputData[col];
  223.                 }
  224.             }
  225.         }
  226.         static bool IsInRange(int row,int col,char[][] matrix)
  227.         {
  228.             return row >= 0 && row < matrix.Length && col >= 0 && col < matrix[row].Length;
  229.         }
  230.     }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement