Guest User

Untitled

a guest
Oct 17th, 2014
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function counter() {
  2.     //For loop to determine output of Radio buttons: Road  
  3.     for (memoryCount = 0; memoryCount < 3; memoryCount++) {
  4.         if (memory.dot[memoryCount].checked)
  5.         break;
  6.     };
  7.  
  8.     console.log(memoryCount);
  9.     newBoard();
  10. };
  11.  
  12.  
  13. //Initiate Variables
  14. var memory_values = [];
  15. var memory_tile_ids = [];
  16. var tiles_flipped = 0;
  17.  
  18.  
  19. //Create randomrizer var
  20. var rand = Math.random();
  21. var rand = rand.toFixed(2);
  22.    
  23.  
  24. //Array to create function for shuffling contecnts of memory_array var (the card's face up content)
  25. Array.prototype.memory_tile_shuffle = function() {
  26.  
  27.  
  28. //prepares var for randomrizing the string
  29. var i = this.length, j, temp;
  30.  
  31.  
  32. //Randomrizes the order of the array selected
  33. while (--i > 0) {
  34.     j = Math.floor(Math.random() * (i + 1));
  35.     temp = this[j];
  36.     this[j] = this[i];
  37.     this[i] = temp;
  38.     };
  39. };
  40.  
  41.  
  42. //Function to create a new board of cards on start or on completion, and adding onclick function to the tiles and a div value (game area)
  43. function newBoard() {
  44.  
  45.  
  46.     //If/Else statement for checking randomrized result
  47.     if(memoryCount == 0) {
  48.         var memory_array = ['1', '1', '2', '2', '3', '3', '4', '4', '5', '5', '6', '6', '7', '7', '8', '8', '9', '9', '10', '10', '11', '11', '12', '12'];
  49.     }else if(memoryCount == 1) {
  50.         var memory_array = ['A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'E', 'E', 'F', 'F', 'G', 'G', 'H', 'H', 'I', 'I', 'J', 'J', 'K', 'K', 'L', 'L'];
  51.     }else if(memoryCount ==2) {
  52.         var memory_array = ['!', '!', '@', '@', '#', '#', '$', '$', '%', '%', '^', '^', '&', '&', '*', '*', '(', '(', ')', ')', '-', '-', '+', '+'];
  53.     };
  54.  
  55.     //forces the amount of tiles flipped to zero, fixes start problem that it thinks you completed the game on page-load (counter starts counting if not set to zero)
  56.     tiles_flipped = 0;
  57.     var output = '';
  58.     memory_array.memory_tile_shuffle();
  59.     //sets div's, and onclick function to every element, from the var memory_array, containing the images!
  60.     for (var i = 0; i < memory_array.length; i++) {
  61.         output += '<div id="tile_' + i + '" onclick="memoryFlipTile(this,\'' + memory_array[i] + '\')"></div>';
  62.     };
  63.    
  64.     document.getElementById('memory_board').innerHTML = output;
  65.    
  66. };
  67.  
  68.  
  69.      //Flipping tile when clicked
  70.      //(ノಠ益ಠ)ノ彡┻━┻
  71.      function memoryFlipTile(tile, val) {
  72.         //If the amount of tiles flipped is not to the max (2 tiles), allow flip by setting the underlying image to the div
  73.          if (tile.innerHTML == "" && memory_values.length < 2) {
  74.              tile.style.background = '#FFF';
  75.              tile.innerHTML = val;
  76.              //If the amount of tiles flipped is zero, allow flip
  77.              if (memory_values.length == 0) {
  78.                  memory_values.push(val);
  79.                  memory_tile_ids.push(tile.id);
  80.              //If the amount of tiles flipped is one, then allow flip!
  81.              } else if (memory_values.length == 1) {
  82.                  memory_values.push(val);
  83.                  memory_tile_ids.push(tile.id);
  84.                  //If 2 tiles are flipped, set the value to 2
  85.                  if (memory_values[0] == memory_values[1]) {
  86.                      tiles_flipped += 2;
  87.                      
  88.                      
  89.                      // Clear both arrays
  90.                      memory_values = [];
  91.                      memory_tile_ids = [];
  92.                      
  93.                      
  94.                      // Check to see if the whole board is cleared, and directly start over when cleared!
  95.                      if (tiles_flipped == memory_array.length) {
  96.                          alert("Board cleared... generating new board");
  97.                          document.getElementById('memory_board').innerHTML = "";
  98.                          newBoard();
  99.                      };
  100.                  } else {
  101.                     //Flip wrong tiles back to normal
  102.                     //┬─┬ ノ( ゜-゜ノ)
  103.                      function flip2Back() {
  104.                          var tile_1 = document.getElementById(memory_tile_ids[0]);
  105.                          var tile_2 = document.getElementById(memory_tile_ids[1]);
  106.                          
  107.                          
  108.                          //Don't touch, Magic
  109.                          tile_1.style.background = 'url(AppComm_Logo_Transperant113x113.png) no-repeat';
  110.                          tile_1.innerHTML = "";
  111.                          tile_2.style.background = 'url(AppComm_Logo_Transperant113x113.png) no-repeat';
  112.                          tile_2.innerHTML = "";
  113.                          
  114.                          
  115.                          //Clear both arrays so you can flip again
  116.                          memory_values = [];
  117.                          memory_tile_ids = [];
  118.                      };
  119.                      
  120.                      //Wait 0.7 sec (700ms) before flipping back to prevent "fast random clicking"
  121.                      setTimeout(flip2Back, 700);
  122.                      
  123.                  }
  124.              }
  125.          }
  126.      }
Advertisement
Add Comment
Please, Sign In to add comment