Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace DangerousFloor
- {
- using System;
- using System.Linq;
- using System.Collections.Generic;
- public class DangerousFloor
- {
- static void Main()
- {
- char[][] matrix = new char[8][];
- FillingMatrix(matrix);
- while (true)
- {
- string commandLine = Console.ReadLine();
- if (commandLine == "END")
- {
- break;
- }
- char[] inputData = commandLine.ToCharArray();
- char currentPiece = inputData[0];
- int startRow = (int)Char.GetNumericValue(inputData[1]);
- int startCol = (int)Char.GetNumericValue(inputData[2]);
- int targetRow = (int)Char.GetNumericValue(inputData[4]);
- int targetCol = (int)Char.GetNumericValue(inputData[5]);
- if (!PieceAtLocation(startRow, startCol, matrix, currentPiece))
- {
- PrintInvalidPiece();
- }
- else
- {
- switch (currentPiece)
- {
- case 'K':
- if (!IsInRange(targetRow,targetCol,matrix))
- {
- PrintOutOfBounds();
- }
- else
- {
- if (!MoveKing(startRow,startCol,targetRow,targetCol,matrix))
- {
- PrintInvalidMove();
- }
- else
- {
- MovePiece(currentPiece, startRow, startCol, targetRow, targetCol, matrix);
- }
- }
- break;
- case 'R':
- if (!IsInRange(targetRow,targetCol,matrix))
- {
- PrintOutOfBounds();
- }
- else
- {
- if (!MoveRook(startRow,startCol,targetRow,targetCol,matrix))
- {
- PrintInvalidMove();
- }
- else
- {
- MovePiece(currentPiece, startRow, startCol, targetRow, targetCol, matrix);
- }
- }
- break;
- case 'B':
- if (!IsInRange(targetRow,targetCol,matrix))
- {
- PrintOutOfBounds();
- }
- else
- {
- if (!MoveBishop(startRow,startCol,targetRow,targetCol,matrix))
- {
- PrintInvalidMove();
- }
- else
- {
- MovePiece(currentPiece, startRow, startCol, targetRow, targetCol, matrix);
- }
- }
- break;
- case 'P':
- if (!IsInRange(targetRow,targetCol,matrix))
- {
- PrintOutOfBounds();
- }
- else
- {
- if (!MovePawn(startRow,startCol,targetRow,targetCol,matrix))
- {
- PrintInvalidMove();
- }
- else
- {
- MovePiece(currentPiece, startRow, startCol, targetRow,targetCol,matrix);
- }
- }
- break;
- case 'Q':
- if (!IsInRange(targetRow,targetCol,matrix))
- {
- PrintOutOfBounds();
- }
- else
- {
- if (!MoveQueen(startRow,startCol,targetRow,targetCol,matrix))
- {
- PrintInvalidMove();
- }
- else
- {
- MovePiece(currentPiece, startRow, startCol, targetRow, targetCol, matrix);
- }
- }
- break;
- default:
- break;
- }
- }
- }
- }
- private static bool MoveQueen(int startRow,int startCol,int targetRow,int targetCol,char[][] matrix)
- {
- int rowDifference = Math.Abs(targetRow - startRow);
- int colDifference = Math.Abs(targetCol - startCol);
- if (rowDifference == colDifference || startRow == targetRow || startCol == targetCol)
- {
- return true;
- }
- return false;
- }
- private static bool MovePawn(int startRow, int startCol, int targetRow, int targetCol, char[][] matrix)
- {
- int colDifference = Math.Abs(targetCol - startCol);
- if (targetRow != startRow + 1 && colDifference < 2)
- {
- return true;
- }
- return false;
- }
- private static bool MoveBishop(int startRow, int startCol, int targetRow, int targetCol, char[][] matrix)
- {
- int rowDifference = Math.Abs(targetRow - startRow);
- int colDifference = Math.Abs(targetCol - startCol);
- if (rowDifference == colDifference)
- {
- return true;
- }
- return false;
- }
- private static bool MoveRook(int startRow, int startCol, int targetRow, int targetCol, char[][] matrix)
- {
- if (targetRow == startRow || targetCol == startCol)
- {
- return true;
- }
- return false;
- }
- private static bool MoveKing(int startRow, int startCol, int targetRow, int targetCol, char[][] matrix)
- {
- if (IsInRange(targetRow,targetCol,matrix))
- {
- int rowDifference = Math.Abs(targetRow - startRow);
- int colDifference = Math.Abs(targetCol - startCol);
- if (rowDifference <= 1 && colDifference <= 1)
- {
- return true;
- }
- }
- else
- {
- PrintOutOfBounds();
- return false;
- }
- return false;
- }
- static void MovePiece(char currentPiece, int startRow,int startCol, int targetRow,int targetCol,char[][] matrix)
- {
- matrix[startRow][startCol] = 'x';
- matrix[targetRow][targetCol] = currentPiece;
- }
- static void PrintOutOfBounds()
- {
- Console.WriteLine($"Move go out of board!");
- }
- static void PrintInvalidMove()
- {
- Console.WriteLine($"Invalid move!");
- }
- static void PrintInvalidPiece()
- {
- Console.WriteLine($"There is no such a piece!");
- }
- static bool PieceAtLocation(int startRow,int startCol,char[][] matrix,char currentPiece)
- {
- return matrix[startRow][startCol] == currentPiece;
- }
- private static void FillingMatrix(char[][] matrix)
- {
- for (int row = 0; row < matrix.Length; row++)
- {
- char[] inputData = Console.ReadLine()
- .Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)
- .Select(char.Parse)
- .ToArray();
- matrix[row] = new char[inputData.Length];
- for (int col = 0; col < matrix[row].Length; col++)
- {
- matrix[row][col] = inputData[col];
- }
- }
- }
- static bool IsInRange(int row,int col,char[][] matrix)
- {
- return row >= 0 && row < matrix.Length && col >= 0 && col < matrix[row].Length;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement