[Reversi]

JavaScript- Adaption des beliebten Brettspiels.

[Code]

[zwischen <HEAD> und </HEAD>]

<SCRIPT LANGUAGE="JavaScript"><!-- //

// dynamic globals
var player= 1; var running= 1;
var score= new Array(3); score[1]= 2; score[2]= 2;

function preload() {
if (document.images) {
counter= new makeArray(3);
counter[0].src= "images/backg.gif";
counter[1].src= "images/counter1.gif";
counter[2].src= "images/counter2.gif";

mark= new makeArray(2);
mark[0].src= "images/blank.gif";
mark[1].src= "images/notblank.gif";

} else {
alert("Sorry, this game needs to run on a browser\nwhich supports the image object.");
}
}


function play(x,y) {
if (document.images["x"+x+"y"+y].src== counter[0].src) {
// okay, it's been played in a blank square

if (check_lines(x,y)) {
// and there's at least one legal line

// so place the counter and swap counters etc.
go(x,y);

// and now the other player
if (running) {
document.images["hi"+player].src= mark[0].src;
player= 3- player; document.images["hi"+player].src= mark[1].src;
}
}
}
}


function go(x,y) {
var old= 3- player; var swap= 0;
document.images["x"+x+"y"+y].src= counter[player].src; score[player]++;

for (j=-1; j<2; j++) {
for (i=-1; i<2; i++) {
swap= check(old,x,y,i,j); score[old]-= swap;
while (swap) {
document.images["x"+(x+(i*swap))+"y"+(y+(j*swap))].src= counter[player].src;
score[player]++; swap--;
}
}
}

update_scores();
}

function check_lines(x,y) {
// we're just concerned here whether or not a legal line exists

// up OR down:
if (check(3-player,x,y,0,-1) || check(3-player,x,y,0,1)) return(1);
// left OR right:
if (check(3-player,x,y,-1,0) || check(3-player,x,y,1,0)) return(1);

// up left OR up right
if (check(3-player,x,y,-1,-1) || check(3-player,x,y,1,-1)) return(1);
// down left OR down right
if (check(3-player,x,y,-1,1) || check(3-player,x,y,1,1)) return(1);

// no legal line in any direction...
return(0);
}


function check(p,tx,ty,dx,dy) {
var count= 0; tx+= dx; ty+= dy;
while ((tx>=0 && tx<8) && (ty>=0 && ty<8) && document.images["x"+tx+"y"+ty].src== counter[p].src) {
// we're still on the board and counting the opposite colour counters...
tx+= dx; ty+= dy; count++;
}

// we've stopped counting the opposite colour counters
// but have we reached a legal end? - ie. our colour counter
// and not the edge of the board or an empty square
if ((tx<0 || tx>7) || (ty<0 || ty>7) || document.images["x"+tx+"y"+ty].src== counter[0].src)
return(0);
else
return(count);
}

function update_scores() {
document.user.play1.value= score[1];
document.user.play2.value= score[2];
if (score[1]== 0 || score[2]== 0 || (score[1]+score[2]== 64)) {
if (score[1]> score[2]) alert("Congratulations, player 1");
else if (score[2]> score[1]) alert("Congratulations, player 2");
else alert("It's a draw");
running= 0;
}
}

function newgame() {
// just reset the board etc..
for (y=0; y<8; y++) {
for (x=0; x<8; x++) {
if ((x==3 && y==3) || (x==4 && y==4)) document.images["x"+x+"y"+y].src= counter[2].src;
else if ((x==3 && y==4) || (x==4 && y==3)) document.images["x"+x+"y"+y].src= counter[1].src;
else document.images["x"+x+"y"+y].src= counter[0].src;
}
}
document.images["hi"+player].src= mark[0].src; player= 1;
document.images["hi1"].src= mark[1].src; running= 1;
score[1]= 2; score[2]= 2; update_scores();
}

function pass() {
// only really allowed to pass if no legal moves available...
// (but if I could analyse moves like that, there'd be a
// computer opponent available - any takers? mail kif@irt.org)

if (running) { document.images["hi"+player].src= mark[0].src;
player= 3- player; document.images["hi"+player].src= mark[1].src; }
}

// The following function was written by Martin Webb at http://www.irt.org/
function makeArray(n) {
this.length= n; for (i=0; i<n; i++) { this[i] = new Image(); }
return this;
}

//--></SCRIPT>

[zwischen <BODY> und </BODY>]

<SCRIPT LANGUAGE="JavaScript"><!-- //

var output= '';
output+= '<TABLE ALIGN=center CELLSPACING=12 CELLPADDING=0 BORDER=0><TR>\n';
output+= '<TR><TD ALIGN=center><H1>Reversi</H1></TD><TD> </TD></TR>\n';

// create the board
output+= '<TD ALIGN=center VALIGN=top><TABLE CELLSPACING=0 CELLPADDING=0 BORDER=1>\n';
for (y=0; y<8; y++) {
output+= '<TR>';
for (x=0; x<8; x++) {
output+= '<TD><A HREF="javascript:play('+x+','+y+')" onFocus="blur();"><IMG SRC="images/';
if ((x==3 && y==3) || (x==4 && y==4)) output+= 'counter2';
else if ((x==3 && y==4) || (x==4 && y==3)) output+= 'counter1';
else output+= 'backg';
output+= '.gif" NAME="x'+x+'y'+y+'" WIDTH=60 HEIGHT=60 ALT="" BORDER=0></A></TD>';
}
output+= '</TR>\n';
}
output+= '</TABLE><br><SMALL><A HREF="mailto:kif\@irt.org">';
output+= 'Keith Drakard</A> v0.10 15th August 1998<br><br><A HREF="../../all.htm">Back to WebGames</A></SMALL></TD>\n';

// and create the whose-go-is-it indicator + score etc
output+= '<TD VALIGN=top><FORM NAME="user"><TABLE CELLSPACING=0 CELLPADDING=4 BORDER=0>\n';
output+= '<TR BGCOLOR="#00cc00"><TD ALIGN=center><B>Player</B></TD><TD ALIGN=center><B>Score</B></TD></TR>\n';
output+= '<TR BGCOLOR="#00ff00"><TD ALIGN=center><IMG SRC="../common/notblank.gif" NAME="hi1" WIDTH=4 HEIGHT=60 ALT=""> ';
output+= '<IMG SRC="images/counter1.gif" WIDTH=60 HEIGHT=60 ALT=""></TD>';
output+= '<TD ALIGN=center><INPUT TYPE="text" NAME="play1" VALUE=2 SIZE=2 onFocus="blur();"></TD></TR>\n';
output+= '<TR BGCOLOR="#00ff00"><TD ALIGN=center><IMG SRC="../common/blank.gif" NAME="hi2" WIDTH=4 HEIGHT=60 ALT=""> ';
output+= '<IMG SRC="images/counter2.gif" WIDTH=60 HEIGHT=60 ALT=""></TD>';
output+= '<TD ALIGN=center><INPUT TYPE="text" NAME="play2" VALUE=2 SIZE=2 onFocus="blur();"></TD></TR>\n';
output+= '<TR BGCOLOR="#00cc00"><TD COLSPAN=2 ALIGN=center>';
output+= '<INPUT TYPE="button" VALUE=" Pass " onClick="pass()"></TD></TR>\n';

output+= '<TR><TD COLSPAN=2> </TD></TR>\n';
output+= '<TR BGCOLOR="#00ff00"><TD COLSPAN=2 ALIGN=center>';
output+= '<INPUT TYPE="button" VALUE=" New Game " onClick="newgame()"></TD></TR>\n';

output+= '<TR><TD COLSPAN=2> </TD></TR>\n';
output+= '</TABLE></FORM></TD>\n';

output+= '</TR></TABLE>\n';

document.write(output);
preload();

//--></SCRIPT>

[Autor]

IRT.org

[Download]

Copyright © 1998- Nightfire Webworker Archiv Script No: 454