Advertisement
Aguzelov

DangerousFloor

Feb 6th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. public class DangerousFloor
  7. {
  8.     private static string[][] _board;
  9.  
  10.     public static void Main()
  11.     {
  12.         InitBoard();
  13.  
  14.         string input;
  15.         while ((input = Console.ReadLine()) != "END")
  16.         {
  17.             Piece.GetInfo(input);
  18.            
  19.             if (_board[Piece.CurrentPos.Row][Piece.CurrentPos.Col] != Piece.PeiceSymbol)
  20.             {
  21.                 OutputMessage.ThereIsNoSuchAPiece();
  22.                 continue;
  23.             }
  24.  
  25.             if (!Piece.ValidMoves.Contains(Piece.NewPos))
  26.             {
  27.                 OutputMessage.InvalidMove();
  28.                 continue;
  29.             }
  30.  
  31.             if (!IsInRange(Piece.NewPos))
  32.             {
  33.                 OutputMessage.MoveGoOutOfBoard();
  34.                 continue;
  35.             }
  36.  
  37.             _board[Piece.CurrentPos.Row][Piece.CurrentPos.Col] = "x";
  38.             _board[Piece.NewPos.Row][Piece.NewPos.Col] = Piece.PeiceSymbol;
  39.         }
  40.     }
  41.  
  42.     private static bool IsInRange(Position position)
  43.     {
  44.         if ((position.Row < 0 || position.Row >= _board.Length) ||
  45.             (position.Col < 0 || position.Col >= _board[position.Row].Length))
  46.         {
  47.             return false;
  48.         }
  49.  
  50.         return true;
  51.     }
  52.    
  53.     private static void InitBoard()
  54.     {
  55.         _board = new string[8][];
  56.  
  57.         for (int row = 0; row < _board.Length; row++)
  58.         {
  59.             _board[row] = Console.ReadLine().Split(new []{','},StringSplitOptions.RemoveEmptyEntries).ToArray();
  60.         }
  61.     }
  62. }
  63.  
  64. public static class Piece
  65. {
  66.     public static string PeiceSymbol { get; set; }
  67.     public static Position CurrentPos { get; set; }
  68.     public static Position NewPos { get; set; }
  69.  
  70.     public static List<Position> ValidMoves;
  71.    
  72.     public static void GetInfo(string info)
  73.     {
  74.         ValidMoves = new List<Position>();
  75.  
  76.         string pattern = @"^([A-Z]{1})(\d{1})(\d{1})-(\d{1})(\d{1})$";
  77.         Match match = Regex.Match(info, pattern);
  78.         if (match.Success)
  79.         {
  80.             PeiceSymbol = match.Groups[1].Value;
  81.             int currentRow = int.Parse(match.Groups[2].Value);
  82.             int currentCol = int.Parse(match.Groups[3].Value);
  83.             CurrentPos = new Position(currentRow, currentCol);
  84.  
  85.             int newRow = int.Parse(match.Groups[4].Value);
  86.             int newCol = int.Parse(match.Groups[5].Value);
  87.             NewPos = new Position(newRow, newCol);
  88.         }
  89.  
  90.         SetValidMoves();
  91.     }
  92.  
  93.     private static void SetValidMoves()
  94.     {
  95.         switch (PeiceSymbol)
  96.         {
  97.             case "K":
  98.                 for (int row = CurrentPos.Row - 1; row <= CurrentPos.Row + 1; row++)
  99.                 {
  100.                     for (int col = CurrentPos.Col - 1; col <= CurrentPos.Col + 1; col++)
  101.                     {
  102.                         ValidMoves.Add(new Position(row, col));
  103.                     }
  104.                 }
  105.                 break;
  106.             case "R":
  107.                 for (int row = 0; row < 16; row++)
  108.                 {
  109.                     ValidMoves.Add(new Position(row, CurrentPos.Col));
  110.                 }
  111.                 for (int col = 0; col < 16; col++)
  112.                 {
  113.                     ValidMoves.Add(new Position(CurrentPos.Row, col));
  114.                 }
  115.                 break;
  116.             case "B":
  117.                 for (int i = 1; i < 16; i++)
  118.                 {
  119.                     ValidMoves.Add(new Position(CurrentPos.Row - i, CurrentPos.Col - i));
  120.                     ValidMoves.Add(new Position(CurrentPos.Row - i, CurrentPos.Col + i));
  121.                     ValidMoves.Add(new Position(CurrentPos.Row + i, CurrentPos.Col - i));
  122.                     ValidMoves.Add(new Position(CurrentPos.Row + i, CurrentPos.Col + i));
  123.                 }
  124.                 break;
  125.             case "Q":
  126.                 for (int i = 1; i < 16; i++)
  127.                 {
  128.                     ValidMoves.Add(new Position(CurrentPos.Row - i, CurrentPos.Col - i));
  129.                     ValidMoves.Add(new Position(CurrentPos.Row - i, CurrentPos.Col + i));
  130.                     ValidMoves.Add(new Position(CurrentPos.Row + i, CurrentPos.Col - i));
  131.                     ValidMoves.Add(new Position(CurrentPos.Row + i, CurrentPos.Col + i));
  132.                 }
  133.                 for (int row = 0; row < 16; row++)
  134.                 {
  135.                     ValidMoves.Add(new Position(row, CurrentPos.Col));
  136.                 }
  137.                 for (int col = 0; col < 16; col++)
  138.                 {
  139.                     ValidMoves.Add(new Position(CurrentPos.Row, col));
  140.                 }
  141.                 break;
  142.             case "P":
  143.                 for (int row = CurrentPos.Row; row >= 0; row--)
  144.                 {
  145.                     ValidMoves.Add(new Position(row, CurrentPos.Col));
  146.                 }
  147.                 break;
  148.         }
  149.     }
  150. }
  151.  
  152. public class Position
  153. {
  154.     public int Row { get; set; }
  155.     public int Col { get; set; }
  156.  
  157.     public Position(int row, int col)
  158.     {
  159.         Row = row;
  160.         Col = col;
  161.     }
  162.  
  163.     public override int GetHashCode()
  164.     {
  165.         return this.GetHashCode();
  166.     }
  167.  
  168.     public override bool Equals(object obj)
  169.     {
  170.         if (!(obj is Position item)) return false;
  171.  
  172.         return this.Equals(item);
  173.     }
  174.  
  175.     public bool Equals(Position other)
  176.     {
  177.         if (other == null) return false;
  178.  
  179.         return this.Row == other.Row && this.Col == other.Col;
  180.     }
  181. }
  182.  
  183. public static class OutputMessage
  184. {
  185.     public static void ThereIsNoSuchAPiece()
  186.     {
  187.         Console.WriteLine("There is no such a piece!");
  188.     }
  189.  
  190.     public static void InvalidMove()
  191.     {
  192.         Console.WriteLine("Invalid move!");
  193.     }
  194.  
  195.     public static void MoveGoOutOfBoard()
  196.     {
  197.         Console.WriteLine("Move go out of board!");
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement