Lazarus19

Untitled

Feb 16th, 2021
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.92 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Snake
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int n = int.Parse(Console.ReadLine());
  10.             char[,] matrix = new char[n, n];
  11.             //вход
  12.             for (int row = 0; row < n; row++)
  13.             {
  14.                 string currentRow = Console.ReadLine();
  15.                 for (int col = 0; col < currentRow.Length; col++)
  16.                 {
  17.                     matrix[row, col] = currentRow[col];
  18.                 }
  19.             }
  20.  
  21.             int snakeRow = 0;
  22.             int snakeCol = 0;
  23.  
  24.             int lairRow1 = 0;
  25.             int lairCol1 = 0;
  26.  
  27.             int lairRow2 = 0;
  28.             int lairCol2 = 0;
  29.  
  30.             //проверка дали матрицата съдържа дадена буква и къде
  31.             for (int row = 0; row < n; row++)
  32.             {
  33.                 for (int col = 0; col < n; col++)
  34.                 {
  35.                     if (matrix[row, col] == 'S')
  36.                     {
  37.                         snakeRow = row;
  38.                         snakeCol = col;
  39.                     }
  40.                     if (matrix[row, col] == 'B' && matrix[row, col] != matrix[lairRow1, lairCol1])
  41.                     {
  42.                         lairRow1 = row;
  43.                         lairCol1 = col;
  44.                     }
  45.                     if (matrix[row, col] == 'B')                        
  46.                     {
  47.                         if (matrix[row,col]!=matrix[lairRow1,lairCol1])
  48.                         {
  49.                             lairRow2 = row;
  50.                             lairCol2 = col;
  51.                         }
  52.                        
  53.                     }
  54.                 }
  55.  
  56.             }
  57.             string input = Console.ReadLine();
  58.             int food = 0;
  59.             while (food! < 10)
  60.             {
  61.                 matrix[snakeRow, snakeCol] = '.';
  62.  
  63.                 snakeRow = MoveRow(snakeRow, input);
  64.                 snakeCol = MoveCol(snakeCol, input);
  65.                 if (!IsPossitionValid(snakeRow, snakeCol, n, n))
  66.                 {
  67.                     Console.WriteLine("Game over!");
  68.                     break;
  69.                 }
  70.                 if (matrix[snakeRow, snakeCol] == '*')
  71.                 {
  72.                     food++;
  73.                 }
  74.                 if (matrix[snakeRow, snakeCol] == 'B')
  75.                 {
  76.                     if (matrix[snakeRow, snakeCol] == matrix[lairRow1, lairCol1])
  77.                     {
  78.                         matrix[snakeRow, snakeCol] = '.';
  79.                         matrix[snakeRow, snakeCol] = matrix[lairRow2, lairCol2];
  80.                         if (!IsPossitionValid(snakeRow, snakeCol, n, n))
  81.                         {
  82.                             Console.WriteLine("Game over!");
  83.                             break;
  84.                         }
  85.                     }
  86.                     else
  87.                     {
  88.                         matrix[snakeRow, snakeCol] = '.';
  89.                         matrix[snakeRow, snakeCol] = matrix[lairRow1, lairCol1];
  90.                         if (!IsPossitionValid(snakeRow, snakeCol, n, n))
  91.                         {
  92.                             Console.WriteLine("Game over!");
  93.                             break;
  94.                         }
  95.                     }
  96.  
  97.                 }
  98.  
  99.                 matrix[snakeRow, snakeCol] = 'S';
  100.                 input = Console.ReadLine();
  101.             }
  102.  
  103.             Console.WriteLine($"You won! You fed the snake.\nFood eaten: {food}");
  104.  
  105.             for (int row = 0; row < matrix.GetLength(0); row++)
  106.             {
  107.                 for (int col = 0; col < matrix.GetLength(1); col++)
  108.                 {
  109.                     Console.Write(matrix[row, col]);
  110.                 }
  111.  
  112.                 Console.WriteLine();
  113.             }
  114.  
  115.         }
  116.  
  117.         //илизане от граници на матрицата
  118.         public static bool IsPossitionValid(int row, int col, int rows, int cols)
  119.         {
  120.             if (row < 0 || row >= rows)
  121.             {
  122.                 return false;
  123.             }
  124.             if (col < 0 || col >= cols)
  125.             {
  126.                 return false;
  127.             }
  128.             return true;
  129.         }
  130.         //движение горе-долу
  131.         public static int MoveRow(int row, string movement)
  132.         {
  133.             if (movement == "up")
  134.             {
  135.                 return row - 1;
  136.             }
  137.             if (movement == "down")
  138.             {
  139.                 return row + 1;
  140.             }
  141.             return row;
  142.         }
  143.         //движение ляво-дясно
  144.         public static int MoveCol(int col, string movement)
  145.         {
  146.             if (movement == "left")
  147.             {
  148.                 return col - 1;
  149.             }
  150.             if (movement == "right")
  151.             {
  152.                 return col + 1;
  153.             }
  154.             return col;
  155.         }
  156.     }
  157. }
  158.  
Advertisement
Add Comment
Please, Sign In to add comment