over the years I've learned an awful lot of programming languages but the one thing that I can't be bothered to do is to read my way through one of those enormous books you know page 1 to 800 telling you everything about this programming language and yet I've learned Ruby c c Java object Pascal modular 2 lots and lots of programming languages I've been programming since the 1980s and I've had plenty of time to figure figure out a way of learning a language as effectively as possible and that's what this video is about what I don't
do is I don't read huge manuals from page one to page 800 I don't do hundreds and hundreds of little programming exercises I know that some programmers when I've taught courses on programming get very wound up very uh obsessed with solving problems of having exercises to do exercises have their place and they can be useful for some things but that's not my approach to learning a new language to learn a new language quickly and effectively pick a project get programming simple as that I'll show you what I mean as always if you followed some of
my other tutorials you'll probably guess the project I'm going to pick it's going to be a simple Adventure game adventure games are great because they bring in all sorts of stuff string handling uh class libraries file handling just about everything really but for you if you don't like adventure games pick something else it could be a dis utility it could be I don't know I really have no idea what interests you but find something that you're interested in and start to learn that way I'll show you how I do this I'm going to show you
how I started learning the D language I picked D because it's quite a it's a fairly wellestablished language but probably one that most people are not familiar with I've never really programmed D before i' I've I've been aware of it over the years but I've had to learn from scratch so I'm going to show you how I got from no knowledge of D to writing a simple Adventure game in about 3 hours let's see how I got started clearly the first thing you have to do when learning a new language is install the compiler or
interpreter that is the thing that's going to run the language for you for D I've gone to the the uh programming website and found the download page there are lots of downloads here for windows and uh various versions of Linux and Macos I'm programming Windows I've decided that uh I'm going to use visual studio and there is a visual studio plugin called visual D I can download that it includes the D compiler so I don't have to do more than one step but if you're using other operating systems then Visual Studio code is quite well
supported it takes a bit more setup and un less you you're very familiar with it it'll take a bit of learning which is why I'm really more comfortable with Visual Studio then I need some reference material well the D site has this uh documentation fairly comprehensive documentation to the language and its libraries and just as an added extra there are also extra resources you can see here there are various tools editors tutorials and books and so I'm going to use this book book this online book or you can also get a pdf version so that
I can dip in when I need some extra help so having got my D IDE up and running this is the one that's in Visual Studio the next thing to do is to start writing something so start a project I can search in visual studio for a particular language uh if you're using another IDE or text editor of course this step is going to be different uh just select an application and see what happens D application I'll just use all the defaults H now it's prompting me console yeah I'll go for console this is the
standard reference D compiler okay I'll accept that finish let's see what happens so it's gone away and it's generated something I can see there's a dprogram file here let's have a look what's in it ah so it's created a simple file for me let me try running that and see if it works so it's compiling it and ah pop down again so I need to put a break point break point will stop it exiting I hope and let me look at okay so hello D world that looks okay what do I do next let's stop
this back in here well let's see if I can guess my way through it I've got some experience with C and C like languages let's try a an array of chars a a string in other words turns out that D has more than one way of doing strings but I'm going to stick with this Char array uh call it s so this is me guessing my way through the language so far it works okay so let's have a look I've got wrin well let's see if there's a reedin reedin is the same name I I
happen to know these from Pascal these are standard uh names in Pascal see if uh see if this works inde D and it's looking good so far it's got no no the error squiggly is gone so let's see if I can append that to this string uh normally that's with a plus s ah it's not liking that so that's where I've had to Resort this is the first time when I was learning this that I had to resort to the documentation and it tells me that I'll save you the time I won't show you it
all now but you just look it up in in online documentation and it says tilder is the way of appending one string to another and instead of putting a break point this time I'm going to put redin and just like Pascal it also lets me omit the uh empty brackets if I wish to and I just found that again by trial and error uh you know no special Magic on this so let's see what happens okay so enter my name it should read that assign it to the string s and it displays the string it's
paused at the end because of that final reading I put it's waiting to read the final line it'll do that when I press enter and there you go so that my first program all of that was done with guesswork apart from this one thing I had to look at this tilder to find out how to append a string so that was my first 5 minutes I said that I took about 3 hours let's see what I accomplished in the first hour well I'm trying here to create a class that's a class that's going to Define
an object when I've looked at other D tutorials they leave classes and object orientation on the whole really till the later chapters you might be well well into the tutorial before you even look at it well I decided that I need to find out how to use this quite quickly so again mostly this is guesswork from my knowledge of other C like languages that's C and Java for example uh create classes and let you derive objects from them so I created a class called thing and I you this is the string data types I said
D does have a string data type so I've G it a name field I had to look up how to create a Constructor it's using this keyword and then you pass to its arguments and here the argument the string argument is used to initialize the name field and I wanted then to have the name itself the name field private so it's encapsulated so the data is hidden and to have accesses and the way that I chose to do it was to have uh accessors called name now even though these have got the same names these
two methods they uh de differentiates between them because they've got different argument lists so I can assign a string using this one and I can return a string the string name using that one now I then descended uh another class room from thing this again was guesswork because I've come across various different syntaxes of creating lines of descent and so it turns out that indeed this colon is put between the new name and the ancestor class name but again if you wanted to you know if you couldn't figure that out just simply looking it up
in some documentation we'll explain that very quickly now the room inherits the name from The Thing class and it adds on these integers north south west east those are the exits in to other rooms then this is its Constructor it takes all those arguments and it calls super again I guessed this from experience with other languages uh it calls the super class passing to it a name and then it initialized these arguments now down here I just messed about I mean this code is all over the place really but that's a really important stage when
you're learning a language just mess about it doesn't matter if you if you mck things up I mean in fact you should because you're really trying to find you a way around the language so I've created a new thing I've uh used writing which I've done before and uh then I've created a new room and now I've written this uh here this is just a prompt character read the string that the user enters created a new thing now s dodap this is to do with how strings are handled the difference between strings and uh character
arrays there is a distinction made so this a string has been read here and I need it to be uh duplicated as a character array I had to look that up that was one thing that I wouldn't just guess I I did look that up and so this is the kind of process I go through when I'm messing around I hit a problem that's the stage at which I have to go to the documentation to look things up I discovered that as expected I can pass an argument like this and Al alternative syntax I I
just found this through messing around and trying it out is to use it like a property in C where you use this like a variable even though it's actually a method and assign the string the single argument to it um the rest I think is fairly straightforward oh the I wanted to display the number the integer on the North side again I had to look up how to convert an integer into a string representation and I found that this is the way to do it two exclamation mark and string and in order to do that
I had to import std.com so again that was something I had to look up um is there anything else that needs explaining here I don't think so oh yes there is one thing I tried to make this private again these are things I had to look at how do you make sections of a class private or public and so by putting private colon I can declare a section of private variables here and then these accessor these methods are public however I discovered that I can actually if I wanted my private variables underscore name that shouldn't
be available if it's private should it so I should have an error there I'm expecting but uh apparently not so again that's something I had to look up why is that privacy not being observed it turns out that in D even if something is private in a class if it's all in the shared module that's this single code file in this case then it will be public which is not what I want so I had to find out next how to create a module again that's something I looked up in documentation so here I would
say in my first I guessed maybe 80% of the code and I ran into a few problems which I've mentioned that's when I had to look up the documentation right so this is my Rewritten version here I've added a module to hold my thing and room class uh in Visual Studio that's done with right clicking the project and then you can click add and add a new item obviously that's going to be different different in other editors or idees you can see I've just created a module called my classes given it the name up here
and it's in a file called my classes. D this is just the same code that I had in the previous uh version I've still got string uncore name marked as private now if I go into the main file the file that starts the program that just creates a room and it prompts me uh sorry it writes this prompt here and it says you are in etc etc the difference here is if I try to access that private field now I get this squiggly it says that there's no property uncore name and so that is how
I've enforced the Privacy just by creating a new module and importing that module okay so after that experimentation and now think I'm ready to start writing the very very simplest possible Adventure game here I want a map containing four rooms I've already defined the room object as I've shown uh pause the int variable defines the player's position uh which starts off a zero I've drawn out this little map so r0 is the name of the first room it goes east to room one R1 South to room two and that goes east to room three and
there are no exits in the other directions I've then created the rooms you can see in the four rooms down here and I've added them to an array so I've defined a room array called map to contain four rooms and I've just added them one by now all of this has been done with guesswork because I've used arrays in other languages and if you've used cop or Java um or or some other C like language you can probably make a good guess at how to do this I I might have made a few mistakes along
the way but it was fairly simple to uh Rectify then I've just written this move to Method so it takes a number and that's the number of the the room that to to be moved to and if it's minus one minus one means uh no exit in that direction well if it's minus one I just display no exit otherwise it updates the player's position that's the pause variable to the new room number and it writes you have moved into and then it goes into it indexes into the map and it gets the name of the
room at that position then there's my main function down here that's what executes when the program runs this is all fairly straightforward uh I've got exit as an integer uh oh first of all it calls init so init where is that so that's this method that's the method that first creates the rooms and the map okay so so it calls in knit then it writes that you are in well it's room zero because pause starts off as zero at the start of the game then I've got this do while loop again I I looked up
Loops in the doc documentation but I was guessing there would be something like do while as that's a common sort of loop so that executes the loop while this condition is true and the condition is that the player has not ENT entered Q the string Q uh to exit so not equals that's again guessed from my knowledge from other SE like languages uh switch case again that's very like in in other C like languages it's just a multiple choice so if the player enters the string s that's readed in from the command prompt up here
if the player enters that string um and it's the string is n then I find the exit uh of at the end position the north position in the room the room is the uh map is is the room object at at the current position and it's the same for s w e and the default is no exit so that should all work in principle let's see if it does so South should go to oh there should have been a room there on East I can't even use quit there is something wrong with my code so
this is this is my other secret weapon before I go on that I should say you know that when I say I'm looking things up um I've got online documentation which I refer to and I'm going to be referring to that shortly because my uh assumption is that there is something wrong with the way that I've read this string the string is not being recognized as as n it's not matching this uh and so on so let's see what's wrong so I'll do it again so let's enter s now that should match the string s
but let's have a look at what s is looks like s ah but if I expand it using the debuger by pressing that Arrow I can see there are two characters there's an S followed by the value 10 that's an asy code 10 which is a a line feed not a character return which is 13 and so when I'm expecting it to match s down here let me put another break point down here I'm expecting it to March s it doesn't because I'm testing that the string is one character long that's just a single s
back to my documentation okay so this is where I start looking things up I've got this programming in D reference here and I'm looking for something to trim off in in many languages it's called trim I I tried that and it didn't didn't work so I've gone to the documentation see if I can find something else and this is what I find in D the function that strips off the um non-printing characters like spaces and carriage returns and line feeds is called strip and I assign it back to a variable as shown in this documentation
so that's all the documentation I looked up to get this to work let's try that so according to this documentation I should be able to call S equals strip s um it seems happy with that one other thing I've had to do by the way again this is where you look in the documentation is I've had to import std. string to get this to work let's see if that works and right this time the break point is hit let's have a look what s is y yeah so this got rid of the Rogue character continue
and it's hit the case statement but I'm expecting it to continue let's get rid of all these break points now so I can just continue watching the running program okay so yes I've moved into a new room East there should be another room and there is East again should be no exit and there is West and I can move around the game by entering the directions and Q should quit because that's the test statement down here uh the exits from this Loop so that's my first I would say about 3 hours from knowing no D
at all to writing a very simple ad object-oriented Adventure game so I'm pretty pleased with that that in short is how I go about learning a new language now as I said this has been about my approach to learning a new language it's not a tutorial to D experienced D programmers will have much more effective and efficient ways I am quite sure of programming what I have shown than I used in this uh in this video because these are just my first few hours learning the language so whether you're learning D or rest or Java
or C or C or C++ whatever you're learning what I really want to get AC cross is an a way of starting to learn that language without getting bogged down with thinking you've got to read every single chapter in in order in some impenetrably long book uh or solve hundreds and hundreds of exercises anyway I hope that's given you an insight into how I approach it and maybe you can take something of use from this video If you like this video give it a thumbs up and to make sure you don't miss any of my
other videos be sure to subscribe to my channel ring the bell and I'll see you again soon with something different