Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function counter() {
- //For loop to determine output of Radio buttons: Road
- for (memoryCount = 0; memoryCount < 3; memoryCount++) {
- if (memory.dot[memoryCount].checked)
- break;
- };
- console.log(memoryCount);
- newBoard();
- };
- //Initiate Variables
- var memory_values = [];
- var memory_tile_ids = [];
- var tiles_flipped = 0;
- //Create randomrizer var
- var rand = Math.random();
- var rand = rand.toFixed(2);
- //Array to create function for shuffling contecnts of memory_array var (the card's face up content)
- Array.prototype.memory_tile_shuffle = function() {
- //prepares var for randomrizing the string
- var i = this.length, j, temp;
- //Randomrizes the order of the array selected
- while (--i > 0) {
- j = Math.floor(Math.random() * (i + 1));
- temp = this[j];
- this[j] = this[i];
- this[i] = temp;
- };
- };
- //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)
- function newBoard() {
- //If/Else statement for checking randomrized result
- if(memoryCount == 0) {
- 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'];
- }else if(memoryCount == 1) {
- 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'];
- }else if(memoryCount ==2) {
- var memory_array = ['!', '!', '@', '@', '#', '#', '$', '$', '%', '%', '^', '^', '&', '&', '*', '*', '(', '(', ')', ')', '-', '-', '+', '+'];
- };
- //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)
- tiles_flipped = 0;
- var output = '';
- memory_array.memory_tile_shuffle();
- //sets div's, and onclick function to every element, from the var memory_array, containing the images!
- for (var i = 0; i < memory_array.length; i++) {
- output += '<div id="tile_' + i + '" onclick="memoryFlipTile(this,\'' + memory_array[i] + '\')"></div>';
- };
- document.getElementById('memory_board').innerHTML = output;
- };
- //Flipping tile when clicked
- //(ノಠ益ಠ)ノ彡┻━┻
- function memoryFlipTile(tile, val) {
- //If the amount of tiles flipped is not to the max (2 tiles), allow flip by setting the underlying image to the div
- if (tile.innerHTML == "" && memory_values.length < 2) {
- tile.style.background = '#FFF';
- tile.innerHTML = val;
- //If the amount of tiles flipped is zero, allow flip
- if (memory_values.length == 0) {
- memory_values.push(val);
- memory_tile_ids.push(tile.id);
- //If the amount of tiles flipped is one, then allow flip!
- } else if (memory_values.length == 1) {
- memory_values.push(val);
- memory_tile_ids.push(tile.id);
- //If 2 tiles are flipped, set the value to 2
- if (memory_values[0] == memory_values[1]) {
- tiles_flipped += 2;
- // Clear both arrays
- memory_values = [];
- memory_tile_ids = [];
- // Check to see if the whole board is cleared, and directly start over when cleared!
- if (tiles_flipped == memory_array.length) {
- alert("Board cleared... generating new board");
- document.getElementById('memory_board').innerHTML = "";
- newBoard();
- };
- } else {
- //Flip wrong tiles back to normal
- //┬─┬ ノ( ゜-゜ノ)
- function flip2Back() {
- var tile_1 = document.getElementById(memory_tile_ids[0]);
- var tile_2 = document.getElementById(memory_tile_ids[1]);
- //Don't touch, Magic
- tile_1.style.background = 'url(AppComm_Logo_Transperant113x113.png) no-repeat';
- tile_1.innerHTML = "";
- tile_2.style.background = 'url(AppComm_Logo_Transperant113x113.png) no-repeat';
- tile_2.innerHTML = "";
- //Clear both arrays so you can flip again
- memory_values = [];
- memory_tile_ids = [];
- };
- //Wait 0.7 sec (700ms) before flipping back to prevent "fast random clicking"
- setTimeout(flip2Back, 700);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment