Advertisement
Guest User

Untitled

a guest
Jul 29th, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. _Mines = {};
  2. APP_Mines.step = 1;
  3. // $('.challenge.minen').css('padding-top', $('.produktinfos.first').height()+30);
  4. $('.produktinfos.first').css('position', 'absolute');
  5.  
  6. // static values
  7. var playFieldSize = 5; // it's always square
  8. var mineNumber = 4;
  9. var site = 'hz';
  10.  
  11. function initPlayField(playFieldSize, mineNumber) {
  12.   var playField = {};
  13.   for (var i = 0; i < playFieldSize; i++) {
  14.     var column = {};
  15.     for (var j = 0; j < playFieldSize; j++) {
  16.       column[j] = 0;
  17.     }
  18.     playField[i] = column;
  19.   }
  20.  
  21.   for (var i = 0; i < mineNumber; i++) {
  22.     do {
  23.       var mineX = parseInt(Math.random()*playFieldSize);
  24.       var mineY = parseInt(Math.random()*playFieldSize);
  25.     } while (playField[mineX][mineY] >= 9);  // repeat until we hit a coordinate which doesn't already contain a bomb
  26.  
  27.     playField[mineX][mineY] = 9;
  28.     // increment neighbour cards:
  29.  
  30.     //  row above
  31.     if(mineY > 0) {
  32.       if (mineX > 0) playField[mineX-1][mineY-1]++;
  33.       playField[mineX][mineY-1]++;
  34.       if (mineX < playFieldSize-1) playField[mineX+1][mineY-1]++;
  35.     }
  36.  
  37.     // same row
  38.     if (mineX > 0) playField[mineX-1][mineY]++;
  39.     if (mineX < playFieldSize-1) playField[mineX+1][mineY]++;
  40.  
  41.     // row below
  42.     if (mineY < playFieldSize-1) {
  43.       if (mineX > 0) playField[mineX-1][mineY+1]++;
  44.       playField[mineX][mineY+1]++;
  45.       if (mineX < playFieldSize-1) playField[mineX+1][mineY+1]++;
  46.     }
  47.   }
  48.   // now every card containing a value >= 9 indicates a bomb
  49.  
  50.   return playField;
  51. }
  52.  
  53. function exposeCards($cards, playField) {
  54.   $cards.each(function(){
  55.     var x = this.name.match(/x(\d?)/)[1];
  56.     var y = this.name.match(/y(\d?)/)[1];
  57.     var imgName = playField[x][y]>0 ? playField[x][y] : 'leer';
  58.     if (parseInt(imgName) >= 9) imgName = 'bombe';
  59.     $(this).replaceWith('<img src="http://games.hoerzu.de/games/app/gamesimages/hz/'+imgName+'.gif" />');
  60.   });
  61. }
  62.  
  63. function renderPlayField(playField) {
  64.   //var playFieldSize = playField.length*playField.length; // square assumed
  65.   var i = 0;
  66.   var x, y;
  67.   $('.spielfeld td').each(function(){
  68.     x = i%playFieldSize;
  69.     y = parseInt(i/playFieldSize);
  70.     $(this).html('<input type="submit" name="x'+x+'y'+y+'" value="?">');
  71.     i++;
  72.   });
  73. }
  74.  
  75. function cycleInfoPages() {
  76.   var $infoPages = $('.minen-container > .produktinfos');
  77.   $($infoPages[APP_Mines.step-1]).hide();
  78.   APP_Mines.step++;
  79.   if (APP_Mines.step > $infoPages.length) APP_Mines.step = 1;
  80.   $($infoPages[APP_Mines.step-1]).hide().css('visibility', 'visible');
  81.   $($infoPages[APP_Mines.step-1]).show();
  82.  
  83.   APP_Mines.clickcounter++;
  84.   if(!(APP_Mines.clickcounter%APP_Mines.refreshfreq)) {
  85.     try { reloadAds('minen'); }
  86.     catch(err) {}
  87.   }
  88. }
  89.  
  90. function startGame() {
  91.   var playField = initPlayField(playFieldSize, mineNumber);
  92.   $('.meldung').hide();
  93.   renderPlayField(playField);
  94.  
  95.   $('.spielfeld input').unbind().click(function(){
  96.     try { tvdTrack(window.location.href); }
  97.     catch(err) {}
  98.  
  99.     var x = this.name.match(/x(\d?)/)[1];
  100.     var y = this.name.match(/y(\d?)/)[1];
  101.  
  102.     if (playField[x][y] < 9) { // no bomb
  103.       if($('.spielfeld input').length == mineNumber+1) { // all bombless cards exposed already?
  104.         // congratulations, but there is no cake
  105.         exposeCards($('.spielfeld input'), playField);
  106.  
  107.         var finishTime = new Date();
  108.         var durationSeconds = parseInt((finishTime-APP_Mines.startTime)/1000);
  109.         $('.meldung.won .spieldauer span').text(durationSeconds);
  110.         $('.meldung.won').fadeIn();
  111.         won = true;
  112.       } else {
  113.         cycleInfoPages();
  114.         exposeCards($(this), playField);
  115.       }
  116.     } else {
  117.       // somebody set up us the bomb
  118.       cycleInfoPages();
  119.       exposeCards($('.spielfeld input'), playField);
  120.       $('.meldung.gameover').fadeIn();
  121.     }
  122.   });
  123. }
  124.  
  125. // main
  126. $('.meldung.gameover input').click(function(){
  127.   cycleInfoPages();
  128.   startGame();
  129. });
  130.  
  131. APP_Mines.startTime = new Date();
  132. APP_Mines.clickcounter = 0;
  133. APP_Mines.refreshfreq = 2; // refresh ads every nth click
  134. startGame();
  135. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement