social1986

Untitled

Feb 6th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.16 KB | None | 0 0
  1. namespace _01._Dangerous_Floor
  2. {
  3.     using System;
  4.        
  5.     public class StartUp
  6.     {
  7.         public static void Main()
  8.         {
  9.             var board = new string[8, 8];
  10.  
  11.             for (int row = 0; row < board.GetLength(0); row++)
  12.             {
  13.                 var currentRow = Console.ReadLine().Split(',');
  14.  
  15.                 for (int col = 0; col < board.GetLength(1); col++)
  16.                 {
  17.                     board[row,col] = currentRow[col].ToString();
  18.                 }
  19.             }
  20.  
  21.             string command;
  22.             while ((command = Console.ReadLine())!= "END")
  23.             {
  24.                 var figure = command[0].ToString();
  25.                 var currentRow = int.Parse(command[1].ToString());
  26.                 var currentCol = int.Parse(command[2].ToString());
  27.                 if (board[currentRow,currentCol] != figure)
  28.                 {
  29.                     Console.WriteLine("There is no such a piece!");
  30.                 }
  31.                 else
  32.                 {
  33.                     var rowToBeMoved = int.Parse(command[4].ToString());
  34.                     var colToBeMoved = int.Parse(command[5].ToString());
  35.  
  36.                     if (!IsTheMoveInsideTheBoard(rowToBeMoved, colToBeMoved, board))
  37.                     {
  38.                         Console.WriteLine("Move go out of board!");
  39.                     }
  40.                     else
  41.                     {
  42.                         if (IsMoveCorrect(figure,currentRow, currentCol, rowToBeMoved,colToBeMoved))
  43.                         {
  44.                             board[rowToBeMoved, colToBeMoved] = figure;
  45.                             board[currentRow, currentCol] = "x";
  46.                             continue;
  47.                         }
  48.                         Console.WriteLine("Invalid move!");
  49.                     }
  50.                 }
  51.             }
  52.         }
  53.  
  54.         public static bool IsTheMoveInsideTheBoard(int rowToBeMooved, int colToBeMoved, string[,] board)
  55.         {
  56.             return 0 <= rowToBeMooved && rowToBeMooved < board.GetLength(0) && 0 <= colToBeMoved && colToBeMoved < board.GetLength(1);
  57.         }
  58.  
  59.         public static bool IsMoveCorrect(string figure, int currentRow, int currentCol, int rowToBeMoved, int colToBeMoved)
  60.         {
  61.             switch (figure)
  62.             {
  63.                 case "K":
  64.                     return currentRow == rowToBeMoved && Math.Abs(currentCol - colToBeMoved) == 1 || currentCol == colToBeMoved && Math.Abs(currentRow - rowToBeMoved) == 1 || Math.Abs(currentRow - rowToBeMoved) == 1 && Math.Abs(currentCol - colToBeMoved) == 1;
  65.                 case "R":
  66.                     return currentRow == rowToBeMoved || currentCol == colToBeMoved;
  67.                 case "B":
  68.                     return Math.Abs(currentRow - rowToBeMoved) == Math.Abs(currentCol - colToBeMoved);
  69.                 case "Q":
  70.                     return Math.Abs(currentRow - rowToBeMoved) == Math.Abs(currentCol - colToBeMoved) || currentRow == rowToBeMoved || currentCol == colToBeMoved;
  71.                 case "P":
  72.                     return rowToBeMoved == currentRow - 1 && currentCol == colToBeMoved;
  73.             }
  74.             return false;
  75.         }
  76.     }
  77. }
Add Comment
Please, Sign In to add comment