Guest User

Untitled

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