Advertisement
zdravko7

[C# Exam] Chess Queens

Aug 4th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.98 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 ChessQueens
  8. {
  9.     class ChessQueens
  10.     {
  11.         static void Main()
  12.         {
  13.             int size = int.Parse(Console.ReadLine());
  14.             int[,] matrix = new int[size, size];
  15.             int step = int.Parse(Console.ReadLine());
  16.             int resultCounter = 0;
  17.  
  18.             for (int row = 0; row < matrix.GetLength(0); row++)
  19.             {
  20.                 for (int col = 0; col < matrix.GetLength(1); col++)
  21.                 {
  22.                     //check horizontal
  23.                     //down
  24.                     try
  25.                     {
  26.                         int extension = row + 1 + step;
  27.  
  28.                         if (matrix[row, col] == matrix[extension, col])
  29.                         {
  30.                             Console.WriteLine("" + TranslateToChessRow(row) + TranslateToChessCol(col)
  31.                                 + " - " + TranslateToChessRow(extension) + TranslateToChessCol(col));
  32.                             resultCounter++;
  33.                         }  
  34.                     }
  35.                     catch (Exception) { }
  36.  
  37.                     //up
  38.                     try
  39.                     {
  40.                         int extension = row - 1 - step;
  41.  
  42.                         if (matrix[row, col] == matrix[extension, col])
  43.                         {
  44.                             Console.WriteLine("" + TranslateToChessRow(row) + TranslateToChessCol(col)
  45.                                 + " - " + TranslateToChessRow(extension) + TranslateToChessCol(col));
  46.                             resultCounter++;
  47.                         }
  48.                     }
  49.                     catch (Exception) { }
  50.  
  51.                     //left
  52.                     try
  53.                     {
  54.                         int extension = col - 1 - step;
  55.  
  56.                         if (matrix[row, col] == matrix[row, extension])
  57.                         {
  58.                             Console.WriteLine("" + TranslateToChessRow(row) + TranslateToChessCol(col)
  59.                                 + " - " + TranslateToChessRow(row) + TranslateToChessCol(extension));
  60.                             resultCounter++;
  61.                         }
  62.                     }
  63.                     catch (Exception) { }
  64.  
  65.                     //right
  66.                     try
  67.                     {
  68.                         int extension = col + 1 + step;
  69.  
  70.                         if (matrix[row, col] == matrix[row, extension])
  71.                         {
  72.                             Console.WriteLine("" + TranslateToChessRow(row) + TranslateToChessCol(col)
  73.                                 + " - " + TranslateToChessRow(row) + TranslateToChessCol(extension));
  74.                             resultCounter++;
  75.                         }
  76.                     }
  77.                     catch (Exception) { }
  78.  
  79.                     //check diagonals
  80.                     //up-left
  81.                     try
  82.                     {
  83.                         int extensionRow = row - 1 - step;
  84.                         int extensionCol = col - 1 - step;
  85.  
  86.                         if (matrix[row, col] == matrix[extensionRow, extensionCol])
  87.                         {
  88.                             Console.WriteLine("" + TranslateToChessRow(row) + TranslateToChessCol(col)
  89.                                 + " - " + TranslateToChessRow(extensionRow) + TranslateToChessCol(extensionCol));
  90.                             resultCounter++;
  91.                         }
  92.                     }
  93.                     catch (Exception) { }
  94.  
  95.                     //up-right
  96.                     try
  97.                     {
  98.                         int extensionRow = row - 1 - step;
  99.                         int extensionCol = col + 1 + step;
  100.  
  101.  
  102.                         if (matrix[row, col] == matrix[extensionRow, extensionCol])
  103.                         {
  104.                             Console.WriteLine("" + TranslateToChessRow(row) + TranslateToChessCol(col)
  105.                                 + " - " + TranslateToChessRow(extensionRow) + TranslateToChessCol(extensionCol));
  106.                             resultCounter++;
  107.                         }
  108.                     }
  109.                     catch (Exception) { }
  110.  
  111.                     //down-left
  112.                     try
  113.                     {
  114.                         int extensionRow = row + 1 + step;
  115.                         int extensionCol = col - 1 - step;
  116.  
  117.                         if (matrix[row, col] == matrix[extensionRow, extensionCol])
  118.                         {
  119.                             Console.WriteLine("" + TranslateToChessRow(row) + TranslateToChessCol(col)
  120.                                 + " - " + TranslateToChessRow(extensionRow) + TranslateToChessCol(extensionCol));
  121.                             resultCounter++;
  122.                         }
  123.  
  124.                     }
  125.                     catch (Exception) { }
  126.  
  127.                     //down-right
  128.                     try
  129.                     {
  130.                         int extensionRow = row + 1 + step;
  131.                         int extensionCol = col + 1 + step;
  132.  
  133.                         if (matrix[row, col] == matrix[extensionRow, extensionCol])
  134.                         {
  135.                             Console.WriteLine("" + TranslateToChessRow(row) + TranslateToChessCol(col)
  136.                                 + " - " + TranslateToChessRow(extensionRow) + TranslateToChessCol(extensionCol));
  137.                             resultCounter++;
  138.                         }
  139.  
  140.                     }
  141.                     catch (Exception) { }
  142.                 }
  143.             }
  144.  
  145.             if (resultCounter == 0)
  146.             {
  147.                 Console.WriteLine("No valid positions");
  148.             }
  149.         }
  150.  
  151.         public static char TranslateToChessRow(int number)
  152.         {
  153.             return (char)(number + 97);
  154.         }
  155.  
  156.         public static int TranslateToChessCol(int number)
  157.         {
  158.             return number + 1;
  159.         }
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement