Daniel_007

02. Snake

Jun 28th, 2020
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace ConsoleApp2
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int size = int.Parse(Console.ReadLine());
  11.             char[,] matrix = new char[size, size];
  12.             int rowSnake = 0;
  13.             int colSnake = 0;
  14.             List<int> burrow = new List<int>(4);
  15.             bool burrowB = false;
  16.             //inputMatrix
  17.             for (int row = 0; row < size; row++)
  18.             {
  19.                 string line = Console.ReadLine();
  20.                 for (int col = 0; col < size; col++)
  21.                 {
  22.                     matrix[row, col] = line[col];
  23.                     if (matrix[row, col] == 'S')
  24.                     {
  25.                         rowSnake = row;
  26.                         colSnake = col;
  27.                     }
  28.                     else if (matrix[row, col] == 'B')
  29.                     {
  30.                         burrowB = true;
  31.                         burrow.Add(row);
  32.                         burrow.Add(col);
  33.                     }
  34.                 }
  35.             }
  36.             //MoveSnake
  37.             int firstBurrowRow = 0;
  38.             int firstBurrowCol = 0;
  39.             int secondBurrowRow = 0;
  40.             int secondBurrowCol = 0;
  41.             if (burrowB)
  42.             {
  43.                  firstBurrowRow = burrow[0];
  44.                  firstBurrowCol = burrow[1];
  45.                  secondBurrowRow = burrow[2];
  46.                  secondBurrowCol = burrow[3];
  47.             }
  48.  
  49.             int foodEaten = 0;
  50.             bool outOfRange = false;
  51.             while (!outOfRange)
  52.             {
  53.                 if (foodEaten >= 10)
  54.                 {
  55.                     break;
  56.                 }
  57.                 string command = Console.ReadLine();
  58.                 matrix[rowSnake, colSnake] = '.';
  59.                 switch (command)
  60.                 {
  61.                     case "up":
  62.                         int rowD = -1;
  63.                         if (InRange(size, rowD, rowSnake))
  64.                         {
  65.                             rowSnake--;
  66.                         }
  67.                         else
  68.                         {
  69.                             outOfRange = true;
  70.                             continue;
  71.                         }
  72.                         break;
  73.                     case "down":
  74.                         rowD = 1;
  75.                         if (InRange(size, rowD, rowSnake))
  76.                         {
  77.                             rowSnake++;
  78.                         }
  79.                         else
  80.                         {
  81.                             outOfRange = true;
  82.                             continue;
  83.                         }
  84.                         break;
  85.                     case "left":
  86.                         int colD = -1;
  87.                         if (InRange(size, colD, colSnake))
  88.                         {
  89.                             colSnake--;
  90.                         }
  91.                         else
  92.                         {
  93.                             outOfRange = true;
  94.                             continue;
  95.                         }
  96.                         break;
  97.                     case "right":
  98.                         colD = 1;
  99.                         if (InRange(size, colD, colSnake))
  100.                         {
  101.                             colSnake++;
  102.                         }
  103.                         else
  104.                         {
  105.                             outOfRange = true;
  106.                             continue;
  107.                         }
  108.                         break;
  109.                 }
  110.                 //check for Food
  111.  
  112.                 if (matrix[rowSnake, colSnake] == '*')
  113.                 {
  114.                     foodEaten++;
  115.                 }
  116.                 else if (matrix[rowSnake, colSnake] == 'B')
  117.                 {
  118.                     matrix[rowSnake, colSnake] = '.';
  119.                     if (rowSnake == firstBurrowRow)
  120.                     {
  121.                         rowSnake = secondBurrowRow;
  122.                         colSnake = secondBurrowCol;
  123.                     }
  124.                     else
  125.                     {
  126.                         rowSnake = firstBurrowRow;
  127.                         colSnake = firstBurrowCol;
  128.                     }
  129.                 }
  130.                 matrix[rowSnake, colSnake] = 'S';
  131.             }
  132.  
  133.             if (outOfRange)
  134.             {
  135.                 Console.WriteLine("Game over!");
  136.             }
  137.             else
  138.             {
  139.                 Console.WriteLine("You won! You fed the snake.");
  140.             }
  141.  
  142.             Console.WriteLine($"Food eaten: {foodEaten}");
  143.             PrintMatrix(matrix,size);
  144.         }
  145.         private static bool InRange(int size, int rowDorColD, int rowOrColPlayer)
  146.         {
  147.             return rowOrColPlayer + rowDorColD >= 0 && rowOrColPlayer + rowDorColD < size;
  148.         }
  149.  
  150.         private static void PrintMatrix(char[,] matrix, int size)
  151.         {
  152.             for (int row = 0; row < size; row++)
  153.             {
  154.                 for (int col = 0; col < size; col++)
  155.                 {
  156.                     Console.Write(matrix[row, col]);
  157.                 }
  158.  
  159.                 Console.WriteLine();
  160.             }
  161.         }
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment