Last active
April 14, 2024 19:04
-
-
Save kyzalex/cb848b6a06505742384ef6cd675fbe55 to your computer and use it in GitHub Desktop.
NEW_Brave new world
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
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.CursorVisible = false; | |
char[,] map = { | |
{'#','#','#','#','#','#','#','#','#','#'}, | |
{'#','.','.','#','.','.','.','.','.','#'}, | |
{'#','.','.','#','.','.','.','.','.','#'}, | |
{'#','.','.','.','.','.','.','.','.','#'}, | |
{'#','#','#','#','#','#','#','#','#','#'}, | |
}; | |
int positionPlayerX = 1; | |
int positionPlayerY = 1; | |
int score = 0; | |
int maxScore = 22; | |
bool isOpen = true; | |
while (isOpen) | |
{ | |
Console.Clear(); | |
DrawMap(map); | |
Console.SetCursorPosition(positionPlayerX, positionPlayerY); | |
Console.Write('@'); | |
Console.SetCursorPosition(32, 0); | |
Console.Write($"Score : {score}"); | |
ConsoleKeyInfo pressedKey = Console.ReadKey(); | |
HandleInput(pressedKey, ref positionPlayerX, ref positionPlayerY, map, ref score, ref isOpen, maxScore); | |
} | |
} | |
static void DrawMap(char[,] array) | |
{ | |
for (int i = 0; i < array.GetLength(1); i++) | |
{ | |
for (int j = 0; j < array.GetLength(0); j++) | |
{ | |
Console.Write(array[j, i]); | |
} | |
Console.WriteLine(); | |
} | |
} | |
static void HandleInput(ConsoleKeyInfo pressedKey, ref int positionPlayerX, ref int positionPlayerY, char[,] map, ref int score, ref bool isOpen, int maxScore) | |
{ | |
int[] direction = GetDirection(pressedKey); | |
int nextPositionPlaeyrX = positionPlayerX + direction[0]; | |
int nextPositionPlayerY = positionPlayerY + direction[1]; | |
char nextCell = map[nextPositionPlaeyrX, nextPositionPlayerY]; | |
if (nextCell == ' ' || nextCell == '.') | |
{ | |
AllowToPass(out positionPlayerX, out positionPlayerY, nextPositionPlaeyrX, nextPositionPlayerY); | |
} | |
if (nextCell == '.') | |
{ | |
ReplaceSymbol(map, nextPositionPlaeyrX, nextPositionPlayerY); | |
GetPermissionToExit(ref score, ref isOpen, maxScore); | |
} | |
} | |
static void AllowToPass(out int positionPlayerX, out int positionPlayerY, int nextPositionPlaeyrX, int nextPositionPlayerY) | |
{ | |
positionPlayerX = nextPositionPlaeyrX; | |
positionPlayerY = nextPositionPlayerY; | |
} | |
static void ReplaceSymbol(char[,] map, int nextPositionPlaeyrX, int nextPositionPlayerY) | |
{ | |
map[nextPositionPlaeyrX, nextPositionPlayerY] = ' '; | |
} | |
static void GetPermissionToExit(ref int score, ref bool isOpen, int maxScore) | |
{ | |
score++; | |
if (score == maxScore) | |
{ | |
isOpen = false; | |
} | |
} | |
static int[] GetDirection(ConsoleKeyInfo pressedKey) | |
{ | |
ConsoleKey upArrowCommand = ConsoleKey.UpArrow; | |
ConsoleKey downArrowCommand = ConsoleKey.DownArrow; | |
ConsoleKey leftArrowCommand = ConsoleKey.LeftArrow; | |
ConsoleKey rightArrowCommand = ConsoleKey.RightArrow; | |
int[] direction = { 0, 0 }; | |
if (pressedKey.Key == upArrowCommand) | |
{ | |
direction[1] = -1; | |
} | |
else if (pressedKey.Key == downArrowCommand) | |
{ | |
direction[1] = 1; | |
} | |
else if (pressedKey.Key == leftArrowCommand) | |
{ | |
direction[0] = -1; | |
} | |
else if (pressedKey.Key == rightArrowCommand) | |
{ | |
direction[0] = 1; | |
} | |
return direction; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment