<!--
 //Copyright: Axel Hennig - webmaster@australienbilder.de
 var relDir = "";
 var ImgOffset = 11;
 var AmZug = 0;
 var newWin;
 var field = new Array(44);
 var steine = 0;
 var cards;
 var musscomm = "";

 function ShowZug()
  {
   AmZug = AmZug + 1;
   if (AmZug == 3) {AmZug = 1}
   if (AmZug == 2) 
     {document.score.tries.value = "ROT";}
   else
     {document.score.tries.value = "GELB";}
   if (steine>0)
    {document.images[3].src = relDir + "compleer.gif";
     document.images[3].alt = "Viel Spass!!!";}
  }

 function MenschZug(Spalte)
  {
   if ((steine < 42) && (field[Spalte]==0))
    { TestPos(Spalte);
      ShowZug();
      if ((AmZug == 2) && (document.game.compi.checked) && (steine < 42)) {CompiZug();}
    }
  }

 function TestGegVic(f)
  {
   var inarow = 0;
   var vorbei = 0;
   var x,y;
   // horizontal
   for (y = 1; y < 7; y++)
    { inarow = 0;
     for (x = 1; x < 8; x++)
      {
       if ((field[(y-1)*7+x] == f) || (field[(y-1)*7+x] == (f+2))) {inarow = inarow + 1;}
       else {inarow = 0};
       if (inarow == 4) {vorbei=1;}
      }
    }
   // vertikal
   for (x = 1; x < 8; x++)
    { inarow = 0;
     for (y = 1; y < 7; y++)
      {
       if ((field[(y-1)*7+x] == f) || (field[(y-1)*7+x] == (f+2))) {inarow = inarow + 1;}
       else {inarow = 0};
       if (inarow == 4) {vorbei=1;}
      }
    }
   // diagonal abfallend
   for (x = -3; x < 5; x++)
    { inarow = 0;
     for (y = 1; y < 7; y++)
      {
       if (((x+y)>0) && ((x+y)<8))
       {
        if ((field[(y-1)*7+x+y] == f) || (field[(y-1)*7+x+y] == (f+2)))
          {inarow = inarow + 1;}
        else {inarow = 0};
        if (inarow == 4) {vorbei=1;}
       }
      }
    }
   // diagonal aufsteigend
   for (x = -3; x < 5; x++)
    { inarow = 0;
     for (y = 1; y < 7; y++)
      {
       if (((x+y)>0) && ((x+y)<8))
       {
        if ((field[(6-y)*7+x+y] == f) || (field[(6-y)*7+x+y] == (f+2)))
          {inarow = inarow + 1;}
        else {inarow = 0};
        if (inarow == 4) {vorbei=1;}
       }
      }
    }
   return vorbei; 
  }

 function CompiZug()
  { var wirow,tx,dep,merkfeld,droh,merk2,vorn,hint,mitt;
    var diese;
    var nicht = new Array(0,0,0,0,0,0,0,0);
    var nicht2 = new Array(0,0,0,0,0,0,0,0);
    var moeg = new Array(0,0,0,0,0,0,0,0);
    var muss = 1;
    var muss2 = 1;
    var ernst = 0;
    musscomm="";
   // Gegner nicht zu 4 hinlegen
   for (tx = 1; tx < 8; tx++)
    {if (field[tx]==0)
      {dep = FallDown(tx);
       if (dep>1)
        {merkfeld=(dep-1)*7+tx;
         merk2=(dep-2)*7+tx;
         field[merkfeld] = 4;
         field[merk2] = 3;
         droh=TestGegVic(1);
         if (droh==1) {nicht[tx]=1;}
         field[merkfeld] = 0;
         field[merk2] = 0;
        }
       moeg[tx] = 1;
       if (nicht[tx]==0) {muss=0;}
      }
    }
   // Gegner nicht zu frei2frei hinlegen
   for (tx = 1; tx < 8; tx++)
    {if (field[tx]==0)
      {dep = FallDown(tx);
       if (dep>1)
        {if (tx<5)
          {hint=FallDown(tx+3);
           if ((field[tx+1]==1) && (field[tx+2]==1) && (hint==(dep-1)))
            {nicht2[tx]=1;}
          }
         if (tx>4)
          {vorn=FallDown(tx-3);
           if ((field[tx-1]==1) && (field[tx-2]==1) && (vorn==(dep-1)))
            {nicht2[tx]=1;}
          }
        }
       if ((nicht2[tx]==0) && (nicht[tx]==0)) {muss2=0;}
      }
    }
   // Zufall, aber nichts hinlegen
   do {wirow = Math.round(Math.random() * 7) + 1;}
   while ((field[wirow] != 0) || ((nicht[wirow]!=0) && (muss == 0))
          || ((nicht2[wirow]!=0) && (muss2 == 0)));
   // Gegner Dreier verbauen (horiz.)
   diese=0;
   for (tx = 1; tx < 5; tx++)
    {for (ty = 1; ty < 7; ty++)
     {vorn=FallDown(tx); hint=FallDown(tx+3);
      if ((field[(ty-1)*7+tx]==0) && (field[(ty-1)*7+tx+3]==0)
          && (field[(ty-1)*7+tx+1]==1) && (field[(ty-1)*7+tx+2]==1)
          && ((vorn==ty) || (hint==ty)))
      {if ((nicht[tx]==0) && (vorn==ty)) {diese=tx;}
       if ((nicht[tx+3]==0) && (hint==ty))
        {if ((diese==0) || ((steine % 3)==1)) {diese=tx+3;}}
      }
     }
    }
   if (diese != 0) {wirow=diese; ernst=3;}
   // Gegner Dreier verbauen (vert.)
   diese=0;
   for (tx = 1; tx < 8; tx++)
    {if (field[tx]==0)
     {vorn=FallDown(tx);
      if (vorn<5)
       {if ((field[vorn*7+tx]==1) && (field[(vorn+1)*7+tx]==1))
        {if (nicht[tx]==0)
          {if ((Math.round(Math.random()*10)+1)<8) {diese=tx;}}
        }
       }
     }
    }
   if ((diese != 0)  && ((Math.round(Math.random()*10)+1)<7)) {wirow=diese; ernst=3;}
   // Gegner Dreier verbauen (diag.aufst.)
   diese=0;
   for (tx = 2; tx < 6; tx++)
    {for (ty = 3; ty < 7; ty++)
     {vorn=FallDown(tx-1); hint=FallDown(tx+2);
      if (((field[ty*7+tx-1]==0) || ((ty*7+tx-1)>42))
          && ((field[(ty-3)*7+tx+2]==0) || (((ty-3)*7+tx+2)<1))
          && (field[(ty-1)*7+tx]==1) && (field[(ty-2)*7+tx+1]==1)
          && ((vorn==(ty+1)) || (hint==(ty-2))))
      {if ((nicht[tx-1]==0) && (vorn==(ty+1))) {diese=tx-1;}
       if ((nicht[tx+2]==0) && (hint==(ty-2)))
        {if ((diese==0) || ((steine % 3)==1)) {diese=tx+2;}}
      }
     }
    }
   if ((diese != 0) && ((Math.round(Math.random()*10)+1)<7)) {wirow=diese; ernst=3;}
   // Gegner Dreier verbauen (diag.abst.)
   diese=0;
   for (tx = 2; tx < 6; tx++)
    {for (ty = 1; ty < 5; ty++)
     {vorn=FallDown(tx-1); hint=FallDown(tx+2);
      if (((field[(ty-2)*7+tx-1]==0) || (((ty-2)*7+tx-1)<0))
          && ((field[(ty+1)*7+tx+2]==0) || (((ty+1)*7+tx+2)>42))
          && (field[(ty-1)*7+tx]==1) && (field[ty*7+tx+1]==1)
          && ((vorn==(ty-1)) || (hint==(ty+2))))
      {if ((nicht[tx-1]==0) && (vorn==(ty-1))) {diese=tx-1;}
       if ((nicht[tx+2]==0) && (hint==(ty+2)))
        {if ((diese==0) || ((steine % 3)==1)) {diese=tx+2;}}
      }
     }
    }
   if ((diese != 0)  && ((Math.round(Math.random()*10)+1)<7)) {wirow=diese; ernst=3;}
   // Gegner frei2frei verbauen
   diese=0;
   for (tx = 1; tx < 5; tx++)
    {for (ty = 1; ty < 7; ty++)
     {vorn=FallDown(tx); hint=FallDown(tx+3);
      if ((field[(ty-1)*7+tx]==0) && (field[(ty-1)*7+tx+3]==0)
          && (field[(ty-1)*7+tx+1]==1) && (field[(ty-1)*7+tx+2]==1)
          && (vorn==ty) && (hint==ty))
      {if (nicht[tx]==0) {diese=tx;}
       if (nicht[tx+3]==0)
        {if ((diese==0) || ((steine % 3)==1)) {diese=tx+3;}}
      }
     }
    }
   if (diese != 0) {wirow=diese; ernst=3;}
   // Gegner frei1frei1frei verbauen
   diese=0;
   for (tx = 1; tx < 4; tx++)
    {for (ty = 1; ty < 7; ty++)
     {vorn=FallDown(tx); mitt=FallDown(tx+2); hint=FallDown(tx+4);
      if ((field[(ty-1)*7+tx]==0) && (field[(ty-1)*7+tx+2]==0)
          && (field[(ty-1)*7+tx+4]==0)
          && (field[(ty-1)*7+tx+1]==1) && (field[(ty-1)*7+tx+3]==1)
          && (vorn==ty) && (hint==ty) && (mitt==ty))
      {if (nicht[tx+2]==0) {diese=tx+2;}
       if ((nicht[tx]==0) && (diese==0)) {diese=tx;}
       if ((nicht[tx+4]==0) && (diese==0)) {diese=tx+4;}
      }
     }
    }
   if (diese != 0) {wirow=diese; ernst=2;}
   // Gegner 4 verbauen
   diese=0;
   for (tx = 1; tx < 8; tx++)
    {if (field[tx]==0)
      {dep = FallDown(tx);
       merkfeld=(dep-1)*7+tx;
       field[merkfeld] = 3;
       droh=TestGegVic(1);
       if (droh==1) {diese=tx}
       field[merkfeld] = 0;
      }
    }
   if (diese != 0) {wirow=diese; ernst=1;}
   // selber frei2frei nutzen
   diese=0;
   for (tx = 1; tx < 5; tx++)
    {for (ty = 1; ty < 7; ty++)
     {vorn=FallDown(tx); hint=FallDown(tx+3);
      if ((field[(ty-1)*7+tx]==0) && (field[(ty-1)*7+tx+3]==0)
          && (field[(ty-1)*7+tx+1]==2) && (field[(ty-1)*7+tx+2]==2)
          && (vorn==ty) && (hint==ty))
      {if (nicht[tx]==0) {diese=tx;}
       if ((diese==0) && (nicht[tx+3]==0)) {diese=tx+3;}
      }
     }
    }
   if ((diese != 0) && ((ernst > 2) || (ernst==0))) {wirow=diese;}
   // selber frei1frei1frei nutzen
   diese=0;
   for (tx = 1; tx < 4; tx++)
    {for (ty = 1; ty < 7; ty++)
     {vorn=FallDown(tx); mitt=FallDown(tx+2); hint=FallDown(tx+4);
      if ((field[(ty-1)*7+tx]==0) && (field[(ty-1)*7+tx+2]==0)
          && (field[(ty-1)*7+tx+4]==0)
          && (field[(ty-1)*7+tx+1]==2) && (field[(ty-1)*7+tx+3]==2)
          && (vorn==ty) && (hint==ty) && (mitt==ty))
      {if (nicht[tx+2]==0) {diese=tx+2;}
      }
     }
    }
   if ((diese != 0) && (ernst != 1)) {wirow=diese;}
   // selber 4?
   diese = 0;
   for (tx = 1; tx < 8; tx++)
    {if (field[tx]==0)
      {dep = FallDown(tx);
       merkfeld=(dep-1)*7+tx;
       field[merkfeld] = 4;
       droh=TestGegVic(2);
       if (droh==1) {diese=tx;}
       field[merkfeld] = 0;
      }
    }
   if (diese != 0) {wirow=diese;}
   // Zug durchfuehren
   if (nicht[wirow]!=0) {musscomm="Aber da blieb mir ja keine bessere Wahl mehr!";}
   if (ernst==1) {musscomm="Aber ich mußte den letzten Stein ja so setzen!"}
   TestPos(wirow);
   ShowZug();
  }

 function Beginn()
  {if (steine == 0)
   {
    document.game.compi.checked = true;
    AmZug=2; CompiZug();
   }
  return;
  }

 function NewGame()
  {
   var x, y, tmp, cards;
   for (x = 1; x < 43; x++)
    {
     document.images[x+ImgOffset].src = relDir + "vgleer.gif";
     field[x] = 0;
    }
   AmZug = 0; ShowZug();
   cards = window.open("vierreg.htm","Regeln","height=570,width=560");
  }

 function FallDown(col)
  {
   var row = 1;
   while ((field[(row-1)*7+col] == 0) && ((row-1)*7+col < 43))
    {row = row + 1;}
   row = row - 1;
   return row; 
  }

 function TestVic()
  {
   var finarow = 0;
   var x,y;
   var ende = 0;
   // horizontal
   for (y = 1; y < 7; y++)
    { finarow = 0;
     for (x = 1; x < 8; x++)
      {
       if (field[(y-1)*7+x] == AmZug) {finarow = finarow + 1;}
       else {finarow = 0};
       if (finarow == 4) {ende = 1;}
      }
    }
   // vertikal
   for (x = 1; x < 8; x++)
    { finarow = 0;
     for (y = 1; y < 7; y++)
      {
       if (field[(y-1)*7+x] == AmZug) {finarow = finarow + 1;}
       else {finarow = 0};
       if (finarow == 4) {ende = 2;}
      }
    }
   // diagonal abfallend
   for (x = -3; x < 5; x++)
    { finarow = 0;
     for (y = 1; y < 7; y++)
      {
       if (((x+y)>0) && ((x+y)<8))
       {
        if (field[(y-1)*7+x+y] == AmZug) {finarow = finarow + 1;}
        else {finarow = 0};
        if (finarow == 4) {ende = 3;}
       }
      }
    }
   // diagonal aufsteigend
   for (x = -3; x < 5; x++)
    { finarow = 0;
     for (y = 1; y < 7; y++)
      {
       if (((x+y)>0) && ((x+y)<8))
       {
        if (field[(6-y)*7+x+y] == AmZug) {finarow = finarow + 1;}
        else {finarow = 0};
        if (finarow == 4) {ende = 4;}
       }
      }
    }
    if (ende>0)
     {if (ende==1) {richt=" waagerecht";}
      else if (ende==2) {richt=" senkrecht";}
      else if (ende==3) {richt=" diagonal abfallend";}
      else {richt=" diagonal aufsteigend";}
      if (AmZug==2)
       {if (document.game.compi.checked) {Sieger="Ich habe gewonnen:";}
        else {Sieger="Rot hat gewonnen:";}
        musscomm="";
       }
      else
       {if (document.game.compi.checked) {Sieger="Sie haben gewonnen:";}
        else {Sieger="Gelb hat gewonnen:";}
       }
      alert(Sieger + richt + '! ' + musscomm);
      steine = 43;
     }
   return; 
  }

 function TestPos(Tile)
  {
   var newRow = 0;
   if ((field[Tile] == 0) && (steine < 42))
    {
     newRow = FallDown(Tile);
     field[(newRow-1)*7+Tile] = AmZug;
     steine = steine +1;
     document.images[ImgOffset+(newRow-1)*7+Tile].src = relDir + "stein" + AmZug + ".gif";
     TestVic();
     if (steine == 42) {alert('Ende! Kein Sieger! :-)');}
    }
   return; 
  }

 //-->

