Thursday, January 19, 2017

Functions, Functions Everywhere

/* AD:SNIP ------------------------------------------------------------


 (Picture copyright: My own work, but you are welcome to distribute it freely or use it as desktop background if you happen to like it.)

First things first, I promised and keep my promises always, that I would advertise for a free german RPG set in the Star Trek Universe. (Most of the regular visitors do  happen to speak English and some other languages as well. As long as you don't speak Melmacian, you should be understood). There are a number of very friendly mostly adult players at the beginning, in between, or way above and beyond their thirties, but also some young players from 15 up to 30. They all are dedicated Star Trek and Star-Wars fans! There is also a wonderful radio belonging to this project, as well as an IRC-Chat for you to hang out there. It is a Chat-RPG after all! (The chatroom is also the place you will find me on an almost regular, spelling daily - basis.) 

You can find the project here: https://www.st-city.net/
The RPG homepage can be found here: http://www.startrek-redemption.de/ueber-uns/
The radio homepage can be found here: https://isn.fm/
And last but not least the IRC-Chat: https://www.st-city.net/chat/
As well as the address for Clients such as mIRC: http://www.startrek-redemption.de/chat/

Klaas, Lady, _7th, On_Tour_Casio, all the regular players, would love to see you pay a visit and maybe stay for a while to play along, listen to the radio, or just be a friendly addition in our friendly chatroom and to have fun with the team, and myself!
----------------------------------------------------------- AD:SNAP */

Finally I got done with all the programming challenges of chapter 6 ... Time to celebrate! Now, how were these challenges? Difficult? Well, some of them were, some were very easy, even fun to write code for, and the last one was nothing short of mind-numbing.

When I read what was to do, and it said there in the heading of the Challenge: Group Project, whether I should just leave it be. Then I thought it wouldn't hurt to round-off what I learned during this chapters challenges, so I went to work away on it. More than once during the three plus some hours it took to write this one, the song the nameless character in the Action-RPG "Two Worlds I" sings spooked through my head. In this game, almost every time it rains, the character starts to sing: "Its raining, its boooooring, the old man is snooooring ...".

I still finished it, and maybe it was a good decision. I learned about another way of overloading a function. In this case this was not wanted nor asked for. In Programming Challenge 6.25, in the function prototype, I declared some default arguments. I did this in another challenge as well where it was asked for, which was Programming Challenge 6.14.

I declared them at the end of the prototype as it should be, but when I called the function in main, all I got was a failure that the function is overloaded, and that the same function is used elsewhere. So, what was the problem then? I declared the default arguments, and the first time around I used some variable names that I already used in one of the other functions. So I changed them for the default arguments, which didn't get me anywhere.

The solution I found was that I had to use the functions default arguments not only in the prototype header, but also in the functions definition as well. I can't remember reading or having learned about that in the sub-chapter that covers default arguments. In the end everything worked and this is what counts. My final thought on this last project is: While the programming challenge asking for overloading functions dealt with exactly that, this one I would christen "Function Overkill", as the challenge asked for writing separate functions for each and every thing that is asked for by the user.

The challenge to solve I liked the most in this chapter was the Rock Paper Scissors Game. I think I wrote something along the lines of - "My first game ... be it ever so humble", yet I think that being a wee bit proud about it is in order, I let you as my readers judge on that one.

On the whole my understanding of loops and their inner workings, as well as my love for the ternary operator, or as I call it in my source-code comments "Conditional statement", grew deeper, as well as my understanding how and when to use it. In the game project I wanted to use it three times, as it offered itself as a perfect solution to circumvent lengthy if-else blocks. The one and only time I actually were able to use it to full effect was in the function which determines the winner of each round.

The other two places I wanted it to use was to display the winning choices. As I said, it would have been a mere 4 lines of code for each of the other two functions. But the problem was that every time the function got in scope, and the winner or winning choices were to be displayed, the program displayed all three conditions, which looked like this:

   * R-P-S Master has chosen O Rock - O Rock beats Scissors % O < R-P-S Master has won
   * R-P-S Master has chosen [] Paper - [] Paper beats Rock [O] R-P-S Master has won
   * R-P-S Master has chosen %< Scissors - %< Scissor beats Paper %> |-_ R-P-S Master has won

I thought I could shift around the problem by introducing some if/else statement, but that didn't work. So I let this idea be and just wrote the if/else clauses instead. I guess it was just the nature of entering a function block that saw all conditions as true as far as winning conditions are concerned, and displayed them accordingly. Although the ternary, taken outside any function, did work as I wrote them.

That is the strange and funny things, I would not say shortcomings of C++ when it comes to functions, that I have learned. For the game itself, which wasn't very challenging at all, it was the first time that I chose to write sort of a prototype, see what works best, then take the things working over to version two, and then to assemble the final version. I also used to use my Mindmanager to create a map for the project, to get some starting ground to work off of. In the final version the map deviates in several points, but I guess this is a natural process. I didn't update the map every time for every change, as this would have taken time away, and unnecessarily so, from writing the game itself.

In bigger projects, in my own future game as well, this approach of not updating any reference material will be dropped. It is easy to lose track of what is and isn't done, and this is something I wouldn't want to risk. Now, after this wall of text, here is something for the eye, the mindmap for the Rock Paper Scissors game.



Doing this chapters challenges I also noticed that I slavishly held myself back in not writing any  functions unasked for to solve the problems. That is up to around Challenge 22, where it is specifically asked for to write one's own functions. I can't remember having held back in any of the previous chapters in using that what I learned in the specific chapter. Deviating a little here and there, making it more fun to write code for, or maybe to amuse people who happen to learn and read or take my code as blueprint for their own work, I find that acceptable. As reference I name the Overloaded Hospital Challenge. I use names of Piers Anthony's Xanth series Characters, Com Passion, Jenny Elf (making for a very tiny hospital bill), Che Centaur, and others. 

I don't have to answer to anyone, there are no teachers, or professors who mark my work and decide whether I pass or fail a challenge, this is why I allow myself this little luxury. I do write and learn this privately to gain knowledge in my language of choice, to solve that, which I learn for some time in the future: Writing and publishing my own game. The only judgment I accept is that of you, that is my readers, who happen to pass by once or come by to visit my blog on a regular basis. Because the fruits of my work are there for you to take after all, and in that respect I make sure that all challenges do what is written, bound to what is asked for in terms of functions, loops, whatever else is asked for, and that the output is correct. In the end it is up to you whether my code is worth something, or does help solve your problems, or overcoming any obstacle you have to face. Just DON'T USE IT FOR YOUR HOMEWORK ASSIGNMENT! You have been warned, I don't take responsibility for your course score dropping below sea-level.

Now, all that said, and many more things I think should probably be said leaving it unsaid, I now look forward to chapter 7. Arrays. I don't know how steep the learning curve will be, or how difficult arrays are in relation to what I learned in the past 3-4 months. But then, does it matter? I look at it this way, as I did since Starting Out with C++, another word or words in my vocabulary list, allowing me to write shorter programs, in a less roundabout way. And there are always some difficulties so it goes double that it does not really matter whether this one or the next, or the final chapter proofs to be more difficult.

So, with this, I wish all of my fellow learners that they never give up, are able to overcome their current problems, so they, too, are able to taste the sweet fruits of success when overcoming a particularly difficult challenge or problem!  

No comments:

Post a Comment