MeGaDeTH_91

Dangerous Floor

Feb 5th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _01._Dangerous_Floor
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. string[,] chessBoard = new string[8, 8];
  11.  
  12. FillChessBoard(ref chessBoard);
  13.  
  14. string command;
  15. while ((command = Console.ReadLine()) != "END")
  16. {
  17. string currFigure = command[0].ToString();
  18. int startRow = int.Parse(command.Substring(1, 1));
  19. int startCol = int.Parse(command.Substring(2, 1));
  20.  
  21. int destinationRow = int.Parse(command.Substring(4, 1));
  22. int destinationCol = int.Parse(command.Substring(5, 1));
  23.  
  24.  
  25. if(!IsPieceValid(chessBoard, startRow, startCol, currFigure))
  26. {
  27. Console.WriteLine($"There is no such a piece!");
  28. }
  29. else if(!IsMoveValid(chessBoard, startRow, startCol, destinationRow, destinationCol, currFigure))
  30. {
  31. Console.WriteLine("Invalid move!");
  32. }
  33. else if(!IsCellValid(chessBoard, destinationRow, destinationCol))
  34. {
  35. Console.WriteLine($"Move go out of board!");
  36. }
  37. else
  38. {
  39. chessBoard[destinationRow, destinationCol] = currFigure;
  40. chessBoard[startRow, startCol] = "x";
  41. }
  42. }
  43. }
  44.  
  45. private static bool IsPieceValid(string[,] chessBoard, int checkRow, int checkCol, string figureType)
  46. {
  47. if(chessBoard[checkRow, checkCol] == figureType)
  48. {
  49. return true;
  50. }
  51. return false;
  52. }
  53.  
  54. private static bool IsMoveValid(string[,] chessBoard, int startRow, int startCol, int destinationRow, int destinationCol, string figureType)
  55. {
  56. if(figureType == "K")
  57. {
  58. return Math.Abs(startRow - destinationRow) <= 1 && Math.Abs(startCol - destinationCol) <= 1;
  59. }
  60. else if (figureType == "R")
  61. {
  62. return IsValidRookMove(startRow, startCol, destinationRow, destinationCol);
  63. }
  64. else if (figureType == "B")
  65. {
  66. return IsValidBishopMove(startRow, startCol, destinationRow, destinationCol);
  67. }
  68. else if (figureType == "Q")
  69. {
  70. return IsValidRookMove(startRow, startCol, destinationRow, destinationCol) || IsValidBishopMove(startRow, startCol, destinationRow, destinationCol);
  71. }
  72. else if (figureType == "P")
  73. {
  74. return Math.Abs(startRow - destinationRow) <= 2;
  75. }
  76. return true;
  77. }
  78.  
  79. private static bool IsValidBishopMove(int startRow, int startCol, int destinationRow, int destinationCol)
  80. {
  81. bool rightUp = Math.Abs(startRow - destinationRow) - Math.Abs(startCol + destinationCol) == 0;
  82. bool rightDown = Math.Abs(startRow + destinationRow) - Math.Abs(startCol + destinationCol) == 0;
  83. bool leftUp = Math.Abs(startRow - destinationRow) - Math.Abs(startCol - destinationCol) == 0;
  84. bool leftDown = Math.Abs(startRow + destinationRow) - Math.Abs(startCol - destinationCol) == 0;
  85.  
  86. if(rightUp || rightDown || leftUp || leftDown)
  87. {
  88. return true;
  89. }
  90. else
  91. {
  92. return false;
  93. }
  94.  
  95. }
  96.  
  97. private static bool IsValidRookMove(int startRow, int startCol, int destinationRow, int destinationCol)
  98. {
  99. return startRow == destinationRow || startCol == destinationCol;
  100. }
  101.  
  102. private static bool IsCellValid(string[,] chessBoard, int row, int col)
  103. {
  104. return row < 8 && row >= 0 && col < 8 && col >= 0;
  105. }
  106.  
  107. private static void FillChessBoard(ref string[,] chessBoard)
  108. {
  109. for (int row = 0; row < 8; row++)
  110. {
  111. string[] currRowInput = Console.ReadLine()
  112. .Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries)
  113. .ToArray();
  114.  
  115. for (int col = 0; col < 8; col++)
  116. {
  117. chessBoard[row, col] = currRowInput[col];
  118. }
  119. }
  120. }
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment