Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.58 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 ChessValidator
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string[][] board = new string[8][];
  14.             for (int row = 0; row < 8; row++)
  15.             {
  16.                 var input = Console.ReadLine().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  17.                 board[row] = input;
  18.             }
  19.  
  20.             var command = Console.ReadLine();
  21.             while (command.ToLower() != "end")
  22.             {
  23.                 StringBuilder inputCommand = new StringBuilder(command);
  24.                 var piece = inputCommand[0].ToString();
  25.                 var positions = command.Split(new char[] { '-' });
  26.  
  27.                 var firstPositionRow = int.Parse(positions[0][1].ToString()); //2
  28.                 var firstPositionCol = int.Parse(positions[0][2].ToString()); //1
  29.  
  30.                 var secondPositionRow = int.Parse(positions[1][0].ToString()); //2
  31.                 var secondPositionCol = int.Parse(positions[1][1].ToString()); //2
  32.  
  33.                 if (board[firstPositionRow][firstPositionCol] == "x" || board[firstPositionRow][firstPositionCol] != piece)
  34.                 {
  35.                     Console.WriteLine("There is no such a piece!");
  36.                 }
  37.                 else if (board[firstPositionRow][firstPositionCol] == piece)
  38.                 {
  39.                     if (piece == "R")
  40.                     {
  41.                         if (firstPositionRow == secondPositionRow && firstPositionCol > secondPositionCol ||
  42.                             firstPositionRow == secondPositionRow && firstPositionCol < secondPositionCol ||
  43.                             firstPositionRow > secondPositionRow && firstPositionCol == secondPositionCol ||
  44.                             firstPositionRow < secondPositionRow && firstPositionCol == secondPositionCol)
  45.                         {
  46.                             if (!isOutOfBoard(secondPositionRow, secondPositionCol))
  47.                             {
  48.                                 board[firstPositionRow][firstPositionCol] = "x";
  49.                                 board[secondPositionRow][secondPositionCol] = piece;
  50.                             }
  51.                             else
  52.                             {
  53.                                 Console.WriteLine("Move go out of board!");
  54.                             }
  55.                         }
  56.                         else
  57.                         {
  58.                             Console.WriteLine("Invalid move!");
  59.                         }
  60.                     }
  61.                     else if (piece == "K")
  62.                     {
  63.                         if (firstPositionRow == secondPositionRow && firstPositionCol + 1 == secondPositionCol ||
  64.                             firstPositionRow == secondPositionRow && firstPositionCol - 1 == secondPositionCol ||
  65.                             firstPositionRow + 1 == secondPositionRow && firstPositionCol == secondPositionCol ||
  66.                             firstPositionRow - 1 == secondPositionRow && firstPositionCol == secondPositionCol ||
  67.                             firstPositionRow - 1 == secondPositionRow && firstPositionCol - 1 == secondPositionCol ||
  68.                             firstPositionRow - 1 == secondPositionRow && firstPositionCol + 1 == secondPositionCol ||
  69.                             firstPositionRow + 1 == secondPositionRow && firstPositionCol - 1 == secondPositionCol ||
  70.                             firstPositionRow + 1 == secondPositionRow && firstPositionCol + 1 == secondPositionCol)
  71.                         {
  72.                             if (!isOutOfBoard(secondPositionRow, secondPositionCol))
  73.                             {
  74.                                 board[firstPositionRow][firstPositionCol] = "x";
  75.                                 board[secondPositionRow][secondPositionCol] = piece;
  76.                             }
  77.                             else
  78.                             {
  79.                                 Console.WriteLine("Move go out of board!");
  80.                             }
  81.                         }
  82.                         else
  83.                         {
  84.                             Console.WriteLine("Invalid move!");
  85.                         }
  86.                     }
  87.                     else if (piece == "P")
  88.                     {
  89.                         if (firstPositionRow - 1 == secondPositionRow && firstPositionCol == secondPositionCol)
  90.                         {
  91.                             if (!isOutOfBoard(secondPositionRow, secondPositionCol))
  92.                             {
  93.                                 board[firstPositionRow][firstPositionCol] = "x";
  94.                                 board[secondPositionRow][secondPositionCol] = piece;
  95.                             }
  96.                             else
  97.                             {
  98.                                 Console.WriteLine("Move go out of board!");
  99.                             }
  100.                         }
  101.                         else
  102.                         {
  103.                             Console.WriteLine("Invalid move!");
  104.                         }
  105.                     }
  106.                     else if (piece == "B")
  107.                     {
  108.                         if (Math.Abs(firstPositionRow - secondPositionRow) == Math.Abs(firstPositionCol - secondPositionCol))
  109.                         {
  110.                             if (!isOutOfBoard(secondPositionRow, secondPositionCol))
  111.                             {
  112.                                 board[firstPositionRow][firstPositionCol] = "x";
  113.                                 board[secondPositionRow][secondPositionCol] = piece;
  114.                             }
  115.                             else
  116.                             {
  117.                                 Console.WriteLine("Move go out of board!");
  118.                             }
  119.                         }
  120.                         else
  121.                         {
  122.                             Console.WriteLine("Invalid move!");
  123.                         }
  124.                     }
  125.                     else if (piece == "Q")
  126.                     {
  127.                         if (Math.Abs(firstPositionRow - secondPositionRow) == Math.Abs(firstPositionCol - secondPositionCol) ||
  128.                             firstPositionRow == secondPositionRow && firstPositionCol > secondPositionCol ||
  129.                             firstPositionRow == secondPositionRow && firstPositionCol < secondPositionCol ||
  130.                             firstPositionRow > secondPositionRow && firstPositionCol == secondPositionCol ||
  131.                             firstPositionRow < secondPositionRow && firstPositionCol == secondPositionCol)
  132.                         {
  133.                             if (!isOutOfBoard(secondPositionRow, secondPositionCol))
  134.                             {
  135.                                 board[firstPositionRow][firstPositionCol] = "x";
  136.                                 board[secondPositionRow][secondPositionCol] = piece;
  137.                             }
  138.                             else
  139.                             {
  140.                                 Console.WriteLine("Move go out of board!");
  141.                             }
  142.                         }
  143.                         else
  144.                         {
  145.                             Console.WriteLine("Invalid move!");
  146.                         }
  147.                     }
  148.                     else if(piece == "N")
  149.                     {
  150.                         var combinationX =
  151.                         if(firstPositionRow - 2 == secondPositionRow && firstPositionCol - 1 == secondPositionCol ||
  152.                             firstPositionRow - 2 == secondPositionRow && firstPositionCol + 1 == secondPositionCol ||
  153.                             firstPositionRow - 1 == secondPositionRow && firstPositionCol - 2 == secondPositionCol ||
  154.                             firstPositionRow - 1 == secondPositionRow && firstPositionCol + 2 == secondPositionCol ||
  155.                             firstPositionRow + 2 == secondPositionRow && firstPositionCol - 1 == secondPositionCol ||
  156.                             firstPositionRow + 2 == secondPositionRow && firstPositionCol + 1 == secondPositionCol ||
  157.                             firstPositionRow + 1 == secondPositionRow && firstPositionCol - 2 == secondPositionCol ||
  158.                             firstPositionRow + 1 == secondPositionRow && firstPositionCol + 2 == secondPositionCol)
  159.                         {
  160.                             if(!isOutOfBoard(secondPositionRow, secondPositionCol))
  161.                             {
  162.                                 board[firstPositionRow][firstPositionCol] = "x";
  163.                                 board[secondPositionRow][secondPositionCol] = piece;
  164.                             }
  165.                             else
  166.                             {
  167.                                 Console.WriteLine("Move go out of board!");
  168.                             }
  169.                         }
  170.                         else
  171.                         {
  172.                             Console.WriteLine("Invalid move!");
  173.                         }
  174.                     }
  175.                 }
  176.                 command = Console.ReadLine();
  177.             }
  178.  
  179.         }
  180.         private static bool isOutOfBoard(int secondPositionRow, int secondPositionCol)
  181.         {
  182.             if (secondPositionRow > 7 || secondPositionCol > 7 || secondPositionRow < 0 || secondPositionCol < 0)
  183.             {
  184.                 return true;
  185.             }
  186.             else
  187.             {
  188.                 return false;
  189.             }
  190.         }
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement