Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var tilesImages = [
- "./img/1.png", "./img/2.png", "./img/3.png", "./img/4.png", "./img/5.png", "./img/6.png", "./img/7.png", "./img/8.png", "./img/9.png", "./img/10.png",
- "./img/11.png", "./img/12.png", "./img/13.png", "./img/14.png", "./img/15.png", "./img/16.png", "./img/17.png", "./img/18.png", "./img/19.png", "./img/20.png"
- ];
- //var difficulty = ""; // 1, 2 ,
- var numberOfTiles = 20;
- var tilesInRow = 5;
- var tilesIndexArray = []; // numerek obrazka
- var clickedTilesArray = []; // 2 element
- var movesCount = 0;
- var removedCoupleTiles = 0;
- var flipAnimationEnds = true;
- $(document).ready(function() {
- startNewGame();
- $('.tile').click(function() {
- $(this).flip({
- direction: 'rl',
- speed: 300
- });
- });
- });
- function resetGameProperties() {
- tilesIndexArray = [];
- clickedTilesArray = [];
- removedCoupleTiles = 0;
- movesCount = 0;
- flipAnimationEnds = true;
- }
- function generateIndexesForTiles() {
- for (var i=0; i < numberOfTiles; i++) {
- tilesIndexArray.push( Math.floor(i/2) ); // wrzucamy pary indkesów kafli 0,0,1,1..
- }
- }
- function populateGameField() {
- var gameField = $('#gameField');
- for (var i=0; i < numberOfTiles; i++) {
- var tile = $('<div class="tile"></div>'); // podpiac id...
- tile.data('tileIndex', i);
- tile.data('tileType', tilesIndexArray[i]);
- tile.bind('click', function() {
- tileClicked( $(this) )
- });
- gameField.append(tile);
- if ( (i+1) % tilesInRow == 0) {
- gameField.append('<br />');
- }
- }
- }
- /* http://bost.ocks.org/mike/shuffle/ - Fisher–Yates Shuffle */
- function shuffle(array) {
- var m = array.length, t, i;
- // While there remain elements to shuffle…
- while (m) {
- // Pick a remaining element…
- i = Math.floor(Math.random() * m--);
- // And swap it with the current element.
- t = array[m];
- array[m] = array[i];
- array[i] = t;
- }
- return array;
- }
- function startNewGame() {
- //var gameField = $('#gameField').empty(); // czysci element i jego potomkow, tylko element - remove()
- resetGameProperties();
- generateIndexesForTiles();
- tilesIndexArray = shuffle(tilesIndexArray);
- populateGameField();
- }
- function tileClicked(tile) {
- if (flipAnimationEnds) {
- if ( !clickedTilesArray[0] || tile.data('tileIndex') !== clickedTilesArray[0].data('tileIndex') ) {
- clickedTilesArray.push(tile);
- console.log('url(' + tilesImages[tile.data('tileType')] + ')');
- tile.flip({
- direction: "lr",
- content:
- tile.css({
- 'background' : 'url(' + tilesImages[tile.data('tileType')] + ')' + 'center center no-repeat'
- }),
- speed: 150
- });
- }
- if (clickedTilesArray.length == 2) {
- flipAnimationEnds = false;
- var equalTiles = clickedTilesArray[0].data('tileType') === clickedTilesArray[1].data('tileType');
- if (equalTiles) {
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement