Skip to content

Instantly share code, notes, and snippets.

@trayforyou
Created April 26, 2024 07:15
Show Gist options
  • Save trayforyou/ba3989f4ef5cc843f533a092195d15b1 to your computer and use it in GitHub Desktop.
Save trayforyou/ba3989f4ef5cc843f533a092195d15b1 to your computer and use it in GitHub Desktop.
using System;
namespace Methods5
{
internal class Program
{
static void Main(string[] args)
{
bool isOpen = true;
char[,] charsOfMap =
{
{'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#',},
{'#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',},
{'#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',},
{'#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',},
{'#',' ',' ',' ','#',' ',' ',' ',' ','f',' ',' ',' ',' ',' ',' ','#',},
{'#',' ','f',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',},
{'#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',},
{'#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',},
{'#',' ',' ','f','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',},
{'#',' ',' ',' ',' ',' ',' ',' ','f',' ',' ',' ',' ',' ',' ',' ','#',},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',},
{'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#',},
};
char player = '@';
int playerPositionX = 3;
int playerPositionY = 2;
int fruits = 0;
while (isOpen)
{
charsOfMap[playerPositionY, playerPositionX] = player;
DrawMap(charsOfMap);
Console.Write($"\n\nФруктов в мешке: {fruits}.");
MovePlayer(player, charsOfMap, ref playerPositionX, ref playerPositionY, ref fruits);
}
}
private static void GetNewPosition(ref int newPositionX, ref int newPositionY)
{
const ConsoleKey KeyMoveRight = ConsoleKey.RightArrow;
const ConsoleKey KeyMoveLeft = ConsoleKey.LeftArrow;
const ConsoleKey KeyMoveUp = ConsoleKey.UpArrow;
const ConsoleKey KeyMoveDown = ConsoleKey.DownArrow;
ConsoleKeyInfo userInput = Console.ReadKey();
switch (userInput.Key)
{
case KeyMoveDown:
newPositionY++;
break;
case KeyMoveUp:
newPositionY--;
break;
case KeyMoveLeft:
newPositionX--;
break;
case KeyMoveRight:
newPositionX++;
break;
}
}
private static void MovePlayer(char player, char[,] charsOfMap, ref int playerPositionX, ref int playerPositionY, ref int fruits)
{
int newPositionX = playerPositionX;
int newPositionY = playerPositionY;
char wallSymbol = '#';
char emptynessSymbol = ' ';
GetNewPosition(ref newPositionX, ref newPositionY);
TakeFruitIfCan(charsOfMap, newPositionY, newPositionX, ref fruits);
if (charsOfMap[newPositionY, newPositionX] != wallSymbol)
{
charsOfMap[playerPositionY, playerPositionX] = emptynessSymbol;
playerPositionX = newPositionX;
playerPositionY = newPositionY;
}
}
private static void TakeFruitIfCan(char[,] charsOfMap, int fruitPositionY, int fruitPositionX, ref int fruits)
{
char fruitSymbol = 'f';
if (charsOfMap[fruitPositionY, fruitPositionX] == fruitSymbol)
{
fruits++;
}
}
private static void DrawMap(char[,] charsOfMap)
{
Console.Clear();
for (int i = 0; i < charsOfMap.GetLength(0); i++)
{
for (int j = 0; j < charsOfMap.GetLength(1); j++)
{
Console.Write(charsOfMap[i, j]);
}
Console.WriteLine();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment