Advertisement
LeRoY_Go

Untitled

Feb 2nd, 2022
783
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.87 KB | None | 0 0
  1. using System;
  2.  
  3. namespace C_Sharp_Junior
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Console.CursorVisible = false;
  10.             Console.WriteLine("Найди выход.Цель игры: Попасть игроком на клетку обозначеной знаком 'X'");
  11.             Console.ReadLine();
  12.             Console.Clear();
  13.             bool isEndGame = true;
  14.             char signEndGame = 'X';
  15.             char signGameObstacle = '#';
  16.             int playerPositionX = 1;
  17.             int playerPositionY = 1;
  18.             int possiblePlayerPositionX = 1;
  19.             int possiblePlayerPositionY = 1;
  20.             char[,] map = {{'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
  21.                            {'#',' ',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
  22.                            {'#',' ','#','#','#',' ','#',' ',' ','#','#','#','#','#','#',' ','#'},
  23.                            {'#',' ',' ',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  24.                            {'#','#','#',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ','#'},
  25.                            {'#',' ','#',' ','#','#','#',' ',' ','#','#',' ','#','#','#',' ','#'},
  26.                            {'#',' ','#',' ',' ',' ',' ',' ',' ','#',' ',' ','#','X','#',' ','#'},
  27.                            {'#',' ',' ',' ','#','#','#',' ',' ','#',' ',' ','#',' ',' ',' ','#'},
  28.                            {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'}};
  29.             DrawMap(map);
  30.             while (isEndGame != false)
  31.             {
  32.                 DrawPlayer(playerPositionX, playerPositionY);
  33.                 GetDirection(ref possiblePlayerPositionX, ref possiblePlayerPositionY);
  34.                 isEndGame = CheckingSign(map, playerPositionX, playerPositionY, possiblePlayerPositionX, possiblePlayerPositionY, signEndGame);
  35.                 if (CheckingSign(map, playerPositionX, playerPositionY, possiblePlayerPositionX, possiblePlayerPositionY, signGameObstacle))
  36.                 {
  37.                     Move(ref playerPositionX, ref playerPositionY, possiblePlayerPositionX, possiblePlayerPositionY);
  38.                 }
  39.             }
  40.             Console.Clear();
  41.             Console.WriteLine("Поздравляю. Вы выиграли.");
  42.             Console.ReadLine();
  43.         }
  44.  
  45.         static void DrawMap(char[,] map)
  46.         {
  47.             for (int i = 0; i < map.GetLength(0); i++)
  48.             {
  49.                 for (int j = 0; j < map.GetLength(1); j++)
  50.                 {
  51.                     Console.Write(map[i, j]);
  52.                 }
  53.                 Console.WriteLine();
  54.             }
  55.         }
  56.  
  57.         static bool CheckingSign(char[,] map, int playerPositionX, int playerPositionY, int possiblePlayerPositionX, int possiblePlayerPositionY, char sign)
  58.         {
  59.             return map[playerPositionX + possiblePlayerPositionX, playerPositionY + possiblePlayerPositionY] != sign;
  60.         }
  61.  
  62.         static void Move(ref int playerPositionX, ref int playerPositionY, int possiblePlayerPositionX, int possiblePlayerPositionY)
  63.         {
  64.             Console.SetCursorPosition(playerPositionY, playerPositionX);
  65.             Console.Write(" ");
  66.             playerPositionX += possiblePlayerPositionX;
  67.             playerPositionY += possiblePlayerPositionY;
  68.             Console.SetCursorPosition(playerPositionY, playerPositionX);
  69.             Console.Write("@");
  70.         }
  71.  
  72.         static void GetDirection(ref int possiblePlayerPositionX, ref int possiblePlayerPositionY)
  73.         {
  74.             bool directionDefinitely = false;
  75.             while (directionDefinitely == false)
  76.             {
  77.                 if (Console.KeyAvailable)
  78.                 {
  79.                     ConsoleKeyInfo key = Console.ReadKey(true);
  80.                     switch (key.Key)
  81.                     {
  82.                         case ConsoleKey.UpArrow:
  83.                             possiblePlayerPositionX = -1; possiblePlayerPositionY = 0;
  84.                             break;
  85.                         case ConsoleKey.DownArrow:
  86.                             possiblePlayerPositionX = 1; possiblePlayerPositionY = 0;
  87.                             break;
  88.                         case ConsoleKey.LeftArrow:
  89.                             possiblePlayerPositionX = 0; possiblePlayerPositionY = -1;
  90.                             break;
  91.                         case ConsoleKey.RightArrow:
  92.                             possiblePlayerPositionX = 0; possiblePlayerPositionY = 1;
  93.                             break;
  94.                     }
  95.                     directionDefinitely = true;
  96.                 }
  97.             }
  98.         }
  99.  
  100.         static void DrawPlayer(int playerPositionX, int playerPositionY)
  101.         {
  102.             Console.SetCursorPosition(playerPositionY, playerPositionX);
  103.             Console.Write("@");
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement