I am always interested in finding ways to teach programming to students and I recently came across a book that teaches C++ through game development. I decided to post my own notes as I read along. The book was written using Windows, and I will be using a Mac, so I will comment any changes that I found I had to make as I go.
The book is “Beginning C++ Through Game Programming (4th Edition) by Michael Dawson”
In order to follow along you need g++ installed. The easiest way to do that is to follow the setting up your mac for development post, or by installing XCode on your mac and adding developer commands to the command line. You can work in XCode to code, or you can install and use another editor, but it is helpful to use one that has syntax highlighting.
Also note, this book does not discuss any game programming libraries or graphics methods. All of the games are entirely text based. So it will all run in the terminal.
In this chapter, you learn the basics of variable and using standard IO. The culminating game is Lost Fortune. The game is a “Mad Libs” kind of game where you enter some information and it tells you a funny story. This is a great first game idea! I really like its simplicity. Since the student can really make any story they like, it is fun to run with a friend, and see if they enjoy the surprise of the story once you put in the various variables. This would be a great first lesson as it has very little code requirements, and has a fun game to share.
Chapter two introduces boolean logic and “the game loop.” I think it is just a perfect amount of additional code to add to a second lesson. The game at the end is the “guess the number in my head” game. In order to create the game, it also introduces random number generation. Although the book does not discuss it much, the system or random numbers generated using the technique in this chapter are fairly weak. It really does point out the importance of keeping it simple with the students. No reason to overload them with complex code when the simple code works. Great Chapter. The game at the end is fun to play and share, and it really feels like a game.
In this chapter, we learn about looping and arrays and the ending game is a word jumble. A word is scrambled up using the random technique and a swap paired with a for loop. Again, this is a fun game and adding on Arrays and a for loop make sense. This game is also fun. It is also very smart to add a hint mechanism to the game. Students can dream up all sorts of words to add and test it on their friends, but it can easily be a difficult game to play. Adding the hint makes it more fun for both sharing and playing. I am still having a great time with the simple escalation of code complexity. It is good the games are getting more fun, but the code is not overly complex.
This chapter introduces the Standard Template Library (STL) and the game at the end is a variant of “Hangman.” This game is also fun, and most students of mine are already familiar with the paper and pencil version so they will be easily able to understand the game before they start to code. At this stage, most students who are learning to code for the first time will still find the basics of coding to be daunting. It’s great to have a familiar game to work on so as to reduce the cognitive load of learning some more complex code.
Introducing functions and the game “Mad Lib”. I usually teach functions to students very early to help them to keep their main function and game loop very simple. A normal game like the one’s in this book would have a few functions like ‘setup’, ‘instructions’ and then ‘play.’ I think it makes a lot of sense to also discuss pseudo-code at the same time. Since often what one line of pseudo-code represents is actually a function. It also makes sense to discuss scope, paramters, default parameters and global variables here.
Chapter 6 & 7
Introducing Pointers and References and a Tic-Tac-Toe game. This chapter was the first in the book where I noticed I was programming in C++. Most of what has been learned up to this point are the same in almost every language that students might learn and it is very applicable to them. Of course, the STL is not a part of that, but most programming languages do have some version of the STL that can be used and has data structures similar to those used in the earlier chapters.
References are something that is common in C++, and this book does a very good job of breaking it down for the student. It also points out with “Notes” within the chapter common pitfalls and problems that students will run into when they try to use references in their own projects. Pointers are also not common in most programming languages that are used to introduce students to software development. This introductory chapter is great and covers pointers and how to use them. We find out more
Pointers are also not common in most programming languages that are used to introduce students to software development. This introductory chapter is great and covers pointers and how to use them. We find out more about how to deal with memory addresses in C++. This chapter is also very well written, and having two chapters to cover this is a good amount. More would not hurt, as this is always one of the most difficult parts of C++.
I do wish the author would have gone over the final version of Tic-Tac-Toe, but this is left up to the reader. Honestly, that is a fine way to do it because you really should be doing the exercises that are in the book as the only way to really learn something like pointers is to practice it, but I do hope readers are not lost as they try to understand the program. Pointers are a very important part of C++ and they are the reason why it is such a powerful language.
Chapter 8 is an introduction to Object Oriented Programming and the game at the end is Critter Caretaker. The book does a very good job introducing the syntax of C++ and the Q&A at the end is very good to help readers understand the value of what they learned in the chapter.
The book shows the peculiar syntax of C++ constructors but does not mention why it is different, and that is expected since most readers would not know another language. If you are wondering why it is valuable, you may want to try not using them, and using const parameters! Mostly this feature was introduced in C++ to help manage constructor initializations. Here is more information. For new students, it is not crazy, but most languages do not do it this way.
Chapter 9 dives deep into many more C++ topics including friend functions, heap allocation, and memory allocation. The chapter is very dense and contains a lot of material that could be expanded upon. Generally, there is enough information on each topic to introduce the concepts and an example to show why you might want to use it.
Instead of a game, this chapter has a game lobby prototype that can be used in really any game to allow players to be added to the game and paired for a game in the future. There is no network code, this is just the queue of people waiting to play.
The final chapter of the book has the game of blackjack and discusses inheritance and polymorphism. Generally, inheritance is not a topic for someone who is just learning object oriented coding methodologies, only because it does cause some issues if used incorrectly.
The final chapter includes a blackjack game, and all the trimmings (in text that is.) So you can use a lot of this code again if you wanted to build another card game. I really liked that about this book, the games are really just starting points for readers to enhance.
Here is my repo for the code that I created while reading the book. The repository also contains many of the code examples from the book. And for the final chapter, I broke out the individual classes in to individual files. A very common organization strategy that is usually used in C++.
I would recommend this book to someone who did not know how to program much or at all and wanted to learn C++ and also enjoyed video games. This is not a book for someone who is interested in game programming in general, it is really for the individual who wants to learn C++. If this sounds like you, and you want to learn to program and you enjoy video games, then this is a great book! It also has some great simple games in that could be used to teach students to code in general.