🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

C# Workshop - Project 1: Maze Generator

Started by
35 comments, last by Telastyn 16 years, 9 months ago
Quote: Original post by ShadowPhoenix
Quote: Original post by Spoonbender
Quote: Original post by ShadowPhoenix
I was thinking of getting 2 bits to represent the room, but it would make the program even slower. The program shall run faster if we use a third bit to indicate if a room is connected or not.

Ok, never mind the number of bits necessary (would be a long discussion, and this probably isn't the place for it)
But I do have a more general question.
Connected to what, do you mean?
Isn't the point that all rooms are connected to all other rooms? (via exactly one path)
So what is there for a room to be connected to?



Can you please explain the number of bits necessary? It sounds COOL!!!

As for the connected part, the faster way of doing it while only wasting 1 byte is to see if the room is in the 'main room'. We only need to have 1 set of room at the end of the moving data, so we only need one bit to see if it in that set or not.

Are you discussion some optimization technique in a beginner workshop? [grin]
Advertisement
Yeah, better start by getting the maze working. Once you have that, feel free to start another thread if you want to discuss clever extensions, optimizations and extra-features you can implement. [grin]
Is there a way to specify the range of characters allowed from the keyboard?

Like, is characters on the keyboard numbered from 1 - 110 whereby you could write a conditional to make sure that only numbers from the keyboard were entered.
I was just wondering if numbers from the keyboard were within a certain range like 1-10.
Then check to see if the char read was between that then parse it to int.

if (Console.Read() < 11)
{
int mazeHght = int.Parse(Console.ReadLine());
}
else
{
Console.Write("You aren't inputting numbers");
}

I rate users. Its just not having much impact as my rating is low...
Digits have a byte value between 0x30 to 0x39, or 48 to 57 in decimal (0-9).
However, the easiest way test if the client entered an integer is to use either exceptions or the TryParse function.
Something like this...
string input = Console.ReadLine().Trim(); //get rid of whitespace on each endint res;if(Int32.TryParse(input, out res)){//res contains the integer that was typed}else{//not a valid integer}
I mean, why would you get your medical advice anywhere else?
What you showed me is great. I want to ask you about the keyboard numbers as a decimal.
So you would read the input as string, then convert it to decimal and then check to see if the decimal fell between 48-57 inclusive?

Thanks so much.

Also, I have a question about this conditional. I have this feeling it's not running it the way I would think because it's possibly short circuited:

            string strHeight = Console.ReadLine().Trim();            // Create an integer variable to hold the converted string            int mazeHeight;            // Try the casting of the string in strHeight to int and return it to the int variable            if (Int32.TryParse(strHeight, out mazeHeight))            {                // Variable mazeHeight contains the integer that was typed                // Check that the number chosen was between 2 and 50 inclusive                if (mazeHeight >= 2 || mazeHeight <= 50)                {                    // Code to continue when number is 2 - 50                }                else                {                    // The number is not 2 - 50                    Console.WriteLine("Please follow instructions.");                }            }            else            {                Console.Write("Not a valid number");            }


Right at the line:
if (Int32.TryParse(strHeight, out mazeHeight))

I think it's bringing in and then returning and then not following the rest of the code in that block below it. It's not meant to go into a block is it for this particular case?
I rate users. Its just not having much impact as my rating is low...
Now I'm not really sure what you're asking.
48-57 are the ascii codes, so they only matter before parsing to an int.
Afterwards you can just do what you did in your code segment.
If you do want to check before parsing it into an integer, you would compare to characters directly.
string input;//get inputforeach(char c in input){if(c => '0' && c <= '9');    //character is a digit}

As for comparing to a real number, I can see no reason to do that without parsing it to an int first.
And your bug is that you used || when you meant && - mazeHeight >= 2 || mazeHeight <= 50 is always true :)
I mean, why would you get your medical advice anywhere else?
DOH!

Thank you.

string input = Console.ReadLine().Trim();        // Get input        foreach (char c in input)        {            if (c >= '0' && c <= '9')            {                // Character is a digit            }        }


This wouldn't be any good anyhows since we have to go up to 50. This would only check single digit numbers right?
I rate users. Its just not having much impact as my rating is low...
Yeah. Of course you could then just do it twice, but int.Parse() is probably a better option [wink]
Parse is a better option, but the above would work as it goes through all the characters in string. You just need to handle parsing the characters yourself.
Which would start by minusing '0' and then multiplying the number by 10.
But there are number of details you would need to handle (e.g. whitespaces). int.parse does most of these for you so why complicate things?
That's great. I was just asking so I understood.

You guys explained it very well.

Thanks a million!
I rate users. Its just not having much impact as my rating is low...

This topic is closed to new replies.

Advertisement