dh-Materialien
JavaScript    
Programmierbeispiele
 

Pairs

n =       

// Online-Spiel Pairs (c)2014 dh

Function
.prototype.timer = function (interval, calls, onend) {
  var count = 0;
  var payloadFunction = this;
  var tStart = new Date();
  var callback = function() {
    return payloadFunction(tStart, count);
  }
  var endFunction = function () {
    if (onend) {
      onend(startTime, count, calls);
    }
  }
  var call = function() {
    count++;
    if ((count < calls)&&(callback() != false)) {
      window.setTimeout(call, interval);
    } else {
      endFunction();
    }
  }
  call();
}


var i;
var j;
var jmin;
var jmax;
 
var n = 8;
var actn = 8;
var foundpairs = 0;

var cards = new Array();
var opc = new Array();

var num = 0;
var nOpen = 0;

var iAct = 0;
var jAct = 0;

var tStart;

function nm(x, y) {
  xhex=x.toString(16);
  yhex=y.toString(16);
  return "b"+xhex+yhex;
}

function xc(name) {
  return parseInt(name[0], 10);
}
function yc(name) {
  return parseInt(name[1], 10);
}


function setCard(i, j, img, target) {
  if (target == "src") {
    document.getElementById(nm(i,j)).src = img+".jpg";
    document.getElementById(nm(i,j)).alt = "nil.jpg";
  }
  if (target == "alt") {
    document.getElementById(nm(i,j)).alt = img+".jpg";
  }
}


function getCardSrc(i, j) {
  img = document.getElementById(nm(i,j)).src;
  pos = img.length - 7;
  return img.substr(pos, 3);
}
function getCardAlt(i, j) {
  alt = document.getElementById(nm(i,j)).alt;
  pos = alt.length - 7;
  return alt.substr(pos, 3);
}


function compareCards(card1, card2) {
  var result = false;
  var img1 = getCardSrc(card1[0],card1[1]);
  var img2 = getCardSrc(card2[0],card2[1]);
  if (img1 == img2) result = true;
  return result;
}

function InArray(elmt, arr) {
  result = false;
  for (k=0; k<arr.length; k++)
    if (arr[k] == elmt)
      result = true;
  return result;
}

function clearArray(arr) {
  arr.splice(0, arr.length);
}


function createRandomList(num) {
  var list = new Array();
  clearArray(list);
  var r;
  while (list.length < num) {
    r = num*Math.random();
    r = Math.round(r + 0.5)-1;
    if (InArray(r, list) == false) list.push(r);
  }
  return list;
}


var pic = new Array(
"abl", "abs", "acp", "acr", "ail", "bbs", "bhz", "blu", "bnt", "bow", "mno", "pqr", "cfg", "cfn", "cic", "clr", "cmp", "cpm", "cps", "crw", "dag", "dgg", "dkl", "dmk", "dms", "dny", "drg", "drr", "dth", "eat", "enl", "epr", "ete", "fem", "gbg", "gcr", "hgf", "hld", "hse", "imy", "jnl", "jun", "kan", "kgb", "khs", "kir", "lac", "let", "mar", "mel", "mov", "mst", "ntd", "ora", "pah", "pap", "pas", "pbl", "pgr", "prd", "rbg", "riv", "sgb", "slf", "srf", "tld", "unt", "utd", "utt", "vln", "vnt", "vny", "vtg", "wbs", "wwf", "ybl", "ylw", "zys", "zyt", "egl", "brs", "byl", "cez", "mdc", "spt", "ind", "lon", "ftl", "fbt", "des", "mft", "sgm", "big", "dsm", "lga", "tsw", "asw", "cbt", "tfs", "abc", "def", "ghi", "jkl");


function init() {
  n = parseInt(document.f_input.e_n.value, 10);
  if (n < 6) n = 6;
  if (n > 10) n = 10;
  if (Math.round(n/2) - n/2 != 0) n = n + 1;
    document.f_input.e_n.value = n.toString(10);

  if (n < 10) {
    for (i=0; i<6; i++) {
      setCard(i, 0, "wht", "src");
      setCard(i, 9, "wht", "src");
    }
  }
  if (n < 8) {
    for (i=0; i<6; i++) {
      setCard(i, 1, "wht", "src");
      setCard(i, 8, "wht", "src");
    }
  }

  actn = n;
  collectCards(n);
  ausgabe("resultat", "");
}


function startGame(n) {
  document.f_input.e_n.value = actn.toString(10);
  shuffleCards(cards, "src");
  for (i=0; i<6; i++)
    for (j=jmin; j<=jmax; j++) {
      setCard(i, j, "nil", "alt");
    }
  turnAllCards();
  clearArray(opc);
  num = 0;
  ausgabe ("resultat", num);
  foundpairs = 0;
  var time = new Date();
  tStart = time.getTime();
}


function shuffleCards(cards, target) {
  var newcards = new Array();
  var len = cards.length;
  var list = createRandomList(len);
  for (i=0; i<len; i++) newcards.push(cards[list[i]]);
  cards = newcards;

  var k = 0;
  for (i=0; i<6; i++)
    for (j=jmin; j<=jmax; j++) {
      setCard(i, j, cards[k], target);
      k++;
    }
}


function turnAllCards() {
  for (i=0; i<6; i++)
    for (j=jmin; j<=jmax; j++) {
      turnCard(nm(i, j).substr(1,2));
    }

}


function collectCards(n) {
  var all = pic.length;
  var image;
  var r;
  var newcards = new Array();
  clearArray(cards);

  jmin = (10-n)/2;
  jmax = 9 - jmin;

  while (cards.length < 6*n/2) {
    r = all*Math.random();
    r = Math.round(r + 0.5)-1;
    image = pic[r];
    if (!InArray(image, cards)) cards.push(image);
  }

  var len = cards.length;
  for (i=0; i<len; i++) cards.push(cards[i]);

  shuffleCards(cards, "src");

  for (i=0; i<6; i++)
    for (j=jmin; j<=jmax; j++)
      setCard(i, j, "nil", "alt");
}


function swapImages(element) {
  var img_alt = element.alt;
  var img_src = element.src;

  element.alt = img_src;
  element.src = img_alt;
}


function turnCard(plc) {
  var element = document.getElementById("b"+plc);
  var wid;
  var k = 0;

  var turnImg = function () {
    if (k < 40) {
      if (k < 20) {
        k++;
        wid = Math.round(60*Math.cos(k*Math.PI/(2*19)));
        element.width = wid.toString();
      }
      if (k == 20) {
        swapImages(element);
      }
      if (k >= 20) {
        k++;
        wid = Math.round(60*Math.sin((k-20)*Math.PI/(2*19)));
        element.width = wid.toString();
      }
    } else {
      return false;
    }
  };

  var completed = function () {};
  turnImg.timer(1, 50, completed);
}


function play(plc) {
  var ToTurn = false;
  iAct = xc(plc);
  jAct = yc(plc);

  if ((jAct >= jmin)&&(jAct <= jmax)&&(foundpairs < 3*n)) {

  switch (opc.length) {

  case 0:
    if (getCardSrc(iAct, jAct) == "nil") {
      turnCard(plc);
      opc.push([iAct,jAct]);
    }
    break;

  case 1:
    if ((iAct == opc[0][0])&&(jAct == opc[0][1])) {
      turnCard(plc);
      clearArray(opc);
      num++;
      ausgabe("resultat", num);
    } else {
      if (getCardAlt(iAct, jAct) != "wht") {
        turnCard(plc);
        opc.push([iAct,jAct]);
        num++;
        ausgabe("resultat", num);
        if (foundpairs == 3*n-1) {
          endGame();
          break;
        }
      }
    }
    break;

  case 2:
    plc0 = opc[0][0].toString()+opc[0][1].toString();
    plc1 = opc[1][0].toString()+opc[1][1].toString();

    if (compareCards(opc[0],opc[1])) {
      setCard(opc[0][0], opc[0][1], "wht", "alt");
      setCard(opc[1][0], opc[1][1], "wht", "alt");
      clearArray(opc);
      foundpairs++;
      check = ((plc!=plc0)&&(plc!=plc1)&&(getCardAlt(iAct,jAct)));
      if (check != "wht") {
        turnCard(plc);
        opc.push([iAct,jAct]);
      }
    } else {
      check = ((plc!=plc0)&&(plc!=plc1)&&(getCardAlt(iAct,jAct)));
      if (check != "wht") {
        turnCard(plc0);
        turnCard(plc1);
        clearArray(opc);
        turnCard(plc);
        opc.push([iAct,jAct]);
      }
    }
  }
  }
}


function endGame() {
  var time = new Date();
  time.getTime();
  tGame = (time.getTime() - tStart)/60000;
  min = tGame.toFixed(2);
  min = min.replace(".", ",");
  txt = num.toString(10)+" Spielzüge in "+min+" Minuten";
  ausgabe ("resultat", txt);
  clearArray(opc);
}


var defaultNode = document.createTextNode('');
function ausgabe (ST, out) {
  with (document.getElementById (ST)) {
    outNode = document.createTextNode('');
    appendChild (defaultNode);
    replaceChild (outNode, document.getElementById(ST).firstChild);
    outNode = document.createTextNode(out);
    appendChild (defaultNode);
    replaceChild (outNode, document.getElementById(ST).firstChild);
  }
}
 



 Home   Back   Top