Aliendreamer

dangerious floor exam preparation 75/100

Feb 6th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.25 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4.  
  5.     class Program
  6.     {
  7.         static void Main()
  8.         {
  9.             string[,]board=new string[8,8];
  10.  
  11.             for (int i = 0; i < board.GetLength(0); i++)
  12.             {
  13.                 string[] figures = Console.ReadLine().Split(new [] {','}, StringSplitOptions.RemoveEmptyEntries).ToArray();
  14.  
  15.                 for (int j = 0; j < board.GetLength(1); j++)
  16.                 {
  17.                     board[i, j] = figures[j];
  18.  
  19.                 }
  20.  
  21.             }
  22.             // matricata e zaredena
  23.  
  24.             string input = Console.ReadLine();
  25.            
  26.            
  27.  
  28.             while (input != "END")
  29.             {
  30.                
  31.                 char[] moves = input.ToCharArray();
  32.                 char figure = moves[0];
  33.                 int row = int.Parse(moves[1].ToString());
  34.                 int col = int.Parse(moves[2].ToString());
  35.                 int nrow =int.Parse(moves[4].ToString());
  36.                 int ncol =int.Parse(moves[5].ToString());
  37.  
  38.                 if (board[row, col] != figure.ToString())
  39.                 {
  40.                     Console.WriteLine("There is no such a piece!");
  41.                 }
  42.                 else if (!Isin(board,nrow,ncol))
  43.                 {
  44.                     Console.WriteLine("Move go out of board!");
  45.                 }
  46.                 else
  47.                 {
  48.                     Moves(board,row,col,nrow,ncol,figure);
  49.                 }
  50.  
  51.               input = Console.ReadLine();
  52.             }
  53.    
  54.  
  55.         }
  56.  
  57.         public static void Moves(string[,] board, int row, int col, int nrow, int ncol,char figure)
  58.         {
  59.             switch (figure)
  60.             {
  61.             case 'K':
  62.                 if (Math.Abs(row - nrow) != 1 && Math.Abs(col - ncol)!=1)
  63.                 {
  64.                   Console.WriteLine("Invalid move!");
  65.                     break;
  66.                 }
  67.                 board[nrow, ncol] = 'K'.ToString();
  68.                 board[row, col] = "x";  
  69.                 break;
  70.             case 'P':
  71.                 if (row - nrow !=1)
  72.                 {
  73.                     Console.WriteLine("Invalid move!");
  74.                     break;
  75.                 }
  76.                 board[nrow, ncol] = 'P'.ToString();
  77.                 board[row, col] = "x";
  78.                 break;
  79.             case 'Q':
  80.                 board[nrow, ncol] = 'Q'.ToString();
  81.                 board[row, col] = "x";
  82.                 break;
  83.              case 'R':
  84.                  if (nrow!=row && ncol!=col)
  85.                  {
  86.                      Console.WriteLine("Invalid move!");
  87.                      break;
  88.                  }
  89.                 board[nrow, ncol] = 'R'.ToString();
  90.                 board[row, col] = "x";
  91.                 break;
  92.             case 'B':
  93.                 if (nrow == row || ncol == col )
  94.                 {
  95.                     Console.WriteLine("Invalid move!");
  96.                     break;
  97.                 }
  98.                 board[nrow, ncol] = 'B'.ToString();
  99.                 board[row, col] = "x";
  100.                 break;
  101.  
  102.  
  103.         }
  104.  
  105.  
  106.  
  107.     }
  108.        
  109.         public static bool Isin(string[,] board,int r, int c)
  110.         {
  111.         return r >= 0 && r < board.GetLength(0) && c >= 0 && c < board.GetLength(1);
  112.     }
  113.  
  114.     }
Advertisement
Add Comment
Please, Sign In to add comment