MiroslavKisov

KnightGame

Feb 9th, 2018
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace KnightGame
  6. {
  7.     public class StartUp
  8.     {
  9.         public static void Main()
  10.         {
  11.             int boardSize = int.Parse(Console.ReadLine());
  12.             char[,] board = new char[boardSize, boardSize];
  13.             var indexes = new Dictionary<List<int>, int>();
  14.             int removeCounter = 0;
  15.             for (int i = 0; i < board.GetLength(0); i++)
  16.             {
  17.                 char[] rows = Console.ReadLine().ToCharArray();
  18.                 for (int j = 0; j < board.GetLength(1); j++)
  19.                 {
  20.                     board[i, j] = rows[j];
  21.                 }
  22.             }
  23.             while (true)
  24.             {
  25.                 SetIndexes(indexes, board);
  26.                 if (indexes.Values.Any(x => x != 0))
  27.                 {
  28.                     RemoveKnight(indexes, board);
  29.                     removeCounter++;
  30.                     indexes.Clear();
  31.                 }
  32.                 else
  33.                 {
  34.                     break;
  35.                 }
  36.             }
  37.             Console.WriteLine(removeCounter);
  38.         }
  39.  
  40.         private static void RemoveKnight(Dictionary<List<int>, int> indexes, char[,] board)
  41.         {
  42.             int max = indexes.Values.Max();
  43.             var coordinatesForRemoval = indexes.FirstOrDefault(x => x.Value == max).Key;
  44.             board[coordinatesForRemoval[0], coordinatesForRemoval[1]] = '0';
  45.         }
  46.  
  47.         private static void SetIndexes(Dictionary<List<int>, int> indexes, char[,] board)
  48.         {
  49.             int counter = 0;
  50.             for (int i = 0; i < board.GetLength(0); i++)
  51.             {
  52.                 for (int j = 0; j < board.GetLength(1); j++)
  53.                 {
  54.                     if (board[i, j] == 'K')
  55.                     {
  56.                         int[] coordinates = { i, j };
  57.                         counter = CountKnights(board, i, j, board.GetLength(0));
  58.                         indexes.Add(new List<int>(coordinates), counter);
  59.                     }
  60.                 }
  61.             }
  62.         }
  63.  
  64.         private static int CountKnights(char[,] board, int i, int j, int length)
  65.         {
  66.             bool isThereAKnight = false;
  67.             int counter = 0;
  68.             if (i + 2 < length && j + 1 < length)
  69.             {
  70.                 isThereAKnight = CheckForKnight(board[i + 2, j + 1]);
  71.                 counter = SetCounter(isThereAKnight, counter);
  72.             }
  73.             if (i + 2 < length && j - 1 >= 0)
  74.             {
  75.                 isThereAKnight = CheckForKnight(board[i + 2, j - 1]);
  76.                 counter = SetCounter(isThereAKnight, counter);
  77.             }
  78.             if (i - 2 >= 0 && j + 1 < length)
  79.             {
  80.                 isThereAKnight = CheckForKnight(board[i - 2, j + 1]);
  81.                 counter = SetCounter(isThereAKnight, counter);
  82.             }
  83.             if (i - 2 >= 0 && j - 1 >= 0)
  84.             {
  85.                 isThereAKnight = CheckForKnight(board[i - 2, j - 1]);
  86.                 counter = SetCounter(isThereAKnight, counter);
  87.             }
  88.             if (j + 2 < length && i + 1 < length)
  89.             {
  90.                 isThereAKnight = CheckForKnight(board[i + 1, j + 2]);
  91.                 counter = SetCounter(isThereAKnight, counter);
  92.             }
  93.             if (j + 2 < length && i - 1 >= 0)
  94.             {
  95.                 isThereAKnight = CheckForKnight(board[i - 1, j + 2]);
  96.                 counter = SetCounter(isThereAKnight, counter);
  97.             }
  98.             if (j - 2 >= 0 && i + 1 < length)
  99.             {
  100.                 isThereAKnight = CheckForKnight(board[i + 1, j - 2]);
  101.                 counter = SetCounter(isThereAKnight, counter);
  102.             }
  103.             if (j - 2 >= 0 && i - 1 >= 0)
  104.             {
  105.                 isThereAKnight = CheckForKnight(board[i - 1, j - 2]);
  106.                 counter = SetCounter(isThereAKnight, counter);
  107.             }
  108.             return counter;
  109.         }
  110.  
  111.         private static int SetCounter(bool isThereAKnight, int counter)
  112.         {
  113.             if (isThereAKnight)
  114.             {
  115.                 counter++;
  116.             }
  117.  
  118.             return counter;
  119.         }
  120.  
  121.         private static bool CheckForKnight(char element)
  122.         {
  123.             if (element == 'K')
  124.             {
  125.                 return true;
  126.             }
  127.             return false;
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment