More Game Programming, with a Detailed Explanation
We will continue creating interactive games from scratch. They are simplistic video games, as modern computer and gaming consoles have such lifelike graphics, animation, and sounds that even the military is using the technology to train solders for combat scenarios. We, however, are essentially using a device only meant to be a controller of equipment to provide us with a little entertainment as we learn the fundamentals of coding. Our next program is the card game 21, also called blackjack. Card games work well for learning how to code because the games have specific rules and outcomes. We have a player and dealer, each given two cards. The cards’ numerical values range from 2 up to 10, and there are four picture cards, for a total of 13 possibilities. We will code a random number of 2 to 14, which gives us all of the possibilities. The code for this is “random (2, 15)”. This first version of the game is incomplete, but it begins the design process. Other more complete versions of the game are provided later. This first iteration does not use nonduplicate numbers and does not address the point value of picture cards; we will do that later. For now, we review concepts in a slow, methodical process and begin to develop the game. Open the Arduino IDE and load the code shown in Listing 7-1 without statements followed by the double forward slashes (//), as they are comment lines only seen by other programmers and not recognized by the processor as code. Comments are important for you and others to understand the thought process of the programmer. When writing your own code, please use them. Also, do not type in the line numbers as they are shown for illustration purposes only.