Last active
June 4, 2025 17:58
-
-
Save Strelok78/c155a7c0c0a45284a8bbad3ada8a838d to your computer and use it in GitHub Desktop.
Console game (move symbol in symbol map)
This file contains hidden or 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.IO; | |
namespace iJunior_Practice | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
char wallSymbol = '#'; | |
char playerSymbol = '@'; | |
char[,] map; | |
string mapFilePath = "map.txt"; | |
int playerX = 1; | |
int playerY = 1; | |
Console.CursorVisible = false; | |
map = ReadMap(mapFilePath); | |
while (true) | |
{ | |
Console.Clear(); | |
Console.ForegroundColor = ConsoleColor.DarkRed; | |
DrawMap(map); | |
Console.ForegroundColor = ConsoleColor.Green; | |
Console.SetCursorPosition(playerX, playerY); | |
Console.Write(playerSymbol); | |
ConsoleKeyInfo pressedKey = Console.ReadKey(); | |
GetDirection(pressedKey, out int offsetX, out int offsetY); | |
Move(map, ref playerX, ref playerY, offsetX, offsetY); | |
} | |
} | |
static char[,] ReadMap(string filePath) | |
{ | |
string[] fileLines = File.ReadAllLines(filePath); | |
int maxRowLength = GetMaxRowLength(fileLines); | |
char[,] map = new char[maxRowLength, fileLines.Length]; | |
for (int y = 0; y < fileLines.Length; y++) | |
{ | |
for (int x = 0; x < maxRowLength; x++) | |
{ | |
if (x < fileLines[y].Length) | |
{ | |
map[x, y] = fileLines[y][x]; | |
} | |
} | |
} | |
return map; | |
} | |
static int GetMaxRowLength(string[] fileLines) | |
{ | |
int maxRowLength = 0; | |
foreach (string line in fileLines) | |
{ | |
if (line.Length > maxRowLength) | |
{ | |
maxRowLength = line.Length; | |
} | |
} | |
return maxRowLength; | |
} | |
static void DrawMap(char[,] map) | |
{ | |
for (int y = 0; y < map.GetLength(1); y++) | |
{ | |
for (int x = 0; x < map.GetLength(0); x++) | |
{ | |
Console.Write(map[x, y]); | |
} | |
Console.WriteLine(); | |
} | |
} | |
static void GetDirection(ConsoleKeyInfo pressedKey, out int offsetX, out int offsetY) | |
{ | |
offsetX = 0; | |
offsetY = 0; | |
switch (pressedKey.Key) | |
{ | |
case ConsoleKey.LeftArrow: | |
offsetX = -1; | |
break; | |
case ConsoleKey.RightArrow: | |
offsetX = 1; | |
break; | |
case ConsoleKey.UpArrow: | |
offsetY = -1; | |
break; | |
case ConsoleKey.DownArrow: | |
offsetY = 1; | |
break; | |
case ConsoleKey.Spacebar: | |
Console.Clear(); | |
Console.WriteLine("Игра завершена."); | |
Environment.Exit(0); | |
break; | |
} | |
} | |
static void Move(char[,] map, ref int playerX, ref int playerY, int offsetX, int offsetY) | |
{ | |
int newX = playerX + offsetX; | |
int newY = playerY + offsetY; | |
if (IsCellAvailable(map, newX, newY)) | |
{ | |
playerX = newX; | |
playerY = newY; | |
} | |
} | |
static bool IsCellAvailable(char[,] map, int x, int y) | |
{ | |
return map[x, y] == ' '; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment