Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Homework4
- {
- class Program
- {
- static void Main(string[] args)
- {
- char[,] map = new char[20, 20];
- Random rand = new Random();
- char wallSymbol = '#';
- char playerSymbol = '@';
- char lootSymbol = '.';
- int userX = 1, userY = 1;
- int countLoot = 0;
- int winCount = 15;
- Console.SetCursorPosition(0, 24);
- Console.WriteLine($"Игра закончится если вы соберете {winCount} - {lootSymbol}");
- Console.WriteLine();
- Console.SetCursorPosition(0, 0);
- DrawMap(ref map, wallSymbol, lootSymbol, rand);
- while(countLoot < winCount)
- {
- if (map[userX,userY] == lootSymbol)
- {
- map[userX, userY] = ' ';
- countLoot++;
- }
- Console.SetCursorPosition(userY, userX);
- Console.Write(playerSymbol);
- Console.SetCursorPosition(0, 25);
- Console.Write($"Сейчас вы собрали - {countLoot}");
- MovePlayer(ref userX, ref userY, map, wallSymbol, playerSymbol);
- }
- Console.SetCursorPosition(0, 26);
- Console.WriteLine("Игра окончена!");
- }
- static void DrawMap(ref char[,] map, char wallSymbol, char lootSymbol, Random rand)
- {
- int wallOrNot;
- for(int i = 0; i < map.GetLength(0); i++)
- {
- map[0, i] = wallSymbol;
- }
- for (int i = 1; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- if(j == 0 || j == map.GetLength(1)-1)
- {
- map[i, j] = wallSymbol;
- }
- else
- {
- wallOrNot = rand.Next(1, 7);
- if (wallOrNot == 6 && (i != 0 && j != 1))
- {
- map[i, j] = wallSymbol;
- }
- else if(wallOrNot == 5)
- {
- map[i, j] = lootSymbol;
- }
- else
- {
- map[i, j] = ' ';
- }
- }
- }
- }
- for (int i = 0; i < map.GetLength(0); i++)
- {
- map[map.GetLength(0)-1, i] = wallSymbol;
- }
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- Console.Write(map[i, j]);
- }
- Console.WriteLine();
- }
- }
- static void MovePlayer(ref int userX, ref int userY, char[,] map, char wallSymbol, char playerSymbol)
- {
- ConsoleKeyInfo charKey = Console.ReadKey();
- switch(charKey.Key)
- {
- case ConsoleKey.UpArrow:
- if (map[userX -1, userY] != wallSymbol)
- {
- DrawVoid(userX, userY);
- userX--;
- }
- break;
- case ConsoleKey.DownArrow:
- if (map[userX + 1, userY] != wallSymbol)
- {
- DrawVoid(userX, userY);
- userX++;
- }
- break;
- case ConsoleKey.LeftArrow:
- if (map[userX, userY - 1] != wallSymbol)
- {
- DrawVoid(userX, userY);
- userY--;
- }
- break;
- case ConsoleKey.RightArrow:
- if (map[userX, userY + 1] != wallSymbol)
- {
- DrawVoid(userX, userY);
- userY++;
- }
- break;
- }
- }
- static void DrawVoid(int userX, int userY)
- {
- Console.SetCursorPosition(userY, userX);
- Console.Write(' ');
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement