elena1234

Snake - multidimensional array

Mar 3rd, 2021 (edited)
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.95 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 arrayLength = int.Parse(Console.ReadLine());
  10.             int rows = arrayLength;
  11.             int cols = arrayLength;
  12.             int rowStart = 0;
  13.             int colStart = 0;
  14.             var matrix = new char[rows, cols];
  15.             for (int row = 0; row < rows; row++)
  16.             {
  17.                 string currentLine = Console.ReadLine();
  18.                 for (int col = 0; col < cols; col++)
  19.                 {
  20.                     matrix[row, col] = currentLine[col];
  21.                     if (matrix[row, col] == 'S')
  22.                     {
  23.                         rowStart = row;
  24.                         colStart = col;
  25.                     }
  26.                 }
  27.             }
  28.  
  29.             int foodEaten = 0;
  30.             while (foodEaten < 10)
  31.             {
  32.                 int currentRow = rowStart;
  33.                 int currentCol = colStart;
  34.                 string command = Console.ReadLine();
  35.                 switch (command)
  36.                 {
  37.                     case "up":
  38.                         currentRow--;
  39.                         break;
  40.                     case "down":
  41.                         currentRow++;
  42.                         break;
  43.                     case "left":
  44.                         currentCol--;
  45.                         break;
  46.                     case "right":
  47.                         currentCol++;
  48.                         break;
  49.                 }
  50.  
  51.                 if (currentRow >= 0 && currentRow < rows && currentCol >= 0 && currentCol < cols) // the snake is in the lair
  52.                 {
  53.                     if (matrix[currentRow, currentCol] != 'B') // The snake doesn't reach a burrow
  54.                     {
  55.                         if (matrix[currentRow, currentCol] == '*')
  56.                         {
  57.                             foodEaten++;
  58.                         }
  59.  
  60.                         matrix[rowStart, colStart] = '.';
  61.                         matrix[currentRow, currentCol] = 'S';
  62.                         rowStart = currentRow;
  63.                         colStart = currentCol;
  64.                     }
  65.  
  66.                     else if (matrix[currentRow, currentCol] == 'B')
  67.                     {
  68.                         matrix[rowStart, colStart] = '.';
  69.                         matrix[currentRow, currentCol] = '.';
  70.  
  71.                         for (int row = 0; row < rows; row++)
  72.                         {
  73.                             for (int col = 0; col < cols; col++)
  74.                             {
  75.                                 if (matrix[row, col] == 'B' && row != currentRow && col != currentCol)
  76.                                 {
  77.                                     rowStart = row;
  78.                                     colStart = col;
  79.                                     matrix[rowStart, colStart] = 'S';
  80.                                     break;
  81.                                 }
  82.                             }
  83.                         }
  84.                     }
  85.                 }
  86.  
  87.                 else // the snake is out
  88.                 {
  89.                     matrix[rowStart, colStart] = '.';
  90.                     Console.WriteLine("Game over!");
  91.                     Console.WriteLine($"Food eaten: {foodEaten}");
  92.                     PrintTheMatrix(rows, cols, matrix);
  93.                     return;
  94.                 }
  95.             }
  96.  
  97.             Console.WriteLine("You won! You fed the snake.");
  98.             Console.WriteLine("Food eaten: 10");
  99.             PrintTheMatrix(rows, cols, matrix);
  100.         }
  101.  
  102.  
  103.         private static void PrintTheMatrix(int rows, int cols, char[,] matrix)
  104.         {
  105.             for (int row = 0; row < rows; row++)
  106.             {
  107.                 for (int col = 0; col < cols; col++)
  108.                 {
  109.                     Console.Write(matrix[row, col]);
  110.                 }
  111.  
  112.                 Console.WriteLine();
  113.             }
  114.         }
  115.     }
  116. }
  117.  
  118.  
Add Comment
Please, Sign In to add comment