Quellcode
<style type="text/css">
table.spiel
{
border-color: black;
border-collapse: collapse;
background-color: #008000;
}
.spiel td
{
height:35px;
width:35px;
padding: 0px;
font-weight: bold;
border: 1px solid black;
text-align:center;
}
.white
{
background: url(games/othello/lx/white.gif) center no-repeat;
color:black;
cursor:default;
}
.towhite
{
background: url(games/othello/lx/towhite.gif) center no-repeat;
color:black;
}
.black
{
background: url(games/othello/lx/black.gif) center no-repeat;
color:white;
cursor:default;
}
.toblack
{
background: url(games/othello/lx/toblack.gif) center no-repeat;
color:white;
}
.normal
{
cursor: auto;
}
.point
{
cursor: pointer;
color:#A080FF;
}
td.point:hover
{
background: url(games/othello/lx/bw.gif) center no-repeat;
}
</style>
<script type="text/javascript"> var progress = 1; var computer = 1;
var player = 2; var showHelp = 0; var Spielfeld = new Array(64);
var tempSpielfeld = new Array(64); var possibilities = new Array();
var tempPossibilities = new Array(); var wert = new Array( 50, -1, 5, 2, 2, 5, -1, 50,
-1, -10, 1, 1, 1, 1, -10, -1,
5, 1, 1, 1, 1, 1, 1, 5,
2, 1, 1, 0, 0, 1, 1, 2,
2, 1, 1, 0, 0, 1, 1, 2,
5, 1, 1, 1, 1, 1, 1, 5,
-1, -10, 1, 1, 1, 1, -10, -1,
50, -1, 5, 2, 2, 5, -1, 50 ); var white, black; function init()
{
for (var i = 0; i < 64; i++)
{
putPiece(0,i);
Spielfeld[i] = 0;
possibilities[i] = new Array();
possibilities[i]['anzahl'] = 0;
possibilities[i]['flips'] = '';
tempPossibilities[i] = new Array();
tempPossibilities[i]['anzahl'] = 0;
tempPossibilities[i]['flips'] = '';
}
putPiece(1, 27);
putPiece(2, 28);
putPiece(2, 35);
putPiece(1, 36);
} function startgame()
{
init();
checkPossibilities(2);
document.getElementById('nG').style.visibility = 'hidden';
if (computer == 2) KI();
else
{
progress = 0;
for (var i = 0; i < 64; i++) if (possibilities[i]['anzahl'] > 0) putPiece (5,i);
}
} function checkMove()
{
var j = 0;
for (var i = 0; i < 64; i++)
{
j += possibilities[i]['anzahl'];
}
return j;
} function put(field)
{ if (Spielfeld[field] != 0) return false; if (possibilities[field]['anzahl'] == 0) return false; progress = 1; putPiece(player, field); flip(player, field); for (var i = 0; i < 64; i++) if (Spielfeld[i] != 1 && Spielfeld[i] != 2) putPiece(0,i); checkPossibilities(computer); if (checkMove() != 0)
{
setTimeout("KI()",2000);
} else
{
progress = 0;
checkPossibilities(player);
if (checkMove() == 0) gameOver();
else for (i = 0; i < 64; i++) if (possibilities[i]['anzahl'] > 0) putPiece (5,i);
}
return true;
} function gameOver()
{
progress = 1;
document.getElementById('O9').firstChild.nodeValue = 'G';
document.getElementById('O10').firstChild.nodeValue = 'A';
document.getElementById('O11').firstChild.nodeValue = 'M';
document.getElementById('O12').firstChild.nodeValue = 'E';
document.getElementById('O19').firstChild.nodeValue = 'O';
document.getElementById('O20').firstChild.nodeValue = 'V';
document.getElementById('O21').firstChild.nodeValue = 'E';
document.getElementById('O22').firstChild.nodeValue = 'R';
if (white > black)
{
.
.
.
}
else if (white < black)
{
.
.
.
}
else
{
.
.
.
}
document.getElementById('nG').style.visibility = 'visible';
} function flip(color, field)
{
var temp = new Array();
temp = possibilities[field]['flips'].split('|');
for (i = 0; i < temp.length-1; i++)
{
putPiece(color,temp[i]);
}
} function fakeFlip(color, field)
{
var temp = new Array();
temp = tempPossibilities[field]['flips'].split('|');
for (var i = 0; i < temp.length-1; i++)
{
Spielfeld[(temp[i])] = color;
}
} function putPiece(color, field)
{ if (color == 5 && progress == 0)
{
document.getElementById('O'+field).className = 'game point';
if (showHelp == 1) document.getElementById('O'+field).firstChild.nodeValue = possibilities[field]['anzahl'];
} else if (color == 0 || (color == 5 && progress == 1))
{
document.getElementById('O'+field).className = 'game normal';
document.getElementById('O'+field).firstChild.nodeValue = ' ';
} else
{
if (Spielfeld[field] != 0)
{
document.getElementById('O'+field).className = (color == 1) ? 'towhite' : 'toblack';
setTimeout("document.getElementById('O"+field+"').className = "+ ((color == 1) ? "'white'; " : "'black'; "), 1500);
}
else document.getElementById('O'+field).className = (color == 1) ? 'white' : 'black';
Spielfeld[field] = color;
document.getElementById('O'+field).firstChild.nodeValue = ' '; white = 0;
black = 0;
for (j = 0; j < 64; j++)
{
if (Spielfeld[j] == 1) white++;
if (Spielfeld[j] == 2) black++;
}
document.getElementById('white').firstChild.nodeValue = white;
document.getElementById('black').firstChild.nodeValue = black;
}
} function checkPossibilities(color)
{
var Reihe, Spalte;
var i, j, k;
var flips = '';
var anzahl = 0; for (i = 0; i < 64; i++)
{
possibilities[i]['anzahl'] = 0;
possibilities[i]['flips'] = '';
} var opponent = (color == 1) ? 2 : 1; for (i = 0; i < 64; i++)
{
if (Spielfeld[i] == color)
{ Reihe = Math.floor(i/8);
Spalte = i%8; for (j = 0; j < 8; j++)
{
if (j == Spalte || j == Spalte-1 || j == Spalte+1) continue; if (Spielfeld[(Reihe*8+j)] == 0)
{
if (Spalte > j)
{
for (k = 1; k < Spalte-j; k++)
{
if (Spielfeld[i-k] == opponent)
{
anzahl++;
flips = flips.concat((i-k) + '|');
}
else
{
anzahl = 0;
flips = '';
break;
}
}
possibilities[(Reihe*8+j)]['anzahl'] += anzahl;
possibilities[(Reihe*8+j)]['flips'] = possibilities[(Reihe*8+j)]['flips'].concat(flips);
anzahl = 0;
flips = '';
}
else
{
for (k = 1; k < j-Spalte; k++)
{
if (Spielfeld[i+k] == opponent)
{
anzahl++;
flips = flips.concat((i+k) + '|');
}
else
{
anzahl = 0;
flips = '';
break;
}
}
possibilities[(Reihe*8+j)]['anzahl'] += anzahl;
possibilities[(Reihe*8+j)]['flips'] = possibilities[(Reihe*8+j)]['flips'].concat(flips);
anzahl = 0;
flips = '';
}
}
} for (j = 0; j < 8; j++)
{
if (j == Reihe || j == Reihe-1 || j == Reihe+1) continue;
if (Spielfeld[(j*8+Spalte)] == 0)
{
if (Reihe > j)
{
for (k = 1; k < Reihe-j; k++)
{
if (Spielfeld[(i-k*8)] == opponent)
{
anzahl++;
flips = flips.concat((i-k*8) + '|');
}
else
{
anzahl = 0;
flips = '';
break;
}
}
possibilities[(j*8+Spalte)]['anzahl'] += anzahl;
possibilities[(j*8+Spalte)]['flips'] = possibilities[(j*8+Spalte)]['flips'].concat(flips);
anzahl = 0;
flips = '';
}
else
{
for (k = 1; k < j-Reihe; k++)
{
if (Spielfeld[(i+k*8)] == opponent)
{
anzahl++;
flips = flips.concat((i+k*8) + '|');
}
else
{
anzahl = 0;
flips = '';
break;
}
}
possibilities[(j*8+Spalte)]['anzahl'] += anzahl;
possibilities[(j*8+Spalte)]['flips'] = possibilities[(j*8+Spalte)]['flips'].concat(flips);
anzahl = 0;
flips = '';
}
}
} for (j = 2; j < 8; j++)
{
if (j <= Reihe && j <= Spalte && Spielfeld[(i-j*9)] == 0)
{
for (k = 1; k < j; k++)
{
if (Spielfeld[(i-k*9)] == opponent)
{
anzahl++;
flips = flips.concat((i-k*9) + '|');
}
else
{
anzahl = 0;
flips = '';
break;
}
}
possibilities[(i-j*9)]['anzahl'] += anzahl;
possibilities[(i-j*9)]['flips'] = possibilities[(i-j*9)]['flips'].concat(flips);
anzahl = 0;
flips = '';
}
if (j <= Reihe && j < 8-Spalte && Spielfeld[(i-j*7)] == 0)
{
for (k = 1; k < j; k++)
{
if (Spielfeld[i-k*7] == opponent)
{
anzahl++;
flips = flips.concat((i-k*7) + '|');
}
else
{
anzahl = 0;
flips = '';
break;
}
}
possibilities[(i-j*7)]['anzahl'] += anzahl;
possibilities[(i-j*7)]['flips'] = possibilities[(i-j*7)]['flips'].concat(flips);
anzahl = 0;
flips = '';
}
if ( j < 8-Reihe && j <= Spalte && Spielfeld[(i+j*7)] == 0)
{
for (k = 1; k < j; k++)
{
if (Spielfeld[i+k*7] == opponent)
{
anzahl++;
flips = flips.concat((i+k*7) + '|');
}
else
{
anzahl = 0;
flips = '';
break;
}
}
possibilities[(i+j*7)]['anzahl'] += anzahl;
possibilities[(i+j*7)]['flips'] = possibilities[(i+j*7)]['flips'].concat(flips);
anzahl = 0;
flips = '';
}
if (j < 8-Reihe && j < 8-Spalte && Spielfeld[(i+j*9)] == 0)
{
for (k = 1; k < j; k++)
{
if (Spielfeld[i+k*9] == opponent)
{
anzahl++;
flips = flips.concat((i+k*9) + '|');
}
else
{
anzahl = 0;
flips = '';
break;
}
}
possibilities[(i+j*9)]['anzahl'] += anzahl;
possibilities[(i+j*9)]['flips'] = possibilities[(i+j*9)]['flips'].concat(flips);
anzahl = 0;
flips = '';
}
}
}
}
} function KI()
{
var i, j, k, l;
var blah, blah2, nimm;
var temp = new Array();
var temp2 = new Array();
var temp3 = new Array(); for (i = 0; i < 64; i++)
{
tempPossibilities[i]['anzahl'] = possibilities[i]['anzahl'];
tempPossibilities[i]['flips'] = possibilities[i]['flips'];
tempSpielfeld[i] = Spielfeld[i];
}
for (i = 0; i < 64; i++)
{
if (tempPossibilities[i]['anzahl'] > 0)
{
Spielfeld[i] = computer;
fakeFlip(computer, i);
}
else continue;
checkPossibilities(player);
if (checkMove() == 0) temp2.push(i);
for (j = 0; j < 64; j++) Spielfeld[j] = tempSpielfeld[j];
}
for (i = 0; i < 64; i++)
{
possibilities[i]['anzahl'] = tempPossibilities[i]['anzahl'];
possibilities[i]['flips'] = tempPossibilities[i]['flips'];
Spielfeld[i] = tempSpielfeld[i];
} if (typeof(temp2[0]) == 'undefined')
{
if (possibilities[0]['anzahl'] > 0) temp2.push(0);
if (possibilities[7]['anzahl'] > 0) temp2.push(7);
if (possibilities[56]['anzahl'] > 0) temp2.push(56);
if (possibilities[63]['anzahl'] > 0) temp2.push(63); if (typeof(temp2[0]) == 'undefined')
{
for (i = 2; i < 6; i++)
{
if (possibilities[i]['anzahl'] > 0) temp2.push(i);
if (possibilities[(i*8)]['anzahl'] > 0) temp2.push(i*8);
if (possibilities[(i*8+7)]['anzahl'] > 0) temp2.push(i*8+7);
if (possibilities[(56+i)]['anzahl'] > 0) temp2.push(56+i);
}
if (Spielfeld[0] == computer && possibilities[1]['anzahl'] > 0) temp2.push(1);
if (Spielfeld[0] == computer && possibilities[8]['anzahl'] > 0) temp2.push(8);
if (Spielfeld[7] == computer && possibilities[6]['anzahl'] > 0) temp2.push(6);
if (Spielfeld[7] == computer && possibilities[15]['anzahl'] > 0) temp2.push(15);
if (Spielfeld[56] == computer && possibilities[48]['anzahl'] > 0) temp2.push(48);
if (Spielfeld[56] == computer && possibilities[57]['anzahl'] > 0) temp2.push(57);
if (Spielfeld[63] == computer && possibilities[55]['anzahl'] > 0) temp2.push(55);
if (Spielfeld[63] == computer && possibilities[62]['anzahl'] > 0) temp2.push(62); if (typeof(temp2[0]) == 'undefined')
{
for (i = 2; i < 6; i++)
{
if (possibilities[(16+i)]['anzahl'] > 0) temp2.push(16+i);
if (possibilities[(i*8+2)]['anzahl'] > 0) temp2.push(i*8+2);
if (possibilities[(i*8+5)]['anzahl'] > 0) temp2.push(i*8+5);
if (possibilities[(40+i)]['anzahl'] > 0) temp2.push(40+i);
}
if (Spielfeld[0] == computer && possibilities[9]['anzahl'] > 0) temp2.push(9);
if (Spielfeld[7] == computer && possibilities[14]['anzahl'] > 0) temp2.push(14);
if (Spielfeld[56] == computer && possibilities[49]['anzahl'] > 0) temp2.push(49);
if (Spielfeld[63] == computer && possibilities[54]['anzahl'] > 0) temp2.push(54); if (typeof(temp2[0]) == 'undefined')
{
for (i = 2; i < 6; i++)
{
if (possibilities[(8+i)]['anzahl'] > 0) temp2.push(8+i);
if (possibilities[(i*8+1)]['anzahl'] > 0) temp2.push(i*8+1);
if (possibilities[(i*8+6)]['anzahl'] > 0) temp2.push(i*8+6);
if (possibilities[(48+i)]['anzahl'] > 0) temp2.push(48+i);
} if (typeof(temp2[0]) == 'undefined')
{
if (possibilities[1]['anzahl'] > 0) temp2.push(1);
if (possibilities[6]['anzahl'] > 0) temp2.push(6);
if (possibilities[8]['anzahl'] > 0) temp2.push(8);
if (possibilities[15]['anzahl'] > 0) temp2.push(15);
if (possibilities[48]['anzahl'] > 0) temp2.push(48);
if (possibilities[55]['anzahl'] > 0) temp2.push(55);
if (possibilities[57]['anzahl'] > 0) temp2.push(57);
if (possibilities[62]['anzahl'] > 0) temp2.push(62); if (typeof(temp2[0]) == 'undefined')
{
if (possibilities[9]['anzahl'] > 0) temp2.push(9);
if (possibilities[14]['anzahl'] > 0) temp2.push(14);
if (possibilities[49]['anzahl'] > 0) temp2.push(49);
if (possibilities[54]['anzahl'] > 0) temp2.push(54);
}
}
}
}
}
} if (white + black < 25)
{
for (i = 0; i < temp2.length; i++) temp3[i] = temp2[i];
} else
{
var Differenz = -1000;
if (temp2.length > 1)
{
for (i = 0; i < 64; i++)
{
tempPossibilities[i]['anzahl'] = possibilities[i]['anzahl'];
tempPossibilities[i]['flips'] = possibilities[i]['flips'];
tempSpielfeld[i] = Spielfeld[i];
}
for (i = 0; i < temp2.length; i++)
{
temp = possibilities[(temp2[i])]['flips'].split('|');
blah = 0;
for (j = 0; j < temp.length-1; j++) blah += wert[(temp[j])];
Spielfeld[(temp2[i])] = computer;
fakeFlip(computer, (temp2[i]));
checkPossibilities(player);
for (j = 0; j < 64; j++)
{
temp = possibilities[j]['flips'].split('|');
blah2 = 0;
for (k = 0; k < temp.length-1; k++) blah2 += wert[(temp[k])];
}
for (j = 0; j < 64; j++)
{
possibilities[j]['anzahl'] = tempPossibilities[j]['anzahl'];
possibilities[j]['flips'] = tempPossibilities[j]['flips'];
Spielfeld[j] = tempSpielfeld[j];
}
if (blah - blah2 > Differenz)
{
while (typeof(temp3[0]) != 'undefined')
{
temp3.pop();
}
temp3.push(temp2[i]);
Differenz = blah - blah2;
}
else if (Differenz == blah - blah2)
{
temp3.push(temp2[i]);
}
}
}
else temp3[0] = temp2[0];
} blah = Math.floor((Math.random() * 1000) % temp3.length);
nimm = temp3[blah];
putPiece(computer, nimm);
flip(computer, nimm); progress = 0;
checkPossibilities(player);
if (checkMove() == 0)
{
checkPossibilities(computer);
if (checkMove() == 0) gameOver();
else setTimeout("KI()", 2000);
}
else for (i = 0; i < 64; i++)
{
if (possibilities[i]['anzahl'] > 0) putPiece (5,i);
else if (Spielfeld[i] == 0) putPiece(0,i);
}
}
</script> <img src="black.gif" height="30" width="30" alt="black">
<br>
<span style="font-size: 20pt; font-weight: bold" id="black">
</span>
<img src="white.gif" height="30" width="30" alt="white">
<br>
<span style="font-size: 20pt; font-weight: bold" id="white">
</span> <script type="text/javascript">
document.write('<table border="3" class="spiel">');
for (var i = 0; i < 8; i++)
{
document.write('<tr>');
for (var j = 0; j < 8; j++)
{
document.write('<td id="O'+(i*8+j)+'" onClick="if (progress == 0) put('+(i*8+j)+')"> <\/td>');
}
document.write('<\/tr>');
}
document.write('<\/table>');
</script> <table width="100%" id="nG">
<tr>
<td width="33%">
<input type="radio" name="Color" onClick="player=2; computer=1" id="C0" checked>
<label for="C0">Player Black, Computer White</label>
<br>
<input type="radio" name="Color" onClick="player=1; computer=2" id="C1">
<label for="C1">Player White, Computer Black</label>
</td>
<td width="34%" align="center">
<input type="button" value="New Game" onclick="startgame(); this.blur()">
</td>
<td width="33%">
<input type="radio" name="Help" onClick="showHelp=0" id="H0" checked>
<label for="H0">do not show help</label>
<br>
<input type="radio" name="Help" onClick="showHelp=1" id="H1">
<label for="H1">show help</label>
</td>
</tr>
</table> <script type="text/javascript">
init();
</script> |