dxoraxs

Untitled

Apr 18th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.30 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace shuffleArray
  5. {
  6.     class Program
  7.     {
  8.         static void Redraw(ref int posX, ref int posY, char[,] array, int DX, int DY, char symbol)
  9.         {
  10.             Console.SetCursorPosition(posX, posY);
  11.             Console.Write(array[posY, posX]);
  12.             posX += DX;
  13.             posY += DY;
  14.             Console.SetCursorPosition(posX, posY);
  15.             Console.Write(symbol);
  16.         }
  17.         static void Main(string[] args)
  18.         {
  19.             Console.CursorVisible = false;
  20.             Random rand = new Random();
  21.  
  22.             int maxPoint = 0;
  23.             int point = 0;
  24.  
  25.             string[] newFile = File.ReadAllLines("map.txt");
  26.             char[,] map = new char[newFile.Length, newFile[1].Length];
  27.  
  28.             int packmanX = 0, packmanY = 0;
  29.             int DX = 1, DY = 0;
  30.             bool die = false;
  31.             bool win = false;
  32.  
  33.             int ghostX = 0, ghostY = 0;
  34.             int GDX = 1, GDY = 0;
  35.             int ghostDir = 0;
  36.  
  37.             for (int i = 0; i < map.GetLength(0); i++)
  38.                 for (int j = 0; j < map.GetLength(1); j++)
  39.                     map[i, j] = newFile[i][j];
  40.  
  41.             for (int i = 0; i < map.GetLength(0); i++)
  42.             {
  43.                 for (int j = 0; j < map.GetLength(1); j++)
  44.                 {
  45.                     if (map[i, j] == '@')
  46.                     {
  47.                         packmanX = j;
  48.                         packmanY = i;
  49.                         map[i, j] = ' ';
  50.                     }
  51.                     else if (map[i, j] == ' ')
  52.                     {
  53.                         map[i, j] = '.';
  54.                         maxPoint++;
  55.                     }
  56.                     else if (map[i, j] == 'X')
  57.                     {
  58.                         ghostX = j;
  59.                         ghostY = i;
  60.                         map[i, j] = '.';
  61.                         maxPoint++;
  62.                     }
  63.                     Console.Write(map[i, j]);
  64.                 }
  65.                 Console.WriteLine();
  66.             }
  67.  
  68.             ConsoleKeyInfo key;
  69.  
  70.             while (!win && !die)
  71.             {
  72.                 if (Console.KeyAvailable)
  73.                 {
  74.                     key = Console.ReadKey(true);
  75.                     switch (key.Key)
  76.                     {
  77.                         case ConsoleKey.UpArrow:
  78.                             DX = 0; DY = -1;
  79.                             break;
  80.                         case ConsoleKey.DownArrow:
  81.                             DX = 0; DY = 1;
  82.                             break;
  83.                         case ConsoleKey.LeftArrow:
  84.                             DX = -1; DY = 0;
  85.                             break;
  86.                         case ConsoleKey.RightArrow:
  87.                             DX = 1; DY = 0;
  88.                             break;
  89.                     }
  90.                 }
  91.  
  92.                 System.Threading.Thread.Sleep(100);
  93.  
  94.                 if (map[packmanY + DY, packmanX + DX] != '#')
  95.                 {
  96.                     Redraw(ref packmanX,ref packmanY, map, DX,DY, '☺');
  97.  
  98.  
  99.                     if (map[packmanY, packmanX] == '.')
  100.                     {
  101.                         point++;
  102.                         map[packmanY, packmanX] = ' ';
  103.                         Console.SetCursorPosition(0, map.GetLength(0) + 5);
  104.                         Console.Write("Вы собрали " + point + "/" + maxPoint + " очков.");
  105.                     }
  106.                 }
  107.                 if (map[packmanY, packmanX] == 'O')
  108.                 {
  109.                     if (packmanX<map.GetLength(0))
  110.                     {
  111.                         Console.SetCursorPosition(packmanX, packmanY);
  112.                         Console.Write(map[packmanY, packmanX]);
  113.                         packmanX = map.GetLength(1) - 1;
  114.                     }
  115.                     else
  116.                         packmanX = 1;
  117.                     Redraw(ref packmanX, ref packmanY, map, DX, DY, '☺');
  118.                 }
  119.  
  120.                 if (map[ghostY + GDY, ghostX + GDX] == '#')
  121.                 {
  122.                     ghostDir = rand.Next(1, 5);
  123.  
  124.                     switch (ghostDir)
  125.                     {
  126.                         case 1:
  127.                             GDX = 0; GDY = -1;
  128.                             break;
  129.                         case 2:
  130.                             GDX = 0; GDY = 1;
  131.                             break;
  132.                         case 3:
  133.                             GDX = -1; GDY = 0;
  134.                             break;
  135.                         case 4:
  136.                             GDX = 1; GDY = 0;
  137.                             break;
  138.                     }
  139.                 }
  140.                 else
  141.                 {
  142.                     Redraw(ref ghostX, ref ghostY, map, GDX, GDY, '☻');
  143.  
  144.                     if (ghostX == packmanX && ghostY == packmanY)
  145.                         die = true;
  146.                 }
  147.                 if (point == maxPoint)
  148.                 {
  149.                     win = true;
  150.                 }
  151.             }
  152.             if (win)
  153.             {
  154.                 Console.Clear();
  155.                 Console.Write("Победа!");
  156.             }
  157.             else
  158.             {
  159.                 Console.Clear();
  160.                 Console.Write("Поражение!");
  161.             }
  162.         }
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment