|
Brian_TheLion wrote: you need to keep within certain rules to have the program compile That is the same for all programming languages, even down to assembler and machine code.
Brian_TheLion wrote: There is no global variable allowed As others have mentioned, global variables tend to cause more problems than they solve; don't use them.
Brian_TheLion wrote: Maybe some programs are better suited to C# than others. Possibly, but you can use C# to write a wide range of different applications.
Brian_TheLion wrote: All programs seem to have a user interface as I've never come across a program that only runs under the DOS prompt. I have no idea what that is supposed to mean. Assuming you mean running in a command window, there are many console applications that do just that.
Brian_TheLion wrote: the program I have in mind that I want to write has many varables between classes. And that is where C# makes it easy for you; it is specifically designed to support proper object oriented programming.
What this, and some of your other questions, suggests, is that you are blaming the language for your lack of understanding. Instead of trying to fit the language to your concept of the program, you need to stop coding and spend some good study time learning the language properly. Only when you have mastered that will you be able to see the best way forward in developing your application.
|
|
|
|
|
Hi Richard.
No I'm not blaiming C# at all, all I'm looking for is a way to use this programming language that suits my needs.
I think it's because I'm use to older languages that had less rules such as quick basic.
As a hobby programmer I want to improve on any programming skills I have so I turned to C# and invested my time in trying to learn this language.
I understand it's the type of language that can't be learnt in a couple of weeks and slowly things that did not make sense at first are starting to make more sense now.
I'm still trying to find a way of one class changing the variables in another class if that's possible.
As for my message about almost all programs requiring a user interface...
You have a choice when writing a c# program of a console program that uses a dos window or a program that has buttons that the user can click on. I don't know of any program sold these days that is a console program.
Brian
|
|
|
|
|
Brian_TheLion wrote: I'm looking for is a way to use this programming language that suits my needs And there is another large problem. You are basing you "needs" on a legacy system written in basic and hoping to convert it to a modern language and development style. You should really accept that you are up for a rewrite.
Look only at the functionality of the original game NOT the code that does the work in basic. If you are going to use a modern OOP language you will need to understand OOP principles BEFORE you begin trying to design the application.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
Hi Mycroft.
I'm doing my best to try and understand OOP from reading and studying examples.
The code that was originally written in BASiC Will be pulled apart and put to geather so that it works under the more modern C# structure, so it is like you say a rewrite. I have a good knowledge of how the code works to do a rewrite.
Brian
|
|
|
|
|
Brian_TheLion wrote: Will be pulled apart and put to geather so that it works under the more modern C# structure It feels like your thinking is incorrect, don't consider pulling the old code apart and restructuring it. Only inspect the code to help you define the functionality of the application NOT how it should be put together.
Seriously do not try and apply basic methodology and structures to c#, everything is now an object with properties and methods. Define your objects...
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
Hi Mycroft.
I may have not used the best term when I said "pulled apart" I was meaning studying the original code to see how it worked to get ideas on how the adventure should work for the new code.
Having used programs like Quick Basic over the years makes it more difficult to break free from the procedure programming method, but I don't give up that easierly.
Brian
|
|
|
|
|
Brian_TheLion wrote: Having used programs like Quick Basic over the years makes it more difficult to break free from the procedure programming method
I, too, started on procedural languages like FORTRAN, COBOL and BASIC and initially found the Object Oriented way seemed artificial, clunky and back-to-front. Now, having spent several years in an OO environment, I can see the benefits and would never go back to the old ways. It can be hard to unlearn things that work and to undo your mental images of how things interact, but it is worth it in the long run.
I'd suggest that you park your adventure game and write something from scratch, forcing yourself to use the OO paradigm (horrible word) and then evaluate what worked well and what seem odd. Then revisit your adventure game looking at the objects (players, inventory items, rooms) and seeing how they map onto classes and then look at their properties (inventory items are-in rooms, players -have- inventory items, players are-in rooms, rooms are-adjacent-to- other rooms) then look at methods that change the properties (players move-to rooms).
|
|
|
|
|
Good advice thanks jsc42.
My Text adventure program is more involved than a simple game so it might be better to try writing some less challenging programs first such as a number guessing program to get some practice.
Brian
|
|
|
|
|
Hi Mycroft.
I'm considering everything at the moment which is why I have not started to write my program in C#.
Brian
|
|
|
|
|
Brian_TheLion wrote: I'm looking for is a way to use this programming language that suits my needs. It will. C# is a rich language that can handle just about any problem you can think of. But, as I and others keep saying, you need to learn and understand the language, and its rules, first.
Brian_TheLion wrote: I'm still trying to find a way of one class changing the variables in another class if that's possible. That is what Properties and Methods are there for.
|
|
|
|
|
Hi Richard.
That's what I'm trying to do by reading books on the subject and asking questions.
I read somewhere that C# is starting to spread to programming apps on tablets.
Brian
|
|
|
|
|
What you really need to do is to stop coding, stop posting questions, and work through some solid study guides to get a full understanding of the basics of the language, classes and structs, value types and reference types, generics, etc, etc. I started by working through .NET Book Zero by Charles Petzold[^] a couple of times, before I attempted to write my first (very simple) C# application.
|
|
|
|
|
Hi Richard.
It's more of understanding what I have read. so if I still don't understand it then I ask questions. You can't ask a book questions.
Not all books give a clear understanding on C#.
Lets take an example:
A book will tell you that the 'set' command is to set a value and the 'get' command is to get a value, but it fails to tell me why I need to use the set and get commands. Why not just have Name = "Peter" like some programming languages, so I'm thinking when and why should I use these commands; some C# code does not use the get set commands.
After doing some research on the internet and studying example code I now have a better idea on how these commands are used.
It's also sometimes the way something is explained that makes it easier to understand.
I downloaded and started to read Net book zero a few weeks ago, I must take another looks at this book. Thanks for the reminder.
Brian
|
|
|
|
|
Brian_TheLion wrote: Why not just have Name = "Peter" That is exactly what it now has.
As I said before, repeated studying will help to make things clear. But you also need to understand that programming languages are continually evolving, so some older guides may not include the latest changes.
|
|
|
|
|
Richard MacCutchan wrote: As others have mentioned, global variables tend to cause more problems than they solve; don't use them. He can get his "global" accessibility by creating a static class. I don't understand (and have never seen) a global static class present problems.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
A static class is a very different animal.
|
|
|
|
|
#realJSOP wrote: I don't understand (and have never seen) a global static class present problems
You've lived a very sheltered life
|
|
|
|
|
Or just maybe, I've never abused the construct.
In all actuality, I was annoyed with the lack of support for global vars when I moved from C++ to .Net, but I adapted.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
I've seen too many cases where other people have abused it, and it makes for some really nasty code.
The better (not good, just better!) code ends up being more about convenience than it does about expressing a cohesive purpose. The worse stuff just screams "I can't figure out another way to do this." Compound that many times over the span of the entire app and the lack of comments or any documentation and, yeah, it gets ugly.
|
|
|
|
|
Brian_TheLion wrote: it seems that you need to keep within certain rules to have the program compile. Well, that's true of ANY language.
Brian_TheLion wrote: Maybe some programs are better suited to C# than others. That's not true at all.
Brian_TheLion wrote: All programs seem to have a user interface as I've never come across a program that only runs under the DOS prompt. Again, not true at all. Having a GUI is pretty much dependent on what/who the app is for. I've written several command line apps in the last four months for our DBAs to run from SQL Agents. These apps didn't need a ui.
Brian_TheLion wrote: I'm being drawn towards C++ as it does allow global variables compared to C#. If you want global vars, simply create a static Globals class and add the properties and methods you need in it. I do it all the time. In point of fact, EVERY app I write has a static Globals class for that very reason.
FWIW, I transitioned from unmanaged C++ to C# back in 2007. It's really not that difficult to do.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
As a note.
If you want to learn how to create games then I would suggest finding one of the game frameworks and using that. There are adventure frameworks but others as well. Using one of those and creating a games (plural) provides a better understanding of the 'domain' for creating games.
If you want to learn OO programming and you are using an adventure game as an excuse then learning the rules for the programming language (singular) is going to be your actual goal. The game itself is unlikely to be viable because without a detailed understanding of the 'domain' both from the business layer and the development layer your initial attempts will have problems with structure. And I speak from experience on that having attempted more than a couple games when I first started. But that is not a detriment because that will aid you in the future as you will understand intrinsically why something doesn't work versus because someone told you that.
|
|
|
|
|
Thanks jschell.
Having an text adventure game waiting to be written does give me something to aim for.
Some books and tutorials give you examples on how each of the C# commands can be used but it's when you bring everything together in a project then I find that you learn more. Step-by-step guilds in building a program are useful and I did one of those recently.
Brian
|
|
|
|
|
I've knocked up this basic template to give you an idea what people are talking about. There is a "global" object you need to track like the player and also the current location, so you can either create these as an instance of a variable and keep a hold of them, passing them to functions\events as needed, or you could create a "static" class that will hold a reference to your player object and current location object. As I said, it's the basics, you'd need to tweak for things like containers as game objects also so you could pick up a bag, or put a small bag inside a big bag etc.
public class GameObject
{
public string Name { get; set; }
public int Weight { get; set; }
}
public abstract class Container
{
public int MaxWeight { get; set; }
public int MaxItems { get; set; }
public List<GameObject> Objects { get; private set; }
public Container() : this(0, 0)
{
}
public Container(int maxWeight, int maxItems)
{
this.Objects = new List<GameObject>();
this.MaxItems = maxItems;
this.MaxWeight = maxWeight;
}
public GameObject Find(string name)
{
return this.Objects.FirstOrDefault(o => o.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase));
}
public bool CanContain(GameObject gameObject)
{
if (this.MaxItems > 0 && Objects.Count >= this.MaxItems)
{
return false;
}
if (this.MaxWeight > 0 && Objects.Sum(o => o.Weight) + gameObject.Weight > this.MaxWeight)
{
return false;
}
return true;
}
}
public class Player : Container
{
public bool Get(Container container, string name)
{
GameObject targetObject = container.Find(name);
if (targetObject == null)
{
return false;
}
if (!this.CanContain(targetObject))
{
return false;
}
container.Objects.Remove(targetObject);
this.Objects.Add(targetObject);
return true;
}
}
public class Room : Container
{
public string Name { get; set; }
public Dictionary<string, Room> Exits { get; set; }
public Room()
{
this.Exits = new Dictionary<string, Room>();
}
}
static void Main(string[] args)
{
Player player = new Player();
Room startRoom = new Room();
startRoom.Name = "Hallway";
startRoom.Objects.Add(new GameObject { Name = "Key", Weight = 1 });
startRoom.Exits.Add("north", new Room
{
Name="Kitchen"
});
Room currentLocation = startRoom;
player.Get(currentLocation, "key");
Room room = currentLocation.Exits["north"];
if (currentLocation.Exits.ContainsKey("north"))
{
currentLocation = currentLocation.Exits["north"];
}
else
{
}
}
|
|
|
|
|
Thanks very much F-ES Sitecore for taking the time to write the code. It will be very useful and I'll learn more by studying your code.
Brian
|
|
|
|
|
Brian_TheLion wrote: I'm being drawn towards C++ as it does allow global variables compared to C#. I know that it's not good to use global variables and most variables should remind within their own class but it's not always easy to design a program like this and the program I have in mind that I want to write has many varables between classes.
Yeah, that's not how you do that. There are a few approaches that are valid from a C# point of view, but by and large a variable in the global namespace is never the answer unless the language itself forces that on you (thank you, JavaScript).
There are better tools: a static container class, a service locator, or best of all dependency injection.
Brian_TheLion wrote: I have in mind that I want to write has many varables between classes. I could write it with less classes but I want to have classes for certain purposes that can be reused in other programs. It also makes the program easier to deal with when changes are made.
Good OOP uses many, many classes that work in conjunction to build a system.I suggest you take some time to learn about SOLID Programming.
Global variables also make maintenance much, much harder in complex software. Modern IDEs have made this a little less significant, but for good, flexible software you should still prefer composability to imperative structure.
"Never attribute to malice that which can be explained by stupidity."
- Hanlon's Razor
|
|
|
|