Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ChessValidator
- {
- class Program
- {
- static void Main(string[] args)
- {
- string[][] board = new string[8][];
- for (int row = 0; row < 8; row++)
- {
- var input = Console.ReadLine().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
- board[row] = input;
- }
- var command = Console.ReadLine();
- while (command.ToLower() != "end")
- {
- StringBuilder inputCommand = new StringBuilder(command);
- var piece = inputCommand[0].ToString();
- var positions = command.Split(new char[] { '-' });
- var firstPositionRow = int.Parse(positions[0][1].ToString()); //2
- var firstPositionCol = int.Parse(positions[0][2].ToString()); //1
- var secondPositionRow = int.Parse(positions[1][0].ToString()); //2
- var secondPositionCol = int.Parse(positions[1][1].ToString()); //2
- if (board[firstPositionRow][firstPositionCol] == "x" || board[firstPositionRow][firstPositionCol] != piece)
- {
- Console.WriteLine("There is no such a piece!");
- }
- else if (board[firstPositionRow][firstPositionCol] == piece)
- {
- if (piece == "R")
- {
- if (firstPositionRow == secondPositionRow && firstPositionCol > secondPositionCol ||
- firstPositionRow == secondPositionRow && firstPositionCol < secondPositionCol ||
- firstPositionRow > secondPositionRow && firstPositionCol == secondPositionCol ||
- firstPositionRow < secondPositionRow && firstPositionCol == secondPositionCol)
- {
- if (!isOutOfBoard(secondPositionRow, secondPositionCol))
- {
- board[firstPositionRow][firstPositionCol] = "x";
- board[secondPositionRow][secondPositionCol] = piece;
- }
- else
- {
- Console.WriteLine("Move go out of board!");
- }
- }
- else
- {
- Console.WriteLine("Invalid move!");
- }
- }
- else if (piece == "K")
- {
- if (firstPositionRow == secondPositionRow && firstPositionCol + 1 == secondPositionCol ||
- firstPositionRow == secondPositionRow && firstPositionCol - 1 == secondPositionCol ||
- firstPositionRow + 1 == secondPositionRow && firstPositionCol == secondPositionCol ||
- firstPositionRow - 1 == secondPositionRow && firstPositionCol == secondPositionCol ||
- firstPositionRow - 1 == secondPositionRow && firstPositionCol - 1 == secondPositionCol ||
- firstPositionRow - 1 == secondPositionRow && firstPositionCol + 1 == secondPositionCol ||
- firstPositionRow + 1 == secondPositionRow && firstPositionCol - 1 == secondPositionCol ||
- firstPositionRow + 1 == secondPositionRow && firstPositionCol + 1 == secondPositionCol)
- {
- if (!isOutOfBoard(secondPositionRow, secondPositionCol))
- {
- board[firstPositionRow][firstPositionCol] = "x";
- board[secondPositionRow][secondPositionCol] = piece;
- }
- else
- {
- Console.WriteLine("Move go out of board!");
- }
- }
- else
- {
- Console.WriteLine("Invalid move!");
- }
- }
- else if (piece == "P")
- {
- if (firstPositionRow - 1 == secondPositionRow && firstPositionCol == secondPositionCol)
- {
- if (!isOutOfBoard(secondPositionRow, secondPositionCol))
- {
- board[firstPositionRow][firstPositionCol] = "x";
- board[secondPositionRow][secondPositionCol] = piece;
- }
- else
- {
- Console.WriteLine("Move go out of board!");
- }
- }
- else
- {
- Console.WriteLine("Invalid move!");
- }
- }
- else if (piece == "B")
- {
- if (Math.Abs(firstPositionRow - secondPositionRow) == Math.Abs(firstPositionCol - secondPositionCol))
- {
- if (!isOutOfBoard(secondPositionRow, secondPositionCol))
- {
- board[firstPositionRow][firstPositionCol] = "x";
- board[secondPositionRow][secondPositionCol] = piece;
- }
- else
- {
- Console.WriteLine("Move go out of board!");
- }
- }
- else
- {
- Console.WriteLine("Invalid move!");
- }
- }
- else if (piece == "Q")
- {
- if (Math.Abs(firstPositionRow - secondPositionRow) == Math.Abs(firstPositionCol - secondPositionCol) ||
- firstPositionRow == secondPositionRow && firstPositionCol > secondPositionCol ||
- firstPositionRow == secondPositionRow && firstPositionCol < secondPositionCol ||
- firstPositionRow > secondPositionRow && firstPositionCol == secondPositionCol ||
- firstPositionRow < secondPositionRow && firstPositionCol == secondPositionCol)
- {
- if (!isOutOfBoard(secondPositionRow, secondPositionCol))
- {
- board[firstPositionRow][firstPositionCol] = "x";
- board[secondPositionRow][secondPositionCol] = piece;
- }
- else
- {
- Console.WriteLine("Move go out of board!");
- }
- }
- else
- {
- Console.WriteLine("Invalid move!");
- }
- }
- else if(piece == "N")
- {
- var combinationX =
- if(firstPositionRow - 2 == secondPositionRow && firstPositionCol - 1 == secondPositionCol ||
- firstPositionRow - 2 == secondPositionRow && firstPositionCol + 1 == secondPositionCol ||
- firstPositionRow - 1 == secondPositionRow && firstPositionCol - 2 == secondPositionCol ||
- firstPositionRow - 1 == secondPositionRow && firstPositionCol + 2 == secondPositionCol ||
- firstPositionRow + 2 == secondPositionRow && firstPositionCol - 1 == secondPositionCol ||
- firstPositionRow + 2 == secondPositionRow && firstPositionCol + 1 == secondPositionCol ||
- firstPositionRow + 1 == secondPositionRow && firstPositionCol - 2 == secondPositionCol ||
- firstPositionRow + 1 == secondPositionRow && firstPositionCol + 2 == secondPositionCol)
- {
- if(!isOutOfBoard(secondPositionRow, secondPositionCol))
- {
- board[firstPositionRow][firstPositionCol] = "x";
- board[secondPositionRow][secondPositionCol] = piece;
- }
- else
- {
- Console.WriteLine("Move go out of board!");
- }
- }
- else
- {
- Console.WriteLine("Invalid move!");
- }
- }
- }
- command = Console.ReadLine();
- }
- }
- private static bool isOutOfBoard(int secondPositionRow, int secondPositionCol)
- {
- if (secondPositionRow > 7 || secondPositionCol > 7 || secondPositionRow < 0 || secondPositionCol < 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement