Skip to content

Instantly share code, notes, and snippets.

@tsk-arh
Last active January 29, 2024 19:38
Show Gist options
  • Save tsk-arh/1a042c2428c364a0dbb3731f9ab1125c to your computer and use it in GitHub Desktop.
Save tsk-arh/1a042c2428c364a0dbb3731f9ab1125c to your computer and use it in GitHub Desktop.
ЗАДАНИЕ Brave new world narcobeta
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp4
{
internal class Program
{
static void Main(string[] args)
{
char[,] map = new char[25, 100];
char player = 'i';
char mapWall = 'X';
char spaceMap = ' ';
char bonus = '@';
char bonusSpawnPeriodUp = 'q';
char bonusSpawnPeriodDown = 'w';
char keyExit = 'c';
char keyBunusAllMap = 'm';
char keyGameMode = 'g';
Random random = new Random();
int mapStartGameRandomPositionMiniumumY = 1;
int mapStartGameRandomPositionMaximumY = map.GetLength(0);
int mapStartGameRandomPositionMiniumumX = 1;
int mapStartGameRandomPositionMaximumX = map.GetLength(1);
int cursorPositionX = random.Next(mapStartGameRandomPositionMiniumumX, mapStartGameRandomPositionMaximumX);
int cursorPositionY = random.Next(mapStartGameRandomPositionMiniumumY, mapStartGameRandomPositionMaximumY);
int quantytyBonus = 0;
bool isPaintWallMode = true;
bool isGameModeOn = true;
bool isRuning = true;
ConsoleColor defoultBackgroundColor = Console.BackgroundColor;
Console.BackgroundColor = defoultBackgroundColor;
Console.CursorVisible = false;
RefreshMap(map, spaceMap, out int bonusesRangeSpawnPeriod);
Task.Run(() => {
while (isRuning)
{
int upDatePause = 10;
ConsoleKeyInfo pressedKey = Console.ReadKey();
EnterSetUpKeyFromUser(map, pressedKey, spaceMap, bonusSpawnPeriodUp, bonusSpawnPeriodDown, keyExit, keyBunusAllMap, keyGameMode, ref isPaintWallMode, ref isGameModeOn, ref bonusesRangeSpawnPeriod, ref quantytyBonus, ref isRuning);
GetMoove(map, pressedKey, isPaintWallMode, mapWall, bonus, ref cursorPositionX, ref cursorPositionY, ref isGameModeOn, ref quantytyBonus);
WritePlayer(cursorPositionX, cursorPositionY, map, player);
Thread.Sleep(upDatePause);
}
});
while (isRuning)
{
int wallMetres = 0;
int quantityBonusLeft = 0;
int upDatePause = 100;
int[,] randomArray = new int[map.GetLength(0), map.GetLength(1)];
int[,] randomColorArray = new int[map.GetLength(0), map.GetLength(1)];
int randomColorArrayMaximum = 20;
for (int i = 0; i < randomArray.GetLength(0); i++)
{
for (int j = 0; j < randomArray.GetLength(1); j++)
{
randomArray[i, j] = random.Next(bonusesRangeSpawnPeriod);
randomColorArray[i, j] = random.Next(randomColorArrayMaximum + 1);
}
}
map = UpdateAllMap(map, randomArray, randomColorArray, player, spaceMap, mapWall, bonus, ref cursorPositionX, ref cursorPositionY, ref wallMetres, ref quantityBonusLeft);
WriteLegend(map, wallMetres, quantytyBonus, quantityBonusLeft, mapWall, keyExit, bonusSpawnPeriodUp, bonusSpawnPeriodDown, keyGameMode, player, bonus, keyBunusAllMap);
Thread.Sleep(upDatePause);
}
}
private static char[,] UpdateAllMap(char[,] map, int[,] randomArray, int[,] randomColorArray, char player, char spaceMap, char mapWall, char bonus, ref int cursorPositionX, ref int cursorPositionY, ref int wallMetres, ref int quantityBonusLeft)
{
WritePlayer(cursorPositionX, cursorPositionY, map, player);
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
SetRandomMapColor(randomColorArray, i, j);
WritePrimeterWall(map, i, j, mapWall);
if (map[i, j] == mapWall)
{
wallMetres++;
Console.SetCursorPosition(j, i);
Console.Write(mapWall);
}
if (map[i, j] == spaceMap)
{
Console.SetCursorPosition(j, i);
Console.Write(spaceMap);
}
WriteBonuses(map, i, j, bonus, ref randomArray, ref quantityBonusLeft);
}
}
return map;
}
private static void WritePlayer(int cursorPositionX, int cursorPositionY, char[,] map, char player)
{
Console.ForegroundColor = ConsoleColor.Yellow;
map[cursorPositionY, cursorPositionX] = player;
Console.SetCursorPosition(cursorPositionX, cursorPositionY);
Console.Write(player);
}
private static void WriteBonuses(char[,] map, int coordinateY, int coordinateX, char bonus, ref int[,] randomArray, ref int quantityBonusLeft)
{
int bonusWriteRandomArrayNumber = 20;
if (map[coordinateY, coordinateX] == ' ' && randomArray[coordinateY, coordinateX] == bonusWriteRandomArrayNumber)
{
map[coordinateY, coordinateX] = bonus;
}
if (map[coordinateY, coordinateX] == bonus)
{
quantityBonusLeft++;
Console.SetCursorPosition(coordinateX, coordinateY);
Console.Write(bonus);
}
}
private static void WritePrimeterWall(char[,] map, int coordinateY, int coordinateX, char mapWall)
{
int[] mapWallIndexes = { 0, map.GetLength(1) - 1 };
int coordinatWallY = map.GetLength(0) - 1;
for (int i = 0; i < mapWallIndexes.Length; i++)
{
if (coordinateY == mapWallIndexes[i] || coordinateX == mapWallIndexes[i])
{
map[coordinateY, coordinateX] = mapWall;
}
}
if (coordinateY == coordinatWallY)
{
map[coordinateY, coordinateX] = mapWall;
}
}
private static void SetRandomMapColor(int[,] rndColorMassive, int coordinateY, int coordinateX)
{
int colorForegroundRedWriteArray = 10;
int colorForegroundMagentaWriteArray = 12;
int[] colorForegroundGreen = { 14, 15 };
if (rndColorMassive[coordinateY, coordinateX] == colorForegroundRedWriteArray)
{
Console.ForegroundColor = ConsoleColor.Red;
}
if (rndColorMassive[coordinateY, coordinateX] == colorForegroundMagentaWriteArray)
{
Console.ForegroundColor = ConsoleColor.Magenta;
}
for (int i = 0; i < colorForegroundGreen.Length; i++)
{
if (rndColorMassive[coordinateY, coordinateX] == colorForegroundGreen[i])
{
Console.ForegroundColor = ConsoleColor.Green;
}
}
}
private static void WriteLegend(char[,] map, int wallMetres, int quantytyBonus, int quantityBonusLeft, char mapWall, char keyExit, char bonusSpawnPediodUp, char bonusSpawnPediodDown, char keyGameMode, char player, char bonus, char keyBunusAllMap)
{
int countVerticalWall = 2;
int countHorisontalWall = 2;
int corners = 4;
int wallAllBasePerimetrLenght = map.GetLength(0) * countVerticalWall + map.GetLength(1) * countHorisontalWall - corners;
int coordinateXgameLegendPartOne = 40;
int coordinateYgamelegendPartOne = map.GetLength(0);
int coodrdinateXgameLegendPartTwo = 10;
int coordinateYgameLegendPartTwo = map.GetLength(0) + 1;
int coordinateXgameLegendPartThree = 10;
int coodrdinateYgameLegendPartThree = map.GetLength(0) + 2;
int coordinateXgameLegendPartFour = 10;
int coordinateYgameLegendPartFour = map.GetLength(0) + 3;
int coordinateXgameLegendPartFive = 10;
int coordinateYgameLegendPartFive = map.GetLength(0) + 4;
string gameLegendPartOne = $"!Brave NEW world!";
string gameLegendPartTwo = $"{mapWall} - стена space - начать или перестать строить стену {keyExit} - выход";
string gameLegendPartThree = $"{wallMetres - wallAllBasePerimetrLenght} метров стены построено {keyGameMode} - перестать считать собранные плюшки";
string gameLegendPartFour = $"{keyBunusAllMap} - очистить карту {player} - игрок {bonusSpawnPediodUp}/{bonusSpawnPediodDown} - меньше/больше плюшек ";
string gameLegendPartFive = $"<- | -> Стрелки - управление {bonus} - плюшки собрано: {quantytyBonus} не собрано: {quantityBonusLeft}";
Console.SetCursorPosition(coordinateXgameLegendPartOne, coordinateYgamelegendPartOne);
Console.Write(gameLegendPartOne);
Console.ForegroundColor = ConsoleColor.Green;
Console.SetCursorPosition(coodrdinateXgameLegendPartTwo, coordinateYgameLegendPartTwo);
Console.Write(gameLegendPartTwo);
Console.SetCursorPosition(coordinateXgameLegendPartThree, coodrdinateYgameLegendPartThree);
Console.Write(gameLegendPartThree);
Console.SetCursorPosition(coordinateXgameLegendPartFour, coordinateYgameLegendPartFour);
Console.Write(gameLegendPartFour);
Console.SetCursorPosition(coordinateXgameLegendPartFive, coordinateYgameLegendPartFive);
Console.Write(gameLegendPartFive);
}
private static void EnterSetUpKeyFromUser(char[,] map, ConsoleKeyInfo pressedKey, char spaceMap, char bonusSpawnPediodUp, char bonusSpawnPeriodDown, char keyExit, char keyBunusAllMap, char keyGameMode, ref bool isPaintWallMode, ref bool isGameModeOn, ref int bonusesRangeSpawnPeriod, ref int quantytyBonus, ref bool isRunning)
{
int zeroBonusCountGameModeFalse = 0;
ConsoleKey spaceBar = ConsoleKey.Spacebar;
if (pressedKey.KeyChar == keyExit)
{
isRunning = false;
}
if (pressedKey.KeyChar == keyGameMode)
{
if (pressedKey.KeyChar == keyGameMode && isGameModeOn)
{
quantytyBonus = zeroBonusCountGameModeFalse;
isGameModeOn = false;
}
else
{
isGameModeOn = true;
}
}
else if (pressedKey.Key == spaceBar)
{
if (pressedKey.Key == spaceBar && isPaintWallMode)
{
isPaintWallMode = false;
}
else
{
isPaintWallMode = true;
}
}
else if (pressedKey.KeyChar == keyBunusAllMap)
{
RefreshMap(map, spaceMap, out bonusesRangeSpawnPeriod);
}
ChangeRangeSpawnPeriod(pressedKey, bonusSpawnPediodUp, bonusSpawnPeriodDown, ref bonusesRangeSpawnPeriod);
}
private static void ChangeRangeSpawnPeriod (ConsoleKeyInfo pressedKey, char bonusSpawnPediodUp, char bonusSpawnPeriodDown, ref int bonusesRangeSpawnPeriod)
{
int bonusUpRange = 1000;
int bonusDownRange = -500;
if (pressedKey.KeyChar == bonusSpawnPediodUp)
{
bonusesRangeSpawnPeriod += bonusUpRange;
}
else if (pressedKey.KeyChar == bonusSpawnPeriodDown)
{
if (bonusesRangeSpawnPeriod > bonusUpRange)
{
bonusesRangeSpawnPeriod += bonusDownRange;
}
}
}
private static void RefreshMap(char[,] map, char spaceMap, out int bonusesRangeSpawnPeriod)
{
bonusesRangeSpawnPeriod = 30000;
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
map[i, j] = spaceMap;
}
}
}
private static char GetDirection(char[,] map, ConsoleKeyInfo pressedKey, out int cursorPositionChangeX, out int cursorPositionChangeY, ref int cursorPositionX, ref int cursorPositionY)
{
cursorPositionChangeX = 0;
cursorPositionChangeY = 0;
ConsoleKey mooveUpKey = ConsoleKey.UpArrow;
ConsoleKey mooveDownKey = ConsoleKey.DownArrow;
ConsoleKey mooveLeftKey = ConsoleKey.LeftArrow;
ConsoleKey mooveRightKey = ConsoleKey.RightArrow;
if (pressedKey.Key == mooveUpKey)
{
cursorPositionChangeY--;
}
else if (pressedKey.Key == mooveDownKey)
{
cursorPositionChangeY++;
}
else if (pressedKey.Key == mooveLeftKey)
{
cursorPositionChangeX--;
}
else if (pressedKey.Key == mooveRightKey)
{
cursorPositionChangeX++;
}
return map[cursorPositionY + cursorPositionChangeY, cursorPositionX + cursorPositionChangeX];
}
private static void GetMoove(char[,] map, ConsoleKeyInfo pressedKey, bool isPaintWallMode, char mapWall, char bonus, ref int cursorPositionX, ref int cursorPositionY, ref bool isGameModeOn, ref int quantytyBonus)
{
char locationContent = GetDirection(map, pressedKey, out int cursorPositionChangeX, out int cursorPositionChangeY, ref cursorPositionX, ref cursorPositionY);
if (isGameModeOn == true && locationContent == bonus)
{
quantytyBonus++;
}
if (locationContent != mapWall)
{
char spaceMap = ' ';
if (isPaintWallMode == true)
{
map[cursorPositionY, cursorPositionX] = mapWall;
}
else
{
map[cursorPositionY, cursorPositionX] = spaceMap;
}
cursorPositionY += cursorPositionChangeY;
cursorPositionX += cursorPositionChangeX;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment