Guest User

e

a guest
Jun 8th, 2023
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.56 KB | None | 0 0
  1. using System;
  2.  
  3. namespace DiamondGame
  4. {
  5.     class Program
  6.     {
  7.         static void Main()
  8.         {
  9.             Console.OutputEncoding = System.Text.Encoding.UTF8;
  10.  
  11.             Console.Write("Widht: ");
  12.             int width = int.Parse(Console.ReadLine());
  13.             Console.Write("Height: ");
  14.             int height = int.Parse(Console.ReadLine());
  15.  
  16.             string[,] field = CreateField(width, height);
  17.  
  18.             ConsoleKeyInfo key;
  19.  
  20.             int allDmCount = CountDiamonds(field);
  21.             bool gameover = false;
  22.  
  23.             do
  24.             {
  25.                 DrawField(field);
  26.  
  27.                 Console.WriteLine("Press arrows or ESC = exit.");
  28.  
  29.                 key = Console.ReadKey();
  30.  
  31.                 if (key.Key == ConsoleKey.LeftArrow || key.Key == ConsoleKey.A)
  32.                 {
  33.                     MoveOurGuy(field, "left");
  34.                 }
  35.                 else if (key.Key == ConsoleKey.RightArrow || key.Key == ConsoleKey.D)
  36.                 {
  37.                     MoveOurGuy(field, "right");
  38.                 }
  39.                 else if (key.Key == ConsoleKey.UpArrow || key.Key == ConsoleKey.W)
  40.                 {
  41.                     MoveOurGuy(field, "up");
  42.                 }
  43.                 else if (key.Key == ConsoleKey.DownArrow || key.Key == ConsoleKey.S)
  44.                 {
  45.                     MoveOurGuy(field, "down");
  46.                 }
  47.                 else if (key.Key == ConsoleKey.Escape)
  48.                 {
  49.                     gameover = true;
  50.                 }
  51.  
  52.                 if (allDmCount == allDmCount - CountDiamonds(field))
  53.                 {
  54.                     gameover = true;
  55.                 }
  56.  
  57.                 Console.Clear();
  58.             } while (!gameover);
  59.  
  60.             Console.WriteLine($"Collected Diamonds: {(allDmCount - CountDiamonds(field)) / (double)allDmCount * 100.0:f2}%");
  61.             Console.WriteLine("Game Over!");
  62.         }
  63.  
  64.         static string[,] CreateField(int width, int height)
  65.         {
  66.             string[,] field = new string[width, height];
  67.  
  68.             Random random = new Random();
  69.  
  70.             for (int i = 0; i < width; i++)
  71.             {
  72.                 for (int j = 0; j < height; j++)
  73.                 {
  74.                     double randomNumber = random.NextDouble();
  75.                     if (randomNumber < 0.1)
  76.                     {
  77.                         field[i, j] = "Diamond";
  78.                     }
  79.                     else if (randomNumber < 0.4)
  80.                     {
  81.                         field[i, j] = "Ground";
  82.                     }
  83.                     else if (randomNumber < 0.7)
  84.                     {
  85.                         field[i, j] = "Grass";
  86.                     }
  87.                     else if (randomNumber < 0.9)
  88.                     {
  89.                         field[i, j] = "Tree";
  90.                     }
  91.                     else
  92.                     {
  93.                         field[i, j] = "Stone";
  94.                     }
  95.                 }
  96.             }
  97.  
  98.             int randomX = random.Next(width);
  99.             int randomY = random.Next(height);
  100.             field[randomX, randomY] = "OurGuy";
  101.  
  102.             return field;
  103.         }
  104.  
  105.         static void DrawField(string[,] field)
  106.         {
  107.             int width = field.GetLength(0);
  108.             int height = field.GetLength(1);
  109.  
  110.             for (int j = 0; j < height; j++)
  111.             {
  112.                 for (int i = 0; i < width; i++)
  113.                 {
  114.                     string cell = field[i, j];
  115.                     Console.ForegroundColor = GetCellColor(cell);
  116.  
  117.                     if (cell == "Ground")
  118.                     {
  119.                         Console.Write("\u2592");
  120.                     }
  121.                     else if (cell == "Grass")
  122.                     {
  123.                         Console.Write("\u2593");
  124.                     }
  125.                     else if (cell == "Tree")
  126.                     {
  127.                         Console.Write("\u2663");
  128.                     }
  129.                     else if (cell == "Stone")
  130.                     {
  131.                         Console.ForegroundColor = ConsoleColor.DarkGray;
  132.                         Console.Write("\u0665");
  133.                     }
  134.                     else if (cell == "Diamond")
  135.                     {
  136.                         Console.ForegroundColor = ConsoleColor.Blue;
  137.                         Console.Write("\u2666");
  138.                     }
  139.                     else if (cell == "OurGuy")
  140.                     {
  141.                         Console.ForegroundColor = ConsoleColor.Yellow;
  142.                         Console.Write("\u263A");
  143.                     }
  144.                 }
  145.                 Console.WriteLine();
  146.             }
  147.  
  148.             Console.ResetColor();
  149.         }
  150.  
  151.         static ConsoleColor GetCellColor(string cell)
  152.         {
  153.             if (cell == "Ground")
  154.             {
  155.                 return ConsoleColor.DarkYellow;
  156.             }
  157.             else if (cell == "Grass")
  158.             {
  159.                 return ConsoleColor.Green;
  160.             }
  161.             else if (cell == "Tree")
  162.             {
  163.                 return ConsoleColor.DarkGreen;
  164.             }
  165.             else if (cell == "Stone")
  166.             {
  167.                 return ConsoleColor.DarkGray;
  168.             }
  169.             else if (cell == "Diamond")
  170.             {
  171.                 return ConsoleColor.Blue;
  172.             }
  173.             else if (cell == "OurGuy")
  174.             {
  175.                 return ConsoleColor.Yellow;
  176.             }
  177.  
  178.             return ConsoleColor.White;
  179.         }
  180.  
  181.         static void MoveOurGuy(string[,] field, string direction)
  182.         {
  183.             int width = field.GetLength(0);
  184.             int height = field.GetLength(1);
  185.  
  186.             int ourGuyX = -1;
  187.             int ourGuyY = -1;
  188.  
  189.             for (int i = 0; i < width; i++)
  190.             {
  191.                 for (int j = 0; j < height; j++)
  192.                 {
  193.                     if (field[i, j] == "OurGuy")
  194.                     {
  195.                         ourGuyX = i;
  196.                         ourGuyY = j;
  197.                         break;
  198.                     }
  199.                 }
  200.                 if (ourGuyX != -1)
  201.                 {
  202.                     break;
  203.                 }
  204.             }
  205.  
  206.             if (ourGuyX == -1)
  207.             {
  208.                 return;
  209.             }
  210.  
  211.             int newOurGuyX = ourGuyX;
  212.             int newOurGuyY = ourGuyY;
  213.  
  214.             if (direction == "left" && ourGuyX > 0)
  215.             {
  216.                 newOurGuyX--;
  217.             }
  218.             else if (direction == "right" && ourGuyX < width - 1)
  219.             {
  220.                 newOurGuyX++;
  221.             }
  222.             else if (direction == "up" && ourGuyY > 0)
  223.             {
  224.                 newOurGuyY--;
  225.             }
  226.             else if (direction == "down" && ourGuyY < height - 1)
  227.             {
  228.                 newOurGuyY++;
  229.             }
  230.  
  231.             if (field[newOurGuyX, newOurGuyY] != "Tree")
  232.             {
  233.                 field[ourGuyX, ourGuyY] = "Grass";
  234.                 field[newOurGuyX, newOurGuyY] = "OurGuy";
  235.             }
  236.         }
  237.  
  238.         static int CountDiamonds(string[,] field)
  239.         {
  240.             int count = 0;
  241.             int width = field.GetLength(0);
  242.             int height = field.GetLength(1);
  243.  
  244.             for (int i = 0; i < width; i++)
  245.             {
  246.                 for (int j = 0; j < height; j++)
  247.                 {
  248.                     if (field[i, j] == "Diamond")
  249.                     {
  250.                         count++;
  251.                     }
  252.                 }
  253.             }
  254.  
  255.             return count;
  256.         }
  257.     }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment