Advertisement
Guest User

Untitled

a guest
May 27th, 2020
1,117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.08 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ReVolt
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             bool hasWin = false;
  10.  
  11.             int playerRow = 0;
  12.             int playerCol = 0;
  13.  
  14.             int size = int.Parse(Console.ReadLine());
  15.             int commandsCount = int.Parse(Console.ReadLine());
  16.  
  17.             char[][] matrix = new char[size][];
  18.             for (int row = 0; row < size; row++)
  19.             {
  20.                 string currentRow = Console.ReadLine();
  21.                 matrix[row] = new char[currentRow.Length];
  22.                 for (int col = 0; col < matrix[row].Length; col++)
  23.                 {
  24.                     matrix[row][col] = currentRow[col];
  25.                     if (matrix[row][col] == 'f')
  26.                     {
  27.                         playerRow = row;
  28.                         playerCol = col;
  29.                     }
  30.                 }
  31.             }
  32.  
  33.             matrix[playerRow][playerCol] = '-';
  34.  
  35.             for (int index = 0; index < commandsCount; index++)
  36.             {
  37.                 string command = Console.ReadLine();
  38.                 MovePlayer(matrix, playerRow, playerCol, command);
  39.                 if (hasWin == true)
  40.                 {
  41.                     break;
  42.                 }
  43.             }
  44.  
  45.  
  46.             void MovePlayer(char[][] matrixHere, int x, int y, string move)
  47.             {
  48.                 if (move == "down")
  49.                 {
  50.                     bool isInside = CheckInField(matrixHere, x + 1, y);
  51.                     playerRow = isInside == true ? playerRow + 1 : 0;
  52.                     if (matrixHere[playerRow][playerCol] == 'B')
  53.                     {
  54.                         MovePlayer(matrix, playerRow, playerCol, "down");
  55.                     }
  56.                     else if (matrixHere[playerRow][playerCol] == 'T')
  57.                     {
  58.                         MovePlayer(matrix, playerRow, playerCol, "up");
  59.                     }
  60.                     else if (matrixHere[playerRow][playerCol] == 'F')
  61.                     {
  62.                         Console.WriteLine("Player won!");
  63.                         hasWin = true;
  64.                     }
  65.                 }
  66.                 else if (move == "up")
  67.                 {
  68.                     bool isInside = CheckInField(matrixHere, x - 1, y);
  69.                     playerRow = isInside == true ? playerRow - 1 : matrixHere.Length - 1;
  70.                     if (matrixHere[playerRow][playerCol] == 'B')
  71.                     {
  72.                         MovePlayer(matrix, playerRow, playerCol, "up");
  73.                     }
  74.                     else if (matrixHere[playerRow][playerCol] == 'T')
  75.                     {
  76.                         MovePlayer(matrix, playerRow, playerCol, "down");
  77.                     }
  78.                     else if (matrixHere[playerRow][playerCol] == 'F')
  79.                     {
  80.                         Console.WriteLine("Player won!");
  81.                         hasWin = true;
  82.                     }
  83.                 }
  84.                 else if (move == "left")
  85.                 {
  86.                     bool isInside = CheckInField(matrixHere, x, y - 1);
  87.                     playerCol = isInside == true ? playerCol - 1 : matrixHere.Length - 1;
  88.                     if (matrixHere[playerRow][playerCol] == 'B')
  89.                     {
  90.                         MovePlayer(matrix, playerRow, playerCol, "left");
  91.                     }
  92.                     else if (matrixHere[playerRow][playerCol] == 'T')
  93.                     {
  94.                         MovePlayer(matrix, playerRow, playerCol, "right");
  95.                     }
  96.                     else if (matrixHere[playerRow][playerCol] == 'F')
  97.                     {
  98.                         Console.WriteLine("Player won!");
  99.                         hasWin = true;
  100.                     }
  101.                 }
  102.                 else if (move == "right")
  103.                 {
  104.                     bool isInside = CheckInField(matrixHere, x, y + 1);
  105.                     playerCol = isInside == true ? playerCol + 1 : 0;
  106.                     if (matrixHere[playerRow][playerCol] == 'B')
  107.                     {
  108.                         MovePlayer(matrix, playerRow, playerCol, "right");
  109.                     }
  110.                     else if (matrixHere[playerRow][playerCol] == 'T')
  111.                     {
  112.                         MovePlayer(matrix, playerRow, playerCol, "left");
  113.                     }
  114.                     else if (matrixHere[playerRow][playerCol] == 'F')
  115.                     {
  116.                         Console.WriteLine("Player won!");
  117.                         hasWin = true;
  118.                     }
  119.                 }
  120.             }
  121.  
  122.             static bool CheckInField(char[][] matrix, int x, int y)
  123.             {
  124.                 return x >= 0 && y >= 0 && x < matrix.Length && y < matrix.Length;
  125.             }
  126.  
  127.             matrix[playerRow][playerCol] = 'f';
  128.  
  129.             if (hasWin == false)
  130.             {
  131.                 Console.WriteLine("Player lost!");
  132.             }
  133.             foreach (var row in matrix)
  134.             {
  135.                 Console.WriteLine(row);
  136.             }
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement