Advertisement
ivanov_ivan

01. Dangerous Floor

Feb 7th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace SomeTest
  8. {
  9.     using System.Diagnostics.CodeAnalysis;
  10.     using System.Runtime.CompilerServices;
  11.  
  12.     class Program
  13.     {
  14.         static char[][] board = new char[8][];
  15.  
  16.         static void Main(string[] args)
  17.         {
  18.             for (int i = 0; i < board.Length; i++)
  19.             {
  20.                 board[i] = Console.ReadLine().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
  21.                     .Select(char.Parse).ToArray();
  22.             }
  23.  
  24.             string inputCommand = string.Empty;
  25.  
  26.             while ("END" != (inputCommand = Console.ReadLine()))
  27.             {
  28.                 string[] argParams = inputCommand.Split('-');
  29.                 char piece = argParams[0][0];
  30.                 int row = argParams[0][1] - '0';
  31.                 int col = argParams[0][2] - '0';
  32.  
  33.                 int targetRow = argParams[1][0] - '0';
  34.                 int targetCol = argParams[1][1] - '0';
  35.  
  36.                 if (board[row][col] != piece)
  37.                 {
  38.                     Console.WriteLine("There is no such a piece!");
  39.                     continue;
  40.                 }
  41.  
  42.                 if (!movePieces(piece, row, targetRow, targetCol, col))
  43.                 {
  44.                     Console.WriteLine("Invalid move!");
  45.                 }
  46.             }
  47.         }
  48.  
  49.         private static bool movePieces(char piece, int row, int targetRow, int targetCol, int col)
  50.         {
  51.             switch (piece)
  52.             {
  53.                 case 'K':
  54.                     bool leftRight = row == targetRow && Math.Abs(targetCol - col) == 1;
  55.                     bool upDown = Math.Abs(row - targetRow) == 1 && targetCol == col;
  56.                     bool diagonally = Math.Abs(row - targetRow) == 1 && Math.Abs(col - targetCol) == 1;
  57.  
  58.                     if (leftRight || upDown || diagonally)
  59.                     {
  60.                         MoveFigure(row, col, targetRow, targetCol, piece);
  61.                         return true;
  62.                     }
  63.  
  64.                     break;
  65.                 case 'P':
  66.                     bool straightForward = targetCol == col && row - targetRow == 1;
  67.                     if (straightForward)
  68.                     {
  69.                         MoveFigure(row, col, targetRow, targetCol, piece);
  70.                         return true;
  71.                     }
  72.  
  73.                     break;
  74.                 case 'R':
  75.                     bool upDownOrLeftRight = row == targetRow || col == targetCol;
  76.                     if (upDownOrLeftRight)
  77.                     {
  78.                         MoveFigure(row, col, targetRow, targetCol, piece);
  79.                         return true;
  80.                     }
  81.  
  82.                     break;
  83.                 case 'B':
  84.                     bool isDiagonallyMove = Math.Abs(row - targetRow) == Math.Abs(col - targetCol);
  85.                     if (isDiagonallyMove)
  86.                     {
  87.                         MoveFigure(row, col, targetRow, targetCol, piece);
  88.                         return true;
  89.                     }
  90.  
  91.                     break;
  92.                 case 'Q':
  93.                     bool queenUpDownOrLeftRight = row == targetRow || col == targetCol;
  94.                     bool queenIsDiagonallyMove = Math.Abs(row - targetRow) == Math.Abs(col - targetCol);
  95.                     if (queenIsDiagonallyMove || queenUpDownOrLeftRight)
  96.                     {
  97.                         MoveFigure(row, col, targetRow, targetCol, piece);
  98.                         return true;
  99.                     }
  100.  
  101.                     break;
  102.             }
  103.             return false;
  104.         }
  105.  
  106.         private static bool CheckForMatrixOutOfRange(int targetRow, int targetCol)
  107.         {
  108.             try
  109.             {
  110.                 char a = board[targetRow][targetCol];
  111.             }
  112.             catch (Exception e)
  113.             {
  114.                 return true;
  115.             }
  116.  
  117.             return false;
  118.         }
  119.  
  120.         private static void MoveFigure(int fromRow, int fromCol, int tRow, int tCol, char figure)
  121.         {
  122.             if (CheckForMatrixOutOfRange(tRow, tCol))
  123.             {
  124.                 Console.WriteLine("Move go out of board!");
  125.                 return;
  126.             }
  127.  
  128.             board[tRow][tCol] = figure;
  129.             board[fromRow][fromCol] = 'x';
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement