10-shut-the-box.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. This activity uses conditionals to provide instructions based on current state of the game. Loops are used to iterate through tiles.
  3. The player must roll the dice and select the tiles to reduce the total down to zero. The game is won when the total is zero
  4. and there are no tiles left.
  5. */
  6. //***********************************Scene - Initialize When Scene Starts Event*********************************************//
  7. // Tile creation
  8. if($this.scene.state() == "PLAY") {
  9. // create an array to store available tiles
  10. var availableTiles = [];
  11. // start with a target amount of 0
  12. var targetAmount = 0;
  13. // create the cloned tiles
  14. for (var t = 1; t < 10; t++) {
  15. var clonedTile = numberedTile.clone();
  16. var tileNumber = clonedTile.findName("number_" + t);
  17. // update the text on the clone's label object
  18. tileNumber.text(t);
  19. // assign the number to the tile
  20. clonedTile.number = t;
  21. // add the tile to the availableTiles array
  22. availableTiles.push(clonedTile);
  23. // place the cloned tile in the scene by setting its x value.
  24. clonedTile.x(20 + 85 * (t - 1));
  25. }
  26. // remove the original tile
  27. numberedTile.remove();
  28. }
  29. // run this function when a dice is clicked
  30. $this.rollDice = function() {
  31. // only allow rolls if targetAmount = 0
  32. if(targetAmount === 0) {
  33. // randomly roll the dic
  34. var die1Value = random(1,6);
  35. var die2Value = random(1,6);
  36. // update the sprite frames
  37. die1.frameIndex(die1Value);
  38. die2.frameIndex(die2Value);
  39. // set the target amount and update UI
  40. targetAmount = die1Value + die2Value;
  41. totalText.text(targetAmount);
  42. // update message text
  43. message.text("Click the tiles to subtract from the total.");
  44. // check for valid moves after every roll
  45. $this.checkForValidMoves();
  46. }
  47. };
  48. // run this function when a tile is clicked
  49. $this.processTile = function(clickedTile) {
  50. // make sure the tile's number is smaller than targetAmount
  51. if(clickedTile.number <= targetAmount) {
  52. // subtract clicked number from targetAmount
  53. targetAmount -= clickedTile.number;
  54. // update the text
  55. totalText.text(targetAmount);
  56. // hide clicked number tile
  57. clickedTile.visible(false);
  58. // loop through the availableTiles array
  59. for (var i = 0; i < availableTiles.length; i++) {
  60. // find the clickedTile in the availableTiles array
  61. if (availableTiles[i] == clickedTile) {
  62. // and remove it with splice function
  63. availableTiles.splice(i, 1);
  64. }
  65. }
  66. if (availableTiles.length === 0) {
  67. $this.checkForEndOfGame();
  68. } else {
  69. // if there are tiles, check for valid moves
  70. // check for valid moves after every clicked tile
  71. $this.checkForValidMoves();
  72. }
  73. }
  74. };
  75. $this.checkForValidMoves = function() {
  76. if(targetAmount > 0) {
  77. // find the smallest available tile
  78. var smallestTile = availableTiles[0];
  79. if (smallestTile.number > targetAmount) {
  80. message.text("There are no valid moves. Play again!");
  81. }
  82. } else {
  83. message.text("Click the dice to roll again!")
  84. }
  85. };
  86. $this.checkForEndOfGame = function() {
  87. if (targetAmount === 0) {
  88. message.text("Good job! You win!");
  89. } else {
  90. message.text("Game over. Try again!");
  91. }
  92. }
  93. //*********************************die 1 and die 2 Objects - Mouse Click Event*********************************************//
  94. $this.scene.rollDice();
  95. //****************************************numberedTile Object - Mouse Click***********************************************//
  96. $this.scene.processTile($this)