NikaBang

ДЗ: Brave new world2.0

Nov 8th, 2025
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.53 KB | Gaming | 0 0
  1. using System;
  2.  
  3. namespace FunctionsHW
  4. {
  5.     internal class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Console.CursorVisible = false;
  10.  
  11.             char[,] map = {
  12.                 { '╔', '═', '╦', '═', '═', '═', '═', '═', '═', '╗' },
  13.                 { '║', ' ', '║', '*', '*', '*', '*', '*', '*', '║' },
  14.                 { '║', '*', '║', '*', '║', '*', '╔', '═', '═', '╣' },
  15.                 { '║', '*', '*', '*', '║', '*', '║', '*', '*', '║' },
  16.                 { '║', '*', '║', '*', '║', '*', '║', '*', '╔', '╣' },
  17.                 { '║', '*', '║', '*', '║', '*', '║', '*', '╚', '╣' },
  18.                 { '║', '*', '║', '*', '║', '*', '║', '*', '*', '║' },
  19.                 { '║', '*', '║', '*', '║', '*', '*', '*', '═', '╣' },
  20.                 { '║', '*', '║', '*', '║', '*', '║', '*', '*', '║' },
  21.                 { '╚', '═', '╩', '═', '╩', '═', '╩', '═', '═', '╝' }
  22.             };
  23.  
  24.             char player = '@';
  25.             char star = '*';
  26.             char road = ' ';
  27.  
  28.             int playerPositionX = 1;
  29.             int playerPositionY = 1;
  30.             int score = 0;
  31.  
  32.             ConsoleKeyInfo pressedKey;
  33.  
  34.             while (true)
  35.             {
  36.                 Console.Clear();
  37.  
  38.                 Console.ForegroundColor = ConsoleColor.Blue;
  39.                 DrawMap(map);
  40.  
  41.                 Console.ForegroundColor = ConsoleColor.Green;
  42.                 Console.SetCursorPosition(playerPositionX, playerPositionY);
  43.                 Console.Write(player);
  44.  
  45.                 Console.ForegroundColor = ConsoleColor.Red;
  46.                 Console.SetCursorPosition(0, 11);
  47.                 Console.Write("Очки: " + score);
  48.  
  49.                 pressedKey = Console.ReadKey();
  50.  
  51.                 int[] direction = GetDirection(pressedKey);
  52.  
  53.                 int nextPlayerPositionX = playerPositionX + direction[0];
  54.                 int nextPlayerPositionY = playerPositionY + direction[1];
  55.  
  56.                 char nextPosition = GetNextPositionChar(map, nextPlayerPositionX, nextPlayerPositionY);
  57.  
  58.                 if (nextPosition == star)
  59.                 {
  60.                     CollectStar(nextPlayerPositionX, nextPlayerPositionY, road, ref score, ref map);
  61.                     Move(nextPlayerPositionX, nextPlayerPositionY, ref playerPositionX, ref playerPositionY);
  62.                 }
  63.  
  64.                 if (nextPosition == road)
  65.                 {
  66.                     Move(nextPlayerPositionX, nextPlayerPositionY, ref playerPositionX, ref playerPositionY);
  67.                 }
  68.             }
  69.         }
  70.  
  71.         static void DrawMap(char[,] map)
  72.         {
  73.             for (int i = 0; i < map.GetLength(0); i++)
  74.             {
  75.                 for (int j = 0; j < map.GetLength(1); j++)
  76.                 {
  77.                     Console.Write(map[i, j]);
  78.                 }
  79.  
  80.                 Console.WriteLine();
  81.             }
  82.         }
  83.  
  84.         static int[] GetDirection(ConsoleKeyInfo pressedKey)
  85.         {
  86.             ConsoleKeyInfo upArrow = new ConsoleKeyInfo('↑', ConsoleKey.UpArrow, false, false, false);
  87.             ConsoleKeyInfo downArrow = new ConsoleKeyInfo('↓', ConsoleKey.DownArrow, false, false, false);
  88.             ConsoleKeyInfo leftArrow = new ConsoleKeyInfo('←', ConsoleKey.LeftArrow, false, false, false);
  89.             ConsoleKeyInfo rightArrow = new ConsoleKeyInfo('→', ConsoleKey.RightArrow, false, false, false);
  90.  
  91.             int[] direction = { 0, 0 };
  92.  
  93.             if (pressedKey.Key == upArrow.Key)
  94.                 direction[1] = -1;
  95.             else if (pressedKey.Key == downArrow.Key)
  96.                 direction[1] = 1;
  97.             else if (pressedKey.Key == leftArrow.Key)
  98.                 direction[0] = -1;
  99.             else if (pressedKey.Key == rightArrow.Key)
  100.                 direction[0] = 1;
  101.  
  102.             return direction;
  103.         }
  104.         static char GetNextPositionChar(char[,] map, int nextPositionX, int nextPositionY)
  105.         {
  106.             char nextPosition = map[nextPositionY, nextPositionX];
  107.  
  108.             return nextPosition;
  109.         }
  110.  
  111.         static void CollectStar(int positionX, int positionY, char road, ref int score, ref char[,] map)
  112.         {
  113.             score++;
  114.             map[positionY, positionX] = road;
  115.         }
  116.  
  117.         static void Move(int positionX, int positionY, ref int playerPositionX, ref int playerPositionY)
  118.         {
  119.             playerPositionX = positionX;
  120.             playerPositionY = positionY;
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment