Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace MultidimensionalArrays
- {
- class Program
- {
- static void Main(string[] args)
- {
- int mSize = int.Parse(Console.ReadLine());
- int commandsCount = int.Parse(Console.ReadLine());
- char[,] matrix = new char[mSize, mSize];
- int playerRow = 0;
- int playerCol = 0;
- bool isWinner = false;
- MatrixInitialize(mSize, matrix, ref playerRow, ref playerCol);
- while (commandsCount >= 0)
- {
- commandsCount--;
- string command = Console.ReadLine();
- if (command == "up")
- {
- if (playerRow - 1 >= 0 && matrix[playerRow - 1, playerCol] == 'F')
- {
- matrix[playerRow, playerCol] = '-';
- playerRow--;
- matrix[playerRow, playerCol] = 'f';
- isWinner = true;
- break;
- }
- else if (playerRow - 1 >= 0 && matrix[playerRow - 1, playerCol] == '-')
- {
- matrix[playerRow, playerCol] = '-';
- playerRow--;
- matrix[playerRow, playerCol] = 'f';
- }
- else if (playerRow - 1 >= 0 && matrix[playerRow + 1, playerCol] == 'B')
- {
- if (playerRow - 2 >= 0 )
- {
- matrix[playerRow, playerCol] = '-';
- playerRow--;
- playerRow--;
- matrix[playerRow, playerCol] = 'f';
- }
- else
- {
- matrix[playerRow, playerCol] = '-';
- playerRow = mSize - 1;
- matrix[playerRow, playerCol] = 'f';
- }
- }
- else if(playerRow - 1 < 0)
- {
- if (matrix[mSize-1, playerCol] == 'F')
- {
- matrix[playerRow, playerCol] = '-';
- playerRow = mSize - 1;
- matrix[playerRow, playerCol] = 'f';
- isWinner = true;
- break;
- }
- else if (matrix[mSize - 1, playerCol] == '-')
- {
- matrix[playerRow, playerCol] = '-';
- playerRow = mSize - 1;
- matrix[playerRow, playerCol] = 'f';
- }
- }
- }
- else if (command == "down")
- {
- if (playerRow + 1 < mSize && matrix[playerRow + 1, playerCol] == 'F')
- {
- matrix[playerRow, playerCol] = '-';
- playerRow++;
- matrix[playerRow, playerCol] = 'f';
- isWinner = true;
- break;
- }
- else if (playerRow + 1 < mSize && matrix[playerRow + 1, playerCol] == '-')
- {;
- matrix[playerRow, playerCol] = '-';
- playerRow++;
- matrix[playerRow, playerCol] = 'f';
- }
- else if (playerRow + 1 < mSize && matrix[playerRow + 1,playerCol] == 'B')
- {
- if (playerRow + 2 < mSize)
- {
- matrix[playerRow, playerCol] = '-';
- playerRow++;
- playerRow++;
- matrix[playerRow, playerCol] = 'f';
- }
- else
- {
- matrix[playerRow, playerCol] = '-';
- playerRow = 0;
- matrix[playerRow, playerCol] = 'f';
- }
- }
- else if (playerRow + 1 == mSize)
- {
- if (matrix[0, playerCol] == 'F')
- {
- matrix[playerRow, playerCol] = '-';
- playerRow = 0;
- matrix[playerRow, playerCol] = 'f';
- isWinner = true;
- break;
- }
- else if (matrix[0, playerCol] == '-')
- {
- matrix[playerRow, playerCol] = '-';
- playerRow = 0;
- matrix[playerRow, playerCol] = 'f';
- }
- else if (matrix[0, playerCol] == 'B')
- {
- matrix[playerRow, playerCol] = '-';
- playerRow = 1;
- matrix[playerRow, playerCol] = 'f';
- }
- }
- }
- else if (command == "left")
- {
- if (playerCol - 1 >= 0 && matrix[playerRow, playerCol - 1] == 'F')
- {
- matrix[playerRow, playerCol] = '-';
- playerCol--;
- matrix[playerRow, playerCol] = 'f';
- isWinner = true;
- break;
- }
- else if (playerCol - 1 >= 0 && matrix[playerRow,playerCol - 1] == '-')
- {
- matrix[playerRow, playerCol] = '-';
- playerCol--;
- matrix[playerRow, playerCol] = 'f';
- }
- else if (playerCol - 1 >= 0 && matrix[playerRow,playerCol - 1] == 'B')
- {
- if (playerCol - 2 >= 0)
- {
- matrix[playerRow, playerCol] = '-';
- playerCol--;
- playerCol--;
- matrix[playerRow, playerCol] = 'f';
- }
- else
- {
- matrix[playerRow, playerCol] = '-';
- playerCol = mSize - 1;
- matrix[playerRow, playerCol] = 'f';
- }
- }
- else if (playerCol - 1 < 0)
- {
- if (matrix[playerRow , mSize - 1] == 'F')
- {
- matrix[playerRow, playerCol] = '-';
- playerCol = mSize - 1;
- matrix[playerRow, playerRow] = 'f';
- isWinner = true;
- break;
- }
- else if (matrix[playerRow, mSize - 1] == '-')
- {
- matrix[playerRow, playerCol] = '-';
- playerRow = mSize - 1;
- matrix[playerRow, playerCol] = 'f';
- }
- else if (matrix[playerRow, mSize - 1] == 'B')
- {
- matrix[playerRow, playerCol] = '-';
- playerRow = mSize - 2;
- matrix[playerRow, playerCol] = 'f';
- }
- }
- }
- else if (command == "right")
- {
- if (playerCol + 1 < mSize && matrix[playerRow, playerCol + 1] == 'F')
- {
- matrix[playerRow, playerCol] = '-';
- playerCol++;
- matrix[playerRow, playerCol] = 'f';
- isWinner = true;
- break;
- }
- else if (playerCol + 1 < mSize && matrix[playerRow,playerCol + 1] == '-')
- {
- matrix[playerRow, playerCol] = '-';
- playerCol++;
- matrix[playerRow, playerCol] = 'f';
- }
- else if (playerCol + 1 < mSize && matrix[playerRow,playerCol + 1] == 'B')
- {
- if (playerCol + 2 < mSize)
- {
- matrix[playerRow, playerCol] = '-';
- playerCol++;
- playerCol++;
- matrix[playerRow, playerCol] = 'f';
- }
- else
- {
- matrix[playerRow, playerCol] = '-';
- playerCol = 0;
- matrix[playerRow, playerCol] = 'f';
- }
- }
- else if (playerCol + 1 == mSize)
- {
- if (matrix[playerRow, 0] == 'F')
- {
- matrix[playerRow, playerCol] = '-';
- playerCol = 0;
- matrix[playerRow, playerCol] = 'f';
- isWinner = true;
- break;
- }
- else if (matrix[playerRow, 0] == '-')
- {
- matrix[playerRow, playerCol] = '-';
- playerCol = 0;
- matrix[playerRow, playerCol] = 'f';
- }
- else if (matrix[playerRow, 0] == 'B')
- {
- matrix[playerRow, playerCol] = '-';
- playerCol = 1;
- matrix[playerRow, playerCol] = 'f';
- }
- }
- }
- } // while
- if (isWinner)
- {
- Console.WriteLine("Player won!");
- }
- else
- {
- Console.WriteLine("Player lost!");
- }
- for (int row = 0; row < mSize; row++)
- {
- for (int col = 0; col < mSize; col++)
- {
- Console.Write(matrix[row,col]);
- }
- Console.WriteLine();
- }
- }
- private static void MatrixInitialize(int mSize, char[,] matrix, ref int playerRow, ref int playerCol)
- {
- for (int rows = 0; rows < mSize; rows++)
- {
- var consoleInput = Console.ReadLine().ToCharArray();
- for (int cols = 0; cols < mSize; cols++)
- {
- if (consoleInput[cols] != '-')
- {
- if (consoleInput[cols] == 'f')
- {
- playerRow = rows;
- playerCol = cols;
- }
- }
- matrix[rows, cols] = consoleInput[cols];
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement