Guest User

Knights Game

a guest
Oct 3rd, 2019
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.86 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace KnightGame2
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int boardSize = int.Parse(Console.ReadLine());
  11.  
  12.             string[,] board = new string[boardSize, boardSize];
  13.             int[,] danger = new int[boardSize, boardSize];
  14.  
  15.             for (int i = 0; i < boardSize; i++)
  16.             {
  17.                 string row = Console.ReadLine();
  18.  
  19.                 for (int j = 0; j < boardSize; j++)
  20.                 {
  21.                     board[i, j] = row[j].ToString();
  22.                 }
  23.             }
  24.             //====
  25.             int count = 8;
  26.             int removedKnights = 0;
  27.             int removedLowestDanger = 0;
  28.             while (count > 0)
  29.             {
  30.                 Evaluate(boardSize,board,danger);
  31.  
  32.                 for (int y = 0; y < boardSize; y++)
  33.                 {
  34.                     for (int x = 0; x < boardSize; x++)
  35.                     {
  36.                         if (danger[y, x] == count && count > 1)
  37.                         {
  38.                             danger[y, x] = 0;
  39.                             board[y, x] = "0";
  40.                             removedKnights++;
  41.                         }
  42.                         else if (danger[y, x] == count && count == 1)
  43.                         {
  44.                             danger[y, x] = 0;
  45.                             board[y, x] = "0";
  46.                             removedKnights++;
  47.                             removedLowestDanger++;
  48.                         }
  49.                     }
  50.                 }
  51.  
  52.                 //Only for displaying purposes
  53.                 //Console.WriteLine();
  54.                 //for (int y = 0; y < boardSize; y++)
  55.                 //{
  56.                 //    for (int x = 0; x < boardSize; x++)
  57.                 //    {
  58.                 //        Console.Write(danger[y, x]);
  59.                 //    }
  60.                 //    Console.WriteLine();
  61.                 //}
  62.  
  63.                 count--;
  64.             }
  65.            
  66.             Console.WriteLine(removedKnights - (removedLowestDanger / 2));
  67.  
  68.         }
  69.  
  70.         public static bool IsWithin(int row, int col, int size, string[,] array)
  71.         {
  72.             if (row >= 0 && col >= 0 && row < size && col < size)
  73.             {
  74.                 return true;
  75.             }
  76.  
  77.             return false;
  78.         }
  79.  
  80.         public static void Evaluate(int boardSize, string[,]board, int[,]danger)
  81.         {
  82.             for (int y = 0; y < boardSize; y++)
  83.             {
  84.                 for (int x = 0; x < boardSize; x++)
  85.                 {
  86.                     int dangerLevel = 0;
  87.                     if (board[y, x] == "K")
  88.                     {
  89.                         bool topTopLeft = IsWithin(x - 1, y - 2, boardSize, board) && board[y - 2, x - 1] == "K";
  90.                         bool topLeftLeft = IsWithin(x - 2, y - 1, boardSize, board) && board[y - 1, x - 2] == "K";
  91.                         bool bottomLeftLeft = IsWithin(x - 2, y + 1, boardSize, board) && board[y + 1, x - 2] == "K";
  92.                         bool bottomBottomLeft = IsWithin(x - 1, y + 2, boardSize, board) && board[y + 2, x - 1] == "K";
  93.                         bool topTopRight = IsWithin(x + 1, y - 2, boardSize, board) && board[y - 2, x + 1] == "K";
  94.                         bool topRightRight = IsWithin(x + 2, y - 1, boardSize, board) && board[y - 1, x + 2] == "K";
  95.                         bool bottomRigghtRight = IsWithin(x + 2, y + 1, boardSize, board) && board[y + 1, x + 2] == "K";
  96.                         bool bottomBottomRight = IsWithin(x + 1, y + 2, boardSize, board) && board[y + 2, x + 1] == "K";
  97.                         if (topTopLeft)
  98.                         {
  99.                             dangerLevel++;
  100.                         }
  101.                         if (topLeftLeft)
  102.                         {
  103.                             dangerLevel++;
  104.                         }
  105.                         if (bottomLeftLeft)
  106.                         {
  107.                             dangerLevel++;
  108.                         }
  109.                         if (bottomBottomLeft)
  110.                         {
  111.                             dangerLevel++;
  112.                         }
  113.                         if (topTopRight)
  114.                         {
  115.                             dangerLevel++;
  116.                         }
  117.                         if (topRightRight)
  118.                         {
  119.                             dangerLevel++;
  120.                         }
  121.                         if (bottomRigghtRight)
  122.                         {
  123.                             dangerLevel++;
  124.                         }
  125.                         if (bottomBottomRight)
  126.                         {
  127.                             dangerLevel++;
  128.                         }
  129.                         danger[y, x] = dangerLevel;
  130.                     }
  131.                 }
  132.             }
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment