{"id":98,"date":"2015-02-13T18:15:49","date_gmt":"2015-02-13T18:15:49","guid":{"rendered":"http:\/\/buildanhtml5game.com\/?page_id=98"},"modified":"2015-02-13T18:15:49","modified_gmt":"2015-02-13T18:15:49","slug":"chapter-7-exercise-1-ending-levels-efficiently","status":"publish","type":"page","link":"http:\/\/buildanhtml5game.com\/?page_id=98","title":{"rendered":"Chapter 7, Exercise 1 &#8211; Ending Levels Efficiently"},"content":{"rendered":"<p><em>Toward the end of each level, the player can only have bubbles of one, two, or three colors left on the board. Giving them a bubble of a color that won\u2019t match any of these causes the player to waste a shot and can make the game more difficult to complete. Change the bubble\u2011generating algorithm so that it gives players only bubbles of a color that can potentially form a match. For example, if only red and blue bubbles remain, the firing bubble should be either red or blue. You will need to amend <code>getNextBubble<\/code> in game.js and choose a bubble type from one of the types that exist in the <code>Board <\/code>object.<\/em><\/p>\n<p>There are two stages to this:<\/p>\n<ol>\n<li>Work out which types of bubbles still existing on the board<\/li>\n<li>Only choose from those types of bubbles<\/li>\n<\/ol>\n<p>This process will work whether the board is full of bubbles or only has a handful remaining.<br \/>\nFirst, we&#8217;re going to add a new method to Board.js to return an array containing the types of bubbles that exist on the board. This doesn&#8217;t need to be sorted and we just want the four type values:<\/p>\n<p><code>var Board = function(){<br \/>\n&nbsp;&nbsp;\u2026<br \/>\n&nbsp;&nbsp;this.isEmpty = function(){<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;return this.getBubbles().length == 0;<br \/>\n&nbsp;&nbsp;};<br \/>\n&nbsp;&nbsp;this.getTypes = function(){<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;var types = [];<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;var bubbles = this.getBubbles();<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;for(var i=0;i&lt;bubbles.length;i++){<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var bubble = bubbles[i];<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($.inArray(bubble.getType(), types) == -1){<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;types.push(bubble.getType());<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;return types;<br \/>\n&nbsp;&nbsp;};<br \/>\n&nbsp;&nbsp;return this;<br \/>\n};<\/code><\/p>\n<p>This uses one of jQuery&#8217;s utility functions to check whether an element already exists in an array:<\/p>\n<p><code>$.inArray(bubble.getType(), types)<\/code><\/p>\n<p>You can <a href=\"http:\/\/api.jquery.com\/jquery.inarray\/\">read about how this works here: http:\/\/api.jquery.com\/jquery.inarray\/<\/a>.<\/p>\n<p>The return result will be an array that might look like this: [3,0,2,1] at the start of the game, which represents that all four bubble types are present. Towards the end, it might only by [0,3], which would mean only two bubble types are left.<\/p>\n<p>Next, we want to choose from these available bubble types when we create a new bubble. We can do this in the <code>getNextBubble <\/code>function in game.js:<\/p>\n<p><code>var getNextBubble = function(board){<br \/>\nvar types = board.getTypes();<br \/>\n&nbsp;&nbsp;var type = types[Math.floor(Math.random() * types.length)];<br \/>\n&nbsp;&nbsp;var bubble = BubbleShoot.Bubble.create(null,null,type);<br \/>\n&nbsp;&nbsp;bubbles.push(bubble);<br \/>\n&nbsp;&nbsp;bubble.setState(BubbleShoot.BubbleState.CURRENT);<br \/>\n&nbsp;&nbsp;bubble.getSprite().addClass(\"cur_bubble\");<br \/>\n&nbsp;&nbsp;var top = 470;<br \/>\n&nbsp;&nbsp;var left = ($(\"#board\").width() - BubbleShoot.ui.BUBBLE_DIMS)\/2;<br \/>\n&nbsp;&nbsp;bubble.getSprite().css({<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;top : top,<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;left : left<br \/>\n&nbsp;&nbsp;});<br \/>\n&nbsp;&nbsp;$(\"#board\").append(bubble.getSprite());<br \/>\n&nbsp;&nbsp;BubbleShoot.ui.drawBubblesRemaining(numBubbles);<br \/>\n&nbsp;&nbsp;numBubbles--;<br \/>\n&nbsp;&nbsp;return bubble;<br \/>\n};<\/code><\/p>\n<p>Now, if you get towards the end of a level, you should never see a bubble of a color that isn&#8217;t already present on the board.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Toward the end of each level, the player can only have bubbles of one, two, or three colors left on the board. Giving them a bubble of a color that won\u2019t match any of these causes the player to waste a shot and can make the game more difficult to complete. Change the bubble\u2011generating algorithm [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":22,"menu_order":71,"comment_status":"open","ping_status":"open","template":"practice-answer-page.php","meta":[],"_links":{"self":[{"href":"http:\/\/buildanhtml5game.com\/index.php?rest_route=\/wp\/v2\/pages\/98"}],"collection":[{"href":"http:\/\/buildanhtml5game.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/buildanhtml5game.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/buildanhtml5game.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/buildanhtml5game.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=98"}],"version-history":[{"count":1,"href":"http:\/\/buildanhtml5game.com\/index.php?rest_route=\/wp\/v2\/pages\/98\/revisions"}],"predecessor-version":[{"id":99,"href":"http:\/\/buildanhtml5game.com\/index.php?rest_route=\/wp\/v2\/pages\/98\/revisions\/99"}],"up":[{"embeddable":true,"href":"http:\/\/buildanhtml5game.com\/index.php?rest_route=\/wp\/v2\/pages\/22"}],"wp:attachment":[{"href":"http:\/\/buildanhtml5game.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=98"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}