dh-Materialien
JavaScript    
Programmierbeispiele
 

Tic-Tac-Toe


// Online-Spiel TicTacToe (c)2008 dh

var
box = new Array(9); // das Spielfeld
var ttt = new Array(8);
ttt[0] = new Array (0,1,2);
ttt[1] = new Array (3,4,5);
ttt[2] = new Array (6,7,8);
ttt[3] = new Array (0,3,6);
ttt[4] = new Array (1,4,7);
ttt[5] = new Array (2,5,8);
ttt[6] = new Array (2,4,6);
ttt[7] = new Array (0,4,8);

function dsum(k) {
  s = box[ttt[k][0]] + box[ttt[k][1]] + box[ttt[k][2]];
  return s;
}

function dd(m,n) {
  danger = ((dsum(m) == 4)&&(dsum(n) == 4)) ? true : false;
  return danger;
}

function isGameOver() {
  for (k=0; k<=7; k++) {
    if ((dsum(k) == 3)||(dsum(k) == 12)) {
      gameover = true;
      win = k;
    }
  }
}

var ply = 0; //  0 = Spiel hat noch nicht begonnen
             // -1 = Computer ist am Zug
             //  1 = Spieler ist am Zug

var image = "";
var iopt = -1;
var win = 0;

var list1 = new Array (0,2,4,7);
var list5 = new Array (2,3,4,8);
var list7 = new Array (1,4,6,8);
var list3 = new Array (0,4,5,6);

var step = 0;

var wait;

function init() {
  for (j=0; j<=8; j++)
    document.getElementById("b" + j).src = "b00.gif";
  for (j=0; j<=8; j++)
    box[j] = 0;
  step = 0;
  gameover = false;
  ausgabe ("resultat", "");
}

function computertostart() {
  init();
  ply = -1;
  cset();
}

function usertostart() {
  init();
  ply = 1;
}

function set(i) {
  if ((box[i] == 0)&&(ply == 1)) {
    document.getElementById("b" + i).src = "b_x.gif";
    box[i] = 4; // Der Wert 4 repräsentiert ein gesetztes Kreuz
    step++;
    ply = -ply;
    wait = window.setTimeout("cset()", 300);
  }
}

function cset() {
  isGameOver();
  if (gameover) { // Der Spieler hat gewonnen
    ausgabe ("resultat", "Gratuliere!")
    for (k=0; k<=2; k++)
      document.getElementById("b" + ttt[win][k]).src = "bxx.gif";
  } else {
    if (step < 9) {
      iopt = -1;

      for (k=0; k<=7; k++) {
        if (dsum(k) == 2) {
          for (l=0; l<=2; l++) {
            z = ttt[k][l];
            if (box[z] == 0) {
              iopt = z;
              gameover = true; // Der Computer hat gewonnen
              ausgabe ("resultat", "Verloren!");
              win = k;
            }
          }
        }
      }

      if (iopt == -1) {
        for (k=0; k<=7; k++) {
          if (dsum(k) == 8) {
            for (l=0; l<=2; l++) {
              z = ttt[k][l];
              if (box[z] == 0) iopt = z;
            }
          }
        }
      }

      if (step == 1) {
        if (box[4] == 4) {
          z = 2*Math.round(3*Math.random());
          if (z == 4) z = 8;
          iopt = z;
        }
        if (box[0] + box[2] + box[6] + box[8] == 4) {
          iopt = 4;
        }
        if (box[1] == 4) {
          iopt = list1[Math.round(3*Math.random())];
        }
        if (box[5] == 4) {
          iopt = list5[Math.round(3*Math.random())];
        }
        if (box[7] == 4) {
          iopt = list7[Math.round(3*Math.random())];
        }
        if (box[3] == 4) {
          iopt = list3[Math.round(3*Math.random())];
        }
      }

      if ((step == 3)&&((dsum(6) == 9)||(dsum(7) == 9))) {
        if (box[4] == 1) {
          iopt = 2*Math.round(3*Math.random()) + 1;
        } else {
          for (k=0; k<=4; k++) {
            if(box[2*k] == 0) iopt = 2*k;
          }
        }
      }

      if (iopt == -1) {
        if ((box[0] == 0)&&((dd(0,3))||(dd(0,7))||(dd(3,7)))) iopt = 0;
        if ((box[1] == 0)&&(dd(0,4))) iopt = 1;
        if ((box[2] == 0)&&((dd(0,5))||(dd(0,6))||(dd(5,6)))) iopt = 2;
        if ((box[3] == 0)&&(dd(1,3))) iopt = 3;
        if ((box[4] == 0)&&((dd(7,6))||(dd(7,1))||(dd(7,4)))) iopt = 4;
        if ((box[4] == 0)&&((dd(6,1))||(dd(6,4))||(dd(1,4)))) iopt = 4;
        if ((box[5] == 0)&&(dd(1,5))) iopt = 5;
        if ((box[6] == 0)&&((dd(2,3))||(dd(2,6))||(dd(3,6)))) iopt = 6;
        if ((box[7] == 0)&&(dd(2,4))) iopt = 7;
        if ((box[8] == 0)&&((dd(2,7))||(dd(2,5))||(dd(5,7)))) iopt = 8;
      }

      if ((iopt == -1)&&(step < 8)) {
        do z = Math.round(8*Math.random()); while (box[z] != 0);
        iopt = z;
      }

      if (step == 8)
      for (k=0; k<=8; k++)
        if (box[k] == 0) iopt = k;

        document.getElementById("b" + iopt).src = "b_o.gif";
        box[iopt] = 1; // Der Wert 1 repräsentiert einen gesetzten Kringel
        step++;

        if ((step <= 9)&&(!gameover)) ply = -ply;
        if (gameover)
          for (k=0; k<=2; k++)
            document.getElementById("b"+ttt[win][k]).src = "boo.gif";
      }
      if ((step >= 9)&&(!gameover)) ausgabe ("resultat", "Unentschieden!");
    }
}

var defaultNode = document.createTextNode("");
function ausgabe (ST, out) {
// gibt den Wert von out an einer bestimmten Stelle im Dokument aus

  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