Advertisement
OldBeliver

Function_04ver01

Mar 30th, 2021
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace easyMap_ver01
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.CursorVisible = false;
  14.  
  15.             int manX = 0;
  16.             int manY = 0;
  17.             int manDirectionX = 0;
  18.             int manDirectionY = 0;
  19.             int emptyCellsNumber = 0;
  20.  
  21.             bool isPlaying = true;
  22.  
  23.             ReadMap(out char[,] defaultMap);
  24.             DrawMap(defaultMap, ref manX, ref manY, ref emptyCellsNumber);
  25.  
  26.             while (isPlaying)
  27.             {
  28.                 if (Console.KeyAvailable)
  29.                 {
  30.                     ConsoleKeyInfo key = Console.ReadKey(true);
  31.  
  32.                     ChangeDirection(key, ref manDirectionX, ref manDirectionY);
  33.  
  34.                     if (defaultMap[manX + manDirectionX, manY + manDirectionY] != '#')
  35.                     {
  36.                         MoveMan(ref manX, ref manY, manDirectionX, manDirectionY);
  37.                     }
  38.                 }
  39.             }
  40.         }
  41.  
  42.         static void ReadMap(out char[,] defaultMap)
  43.         {
  44.             defaultMap = new char[27, 37]
  45.             {
  46.                 {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
  47.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  48.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  49.                 {'#',' ',' ',' ','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#',' ',' ',' ','#'},
  50.                 {'#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#'},
  51.                 {'#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#'},
  52.                 {'#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#'},
  53.                 {'#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#'},
  54.                 {'#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#','#',' ','#','#'},
  55.                 {'#',' ',' ',' ','#',' ',' ','#','#','#','#','#','#',' ',' ','#','#','#','#','#','#','#',' ',' ','#','#','#','#','#','#',' ',' ',' ',' ',' ',' ','#'},
  56.                 {'#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  57.                 {'#',' ',' ',' ','#',' ',' ','#','#','#','#','#','#',' ',' ','#','#','#','#','#','#','#',' ',' ','#','#','#','#','#','#',' ',' ',' ',' ',' ',' ','#'},
  58.                 {'#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  59.                 {'#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ','#',' ',' ','♂',' ',' ','#',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  60.                 {'#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  61.                 {'#',' ',' ',' ','#',' ',' ','#','#','#','#','#','#',' ',' ','#','#','#','#','#',' ','#',' ',' ','#','#','#','#','#','#',' ',' ',' ',' ',' ',' ','#'},
  62.                 {'#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  63.                 {'#',' ',' ',' ','#',' ',' ','#','#','#','#','#','#',' ',' ','#','#','#','#','#','#','#',' ',' ','#','#','#','#','#','#',' ',' ',' ',' ',' ',' ','#'},
  64.                 {'#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#','#',' ','#','#'},
  65.                 {'#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#'},
  66.                 {'#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#'},
  67.                 {'#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#'},
  68.                 {'#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#'},
  69.                 {'#',' ',' ',' ','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#',' ',' ',' ','#'},
  70.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  71.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},//30
  72.                 {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'}
  73.             };
  74.         }
  75.  
  76.         static void DrawMap(char[,] defaultMap, ref int manX, ref int manY, ref int emptyCellsNumber)
  77.         {
  78.             for (int i = 0; i < defaultMap.GetLength(0); i++)
  79.             {
  80.                 for (int j = 0; j < defaultMap.GetLength(1); j++)
  81.                 {
  82.                     Console.Write(defaultMap[i, j]);
  83.  
  84.                     if (defaultMap[i, j] == '♂')
  85.                     {
  86.                         manX = i;
  87.                         manY = j;
  88.                     }
  89.  
  90.                     if (defaultMap[i, j] == ' ')
  91.                     {
  92.                         emptyCellsNumber++;
  93.                     }
  94.                 }
  95.                 Console.WriteLine();
  96.             }
  97.  
  98.             SpreadCards(defaultMap, emptyCellsNumber);
  99.         }
  100.  
  101.         static void SpreadCards(char[,] defaultMap, int emptyCellsNumber)
  102.         {
  103.             int heart = 3;
  104.             int diamond = 4;
  105.             int club = 5;
  106.             int spade = 6;
  107.             int cardCell;
  108.             Random rand = new Random();
  109.  
  110.             int[][] emptyCellsArray = new int[emptyCellsNumber][];
  111.             int index = 0;
  112.  
  113.             for (int i = 0; i < defaultMap.GetLength(0); i++)
  114.             {
  115.                 for (int j = 0; j < defaultMap.GetLength(1); j++)
  116.                 {
  117.                     if (defaultMap[i, j] == ' ')
  118.                     {
  119.                         emptyCellsArray[index] = new int[2] { i, j };
  120.                         index++;
  121.                     }
  122.                 }
  123.             }            
  124.  
  125.             for (int cardSuit = heart; cardSuit < spade + 1; cardSuit++)
  126.             {
  127.                 for (int i = 0; i < 5; i++)
  128.                 {
  129.                     cardCell = rand.Next(0, emptyCellsNumber + 1);
  130.  
  131.                     Console.SetCursorPosition(emptyCellsArray[cardCell][1], emptyCellsArray[cardCell][0]);
  132.                     Console.Write((char)cardSuit);
  133.                 }
  134.             }
  135.         }
  136.  
  137.         static void MoveMan(ref int manX, ref int manY, int manDirectionX, int manDirectionY)
  138.         {
  139.             char man = (char)11;
  140.  
  141.             Console.SetCursorPosition(manY, manX);
  142.             Console.Write(' ');
  143.  
  144.             manX += manDirectionX;
  145.             manY += manDirectionY;
  146.  
  147.             Console.SetCursorPosition(manY, manX);
  148.             Console.Write(man);
  149.         }
  150.  
  151.         static void ChangeDirection(ConsoleKeyInfo key, ref int upDown, ref int leftRight)
  152.         {
  153.             switch (key.Key)
  154.             {
  155.                 case ConsoleKey.UpArrow:
  156.                     upDown = -1; leftRight = 0;
  157.                     break;
  158.                 case ConsoleKey.DownArrow:
  159.                     upDown = 1; leftRight = 0;
  160.                     break;
  161.                 case ConsoleKey.LeftArrow:
  162.                     upDown = 0; leftRight = -1;
  163.                     break;
  164.                 case ConsoleKey.RightArrow:
  165.                     upDown = 0; leftRight = 1;
  166.                     break;
  167.             }
  168.         }
  169.     }
  170. }
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement