Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- _Mines = {};
- APP_Mines.step = 1;
- // $('.challenge.minen').css('padding-top', $('.produktinfos.first').height()+30);
- $('.produktinfos.first').css('position', 'absolute');
- // static values
- var playFieldSize = 5; // it's always square
- var mineNumber = 4;
- var site = 'hz';
- function initPlayField(playFieldSize, mineNumber) {
- var playField = {};
- for (var i = 0; i < playFieldSize; i++) {
- var column = {};
- for (var j = 0; j < playFieldSize; j++) {
- column[j] = 0;
- }
- playField[i] = column;
- }
- for (var i = 0; i < mineNumber; i++) {
- do {
- var mineX = parseInt(Math.random()*playFieldSize);
- var mineY = parseInt(Math.random()*playFieldSize);
- } while (playField[mineX][mineY] >= 9); // repeat until we hit a coordinate which doesn't already contain a bomb
- playField[mineX][mineY] = 9;
- // increment neighbour cards:
- // row above
- if(mineY > 0) {
- if (mineX > 0) playField[mineX-1][mineY-1]++;
- playField[mineX][mineY-1]++;
- if (mineX < playFieldSize-1) playField[mineX+1][mineY-1]++;
- }
- // same row
- if (mineX > 0) playField[mineX-1][mineY]++;
- if (mineX < playFieldSize-1) playField[mineX+1][mineY]++;
- // row below
- if (mineY < playFieldSize-1) {
- if (mineX > 0) playField[mineX-1][mineY+1]++;
- playField[mineX][mineY+1]++;
- if (mineX < playFieldSize-1) playField[mineX+1][mineY+1]++;
- }
- }
- // now every card containing a value >= 9 indicates a bomb
- return playField;
- }
- function exposeCards($cards, playField) {
- $cards.each(function(){
- var x = this.name.match(/x(\d?)/)[1];
- var y = this.name.match(/y(\d?)/)[1];
- var imgName = playField[x][y]>0 ? playField[x][y] : 'leer';
- if (parseInt(imgName) >= 9) imgName = 'bombe';
- $(this).replaceWith('<img src="http://games.hoerzu.de/games/app/gamesimages/hz/'+imgName+'.gif" />');
- });
- }
- function renderPlayField(playField) {
- //var playFieldSize = playField.length*playField.length; // square assumed
- var i = 0;
- var x, y;
- $('.spielfeld td').each(function(){
- x = i%playFieldSize;
- y = parseInt(i/playFieldSize);
- $(this).html('<input type="submit" name="x'+x+'y'+y+'" value="?">');
- i++;
- });
- }
- function cycleInfoPages() {
- var $infoPages = $('.minen-container > .produktinfos');
- $($infoPages[APP_Mines.step-1]).hide();
- APP_Mines.step++;
- if (APP_Mines.step > $infoPages.length) APP_Mines.step = 1;
- $($infoPages[APP_Mines.step-1]).hide().css('visibility', 'visible');
- $($infoPages[APP_Mines.step-1]).show();
- APP_Mines.clickcounter++;
- if(!(APP_Mines.clickcounter%APP_Mines.refreshfreq)) {
- try { reloadAds('minen'); }
- catch(err) {}
- }
- }
- function startGame() {
- var playField = initPlayField(playFieldSize, mineNumber);
- $('.meldung').hide();
- renderPlayField(playField);
- $('.spielfeld input').unbind().click(function(){
- try { tvdTrack(window.location.href); }
- catch(err) {}
- var x = this.name.match(/x(\d?)/)[1];
- var y = this.name.match(/y(\d?)/)[1];
- if (playField[x][y] < 9) { // no bomb
- if($('.spielfeld input').length == mineNumber+1) { // all bombless cards exposed already?
- // congratulations, but there is no cake
- exposeCards($('.spielfeld input'), playField);
- var finishTime = new Date();
- var durationSeconds = parseInt((finishTime-APP_Mines.startTime)/1000);
- $('.meldung.won .spieldauer span').text(durationSeconds);
- $('.meldung.won').fadeIn();
- won = true;
- } else {
- cycleInfoPages();
- exposeCards($(this), playField);
- }
- } else {
- // somebody set up us the bomb
- cycleInfoPages();
- exposeCards($('.spielfeld input'), playField);
- $('.meldung.gameover').fadeIn();
- }
- });
- }
- // main
- $('.meldung.gameover input').click(function(){
- cycleInfoPages();
- startGame();
- });
- APP_Mines.startTime = new Date();
- APP_Mines.clickcounter = 0;
- APP_Mines.refreshfreq = 2; // refresh ads every nth click
- startGame();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement