Advertisement
Guest User

Untitled

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