PhotoShaman

Лабиринт с масивами

Jul 18th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication3
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Console.CursorVisible = false;
  13.             //ввод
  14.             int[,] maze = new int[,]
  15.             {
  16.                 {1,1,1,1,0,1,1,1,1,1},
  17.                 {1,0,1,1,0,1,0,0,0,1},
  18.                 {1,0,0,0,0,1,0,1,0,1},
  19.                 {1,1,1,1,0,1,0,1,0,1},
  20.                 {0,0,0,0,0,1,0,1,0,1},
  21.                 {1,1,0,1,1,1,0,1,0,1},
  22.                 {1,1,0,0,0,0,0,1,0,1},
  23.                 {1,1,1,1,1,1,1,1,0,1},
  24.                 {1,0,0,0,0,0,0,0,0,1},
  25.                 {1,1,1,1,1,1,0,1,1,1}
  26.             };
  27.             //координаты игрока
  28.             int x = 1, y = 1;
  29.             while (true)
  30.             {
  31.                 //рисуем лабиринт
  32.                 Console.Clear();
  33.                 for (int i = 0; i < maze.GetLength(0); i++)
  34.                 {
  35.                     for (int j = 0; j < maze.GetLength(1); j++)
  36.                     {
  37.                         if (maze [i, j] == 0) Console.Write(".");
  38.                         if (maze[i, j] == 1) Console.Write("#");
  39.  
  40.                     }
  41.                     Console.WriteLine();
  42.                  }
  43.                 Console.CursorLeft = x;
  44.                 Console.CursorTop = y;
  45.                 Console.ForegroundColor=ConsoleColor.DarkYellow;
  46.                 Console.Write("@");
  47.                 Console.BackgroundColor = ConsoleColor.Cyan;
  48.                 Console.ForegroundColor = ConsoleColor.Gray;
  49.                 Console.BackgroundColor = ConsoleColor.Black;
  50.                
  51.                 // обработка ввода
  52.                 ConsoleKeyInfo ki = Console.ReadKey(True);
  53.                 if (ki.Key == ConsoleKey.Escape) break;
  54.                 if (ki.Key == ConsoleKey.LeftArrow && maze[y, x - 1] == 0) x--;
  55.                 if (ki.Key == ConsoleKey.RightArrow && maze[y, x + 1] == 0) x++;
  56.                 if (ki.Key == ConsoleKey.UpArrow && maze[y-1, x] == 0) y--;
  57.                 if (ki.Key == ConsoleKey.DownArrow && maze[y+1, x] == 0) y++;
  58.             }
  59.            
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment