Skip to main content
GameDev.net gamedev.net
🔒 Locked

Simple projects? (C#)

Started by melloorr Dec 30, 2011 at 2:52 PM 16 replies 4.1k views
Original Post
melloorr
melloorr
Hello everyone, been a while since I was here but I want to get back into C# so I need some tasks/small projects that will allow me learn the basics again, and let me learn new things too.

Does anyone have any ideas please?

Thanks, melloorr.
melloorr
melloorr

One fun relatively simple project I enjoyed was the maze generator project from the C# Workshop.

Wow, looks pretty complicated to be honest, should take me a few months to do wacko.png

Would basic C# be enough for me to do it?
SiCrane
SiCrane
That was the first project for the C# workshop. For about four or five weeks worth of lessons and a lot of people new to C# before the workshop managed to complete working programs. So yes, basic C# should be enough.

I will admit it probably looks a lot more intimidating because during one or other of the forum upgrades line breaks got stripped out of the post so you've got huge walls of text where it was broken up before.
melloorr
melloorr
Yeah it is pretty hard to read to be honest, but I will at least attempt it, and if I cannot do it, I will go through some of the lessons then attempt it again.
melloorr
melloorr
@SiCrane, I don't suppose you or any other mods can edit the post please?
If I take my eyes away for a second, I have to spend 5 mins trying to find where I was up to.

Thanks
SiCrane
SiCrane
Actually, clicking on the edit button not only strips out line breaks but also manages to strip out spaces. I'm almost positive that I'd do more damage than help if I tried editing the actual post. As an alternative, you could try cutting and pasting the post contents into a Word document (or whatever your favorite rich text editor is) and adding line breaks manually to that.
melloorr
melloorr
Okay I'm not getting this. I cannot do it.

I just can't work out how to get one variable to another, method, class. I have the book Pro C# 2010, but it hardly has any information on it (despite the size of the it).

Are there any tutorials that can get me used to working with multiple classes, and methods?
SiCrane
SiCrane
Could you show an example of what you're having trouble with? I'm not entirely sure what you're asking.
melloorr
melloorr
Well, looking at some of their answers to the project, they have classes withint width in them, but I do not know how to get int width, into a different class to use it with that class also.
SiCrane
SiCrane
To access a member of a class instance you just use the . operator on that instance. So if you have a class Maze with a int member variable named width and you had a Maze instance named maze you could just use maze.width to get the width value for that particular maze.
melloorr
melloorr
Hmm, still not quite getting it. I think I will do some more reading up on it, then maybe try a text based game first, then attempt that again.

Thanks for the help
SiCrane
SiCrane
I would still like to help you get this point. Can you show a more complete example of what you're trying to do?
melloorr
melloorr

I would still like to help you get this point. Can you show a more complete example of what you're trying to do?


This is the code that I currently have (not much):

using System;
class MazeGenerator
{
static void Main()
{

Console.Title = "Welcome to the Random Maze Generator for C#";
Console.WriteLine("Welcome to the Random Maze Generator for C#");
Console.WriteLine("Please press Enter to continue to the Generator");
MazeQuery();

Console.ReadLine();
}
static void MazeQuery()
{
int mazeWidth, mazeHeight, wall, numberOfRooms;
do
{
Console.Write("Please enter the number of columns: ");
mazeWidth = int.Parse(Console.ReadLine());
} while (mazeWidth < 2 || mazeWidth > 50);
do
{
Console.Write("Please enter the number of rows: ");
mazeHeight = int.Parse(Console.ReadLine());
} while (mazeHeight < 2 || mazeHeight > 50);
numberOfRooms = mazeWidth * mazeHeight;

wall = numberOfRooms * 2 - (mazeWidth + mazeHeight);
Console.ReadLine();
}
}


I don't know how to get numberOfRooms or wall to a new method or class to calculate what walls I have to knock down.
boogyman19946
boogyman19946
Perhaps if you see a code example you'll be able to put things together a little better.

Suppose you have the following:



public class Maze
{
public int width;
}


Then the following code does the following: creates an instance of Maze and calls it myMaze, creates a variable called newWidth with value 32, and writes newWidth into the width variable of the Maze class instance (myMaze)


public class Maze
{
public int width;
}

public class Main
{
public static void Main(string[] arguments)
{
Maze myMaze = new Maze();
int newWidth = 32;
myMaze.width = newWidth;
}
}


If you want to save the width variable into something else, say, the newWidth variable, you can do it like so:



public class Maze
{
public int width;
}

public class Main
{
public static void Main(string[] arguments)
{
Maze myMaze = new Maze();
int newWidth = myMaze.width;
}
}


EDIT: you can create a Maze instance in Main() and pass it as an argument to the mazeQuery() function where you can save the data into its members.
Yo dawg, don't even trip.
SiCrane
SiCrane
In this case you'd use a function parameter. So if you had

static void SomeFunction(int rooms) {
// stuff
}

You could call it in MazeQuery() like:

SomeFunction(numberOfRooms);
melloorr
melloorr
I get it now, and if I wanted to get something from a class, I could also use something like this:

using System;
class SimpleClass
{
public static void Main()
{
Console.WriteLine(ThatClass.yes);
Console.ReadLine();
}
}
class ThatClass
{
public static int yes = 10;
}
boogyman19946
boogyman19946
That's correct. Since variable yes is static, you have no need to instantiate the class ThatClass before using it, you can just call it directly like you have above.
Yo dawg, don't even trip.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.