As a fool C # taught. Part 1







Record 1



I decided to record. Mainly because I like to record. Although not. Actually, I want to tell a story. The story that programming is interesting. The fact that programming can be not only a profession, but also a great hobby, like photography, chess, painting or playing the guitar. The fact that it is not necessary to have innate talent, superpower or years of special education to achieve small but big victories in any business.



This is a diary of a person learning programming for fun. Not for a profession, not for a living. I do not live in Nigeria with a push-button telephone and I am not in a desperate situation. I just study and program for entertainment in my free time from work and family affairs. Show in real time. Published in batches of 5 entries.



For some reason, the existing mentality (mine and others) protects us from bold experiments with something new. “I don't know how” sounds like a damn death sentence. Every day I see this at work, when colleagues are faced with an unfamiliar task. I can not. The end of the program.



My seven year old son is afraid to try unfamiliar activities. Because he does not know how. But we must understand that in his years he does not know how much.



Sometimes I tell him, “Buddy, if people did only what they can, we would still live in caves, and specifically you would spend all day mooing, crawling and pooping in pants, instead of playing Angry Birds. Although no, you would crawl without pants, because no one would know how to sew .



Why am I? To the fact that I absolutely do not know how to program. But I'm going to learn. Not because I want to become a developer - I already have a profession. But because I can. Although actually I have three reasons.



The first is to stretch your brain. I am convinced that life cannot be built around any one activity. It is necessary to occupy your mind with different tasks. And the more diverse they are, the better for the mind.



The second reason is practical. I'm going to write a game (corny, yes), which we will play and have fun with. We already have an idea - in the last iteration we tried to make a stick, but the mechanics turned out to be too complicated.



The third reason is educational. I will try to change the attitude of society towards programming. It is generally accepted that programming is the destiny of professionals. No one is programming as a hobby. The concept of “amateur programming” (remember this word) does not exist at all.



Some of the pros reading me will now attack with comments that I will devalue their profession and the skills accumulated over the years. Do not hurry. For now, just believe me that this is not so, and we will come back to this topic later.



In short, I decided to learn C #. Because my idea needs an engine, and as I understand it, Unity is the best. And Unity is C #. This is the first.



The second thing I realized is that starting with Unity is too early. Almost all educational materials assume that the reader is already familiar with the language. And all I know about C # is that it’s correct to say “Sea Sharp”, and not “Tse with a grid”. So let's start with the language itself.



The third thing that I understood is that the training will be thorough. Since Sharpe is a new language for me, you need something “for dummies”, but there isn’t one.



There is a textbook “C # for schoolchildren” by Martin Dreyer, but it is absolutely impossible to read it. First, acquaintance with programming begins with the study of classes and objects. Too complicated and incomprehensible. Secondly, in one paragraph of essence there are three pages of examples from the real world. Too much. I am not ready to spend so much time on the story of how a girl on a desert island convinces a robot-alien to make an elephant pick a coconut.



There is another extreme. Tutorials from mycsharp.ru and the “Complete Guide to the C # 8.0 Programming Language and .NET Core 3 Platform” at metanit.com/sharp/tutorial



There are code examples from which you can reuse something, but the examples are too abstract like “Let's write a program that puts objects a, b and c in memory, it's so exciting!” But this is slightly better than elephants and coconuts, especially since I have my own teaching methodology.



In general, I decided that I could handle it and set off to install Visual Studio.



Record 2



Everything went according to plan. The next day, I caught the Black Screen. I didn’t understand what it was, but it looks like the video driver was updated ahead of Windows and turned out to be incompatible with the old version.



Once I was neat in such things. With any problems, I simply restored the OS partition from a carefully prepared image, but it was a long time ago. Windows has become more stable, I have become more careless. And so.



I spent a week trying to fix everything. I tried all the recipes. I found a way to start the command line, got the ancient Far, transferred the files, on the third attempt I picked up the appropriate assembly (I use custom Windows) with the already installed problematic update.



Here I must say a few words about my background. I do not know how to program, but in general I consider myself a computer person. About 8 years old, the first PC in my life appeared at our place. Packard Bell 486. Cool at the time. It had two programs - Lexicon and Info-accountant.







I played Lexicon.



From the characters I, / and \ I drew a race track, and the cursor was a car. Such are the toys nailed to the floor. No internet, no friends with computers (at first). I had fun as I could.



In addition to two programs, the computer was accompanied by a full-fledged MS-DOS on a stack of floppy disks and I often experimented with different teams. I got to the one called QBASIC.



There was a small manual in basic, and I understood something. He learned one or two commands, and was able to write programs that displayed any letters on the screen. It was cool.



In addition, there were computer science lessons in high school. Turbo Pascal. We were taught to write real code. Of course, in notebooks. But I had a computer (already some Pentium) and I could practice.



So I met with the most basic things. If-then-else constructs and loops. The peak of my skill was the Roulette program. Roulette was a game. You choose a number from 0 to 10 and make a bet. Guessed - a bonus, no - loss of a bet. And so, until the money runs out. I was very proud.



Nevertheless, I was a complete humanitarian and did not begin to connect my life with computer science. So after school experiments, I did not think about programming for about 19 years.



ABOUT! Visual Studio seems to be completed. What a pretty one. All black. Let's figure out what's what.



Record 3



Actually, this is not my first attempt at programming. About a year ago, a whim already came upon me, and I tried VBA. He was able to master the syntax of several teams and wrote the same roulette as it was then at school. Only better. Now I had a graphical interface of fields and buttons, as well as functions for saving and loading the game (data was stored in excel tables).



Nevertheless, I quickly dropped the case. For the most part, from the inability to use images normally, and maybe also from laziness. I don’t remember exactly. In any case, we will consider my VBA experience a kind of warm-up. Now it's time for a real hardcore.



I spent 4-5 nights at Sharpe. I read two chapters from the textbook. What can I say? I am not at all inspired by the academic approach based on writing pointless programs that do abstract computing. I'm not sure that I can be proud of the application that considers the area of ​​the triangle.



So I came up with my own curriculum. With blackjack and ... well, almost. I learned some of the most important commands: Console.WriteLine for displaying labels, Console.Readline for keyboard input, arithmetic operations, an if-else conditional construct, while loop, and of course Random for random numbers.



You probably guessed it already. Again I wrote console roulette :)







Sensations are a delight. She works! You can lose or win in it! And this is a real program - it has 68 lines of incomprehensible code, including as many as 5 called methods.



Was it difficult? Yes. I read manuals, made mistakes and broke everything, spent hours searching for defects, read manuals again, got confused in the code and rewrote everything several times, simplifying constructions in loops and removing unnecessary variables.



But the result was worth it. I am full of hope and move on.



Record 4



The first hurdle. I got to the concept of classes and objects. The concept is completely incomprehensible. How would the idea itself be clear, but how and why to apply it? Create classes for player, casino and roulette? Then come up with objects in them? And what will they do? Too tight. It seems unnatural and just confuses the program.



It came to me in a few days. I walked with my son in the playground and looked at the world around with object-oriented eyes.



class  {string Name;}, class  {string Name; int Age;}   = new  ();   = new  ();
      
      





...



And then I understood everything. My roulette program is too dumb and primitive. Classes are simply not needed there. It was a good exercise to learn procedural programming, but nothing more.



It was necessary to come up with a new exercise in order to understand the classes. And I came up with. Right here I sat on a bench and wrote myself a puzzle on the phone. Make a mini RPG. There will be a hero with his parameters of strength and health. Each turn, he will go into a new cave and meet a troll there with his own parameters. They will fight by rolling a die, as in Never Interest. And there you can find weapons and armor.



The main classes asked for themselves:





It should be interesting. Can I handle it? Easily. Go.



Record 5



It didn’t work out easily. I quickly sketched classes with fields. I found out from the textbook what a designer is and washed down the designers of armor and weapons. I made the interface, figuring out how to change the color of the text.



Now I have a greeting, player stats, weapons and armor displayed on my screen. There is a menu from which you can start the next move or exit the game.







And even that was not easy. I constantly came across errors. Special when porting code to methods. Some garbage does not exist in any context. Another garbage is not available due to the level of protection ...



I really missed some kind of guide for dummies on how to structure the code. Where to put the method for generating the item? To class Item? To the class Program? Just leave in code? We have to experiment, catch and correct errors.



Separate two days I was tormented with a random number generator, which gave me a shield and a sword with the same parameters. I realized that normal solutions are not yet available to my mind and paused for one second between two generations.



Then the euphoria ended and a stupor began. I managed to generate two items manually, naming them Weapon1 and Armor1. But I will need to do this after each move, save them in the inventory, change ... How do I create objects programmatically? How to generate their names and, all the more, choose the parameters necessary for changing their parameters?



VBA was easier. The Internet is full of script examples for each particular task. Finding and reusing the right one is not a problem. Very often I didn’t understand at all half of the code written, but it worked magically, and then I have to figure it out.



The textbook does not help. They already talk about lambda expressions and multithreading, and examples are still at the level of “Find the sum of all even numbers in a number of numbers”. Boredom. A search on the Internet also does not give answers, because I somehow formulate the question wrongly.



I suspect that you need to connect the database and write objects there. I have to learn SQL.



All Articles