i cant figure out whats wrong with my code
using System;
namespace The_Maze_Game
{
class Program
{
static void Main(string[] args)
{
// set appearance
Console.Title = "Maze Game";
Console.ForegroundColor = ConsoleColor.Green;
// Correct initialization of a 2D character array
while (true)
{
char[,] map =
{
{'#','#','#','#','#','#','#','#','#'},
{'#','0','0','0','0','0','0','0','#'},
{'#','#','#','0','0','0','0','0','#'},
{'#','#','@','0','0','0','0','#','#'},
{'#','0','0','0','0','0','0','#','#'},
{'#','#','#','#','#','#','#','#','#'}
};
int playerRow = 3;
int playerCol = 2;
for (int i = 0; i < map.GetLength(0); i++){
for (int j = 0; j < map.GetLength(1); j++){
Console.Write(map[i, j]);}
Console.WriteLine();}
ConsoleKeyInfo keyInfo = Console.ReadKey(intercept: true);
map[playerCol, playerRow] = '0';
if (keyInfo.Key == ConsoleKey.W){
playerCol++;}
if (keyInfo.Key == ConsoleKey.A){
playerRow--;}
if (keyInfo.Key == ConsoleKey.S){
playerCol--;}
if (keyInfo.Key == ConsoleKey.D){
playerRow++;}
map[playerCol, playerRow] = '@';
// waits before closing program
Console.ReadKey();}
}
}
}
5 Replies
so, for starters please use $code for nicer formatting
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/and, what are you expecting to happen, and what's actually happening?
a few notes:
1) you have col and row inverted
map[playerCol, playerRow]
should be map[playerRow, playerCol]
2) you also have it inverted in your if's... d should be col++ s should be row++ a should be col-- and w row --
3) Assuming # are walls u need logic so it doesn't cross the walls, otherwise u will replace walls with 0
4) so instead of modifying the the player position before the key is pressed to 0, save the row/col and modify it after u check if the player can move to the direction it tried to.
thx