Advertisement
Briotar

Homework 4

May 14th, 2021
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.45 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Homework4
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             char[,] map = new char[20, 20];
  10.             Random rand = new Random();
  11.             char wallSymbol = '#';
  12.             char playerSymbol = '@';
  13.             char lootSymbol = '.';
  14.             int userX = 1, userY = 1;
  15.             int countLoot = 0;
  16.             int winCount = 15;
  17.  
  18.             Console.SetCursorPosition(0, 24);
  19.             Console.WriteLine($"Игра закончится если вы соберете {winCount} - {lootSymbol}");
  20.             Console.WriteLine();
  21.             Console.SetCursorPosition(0, 0);
  22.  
  23.             DrawMap(ref map, wallSymbol, lootSymbol, rand);
  24.            
  25.  
  26.             while(countLoot < winCount)
  27.             {
  28.                 if (map[userX,userY] == lootSymbol)
  29.                 {
  30.                     map[userX, userY] = ' ';
  31.                     countLoot++;
  32.                 }
  33.                 Console.SetCursorPosition(userY, userX);
  34.                 Console.Write(playerSymbol);
  35.                 Console.SetCursorPosition(0, 25);
  36.                 Console.Write($"Сейчас вы собрали - {countLoot}");
  37.                 MovePlayer(ref userX, ref userY, map, wallSymbol, playerSymbol);
  38.             }
  39.  
  40.             Console.SetCursorPosition(0, 26);
  41.             Console.WriteLine("Игра окончена!");
  42.  
  43.         }
  44.  
  45.         static void DrawMap(ref char[,] map, char wallSymbol, char lootSymbol, Random rand)
  46.         {
  47.             int wallOrNot;
  48.  
  49.             for(int i = 0; i < map.GetLength(0); i++)
  50.             {
  51.                 map[0, i] = wallSymbol;
  52.             }
  53.            
  54.             for (int i = 1; i < map.GetLength(0); i++)
  55.             {
  56.                 for (int j = 0; j < map.GetLength(1); j++)
  57.                 {
  58.                     if(j == 0 || j == map.GetLength(1)-1)
  59.                     {
  60.                         map[i, j] = wallSymbol;
  61.                     }
  62.                     else
  63.                     {
  64.                         wallOrNot = rand.Next(1, 7);
  65.                         if (wallOrNot == 6 && (i != 0 && j != 1))
  66.                         {
  67.                             map[i, j] = wallSymbol;
  68.                         }
  69.                         else if(wallOrNot == 5)
  70.                         {
  71.                             map[i, j] = lootSymbol;
  72.                         }
  73.                         else
  74.                         {
  75.                             map[i, j] = ' ';
  76.                         }
  77.                     }
  78.                 }
  79.             }
  80.  
  81.             for (int i = 0; i < map.GetLength(0); i++)
  82.             {
  83.                 map[map.GetLength(0)-1, i] = wallSymbol;
  84.             }
  85.            
  86.             for (int i = 0; i < map.GetLength(0); i++)
  87.             {
  88.                 for (int j = 0; j < map.GetLength(1); j++)
  89.                 {
  90.                     Console.Write(map[i, j]);
  91.                 }
  92.                 Console.WriteLine();
  93.             }
  94.         }
  95.  
  96.         static void MovePlayer(ref int userX, ref int userY, char[,] map, char wallSymbol, char playerSymbol)
  97.         {
  98.             ConsoleKeyInfo charKey = Console.ReadKey();
  99.  
  100.             switch(charKey.Key)
  101.             {
  102.                 case ConsoleKey.UpArrow:
  103.                     if (map[userX -1, userY] != wallSymbol)
  104.                     {
  105.                         DrawVoid(userX, userY);
  106.                         userX--;
  107.                     }
  108.                     break;
  109.                 case ConsoleKey.DownArrow:
  110.                     if (map[userX + 1, userY] != wallSymbol)
  111.                     {
  112.                         DrawVoid(userX, userY);
  113.                         userX++;
  114.                     }
  115.                     break;
  116.                 case ConsoleKey.LeftArrow:
  117.                     if (map[userX, userY - 1] != wallSymbol)
  118.                     {
  119.                         DrawVoid(userX, userY);
  120.                         userY--;
  121.                     }
  122.                     break;
  123.                 case ConsoleKey.RightArrow:
  124.                     if (map[userX, userY + 1] != wallSymbol)
  125.                     {
  126.                         DrawVoid(userX, userY);
  127.                         userY++;
  128.                     }
  129.                     break;
  130.             }
  131.         }
  132.  
  133.         static void DrawVoid(int userX, int userY)
  134.         {
  135.             Console.SetCursorPosition(userY, userX);
  136.             Console.Write(' ');
  137.         }
  138.     }
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement