{"id":4267,"date":"2020-08-01T00:01:07","date_gmt":"2020-08-01T07:01:07","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4267"},"modified":"2020-08-15T08:20:25","modified_gmt":"2020-08-15T15:20:25","slug":"multiples-of-four","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4267","title":{"rendered":"Multiples of Four"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/08\/0801-figure1.png\" alt=\"\" width=\"550\" height=\"20\" class=\"aligncenter size-full wp-image-4273\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/08\/0801-figure1.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/08\/0801-figure1-300x11.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/08\/0801-figure1-500x18.png 500w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><br \/>\nI enjoy playing number games. I&#8217;m not good at it, but I enjoy playing them. Recently such a game piqued my curiosity enough to consider it for my monthly <em>C For Dummies<\/em> exercise: 21<br \/>\n<!--more--><br \/>\nNo, not the card game 21, also known as Blackjack. This is a counting game:<\/p>\n<p>Each player states a number between 1 and 3 to be added to a running total. Player One guesses 2, then Player Two adds 3 to make 5, and so on up to 21. The first player to arrive at 21 either wins or loses, depending on the game variation.<\/p>\n<p>I coded this number game and will present it in a future Lesson. But wanting to cheat, I did some research to discover whether any magic exists behind winning the game.<\/p>\n<p>The immediate thing I discovered, assuming the desire is to reach (not avoid) 21, was that whoever gets to 18 first loses. Once you hit or just pass 18, the next player can add 1, 2, or 3 to reach 21. You can wind back this value to try to figure where the first magic number is, after which you can follow a pattern to win the game. I didn&#8217;t bother pursuing this approach further.<\/p>\n<p>Later, I read that it&#8217;s possible to win providing you go second and add values so that the result is a multiple of 4. For example, if the other player guesses 3, you guess 1 to make 4. If they guess 6, you guess 2 to make 8. And so on.<\/p>\n<p>Anyway.<\/p>\n<p>It turns out the multiple-of-four trick didn&#8217;t work. Still, I was intrigued enough to code a simulation of this process, which is this month&#8217;s Exercise.<\/p>\n<p>Your task is to write code that pits two players against each other. Call them &#8220;player&#8221; and &#8220;computer.&#8221;<\/p>\n<p>Starting with a total value of zero, the &#8220;player&#8221; goes first and selects a random value between 1 and 3. The &#8220;computer&#8221; goes next, adding a random value between 1 and 3 &mdash; but ensuring that the total is evenly-divisible by 4. This process repeats until the total value is over 30.<\/p>\n<p>Here&#8217;s sample output:<\/p>\n<p><code>Player adds 2, total is 2<br \/>\nComputer adds 2 to make 4<br \/>\nPlayer adds 1, total is 5<br \/>\nComputer adds 3 to make 8<br \/>\nPlayer adds 2, total is 10<br \/>\nComputer adds 2 to make 12<br \/>\nPlayer adds 3, total is 15<br \/>\nComputer adds 1 to make 16<br \/>\nPlayer adds 2, total is 18<br \/>\nComputer adds 2 to make 20<br \/>\nPlayer adds 3, total is 23<br \/>\nComputer adds 1 to make 24<br \/>\nPlayer adds 3, total is 27<br \/>\nComputer adds 1 to make 28<br \/>\nPlayer adds 2, total is 30<br \/>\nComputer adds 2 to make 32<\/code><\/p>\n<p>No matter what the &#8220;player&#8221; guesses, the &#8220;computer&#8221; adds 1, 2, or 3 to make the total a multiple of 4: 4, 8, 12, 16, 20, 24, 28, and finally 32.<\/p>\n<p>Sure, this is a silly exercise (aren&#8217;t they all?), but it&#8217;s how I was able to determine the multiple-of-four trick couldn&#8217;t guarantee a win for my 21 number-guessing game.<\/p>\n<p>Try this Exercise on your own before you peek at <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4279\">my solution<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>No matter what the player guesses, the computer must round up the value to the next multiple of four. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4267\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-4267","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4267","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4267"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4267\/revisions"}],"predecessor-version":[{"id":4316,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4267\/revisions\/4316"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}