Question Editing List items

Developer917

New member
Joined
Dec 3, 2020
Messages
1
Programming Experience
1-3
So I'm currently working on a game for school, I'll save you the details but basically its a tile based game.
Since I'm only allowed to make this in VS im struggling with how to make a proper working system to load in different levels of 10x10 maps.
I'm trying to work with a list since an Array wasn't working for me either, I have a list of <Tiles> which is a class I programmed.
My question is how can I edit an item within a list, beceause if do _level1.Add(objTile), I just end uw with a blank tile.
Is there a way I can change the Color property of the tiles separatly for each tile?

Thanks to anyone for taking you time to reply to my question, you're awesome!
 
C# console tile game:
using System;

namespace TilesGameCSharp
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Game game = new Game();
            game.ShowIntro();
            game.Play();
        }
    }

    public class Game
    {
        private readonly Level[] Levels = new Level[10];
        private const int USER_GAVE_UP = -1;
        private const int USER_WANTS_HELP = -2;

        public Game()
        {
            for (int i = 0; i < 10; i++)
            {
                Levels[i] = new Level() { Number = i + 1 };
                Levels[i].SetRandomColors(i + 1);
            }
        }

        public void ShowIntro()
        {
            Console.Clear();
            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("===[Tiles game]===\n");
            Console.WriteLine("Remember the numbers for each color!\n");
            for (int c = 1; c < 15; c++)
            {
                Console.BackgroundColor = (ConsoleColor)c;
                Console.ForegroundColor = ConsoleColor.Black;
                Console.Write(Enum.GetName(typeof(ConsoleColor), c).PadRight(20));
                Console.ForegroundColor = ConsoleColor.White;
                Console.BackgroundColor = ConsoleColor.Black;
                Console.Write("= {0}\n", c);
            }
            Console.WriteLine("\nWhen ready press any key when ready to play!");
            Console.ReadKey();
            Console.Clear();
        }

        public void Play()
        {
            int input;
            int numberOfMoves = 0;
            int totalNumberOfMoves = 0;
            int level = 0;
            do
            {
                DrawLevel(level);
                Console.Write("\nPlease enter color number (1-14) that exist in map above then press [ENTER]\n" +
                              "Press H and [ENTER] for help with color numbers.\n" +
                              "Press any other key and [ENTER] to quit.\n> ");
                input = GetUserInput(Console.ReadLine(), 14);
                if (input == USER_GAVE_UP)
                {
                    Console.WriteLine("\nYou gave up after {0} moves - game ended!", totalNumberOfMoves + numberOfMoves);
                    return;
                }
                if (input == USER_WANTS_HELP)
                {
                    ShowIntro();
                    continue;
                }
                numberOfMoves += 1;
                SelectColors(input, level);
                if (GameFinished())
                {
                    totalNumberOfMoves += numberOfMoves;
                    Console.WriteLine("\nYou finished the game in " +
                                      totalNumberOfMoves + " moves!");
                    return;
                }
                if (LevelFinished(level))
                {
                    DrawLevel(level);
                    Console.WriteLine("\nYou finished level " + level + 1 +
                                      " in " + numberOfMoves + " moves!");
                    level += 1;
                    totalNumberOfMoves += numberOfMoves;
                    numberOfMoves = 0;
                    if (level > Levels.Length - 1)
                    {
                        Console.WriteLine("\nGame Over - No more levels!");
                        return;
                    }
                    Console.WriteLine("\nPress any key to advance to the next level!");
                    Console.ReadKey();
                }
            }
            while (input < 15);
        }

        private void SelectColors(int selectColor, int activeLevel)
        {
            Level level = Levels[activeLevel];
            for (int y = level.Map.GetLowerBound(0); y < level.Map.GetUpperBound(0); y++)
            {
                for (int x = level.Map.GetLowerBound(1); x < level.Map.GetUpperBound(1); x++)
                {
                    if ((level.Map[y, x].Color == (ConsoleColor)selectColor))
                    {
                        level.Map[y, x].Selected = true;
                    }
                }
            }
        }

        private void DrawLevel(int activeLevel)
        {
            Console.Clear();
            Level level = Levels[activeLevel];
            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("===[Level #{0}]===\n", level.Number + 1);
            for (int y = level.Map.GetLowerBound(0); y < level.Map.GetUpperBound(0); y++)
            {
                for (int x = level.Map.GetLowerBound(1); x < level.Map.GetUpperBound(1); x++)
                {
                    Console.BackgroundColor = (level.Map[y, x].Selected) ? ConsoleColor.Green : ConsoleColor.Black;
                    Console.ForegroundColor = level.Map[y, x].Color;
                    Console.Write("* ");
                    Console.BackgroundColor = ConsoleColor.Black;
                    Console.ForegroundColor = ConsoleColor.White;
                }
                Console.WriteLine();
            }
        }

        private bool LevelFinished(int activeLevel)
        {
            Level level = Levels[activeLevel];
            for (int y = level.Map.GetLowerBound(0); y < level.Map.GetUpperBound(0); y++)
            {
                for (int x = level.Map.GetLowerBound(1); x < level.Map.GetUpperBound(1); x++)
                {
                    if (!level.Map[y, x].Selected)
                    {
                        return false;
                    }
                }
            }

            return true;
        }

        private bool GameFinished()
        {
            for (int level = 0; level < Levels.Length - 1; level++)
            {
                if (!LevelFinished(level))
                {
                    return false;
                }
            }

            return true;
        }

        private int GetUserInput(string input, int maxNum)
        {
            if (input.ToUpper() == "H")
            {
                return USER_WANTS_HELP;
            }

            if (int.TryParse(input, out int num))
            {
                if (num > 0 && num < (maxNum + 1))
                {
                    return num;
                }
                else
                {
                    return USER_GAVE_UP;
                }
            }
            else
            {
                return USER_GAVE_UP;
            }
        }

        private class Level
        {
            public int Number { get; set; }
            public Tile[,] Map = new Tile[9, 9];

            public void SetRandomColors(int maxNumberOfColors)
            {
                Random rnd = new Random();
                int[] levelColors = new int[maxNumberOfColors + 1];
                for (int c = 0; c <= maxNumberOfColors; c++)
                {
                    levelColors[c] = rnd.Next(1, 15);
                }

                for (int y = 0; y < Map.GetUpperBound(0); y++)
                {
                    for (int x = 0; x < Map.GetUpperBound(1); x++)
                    {
                        Map[y, x] = new Tile() { Color = (ConsoleColor)levelColors[rnd.Next(0, maxNumberOfColors)] };
                    }
                }
            }
        }

        private class Tile
        {
            public ConsoleColor Color { get; set; }
            public bool Selected { get; set; }
        }
    }
}
 
Last edited:
Considering that this is a C# forum, that VB.NET code in post #3 doesn't look like it would compile in a C# program even if it is in VS2019.
 
I recommend posting at sister forum VB.NET Developer Community
Closing this thread since C# Forums is just for C# questions.
I have reopened the thread. I think that you may have mistakenly thought that post #3 was from the OP but it was not. The OP is presumably asking for help with C# rather than VB. If they're prepared to take the time to read it, they may still get some ideas from the VB code.
 
Last edited:
I missread the question, I made a c# version now and edited the post...
I have reopened the thread. I think that you may have mistakenly thought that post #3 was from the OP but it was not. The OP is presumably asking for help with C# rather than VB. If they're prepared to take the time to read it, they may still get some ides from the VB code.
Missread the question and have now changed to c#...
 
Please don't go back editing your posts, especially after someone has commented about it. Now it looks like I'm crazy in post #4 for saying that the code in post #3 is VB.NET when now the code has C# code in it.
 
Please don't go back editing your posts, especially after someone has commented about it. Now it looks like I'm crazy in post #4 for saying that the code in post #3 is VB.NET when now the code has C# code in it.
If you can do it quickly, editing a mistake in a post is a good idea. If your post contains incorrect information then editing can be a good idea. In other cases, adding a new post or adding to the existing post after the word "EDIT" is probably a better idea. One issue with editing existing posts is that those subscribed to the thread don't get notifications, although @Joakim did add a new post in this case, so that issue was addressed.

BTW @Skydiver, you look crazy for so many reasons that one more doesn't make any difference. ;) At least each post has a History function these days, so there is evidence of your sanity in this case.
 
LOL! Long term effects of hypoxia from my many high altitude jumps, too many drinks at the end of day of skydiving, lead contamination from too much shooting, more drinking after find have been cleaned and put away, and just running around flying from idea to idea! ;-)
 
Last edited:
If you can do it quickly, editing a mistake in a post is a good idea. If your post contains incorrect information then editing can be a good idea. In other cases, adding a new post or adding to the existing post after the word "EDIT" is probably a better idea. One issue with editing existing posts is that those subscribed to the thread don't get notifications, although @Joakim did add a new post in this case, so that issue was addressed.

BTW @Skydiver, you look crazy for so many reasons that one more doesn't make any difference. ;) At least each post has a History function these days, so there is evidence of your sanity in this case.
To my defense when I did the edit someone had locked the thread and I could not add a new post, but anyhow, maybe less pointing fingers and more trying to help people with their questions is my thought process :) And of course Skydiver I wouldn't want you to look bad, hurting your ego is the last thing I'd wanted to do, so here is a link to the VB.net version I did first just for your peace of mind (is the irony showing through?) Source on Github
 
Back
Top Bottom