Advertisement
Levi0227

Buffalo hunter game

Nov 29th, 2023
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.89 KB | Source Code | 0 0
  1. using System.Security.Cryptography.X509Certificates;
  2.  
  3. namespace _12_het
  4. {
  5.     internal class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Console.Write("m = ");
  10.             int m = int.Parse(Console.ReadLine());
  11.            
  12.             Console.Write("b = ");
  13.             int b = int.Parse(Console.ReadLine());
  14.  
  15.             //Field mezo = new Field(m);
  16.             //Buffalo boleny = new Buffalo(0,0);
  17.  
  18.             ////Console.WriteLine($"CÊl: ({mezo.TargetX},{mezo.TargetY})");
  19.             ////int tempx = 3;
  20.             ////int tempy = 6;
  21.             ////Console.WriteLine($"Megengedett -e: {mezo.AllowedPosition(tempx,tempy)}");
  22.  
  23.             //mezo.Show();
  24.  
  25.             //boleny.Show();
  26.  
  27.             Game jatek = new Game(m, b);
  28.             jatek.Run();
  29.         }
  30.  
  31.     }
  32. }
  33.  
  34.  
  35. ---------------------------------------------------------------------------------------------
  36.  
  37.  
  38. namespace _12_het
  39. {
  40.     internal class Field
  41.     {
  42.         int[,] jatekter;
  43.  
  44.         public Field(int m)
  45.         {
  46.             jatekter = new int[m, m];
  47.         }
  48.  
  49.         public int TargetX()
  50.         {
  51.             return jatekter.GetLength(0) - 1;
  52.         }
  53.  
  54.         public int TargetY()
  55.         {
  56.             return jatekter.GetLength(1) - 1;
  57.         }
  58.  
  59.         public bool AllowedPosition(int x, int y)
  60.         {
  61.  
  62.             return x <= TargetX() && y <= TargetY() && x >= 0 && y >= 0;
  63.         }
  64.  
  65.         public void Show()
  66.         {
  67.             for (int i = 0; i < jatekter.GetLength(0); i++)
  68.             {
  69.                 for (int j = 0; j < jatekter.GetLength(1); j++)
  70.                 {
  71.                     if (i == 0 || i == jatekter.GetLength(0) - 1)
  72.                     {
  73.                         Console.Write("-");
  74.                     }
  75.                     else
  76.                     {
  77.                         if (j == 0 || j == jatekter.GetLength(1) - 1)
  78.                         {
  79.                             Console.Write("|");
  80.                         }
  81.                         else
  82.                         {
  83.                             Console.Write(" ");
  84.                         }
  85.                     }
  86.                 }
  87.                 Console.WriteLine();
  88.             }
  89.         }
  90.     }
  91. }
  92.  
  93.  
  94. ---------------------------------------------------------------------------------------------
  95.  
  96.  
  97. namespace _12_het
  98. {
  99.     internal class Buffalo
  100.     {
  101.         int aktualisBolenyX;
  102.         int aktualisBolenyY;
  103.         bool allapot;
  104.  
  105.         public Buffalo(int aktualisBolenyX, int aktualisBolenyY)
  106.         {
  107.             this.aktualisBolenyX = 0;
  108.             this.aktualisBolenyY = 0;
  109.             allapot = true;
  110.         }
  111.  
  112.         public int X()
  113.         {
  114.             return aktualisBolenyX;
  115.         }
  116.  
  117.         public int Y()
  118.         {
  119.             return aktualisBolenyY;
  120.         }
  121.  
  122.         public void Move(Field jatekter)
  123.         {
  124.             Random rnd = new Random();
  125.             int lepsek = rnd.Next(3);
  126.  
  127.             if (lepsek == 0)
  128.             {
  129.                 if (jatekter.AllowedPosition(aktualisBolenyX + 1, aktualisBolenyY) == true)
  130.                 {
  131.                     this.aktualisBolenyX += 1;
  132.                 }
  133.             }
  134.  
  135.             if (lepsek == 1)
  136.             {
  137.                 if (jatekter.AllowedPosition(aktualisBolenyX, aktualisBolenyY + 1) == true)
  138.                 {
  139.                     this.aktualisBolenyY += 1;
  140.                 }
  141.             }
  142.  
  143.             if (lepsek == 1)
  144.             {
  145.                 if (jatekter.AllowedPosition(aktualisBolenyX + 1, aktualisBolenyY + 1) == true)
  146.                 {
  147.                     this.aktualisBolenyX += 1;
  148.                     this.aktualisBolenyY += 1;
  149.                 }
  150.             }
  151.         }
  152.  
  153.         public void Deactivate()
  154.         {
  155.             allapot = false;
  156.         }
  157.  
  158.         public void Show()
  159.         {
  160.             if (allapot == true)
  161.             {
  162.                 Console.ForegroundColor = ConsoleColor.Green;
  163.             }
  164.             else
  165.             {
  166.                 Console.ForegroundColor = ConsoleColor.Red;
  167.             }
  168.  
  169.             Console.SetCursorPosition(aktualisBolenyX, aktualisBolenyY);
  170.             Console.WriteLine("B");
  171.  
  172.             Console.ResetColor();
  173.         }
  174.     }
  175. }
  176.  
  177.  
  178. ---------------------------------------------------------------------------------------------
  179.  
  180.  
  181. using System.Security.Cryptography.X509Certificates;
  182.  
  183. namespace _12_het
  184. {
  185.     internal class Game
  186.     {
  187.         Field jatekter;
  188.         List<Buffalo> bolenyek;
  189.  
  190.         public bool IsOver { get; private set; }
  191.  
  192.         public Game(int jatekmeret, int bolenyCount)
  193.         {
  194.             jatekter = new Field(jatekmeret);
  195.             bolenyek = new List<Buffalo>();
  196.  
  197.             for (int i = 0; i < bolenyCount; i++)
  198.             {
  199.                 bolenyek.Add(new Buffalo(0, 0));
  200.             }
  201.  
  202.             IsOver = false;
  203.         }
  204.  
  205.         private void VisualizeElements()
  206.         {
  207.             Console.Clear();
  208.  
  209.             jatekter.Show();
  210.  
  211.             for (int i = 0; i < bolenyek.Count; i++)
  212.             {
  213.                 bolenyek[i].Show();
  214.             }
  215.         }
  216.  
  217.         private void Shoot(int x, int y)
  218.         {
  219.             for (int i = 0; i < bolenyek.Count; i++)
  220.             {
  221.                 if (x == bolenyek[i].X() && y == bolenyek[i].Y())
  222.                 {
  223.                     bolenyek[i].Deactivate();
  224.                 }
  225.             }
  226.         }
  227.  
  228.         public void Run()
  229.         {
  230.             while (!IsOver)
  231.             {
  232.                 VisualizeElements();
  233.  
  234.                 Console.SetCursorPosition(0,jatekter.TargetY()+2);
  235.  
  236.                 Console.Write("x = ");
  237.                 int x = int.Parse(Console.ReadLine());
  238.                 Console.Write("\ny = ");
  239.                 int y = int.Parse(Console.ReadLine());
  240.  
  241.                 Shoot(x, y);
  242.  
  243.             }
  244.         }
  245.     }
  246. }
  247.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement