Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.5
I need Help with this code the character eats the walls. I need suggestions and ideas on how I can fix this problem, btw I'm trying to do an rpg game.
 
Thank you in advance
N.B I'm still in my very early game dev stages(first game).
 
This is the code:
for the map
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Map_v1._0
{
    class Map
    {
 
        public int XMax = 100;
        public int YMax = 57;
        public char[,] MapArray = new char[100, 57];
        public int XExit = 5;
        public int YExit = 5;
        public int XEntrance = -1;
        public int YEntrance = -1;
        public int[] MonsterArray = new int[50];
        public Random random = new Random(DateTime.Now.Millisecond);
        public int AmountOfCaves = 17;
        public int CaveCount = 0;
        public int DeathCount = 0;
 

        public void Generate_Run(Map map, Navigation player1)
        {
            // Clear rooms
            map.Generator_Clear(map, player1);
 
            // Generate Maze
            map.Generator_Maze(map, player1);
        }
 
        // map Generation
        public void Generator_Clear(Map map, Navigation player1)
        {
            for (int y = 0; y < YMax; y++)
            {
                for (int x = 0; x < XMax; x++)
                {
                    // Create dirt level clear it.
                    map.MapArray[x, y] = '█';
 
                }
            }
        }
 
        public void Generator_Maze(Map map, Navigation player1)
        {
            int AmountOfCaves = 17;
            int CaveCount = 0;
            int DeathCount = 0;
 
            while (CaveCount < AmountOfCaves)
            {
                // Increment 1 cave
                CaveCount++;
 
                // Create a random varible
                Random randy = new Random(DateTime.Now.Millisecond);
 
                // Random a start location
                int x = randy.Next(0, map.XMax);
                int y = randy.Next(0, map.YMax);
                bool Dead = false;
 
                // Set the direction
                int Direction = 1;
                int count = 0;
 
                // Move a random direction, if Direction isn't 0 continue
                while (count < 1000)
                {
                    // Clear the space
                    if (y > 1 && y < map.YMax - 1 && Dead == false)
                        map.MapArray[x, y] = ' ';
 
                    // Set the exit
                    map.MapArray[map.XExit, map.YExit] = 'E';
 
                    // Bools
                    bool Up = false;
                    bool Right = false;
                    bool Down = false;
                    bool Left = false;
                    bool UUp = false;
                    bool RRight = false;
                    bool DDown = false;
                    bool LLeft = false;
                    bool Moved = false;
                    char LastMove = ' ';
                    Dead = false;
 
                    // Check spaces
                    // Move only selected direction

                    while (Moved == false)
                    {
                        // Check spaces
                        // If we're within bounds
                        if ((y - 1 > 1 && map.MapArray[x, y - 1] == '█') || LastMove == 'D')
                        {
                            Up = true;
                        }
                        // Right
                        // If we're within bounds
                        if ((x + 1 < map.XMax - 1 && map.MapArray[x + 1, y] == '█') || LastMove == 'L')
                        {
                            Right = true;
                        }
                        // Down
                        // If we're within bounds
                        if ((y + 1 < map.YMax - 1 && map.MapArray[x, y + 1] == '█') || LastMove == 'U')
                        {
                            Down = true;
                        }
                        // Left
                        // If we're within bounds
                        if ((x - 1 > 1 && map.MapArray[x - 1, y] == '█') || LastMove == 'R')
                        {
                            Left = true;
                        }
                        // 1 across
                        // If we're within bounds
                        if ((y - 2 > 1 && map.MapArray[x, y - 2] == '█') || LastMove == 'D')
                        {
                            UUp = true;
                        }
                        // Right
                        // If we're within bounds
                        if ((x + 2 < map.XMax - 1 && map.MapArray[x + 2, y] == '█') || LastMove == 'L')
                        {
                            RRight = true;
                        }
                        // Down
                        // If we're within bounds
                        if ((y + 2 < map.YMax - 1 && map.MapArray[x, y + 2] == '█') || LastMove == 'U')
                        {
                            DDown = true;
                        }
                        // Left
                        // If we're within bounds
                        if ((x - 2 > 1 && map.MapArray[x - 2, y] == '█') || LastMove == 'R')
                        {
                            LLeft = true;
                        }
 

                        // Set the direction for this loop
                        Direction = randy.Next(1, 5);
 
                        // Move accordingly
                        switch (Direction)
                        {
                            case 0:
                                break;
                            // Up
                            case 1:
                                // If we're within bounds
                                if (UUp == true)
                                {
                                    y--;
                                    Moved = true;
                                    LastMove = 'U';
                                }
                                break;
                            // Right
                            case 2:
                                // If we're within bounds
                                if (RRight == true)
                                {
                                    x++;
                                    Moved = true;
                                    LastMove = 'R';
                                }
                                break;
                            // Down
                            case 3:
                                // If we're within bounds
                                if (DDown == true)
                                {
                                    y++;
                                    Moved = true;
                                    LastMove = 'D';
                                }
                                break;
                            // Left
                            case 4:
                                // If we're within bounds
                                if (LLeft == true)
                                {
                                    x--;
                                    Moved = true;
                                    LastMove = 'L';
                                }
                                break;
                        }
 
                        if ((Up == false && Right == false && Down == false && Left == false) ||
                            (UUp == false && RRight == false && DDown == false && LLeft == false))
                        {
                            Moved = true;
                            map.MapArray[x, y] = ' ';
                            x = randy.Next(0, map.XMax);
                            y = randy.Next(0, map.YMax);
                            Dead = true;
                            DeathCount++;
                        }
                    }
 
                    count++;
 
                }
 
            }
 
            while (CaveCount < AmountOfCaves)
            {
                // Increment 1 cave
                CaveCount++;
 
                // Create a random varible
                Random randy = new Random(DateTime.Now.Millisecond);
 
                // Random a start location
                int x = randy.Next(0, map.XMax);
                int y = randy.Next(0, map.YMax);
                bool Dead = false;
 
                // Set the direction
                int Direction = 1;
                int count = 0;
 
                // Move a random direction, if Direction isn't 0 continue
                while (count < 1000)
                {
                    // Clear the space
                    if (y > 1 && y < map.YMax - 1 && Dead == false)
                        map.MapArray[x, y] = ' ';
 
                    // Set the exit
                    map.MapArray[map.XExit, map.YExit] = 'E';
 
                    // Bools
                    bool Up = false;
                    bool Right = false;
                    bool Down = false;
                    bool Left = false;
                    bool UUp = false;
                    bool RRight = false;
                    bool DDown = false;
                    bool LLeft = false;
                    bool Moved = false;
                    char LastMove = ' ';
                    Dead = false;
 
                    // Check spaces
                    // Move only selected direction

                    while (Moved == false)
                    {
 
                        // Check spaces
                        // If we're within bounds
                        if ((y - 1 > 1 && map.MapArray[x, y - 1] == '█') || LastMove == 'D')
                        {
                            Up = true;
                        }
                        // Right
                        // If we're within bounds
                        if ((x + 1 < map.XMax - 1 && map.MapArray[x + 1, y] == '█') || LastMove == 'L')
                        {
                            Right = true;
                        }
                        // Down
                        // If we're within bounds
                        if ((y + 1 < map.YMax - 1 && map.MapArray[x, y + 1] == '█') || LastMove == 'U')
                        {
                            Down = true;
                        }
                        // Left
                        // If we're within bounds
                        if ((x - 1 > 1 && map.MapArray[x - 1, y] == '█') || LastMove == 'R')
                        {
                            Left = true;
                        }
                        // 1 across
                        // If we're within bounds
                        if ((y - 2 > 1 && map.MapArray[x, y - 2] == '█') || LastMove == 'D')
                        {
                            UUp = true;
                        }
                        // Right
                        // If we're within bounds
                        if ((x + 2 < map.XMax - 1 && map.MapArray[x + 2, y] == '█') || LastMove == 'L')
                        {
                            RRight = true;
                        }
                        // Down
                        // If we're within bounds
                        if ((y + 2 < map.YMax - 1 && map.MapArray[x, y + 2] == '█') || LastMove == 'U')
                        {
                            DDown = true;
                        }
                        // Left
                        // If we're within bounds
                        if ((x - 2 > 1 && map.MapArray[x - 2, y] == '█') || LastMove == 'R')
                        {
                            LLeft = true;
                        }
 
                    }
 
                    // Set the direction for this loop
                    Direction = randy.Next(1, 5);
 
                    // Move accordingly
                    switch (Direction)
                    {
                        // Up
                        case 1:
                            // If we're within bounds
                            if (UUp == true)
                            {
                                y--;                                
                                Moved = true;
                                LastMove = 'U';
                            }
                            break;
                        // Right
                        case 2:
                            // If we're within bounds
                            if (RRight == true)
                            {
                                x++;
                                Moved = true;
                                LastMove = 'R';
                            }
                            break;
                        // Down
                        case 3:
                            // If we're within bounds
                            if (DDown == true)
                            {
                                y++;
                                Moved = true;
                                LastMove = 'D';
                            }
                            break;
                        // Left
                        case 4:
                            // If we're within bounds
                            if (LLeft == true)
                            {
                                x--;
                                Moved = true;
                                LastMove = 'L';
                            }
                            break;
                    }
 

                    if ((Up == false && Right == false && Down == false && Left == false) ||
    (UUp == false && RRight == false && DDown == false && LLeft == false))
                    {
                        Moved = true;
                        map.MapArray[x, y] = ' ';
                        x = randy.Next(0, map.XMax);
                        y = randy.Next(0, map.YMax);
                        Dead = true;
                        DeathCount++;
                    }
 

                }
                count++;
 
            }
 

 
        }
 

 
        // Display map
        public void Generator_Display(Map map, Navigation player1)
        {
 
            for (int y = 0; y < map.YMax; y++)
            {
                for (int x = 0; x < map.XMax; x++)
                {
                    if (map.MapArray[x, y] == '@')
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.Write(map.MapArray[x, y]);
                        Console.ForegroundColor = ConsoleColor.White;
                    }
                    else
                        Console.Write(map.MapArray[x, y]);
 
                }
                Console.WriteLine(" ");
            }
        }
    }
}
 

 
The main program, which uses character movement by using cursor controls.
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Map_v1._0
{
    class Program
    {
 
        public static Navigation Player { get; set; } // Player string " " that will be moved around
 

        static void Main(string[] args)
            {
                
 
                LoadGame();
                
                ConsoleKeyInfo keyInfo;
                while ((keyInfo = Console.ReadKey(true)).Key != ConsoleKey.Escape)
                {
                    switch (keyInfo.Key)
                    {
                        case ConsoleKey.UpArrow:
                            MoveHero(0, -1);
                            break;
 
                        case ConsoleKey.RightArrow:
                            MoveHero(1, 0);
                            break;
 
                        case ConsoleKey.DownArrow:
                            MoveHero(0, 1);
                            break;
 
                        case ConsoleKey.LeftArrow:
                            MoveHero(-1, 0);
                            break;   
                        case ConsoleKey.Enter:
                            int MapLevels = 5;
                            Map[] map = new Map[MapLevels];
 
                            Map currentmap = new Map();
 
                           currentmap.Generate_Run(currentmap, Player);
                           currentmap.Generator_Display(currentmap, Player);
                            break;
                        
                    }
                }
            }
 
            /// <summary>
            /// Paint the new hero
            /// </summary>
            static void MoveHero(int x, int y)
            {
                Navigation newHero = new Navigation()
                {
                    Up = Player.Up + x,
                    Down = Player.Down + y
                };
 
                if (CanMove(newHero))
                {
                    RemoveHero();
 
                    //Console.BackgroundColor = My_Player;
                    Console.SetCursorPosition(newHero.Up, newHero.Down);
                    Console.Write("i");
 
                    Player = newHero;
                }
            }
 
            /// <summary>
            /// Overpaint the old hero
            /// </summary>
            static void RemoveHero()
            {
                //Console.BackgroundColor = BACKGROUND_COLOR;
                Console.SetCursorPosition(Player.Up, Player.Down);
                Console.Write(" ");                
                
            }
            
 
            /// <summary>
            /// Make sure that the new coordinate is not placed outside the
            /// console window (since that will cause a runtime crash
            /// </summary>
            static bool CanMove(Navigation c)
            {
                if (c.Up < 0 || c.Up >= Console.WindowWidth)
                    return false;
 
                if (c.Down < 0 || c.Down >= Console.WindowHeight)
                    return false;
 
                return true;
            }
 
            /// <summary>
            /// Paint a background color
            /// </summary>
            /// <remarks>
            /// It is very important that you run the Clear() method after
            /// changing the background color since this causes a repaint of the background
            /// </remarks>
            static void SetBackgroundColor()
            {
               // Console.BackgroundColor = BACKGROUND_COLOR;
                Console.Clear(); //Important!
            }
 
            
            static void LoadGame()
            {
                SetBackgroundColor();
 
                Player = new Navigation()
                {
                    Up = 0,
                    Down = 0
                };
 
                MoveHero(0, 0);
 
            }
        }
    }
 

 
The Navigation class
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Map_v1._0
{    
 
    class Navigation
        {
            
            public int Up { get; set; } //Left x+ 1 will move player to the right & x-1 will move the obj left
            public int Down { get; set; } //Top and bottom axis y+1 up and y- 1 bottom
        }
}
 
Posted 14-Feb-13 8:32am
Max-911407
Edited 14-Feb-13 20:46pm
v4
Comments
richcb at 14-Feb-13 14:42pm
   
If you are stuck, post the code you are stuck on so others can help you.
Sergey Alexandrovich Kryukov at 14-Feb-13 14:42pm
   
Stuck with what, exactly? What have you done already? And keep in mind that it could be a too big topic for this forum...
—SA
Rob Grainger at 14-Feb-13 16:09pm
   
This seems to be totally invalid C# code. For example "public void Generator_Maze(Map map), Player player1)" has an extra parameter outside the original parentheses ('(', ')'). A C# compiler will choke loudly at these.
Maxwell mapako at 15-Feb-13 1:33am
   
Sorry about that guys, I have been trying out some new things so i'll just fix those errors quickly, Biggest problem is I don't fully understand all of the map generation code. If maybe I can get a link for a tutorial would really help a lot
rlloyd2001 at 14-Feb-13 18:36pm
   
What do you want to link together. The Map class is in the same namespace: "FinalMapTest" which means you can use it in the Program class. However, you have compile/syntax errors in your Map class that need correcting so that project will compile.
Maxwell mapako at 15-Feb-13 1:44am
   
Yes, I tried to parse my player class which which I only sent the cursor position.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

There seem to be a few things that are a bit weird with that code, but I'm going to focus on what causes the Hero to eat the walls.
 
In your method static bool CanMove(Navigation c), the only constraints applied are checking if you Hero moves off the sceren, you also need to check if they've hit a wall. Changing it to something like this might do the trick for you;
static bool CanMove(Navigation c, Map map)
{
    if (c.Up < 0 || c.Up >= Console.WindowWidth)
        return false;
 
    if (c.Down < 0 || c.Down >= Console.WindowHeight)
        return false;
 
    // Check if the map is clear of everything (this needs to be refined to cater for exists and such)
    if (map.MapArray[c.Up, c.Down] != ' ')
        return false;
 
    return true;
}
 
This also means that you can't have your Hero starting at (0, 0) as that is a wall, I've therefore also amended the LoadGame to cater for this, below is my rewritten version of your Program class;
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Map_v1._0
{
    class Program
    {
 
        public static Navigation Player { get; set; } // Player string " " that will be moved around

 
        static void Main(string[] args)
        {
 

 
            int MapLevels = 5;
            Map[] map = new Map[MapLevels];
 
            Map currentmap = new Map();
 
            currentmap.Generate_Run(currentmap, Player);
            currentmap.Generator_Display(currentmap, Player);
 
            LoadGame(currentmap);
 
            ConsoleKeyInfo keyInfo;
            while ((keyInfo = Console.ReadKey(true)).Key != ConsoleKey.Escape)
            {
                switch (keyInfo.Key)
                {
                    case ConsoleKey.UpArrow:
                        MoveHero(0, -1, currentmap);
                        break;
 
                    case ConsoleKey.RightArrow:
                        MoveHero(1, 0, currentmap);
                        break;
 
                    case ConsoleKey.DownArrow:
                        MoveHero(0, 1, currentmap);
                        break;
 
                    case ConsoleKey.LeftArrow:
                        MoveHero(-1, 0, currentmap);
                        break;
                }
            }
        }
 
        /// <summary>
        /// Paint the new hero
        /// </summary>
        static void MoveHero(int x, int y, Map map)
        {
            Navigation newHero = new Navigation()
            {
                Up = Player.Up + x,
                Down = Player.Down + y
            };
 
            if (CanMove(newHero, map))
            {
                RemoveHero(map);
 
                //Console.BackgroundColor = My_Player;
                Console.SetCursorPosition(newHero.Up, newHero.Down);
                Console.Write("i");
 
                Player = newHero;
            }
        }
 
        /// <summary>
        /// Overpaint the old hero
        /// </summary>
        static void RemoveHero(Map map)
        {
            //Console.BackgroundColor = BACKGROUND_COLOR;
            Console.SetCursorPosition(Player.Up, Player.Down);
 
            Console.Write(map.MapArray[Player.Up, Player.Down]);
        }
 

        /// <summary>
        /// Make sure that the new coordinate is not placed outside the
        /// console window (since that will cause a runtime crash
        /// </summary>
        static bool CanMove(Navigation c, Map map)
        {
            if (c.Up < 0 || c.Up >= Console.WindowWidth)
                return false;
 
            if (c.Down < 0 || c.Down >= Console.WindowHeight)
                return false;
 
            if (map.MapArray[c.Up, c.Down] == 'E')
                return true;
 
            // See if map is clear
            if (map.MapArray[c.Up, c.Down] != ' ')
                return false;
 
            return true;
        }
 
        /// <summary>
        /// Paint a background color
        /// </summary>
        /// <remarks>
        /// It is very important that you run the Clear() method after
        /// changing the background color since this causes a repaint of the background
        /// </remarks>
        static void SetBackgroundColor()
        {
            // Console.BackgroundColor = BACKGROUND_COLOR;
            Console.Clear(); //Important!
        }
 

        static void LoadGame(Map map)
        {
            //SetBackgroundColor();

            Player = new Navigation()
            {
                Up = 0,
                Down = 0
            };
 

            for (int r = 0; r < 10; ++r)
            {
                for (int c = 0; c < 10; ++c)
                {
                    if (map.MapArray[c, r] == ' ')
                        MoveHero(c, r, map);
                }
            }
 
        }
    }
}
 

I know game programming can be a daunting experience, but keep at it as it is very rewarding when you make something that works Smile | :)
 
Hope this helps,
Fredrik
  Permalink  
v2
Comments
Max-911 at 16-Feb-13 9:39am
   
Thank you very much Fredrik!! :)
Fredrik Bornander at 16-Feb-13 10:23am
   
Glad I could help.
Max-911 at 16-Feb-13 10:37am
   
Ok last question, I can't over write char 'E' which is my exit point on the map with the co-ordinate 5,5 though I released that it's because of the if (map.MapArray[c.Up, c.Down] != ' ') return false, I've tried a few things that failed. e.g if (map.MapArray[c.Up, c.Down] != ' ' || map.MapArray[c.Up, c.Down] != 'E') return false;
Fredrik Bornander at 16-Feb-13 10:54am
   
You need to replace the position on the map where the player has been with the map data, not with an empty space like you do now. Also, like you pointed out you need to change the if-statement to allow the player to walk on the exit.
 
I've updated my answer with a new Program class that caters for this.
Max-911 at 16-Feb-13 11:52am
   
Thanks again!
Fredrik Bornander at 16-Feb-13 12:00pm
   
Again, I'm glad to have helped :)
Max-911 at 20-Feb-13 1:46am
   
I ran into a little bug, every time I start the game the "i" won't move at all till I press enter to gen the map again, I'm not really sure why it does that.:( Problem seems to the map array having default char values.
Fredrik Bornander at 20-Feb-13 3:32am
   
I think you will run into more than a few bugs with that code, it looked a bit messy.
If you feel you're confident in Object Oriented programming, try rewriting it using OO-principles and I'm sure it will be easier to follow.
Max-911 at 20-Feb-13 5:28am
   
I managed to fix the problem, thanks I'll consider rewriting the code.
Fredrik Bornander at 20-Feb-13 14:51pm
   
Cool, if you're running into more issues then (and I'm not saying CodeProject isn't a good place to ask) these sites are really good for asking game development related questions;
http://gamedev.stackexchange.com/
http://gamedev.net
Max-911 at 20-Feb-13 15:08pm
   
Awesome, it's always good to have more than 1 source. Thank you for your patients!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 16 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid