C# Workshop - Project 1: Maze Generator

Started by
35 comments, last by Telastyn 16 years, 8 months ago
It is hard to learn the feel of the 'C# way' without writing code and using the standard library. C# and java are exceptionally similar in syntax and constructs, but do tend to have a different feel to how things should be done.

Just like you're not learning C# for the syntax, the project perhaps isn't so much about making a labyrinth as it is providing an excuse to get experience working with the language and tools.
Advertisement
Here's my (setup code) so far. Any tips or critiques will be quite helpful.
public class Maze{    private int totalRooms;    private int columns;    private int rows;    private class Room    {        public bool isWallDown;        public Room ()        {            isWallDown = false;        }    }    private Room[] rooms;    private enum MazeWall { Horizontal, Vertical };        private class Wall    {        public MazeWall wallType;        public Room room1;        public Room room2;    }     private Wall[] walls;    public static void Main(string[] args)    {        Maze aMaze = new Maze();        System.Console.WriteLine(" - - - - ");        System.Console.WriteLine("| | | | |");        System.Console.WriteLine(" - - - - ");    }    public Maze()    {        System.Console.WriteLine("Please enter number of rows: ");        string rows = System.Console.ReadLine();        System.Console.WriteLine("Please enter number of columns: ");        string columns = System.Console.ReadLine();        this.rows = int.Parse(rows);        this.columns = int.Parse(columns);        this.totalRooms = this.rows * this.columns;        walls = new Wall[(this.columns * (this.rows - 1)) + (this.rows * (this.columns - 1))];        rooms = new Room[this.columns * this.rows];        System.Console.WriteLine(this.rooms.ToString());    }    private void setWalls()    {           int wallCount = 0;        for (int i = 0; i < rows; ++i)        {            for (int j = 0; j < (columns - 1); ++j)            {                walls[wallCount++].wallType = MazeWall.Vertical;            }            for (int k = 0; k < columns; ++k)            {                walls[wallCount++].wallType = MazeWall.Horizontal;            }        }        if (wallCount > walls.Length)            System.Console.WriteLine("This algorithm is wrong!!");    }    private void setRoomsToWalls()    {        for (int i = 0; i < walls.Length; ++i)        {            if (walls.wallType == MazeWall.Vertical)            {                walls.room1 = rooms;                walls.room2 = rooms;<br>            }<br>            <span class="cpp-keyword">else</span><br>            {<br>                walls<span style="font-weight:bold;">.room<span class="cpp-literal"><span class="cpp-number">1</span></span> = rooms;<br>                walls<span style="font-weight:bold;">.room<span class="cpp-literal"><span class="cpp-number">2</span></span> = rooms[(i - (columns - <span class="cpp-literal"><span class="cpp-number">2</span></span>)) + columns];<br>            }<br>        }<br>    }<br><br>}<br><br><br></pre></div><!–ENDSCRIPT–> 

Beginner in Game Development?  Read here. And read here.

 

Quote:Original post by Alpha_ProgDes
Here's my (setup code) so far. Any tips or critiques will be quite helpful.
*** Source Snippet Removed ***


Your constructor is rather inflexible. What if you wanted to make the maze based on command line parameters? Or not print (because you were using it in a directx program)?
That's the only thing that's "wrong"? Great!
  • Make multiple constructors
  • Move input into it's own function

Got it!

Beginner in Game Development?  Read here. And read here.

 

@Darklighter

Quote:
// Properties
public int ID
{
get { return itsID; }
}

public int Room0
{
get { return itsRoom0; }
}

public int Room1
{
get { return itsRoom1; }
}

public bool IsUp
{
get { return isUp; }
set { isUp = value; }
}
}


I was looking over your code to learn about how you did your maze. I am wondering about the way that properties were used. Since from what I've read about properties are great for doing some validation before allowing access to the fields. It seems that that's not being done here? I'm just wondering that if you aren't going to do any validation whether or not it would just be just as good to just use a field to save your self some typing.
I know that most are using everything they are learning to practice so maybe that's why they are used in this case, but I would still like to know opinions.

Thanks in advance.
I rate users. Its just not having much impact as my rating is low...
Quote:Original post by foolios
@Darklighter

Quote:
// Properties
public int ID
{
get { return itsID; }
}

public int Room0
{
get { return itsRoom0; }
}

public int Room1
{
get { return itsRoom1; }
}

public bool IsUp
{
get { return isUp; }
set { isUp = value; }
}
}


I was looking over your code to learn about how you did your maze. I am wondering about the way that properties were used. Since from what I've read about properties are great for doing some validation before allowing access to the fields. It seems that that's not being done here? I'm just wondering that if you aren't going to do any validation whether or not it would just be just as good to just use a field to save your self some typing.
I know that most are using everything they are learning to practice so maybe that's why they are used in this case, but I would still like to know opinions.

Thanks in advance.

[Opinion]
Hehe, tricky one. You could argue that having the properties in place from the beginning would make it easier to add validation later on. Or that it'd be more consistent. On the other hand, if you make it as a public field, it's usually easy enough to encapsulate behind a property later on. So in my opinion, it doesn't make a big difference. Do whatever you like.
There's nothing wrong with making the properties from the beginning, at least. But I don't blame you for wanting to save those couple of lines of typing either [grin]
Quote:Original post by foolios
@Darklighter

I was looking over your code to learn about how you did your maze. I am wondering about the way that properties were used. Since from what I've read about properties are great for doing some validation before allowing access to the fields. It seems that that's not being done here? I'm just wondering that if you aren't going to do any validation whether or not it would just be just as good to just use a field to save your self some typing.
I know that most are using everything they are learning to practice so maybe that's why they are used in this case, but I would still like to know opinions.

Thanks in advance.


Note that the first bunch are get only. This means that trying to assign to the properties will yield a compile time error. Thus they're quite different from simple public fields or even readonly public fields.

[Opinion]
For the simple wrapper case of IsUp, it depends on the expected usage pattern. If you will implement validation later, go with the property. If you might want to allow protected full access but limited public access, definitely go with the property. If you're doing reflection and need IsUp to be a Field/Property go with the property.

If you don't expect to ever need validation and want other stuff to freely modify the variable for whatever reason then a field should be sufficient.

This topic is closed to new replies.

Advertisement