Advertisement
bullit3189

Dangerous Floor

Jun 10th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _01._Dangerous_Floor
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. char[,] matrix = new char[8, 8];
  10.  
  11. for(int i=0; i<matrix.GetLength(0); i++)
  12. {
  13. string[] row = Console.ReadLine().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  14.  
  15. for(int j=0; j<matrix.GetLength(1); j++)
  16. {
  17. matrix[i, j] = row[j][0];
  18. }
  19. }
  20.  
  21. string command;
  22.  
  23. while((command = Console.ReadLine()) != "END")
  24. {
  25. char piece = command[0];
  26. byte startRow = byte.Parse(command[1].ToString());
  27. byte startCol = byte.Parse(command[2].ToString());
  28. byte endRow = byte.Parse(command[4].ToString());
  29. byte endCol = byte.Parse(command[5].ToString());
  30.  
  31. if(matrix[startRow, startCol] != piece)
  32. {
  33. Console.WriteLine("There is no such a piece!");
  34. continue;
  35. }
  36.  
  37. if(!CanMove(piece, startRow, startCol, endRow, endCol))
  38. {
  39. Console.WriteLine("Invalid move!");
  40. continue;
  41. }
  42.  
  43. if(endRow > 7 || endCol > 7)
  44. {
  45. Console.WriteLine("Move go out of board!");
  46. continue;
  47. }
  48.  
  49. matrix[startRow, startCol] = 'x';
  50. matrix[endRow, endCol] = piece;
  51. }
  52. }
  53.  
  54. private static bool CanMove(char piece, byte startRow, byte startCol, byte endRow, byte endCol)
  55. {
  56. switch (piece)
  57. {
  58. case 'K':
  59. return Math.Max(Math.Abs(endRow - startRow), Math.Abs(endCol - startCol)) == 1;
  60. case 'B':
  61. return Math.Abs(endRow - startRow) == Math.Abs(endCol - startCol);
  62. case 'R':
  63. return startRow == endRow || startCol == endCol;
  64. case 'Q':
  65. return Math.Abs(endRow - startRow) == Math.Abs(endCol - startCol) || (startRow == endRow || startCol == endCol);
  66. case 'P':
  67. return startCol == endCol && startRow == endRow + 1;
  68. default:
  69. throw new ArgumentException();
  70. }
  71. }
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement