Guest User

Untitled

a guest
Apr 24th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.  
  11.         static int[] kezdopoz;
  12.         static int[] celpoz;
  13.  
  14.  
  15.         static int[,] labirintus =
  16.             {
  17.                 {9,9,9,9,9,9,9,9,9,9},
  18.                 {9,0,0,9,0,9,0,9,0,5},
  19.                 {9,0,9,9,0,9,0,9,0,9},
  20.                 {9,0,0,0,0,9,0,0,0,9},
  21.                 {9,9,0,9,9,9,9,9,0,9},
  22.                 {9,0,0,0,0,0,0,9,0,9},
  23.                 {9,0,9,9,9,0,9,0,0,9},
  24.                 {9,0,0,0,9,0,0,0,9,9},
  25.                 {9,0,9,0,9,0,9,0,0,9},
  26.                 {9,6,9,9,9,9,9,9,9,9}
  27.             };
  28.  
  29.  
  30.         static void bejar(int[] mezo)
  31.         {
  32.             while (true)
  33.             {
  34.  
  35.                 if (mezo[0] == -1)                    
  36.                     break;
  37.                    
  38.  
  39.                 labirintus[mezo[0], mezo[1]] = 1;
  40.  
  41.                 Console.Clear();
  42.                 kiirat();
  43.                 Console.ReadKey();
  44.  
  45.                 if (getLehetsegesLepes(mezo) != null)
  46.                 {
  47.                     mezo = getLehetsegesLepes(mezo);
  48.                 }
  49.                 else
  50.                 {
  51.                     labirintus[mezo[0], mezo[1]] = 2;
  52.                     mezo = visszalep(mezo);
  53.                 }
  54.                    
  55.             }
  56.  
  57.         }
  58.  
  59.         static int[] visszalep(int[] mezo)
  60.         {
  61.             int x = mezo[0];
  62.             int y = mezo[1];
  63.  
  64.             List<int[]> lIndexek = new List<int[]>();
  65.  
  66.  
  67.  
  68.             for (int i = 1; i < 5; i++)
  69.                 switch (i)
  70.                 {
  71.  
  72.  
  73.                     case 1:
  74.                         if (y + 1 <= 9)
  75.                             lIndexek.Add(new int[] { x, y + 1 });
  76.                         break;
  77.  
  78.  
  79.  
  80.                     case 2:
  81.                         if (x + 1 <= 9)
  82.                             lIndexek.Add(new int[] { x + 1, y });
  83.                         break;
  84.  
  85.  
  86.  
  87.                     case 3:
  88.                         if (y - 1 >= 0)
  89.                             lIndexek.Add(new int[] { x, y - 1 });
  90.                         break;
  91.  
  92.  
  93.  
  94.                     case 4:
  95.                         if (x - 1 >= 0)
  96.                             lIndexek.Add(new int[] { x - 1, y });
  97.                         break;
  98.                 }
  99.  
  100.  
  101.             foreach (int[] l in lIndexek)
  102.             {
  103.                 if (labirintus[l[0], l[1]] == 1)
  104.                     return l;
  105.             }
  106.  
  107.             return null;
  108.         }
  109.  
  110.         static int[] getLehetsegesLepes(int[] mezo)
  111.         {
  112.             int x = mezo[0];
  113.             int y = mezo[1];
  114.  
  115.             List<int[]> lIndexek = new List<int[]>();
  116.  
  117.  
  118.  
  119.             for (int i = 1; i < 5; i++)
  120.                 switch (i)
  121.                 {
  122.                    
  123.  
  124.                     case 1:
  125.                         if (y + 1 <= 9)
  126.                             lIndexek.Add(new int[] { x, y + 1 });
  127.                         break;
  128.  
  129.                    
  130.  
  131.                     case 2:
  132.                         if (x + 1 <= 9)
  133.                             lIndexek.Add(new int[] { x + 1, y });
  134.                         break;
  135.  
  136.                    
  137.  
  138.                     case 3:
  139.                         if (y - 1 >= 0)
  140.                             lIndexek.Add(new int[] { x, y - 1 });
  141.                         break;
  142.  
  143.                    
  144.  
  145.                     case 4:
  146.                         if (x - 1 >= 0)
  147.                             lIndexek.Add(new int[] { x - 1, y });
  148.                         break;
  149.                 }
  150.  
  151.  
  152.             foreach (int[] l in lIndexek)
  153.             {
  154.                 if (labirintus[l[0], l[1]] == 5)
  155.                     return new int[] { -1, -1 };
  156.             }
  157.  
  158.             foreach (int[] l in lIndexek)
  159.             {
  160.                 if (labirintus[l[0], l[1]] == 0)
  161.                     return l;
  162.             }
  163.  
  164.  
  165.  
  166.             return null;
  167.         }
  168.  
  169.         static void keresPoz()
  170.         {
  171.             for (int i = 0; i < 10; i++)
  172.                 for (int j = 0; j < 10; j++)
  173.                 {
  174.                     if (labirintus[i, j] == 6)
  175.                         kezdopoz = new int[] { i, j };
  176.                     else if (labirintus[i, j] == 5)
  177.                         celpoz = new int[] { i, j };
  178.                 }
  179.         }
  180.  
  181.         static void kiirat()
  182.         {
  183.             for (int i = 0; i < 10; i++)
  184.             {
  185.                 for (int j = 0; j < 10; j++)
  186.                 {
  187.                     int mezo = labirintus[i, j];
  188.  
  189.                     switch (mezo)
  190.                     {
  191.                         case 1:
  192.                             Console.BackgroundColor = ConsoleColor.Green;
  193.                             break;
  194.                         case 2:
  195.                             Console.BackgroundColor = ConsoleColor.Red;
  196.                             break;
  197.                         case 5:
  198.                             Console.BackgroundColor = ConsoleColor.Cyan;
  199.                             break;
  200.                         case 6:
  201.                             Console.BackgroundColor = ConsoleColor.Cyan;
  202.                             break;
  203.                         case 9:
  204.                             Console.BackgroundColor = ConsoleColor.DarkGray;
  205.                             break;
  206.                         default:
  207.                             break;
  208.  
  209.                     }
  210.  
  211.                     Console.Write("{0,2}", " ");
  212.  
  213.                     Console.BackgroundColor = ConsoleColor.Black;
  214.  
  215.                 }
  216.  
  217.                 Console.WriteLine();
  218.             }
  219.         }
  220.  
  221.         static void Main(string[] args)
  222.         {
  223.  
  224.             keresPoz();
  225.  
  226.             bejar(kezdopoz);
  227.             Console.WriteLine("kijutottunk");
  228.             Console.ReadKey();
  229.  
  230.  
  231.         }
  232.     }
  233. }
Add Comment
Please, Sign In to add comment