Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace ConsoleApp2
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- int maxCommands = int.Parse(Console.ReadLine());
- char[,] playground = new char[n, n];
- int[] playerCoordinates = new int[2];
- int[] finalCoordinates = new int[2];
- int commandsCount = 0;
- string output = string.Empty;
- PlaygroundImplementation(playground, n, playerCoordinates, finalCoordinates);
- while (true)
- {
- if (commandsCount == maxCommands)
- {
- output = "Player lost!";
- break;
- }
- string command = Console.ReadLine();
- commandsCount++;
- PlayerMovement(playerCoordinates, playground, n, command);
- if (playground[finalCoordinates[0], finalCoordinates[1]] == 'f')
- {
- output = "Player won!";
- break;
- }
- }
- Console.WriteLine(output);
- PrintPlayground(playground, n);
- static void PrintPlayground(char[,] playground, int n)
- {
- for (int rows = 0; rows < n; rows++)
- {
- for (int cols = 0; cols < n; cols++)
- {
- Console.Write(playground[rows, cols]);
- }
- Console.WriteLine();
- }
- }
- static bool CellValidation(int row, int col, char[,] playground, int n)
- {
- bool validation = false;
- if (0 <= row && row < n && 0 <= col && col < n)
- {
- validation = true;
- }
- return validation;
- }
- static void PlaygroundImplementation(char[,] playground, int n, int[] playerCoordinates, int[] finalCoordinates)
- {
- for (int rows = 0; rows < n; rows++)
- {
- char[] input = Console.ReadLine().ToCharArray();
- for (int cols = 0; cols < n; cols++)
- {
- playground[rows, cols] = input[cols];
- if (input[cols] == 'f')
- {
- playerCoordinates[0] = rows;
- playerCoordinates[1] = cols;
- }
- if (input[cols] == 'F')
- {
- finalCoordinates[0] = rows;
- finalCoordinates[1] = cols;
- }
- }
- }
- }
- static void Move(char[,] playground, int[] playerCoordinates, string command, int n)
- {
- switch (command)
- {
- case "up": //rows--
- {
- if(playground[playerCoordinates[0] - 1, playerCoordinates[1]] == 'B')
- {
- playground[playerCoordinates[0], playerCoordinates[1]] = '-';
- playerCoordinates[0]--;
- PlayerMovement(playerCoordinates, playground, n, command);
- playground[playerCoordinates[0], playerCoordinates[1]] = 'f';
- }
- else
- {
- if (playground[playerCoordinates[0], playerCoordinates[1]] == 'B')
- {
- playground[playerCoordinates[0], playerCoordinates[1]] = 'B';
- }
- else
- {
- playground[playerCoordinates[0], playerCoordinates[1]] = '-';
- }
- playerCoordinates[0]--;
- playground[playerCoordinates[0], playerCoordinates[1]] = 'f';
- }
- break;
- }
- case "down": //rows++
- {
- if (playground[playerCoordinates[0] + 1, playerCoordinates[1]] == 'B')
- {
- playground[playerCoordinates[0], playerCoordinates[1]] = '-';
- playerCoordinates[0]++;
- PlayerMovement(playerCoordinates, playground, n, command);
- playground[playerCoordinates[0], playerCoordinates[1]] = 'f';
- }
- else
- {
- if (playground[playerCoordinates[0], playerCoordinates[1]] == 'B')
- {
- playground[playerCoordinates[0], playerCoordinates[1]] = 'B';
- }
- else
- {
- playground[playerCoordinates[0], playerCoordinates[1]] = '-';
- }
- playerCoordinates[0]++;
- playground[playerCoordinates[0], playerCoordinates[1]] = 'f';
- }
- break;
- }
- case "left": //cols--
- {
- if (playground[playerCoordinates[0], playerCoordinates[1] - 1] == 'B')
- {
- playground[playerCoordinates[0], playerCoordinates[1]] = '-';
- playerCoordinates[1]--;
- PlayerMovement(playerCoordinates, playground, n, command);
- playground[playerCoordinates[0], playerCoordinates[1]] = 'f';
- }
- else
- {
- if (playground[playerCoordinates[0], playerCoordinates[1]] == 'B')
- {
- playground[playerCoordinates[0], playerCoordinates[1]] = 'B';
- }
- else
- {
- playground[playerCoordinates[0], playerCoordinates[1]] = '-';
- }
- playerCoordinates[1]--;
- playground[playerCoordinates[0], playerCoordinates[1]] = 'f';
- }
- break;
- }
- case "right": //cols++
- {
- if (playground[playerCoordinates[0], playerCoordinates[1] + 1] == 'B')
- {
- playground[playerCoordinates[0], playerCoordinates[1]] = '-';
- playerCoordinates[1]++;
- PlayerMovement(playerCoordinates, playground, n, command);
- playground[playerCoordinates[0], playerCoordinates[1]] = 'f';
- }
- else
- {
- if(playground[playerCoordinates[0], playerCoordinates[1]] == 'B')
- {
- playground[playerCoordinates[0], playerCoordinates[1]] = 'B';
- }
- else
- {
- playground[playerCoordinates[0], playerCoordinates[1]] = '-';
- }
- playerCoordinates[1]++;
- playground[playerCoordinates[0], playerCoordinates[1]] = 'f';
- }
- break;
- }
- default:
- break;
- }
- }
- static void PlayerMovement(int[] playerCoordinates, char[,] playground, int n, string command)
- {
- int row = playerCoordinates[0];
- int col = playerCoordinates[1];
- switch (command)
- {
- case "up": // rows--
- {
- if (CellValidation(row - 1, col, playground, n))
- {
- if (playground[row - 1, col] == 'T')
- {
- break;
- }
- else
- {
- Move(playground, playerCoordinates, command, n);
- }
- }
- else
- {
- if (playground[row, col] == 'B')
- {
- playground[row, col] = 'B';
- }
- else
- {
- playground[row, col] = '-';
- }
- playerCoordinates[0] = n - 1;
- playground[playerCoordinates[0], col] = 'f';
- }
- break;
- }
- case "down": //rows++
- {
- if (CellValidation(row + 1, col, playground, n))
- {
- if (playground[row + 1, col] == 'T')
- {
- break;
- }
- else
- {
- Move(playground, playerCoordinates, command, n);
- }
- }
- else
- {
- if (playground[row, col] == 'B')
- {
- playground[row, col] = 'B';
- }
- else
- {
- playground[row, col] = '-';
- }
- playerCoordinates[0] = 0;
- playground[playerCoordinates[0], col] = 'f';
- }
- break;
- }
- case "left": //cols--
- {
- if (CellValidation(row, col - 1, playground, n))
- {
- if (playground[row, col - 1] == 'T')
- {
- break;
- }
- else
- {
- Move(playground, playerCoordinates, command, n);
- }
- }
- else
- {
- if(playground[row, col] == 'B')
- {
- playground[row, col] = 'B';
- }
- else
- {
- playground[row, col] = '-';
- }
- playerCoordinates[1] = n - 1;
- playground[row, playerCoordinates[1]] = 'f';
- }
- break;
- }
- case "right": //cols++
- {
- if (CellValidation(row, col + 1, playground, n))
- {
- if (playground[row, col + 1] == 'T')
- {
- break;
- }
- else
- {
- Move(playground, playerCoordinates, command, n);
- }
- }
- else
- {
- if (playground[row, col] == 'B')
- {
- playground[row, col] = 'B';
- }
- else
- {
- playground[row, col] = '-';
- }
- playerCoordinates[1] = 0;
- playground[row, playerCoordinates[1]] = 'f';
- }
- break;
- }
- default:
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement