07-ninja-puzzle.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. This activity uses arrays and loops to store and spawn tile objects. Swap algorithms are used to help program the logic. The player must click the tiles to complete a traditional slide puzzle.
  3. */
  4. //******************Scene Object - Initialize When Scene Starts Event****************//
  5. // this function takes an array
  6. // shuffles it randomly and returns it
  7. $this.shuffle = function(arr){
  8. arr.sort(() => random(1, -1));
  9. return arr;
  10. };
  11. // set up the scene
  12. if ($this.scene.state() == "PLAY") {
  13. // store the solution as ordered array of tile numbers
  14. $this.solution = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0];
  15. // use the shuffle function to randomize the puzzle tiles
  16. $this.puzzleTiles = $this.shuffle([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0]);
  17. // set up loop helper variables to place the tiles
  18. var xOffset = 200;
  19. var yOffset = 100;
  20. var tileSpacing = 100;
  21. // keep track of where we are in the puzzle array
  22. var tileIndex = 0;
  23. // loop through the grid row by row to place 16 tiles
  24. for (var row = 0; row < 4; row++) {
  25. for (var col=0; col < 4; col++) {
  26. // find the current tile number
  27. // based on the shuffled puzzle array
  28. var currentTileNumber = $this.puzzleTiles[tileIndex];
  29. // find the tile object
  30. var tile = $this.findName("tile" + currentTileNumber);
  31. // assign the tile its number for easy use
  32. tile.number = currentTileNumber;
  33. // make the tile see-through
  34. tile.opacity(0.5);
  35. // place the tilein the correct spot in the grid
  36. tile.x(col * tileSpacing + xOffset);
  37. tile.y(row * tileSpacing + yOffset);
  38. // increment to the next tile
  39. tileIndex++;
  40. }
  41. }
  42. // after all the tiles have placed, check the solution
  43. $this.checkSolution();
  44. }
  45. // this function runs when the player clicks on a tile
  46. $this.checkMove = function(clickedTile) {
  47. // see how far away the clicked tile is from the blank tile
  48. // use the absolute value function to always get a positive number
  49. var xDistance = Math.abs(clickedTile.x() - tile0.x());
  50. var yDistance = Math.abs(clickedTile.y() - tile0.y());
  51. // if the clicked tile is only 1 tile away
  52. if ((xDistance === 100 && yDistance === 0) ||
  53. (xDistance === 0 && yDistance === 100 )) {
  54. // swap the two tiles using temporary variables
  55. // first store the location of the clicked tile
  56. var originalClickedTileX = clickedTile.x();
  57. var originalClickedTileY = clickedTile.y();
  58. // move the clicked tile to the blank tile's location
  59. clickedTile.x(tile0.x());
  60. clickedTile.y(tile0.y());
  61. // move the blank tile to the saved clicked tile location
  62. tile0.x(originalClickedTileX);
  63. tile0.y(originalClickedTileY);
  64. // swap the tiles in the puzzle array
  65. // find the original index of the clicked tile in the puzzleTiles array
  66. var originalClickedTileIndex = $this.puzzleTiles.indexOf(clickedTile.number);
  67. // find the original index of the blank tile in the puzzleTiles array
  68. var originalBlankIndex = $this.puzzleTiles.indexOf(0);
  69. // swap the blank tile and the clickedtile in the puzzle array
  70. $this.puzzleTiles[originalClickedTileIndex] = 0;
  71. $this.puzzleTiles[originalBlankIndex] = clickedTile.number;
  72. // after every swap, check for a solution
  73. $this.checkSolution();
  74. }
  75. };
  76. // this function runs aftrer every tile swap
  77. $this.checkSolution = function() {
  78. // store the number of correct tiles
  79. var correctTiles = 0;
  80. // loop through each tile in puzzle array
  81. for (var i = 0; i < 16; i++) {
  82. // find the tile
  83. var tile = $this.findName ("tile" + $this.puzzleTiles[i]);
  84. // compare the current tile with the correct tile
  85. if (tile.number === $this.solution[i]) {
  86. // if the tile is correct, make it completely visible
  87. tile.opacity(1);
  88. // increment the correctTiles variable
  89. correctTiles++;
  90. } else {
  91. // if the tile is incorrect, make it see- through
  92. tile.opacity(0.5);
  93. }
  94. }
  95. // update the message to the player
  96. message.text("Correct Tiles: " + correctTiles);
  97. };
  98. //************************all tiles - Mouse Click Event***************************//
  99. $this.scene.checkMove($this);