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 SomeTest
- {
- using System.Diagnostics.CodeAnalysis;
- using System.Runtime.CompilerServices;
- class Program
- {
- static char[][] board = new char[8][];
- static void Main(string[] args)
- {
- for (int i = 0; i < board.Length; i++)
- {
- board[i] = Console.ReadLine().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
- .Select(char.Parse).ToArray();
- }
- string inputCommand = string.Empty;
- while ("END" != (inputCommand = Console.ReadLine()))
- {
- string[] argParams = inputCommand.Split('-');
- char piece = argParams[0][0];
- int row = argParams[0][1] - '0';
- int col = argParams[0][2] - '0';
- int targetRow = argParams[1][0] - '0';
- int targetCol = argParams[1][1] - '0';
- if (board[row][col] != piece)
- {
- Console.WriteLine("There is no such a piece!");
- continue;
- }
- if (!movePieces(piece, row, targetRow, targetCol, col))
- {
- Console.WriteLine("Invalid move!");
- }
- }
- }
- private static bool movePieces(char piece, int row, int targetRow, int targetCol, int col)
- {
- switch (piece)
- {
- case 'K':
- bool leftRight = row == targetRow && Math.Abs(targetCol - col) == 1;
- bool upDown = Math.Abs(row - targetRow) == 1 && targetCol == col;
- bool diagonally = Math.Abs(row - targetRow) == 1 && Math.Abs(col - targetCol) == 1;
- if (leftRight || upDown || diagonally)
- {
- MoveFigure(row, col, targetRow, targetCol, piece);
- return true;
- }
- break;
- case 'P':
- bool straightForward = targetCol == col && row - targetRow == 1;
- if (straightForward)
- {
- MoveFigure(row, col, targetRow, targetCol, piece);
- return true;
- }
- break;
- case 'R':
- bool upDownOrLeftRight = row == targetRow || col == targetCol;
- if (upDownOrLeftRight)
- {
- MoveFigure(row, col, targetRow, targetCol, piece);
- return true;
- }
- break;
- case 'B':
- bool isDiagonallyMove = Math.Abs(row - targetRow) == Math.Abs(col - targetCol);
- if (isDiagonallyMove)
- {
- MoveFigure(row, col, targetRow, targetCol, piece);
- return true;
- }
- break;
- case 'Q':
- bool queenUpDownOrLeftRight = row == targetRow || col == targetCol;
- bool queenIsDiagonallyMove = Math.Abs(row - targetRow) == Math.Abs(col - targetCol);
- if (queenIsDiagonallyMove || queenUpDownOrLeftRight)
- {
- MoveFigure(row, col, targetRow, targetCol, piece);
- return true;
- }
- break;
- }
- return false;
- }
- private static bool CheckForMatrixOutOfRange(int targetRow, int targetCol)
- {
- try
- {
- char a = board[targetRow][targetCol];
- }
- catch (Exception e)
- {
- return true;
- }
- return false;
- }
- private static void MoveFigure(int fromRow, int fromCol, int tRow, int tCol, char figure)
- {
- if (CheckForMatrixOutOfRange(tRow, tCol))
- {
- Console.WriteLine("Move go out of board!");
- return;
- }
- board[tRow][tCol] = figure;
- board[fromRow][fromCol] = 'x';
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement