-
-
Save GigaOrts/472c5b9371363abca7a0e3132b4db2c7 to your computer and use it in GitHub Desktop.
ЗАДАНИЕ Brave new world narcobeta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | |
Random random = new Random(); | |
int mapGameTerritoryPositionMiniumumY = 1; | |
int mapGameTerritoryPositionMaximumY = map.GetLength(0); | |
int mapGameTerritoryPositionMiniumumX = 1; | |
int mapGameTerritoryPositionMaximumX = map.GetLength(1); | |
int cursorPositionX = random.Next(mapGameTerritoryPositionMiniumumX, mapGameTerritoryPositionMaximumX); | |
int cursorPositionY = random.Next(mapGameTerritoryPositionMiniumumY, mapGameTerritoryPositionMaximumY); | |
int quantytyBonus = 0; | |
bool isPaintWallMode = true; | |
bool isGameModeOn = true; | |
bool isRuning = true; | |
ConsoleColor defoultBackgroundColor = Console.BackgroundColor; | |
Console.BackgroundColor = defoultBackgroundColor; | |
Console.CursorVisible = false; | |
RefreshMap(out int bonusesRangeSpawnPeriod, map); | |
while (isRuning) | |
{ | |
ConsoleKeyInfo pressedKey = default; | |
if (Console.KeyAvailable) | |
pressedKey = Console.ReadKey(); | |
EnterSetUpKeyFromUser(map, pressedKey, ref isPaintWallMode, ref isGameModeOn, ref bonusesRangeSpawnPeriod, ref quantytyBonus, ref isRuning); | |
TryMoove(ref cursorPositionX, ref cursorPositionY, map, pressedKey, ref isGameModeOn, ref quantytyBonus, isPaintWallMode); | |
WritePlayer(cursorPositionX, cursorPositionY, map); | |
int wallMetres = 0; | |
int quantityBonusLeft = 0; | |
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(ref cursorPositionX, ref cursorPositionY, map, randomArray, ref quantytyBonus, ref wallMetres, ref quantityBonusLeft, randomColorArray); | |
Thread.Sleep(100); | |
} | |
} | |
private static char[,] UpdateAllMap(ref int cursorPositionX, ref int cursorPositionY, char[,] map, int[,] randomArray, ref int quantytyBonus, ref int wallMetres, ref int quantityBonusLeft, int[,] rndColorMassive) | |
{ | |
char mapWall = 'X'; | |
char spaceMap = ' '; | |
WritePlayer(cursorPositionX, cursorPositionY, map); | |
for (int i = 0; i < map.GetLength(0); i++) | |
{ | |
for (int j = 0; j < map.GetLength(1); j++) | |
{ | |
ChoseRandomMapColor(rndColorMassive, i, j); | |
WritePrimeterWall(map, i, j); | |
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, ref randomArray, ref quantityBonusLeft, i, j); | |
} | |
} | |
WriteLegend(map, wallMetres, quantytyBonus, quantityBonusLeft); | |
return map; | |
} | |
private static void WritePlayer(int cursorPositionX, int cursorPositionY, char[,] map) | |
{ | |
char player = 'i'; | |
Console.ForegroundColor = ConsoleColor.Yellow; | |
map[cursorPositionY, cursorPositionX] = player; | |
Console.SetCursorPosition(cursorPositionX, cursorPositionY); | |
Console.Write(player); | |
} | |
private static void WriteBonuses(char[,] map, ref int[,] randomArray, ref int quantityBonusLeft, int coordinateY, int coordinateX) | |
{ | |
char bonus = '@'; | |
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 = 'X'; | |
int[] mapWallIndexes = { 0, map.GetLength(1) - 1 }; | |
int coordinatWallY = map.GetLength(0) - 1; | |
for (int k = 0; k < mapWallIndexes.Length; k++) | |
{ | |
if (coordinateY == mapWallIndexes[k] || coordinateX == mapWallIndexes[k]) | |
{ | |
map[coordinateY, coordinateX] = mapWall; | |
} | |
} | |
if (coordinateY == coordinatWallY) | |
{ | |
map[coordinateY, coordinateX] = mapWall; | |
} | |
} | |
private static void ChoseRandomMapColor(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 k = 0; k < colorForegroundGreen.Length; k++) | |
{ | |
if (rndColorMassive[coordinateY, coordinateX] == colorForegroundGreen[k]) | |
{ | |
Console.ForegroundColor = ConsoleColor.Green; | |
} | |
} | |
} | |
private static void WriteLegend(char[,] map, int wallMetres, int quantytyBonus, int quantityBonusLeft) | |
{ | |
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 = $"X - стена space - начать или перестать строить стену с - выход"; | |
string gameLegendPartThree = $"{wallMetres - wallAllBasePerimetrLenght} метров стены построено g - перестать считать собранные плюшки"; | |
string gameLegendPartFour = $"M - очистить карту i - игрок q/w - меньше/больше плюшек "; | |
string gameLegendPartFive = $"<- | -> Стрелки - управление @ - плюшки собрано: {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, ref bool isPaintWallMode, ref bool isGameModeOn, ref int bonusesRangeSpawnPeriod, ref int quantytyBonus, ref bool isRunning) | |
{ | |
int zeroBonusCountGameModeFalse = 0; | |
char keyExit = 'c'; | |
char keyBunusAllMap = 'm'; | |
char keyGameMode = 'g'; | |
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(out bonusesRangeSpawnPeriod, map); | |
} | |
ChangeRangeSpawnPeriod(pressedKey, ref bonusesRangeSpawnPeriod); | |
} | |
private static void ChangeRangeSpawnPeriod(ConsoleKeyInfo pressedKey, ref int bonusesRangeSpawnPeriod) | |
{ | |
char bonusSpawnPediodUp = 'q'; | |
char bonusSpawnPeriodDown = 'w'; | |
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(out int bonusesRangeSpawnPeriod, char[,] map) | |
{ | |
char spaceMap = ' '; | |
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(ref int cursorPositionX, ref int cursorPositionY, char[,] map, ConsoleKeyInfo pressedKey, out int cursorPositionChangeX, out int cursorPositionChangeY) | |
{ | |
cursorPositionChangeX = 0; | |
cursorPositionChangeY = 0; | |
ConsoleKey upArrow = ConsoleKey.UpArrow; | |
ConsoleKey downArrow = ConsoleKey.DownArrow; | |
ConsoleKey leftArrow = ConsoleKey.LeftArrow; | |
ConsoleKey rightArrow = ConsoleKey.RightArrow; | |
if (pressedKey.Key == upArrow) | |
{ | |
cursorPositionChangeY--; | |
} | |
else if (pressedKey.Key == downArrow) | |
{ | |
cursorPositionChangeY++; | |
} | |
else if (pressedKey.Key == leftArrow) | |
{ | |
cursorPositionChangeX--; | |
} | |
else if (pressedKey.Key == rightArrow) | |
{ | |
cursorPositionChangeX++; | |
} | |
return map[cursorPositionY + cursorPositionChangeY, cursorPositionX + cursorPositionChangeX]; | |
} | |
private static void TryMoove(ref int cursorPositionX, ref int cursorPositionY, char[,] map, ConsoleKeyInfo pressedKey, ref bool isGameModeOn, ref int quantytyBonus, bool isPaintWallMode) | |
{ | |
char mapWall = 'X'; | |
char bonus = '@'; | |
char locationContent = GetDirection(ref cursorPositionX, ref cursorPositionY, map, pressedKey, out int cursorPositionChangeX, out int cursorPositionChangeY); | |
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