szybki
Wymiatacz
Dołączył: 17 Lis 2005
Posty: 565
Przeczytał: 0 tematów
Pomógł: 2 razy Ostrzeżeń: 5/5
|
Wysłany: Śro 8:37, 04 Sty 2006 Temat postu: |
|
|
KÓŁKO I KRZYŻYK:
Kod: | <SCRIPT language=JavaScript>
<!--
step = 0;
diff=3;
function clear_all(form) {
step = 0;
for (i=0;i<9; ++i) {
position="a"+i;
form[position].value="";
}
}
function clickit(field) {
if (step == -1) {alert("Wciśnij reset, aby zagrać ponownie"); return;}
position=field.name.substring(1,2,1);
position = 'a'+position;
if (field.form[position].value !="") {alert("Ruch niedozwolny"); return;}
field.form[position].value="X";
if (eval_pos(field.form)) {
field.form.output.value="Wygrałeś!";
step = -1;
return;
}
position=get_move(field.form);
field.form.output.value=' ' + position.substring(1,2,1);
if (position=="") {
field.form.output.value="Niema zwycięscy.";
step = -1;
return;
}
field.form[position].value="O";
if (eval_pos(field.form)) {
field.form.output.value="Przegrałeś;!";
step = -1;
}
}
function eval_pos(form) {
if ((form.a0.value!="" &&
form.a0.value==form.a3.value && form.a0.value==form.a6.value)||
(form.a0.value!="" && form.a0.value==form.a1.value && form.a0.value==form.a2.value) || (form.a0.value!="" && form.a0.value==form.a4.value && form.a0.value==form.a8.value) ||
(form.a1.value!="" && form.a1.value==form.a4.value && form.a1.value==form.a7.value) || (form.a2.value!=""
&& form.a2.value==form.a5.value && form.a2.value==form.a8.value) ||
(form.a2.value!="" && form.a2.value==form.a4.value && form.a2.value==form.a6.value) || (form.a3.value!=""
&& form.a3.value==form.a4.value && form.a3.value==form.a5.value) ||
(form.a6.value!="" && form.a6.value==form.a7.value && form.a6.value==form.a8.value))
return true;
else
return false;
}
function f(a) {
if (a == "") return "."; else return a;
}
function comp_move(form,player,weight,depth) {
var cost;
var bestcost=-2;
var position;
var newplayer;
if (player=="X") newplayer="O"; else newplayer="X";
if (depth==diff) return 0;
if (eval_pos(form)) return 1;
for (var i=0; i<9; ++i) {
position='a'+i;
if (form[position].value != "")
continue;
form[position].value=player;
cost = comp_move(form,newplayer, -weight, depth+1);
if (cost > bestcost) {
bestcost=cost;
if (cost==1) i=9;
}
form[position].value="";
}
if (bestcost==-2) bestcost=0;
return(-bestcost);
}
function get_move(form) {
var cost;
var bestcost=-2;
bestmove="";
if (step++ == 0)
if (form.a4.value=="")
return "a4";
else
if (form.a0.value=="")
return "a0";
for (var i=0; i<9; ++i) {
localposition='a'+i;
if (form[localposition].value != "")
continue;
form[localposition].value="O";
cost=comp_move(form,"X", -1, 0);
if (cost > bestcost) {
if (cost==1) i=9;
bestmove=localposition;
bestcost=cost;
}
form[localposition].value="";
}
return bestmove;
}
function complain(field) {
field.form.output.focus();
alert("Ruch niedozwolony!");
}
// -->
</SCRIPT><center>
<p align="center"><b>KÓŁKO i KRZYŻYK</b>
<br>
<br>
<FORM>
<DIV align=center>
<CENTER>
<P><INPUT name=a0 onfocus=complain(this) size=1> <INPUT name=b0 onclick=clickit(this) type=button> <INPUT name=a1
onfocus=complain(this) size=1> <INPUT name=b1 onclick=clickit(this) type=button> <INPUT name=a2
onfocus=complain(this) size=1> <INPUT name=b2 onclick=clickit(this) type=button> <BR><INPUT name=a3
onfocus=complain(this) size=1> <INPUT name=b3 onclick=clickit(this) type=button> <INPUT name=a4
onfocus=complain(this) size=1> <INPUT name=b4 onclick=clickit(this) type=button> <INPUT name=a5
onfocus=complain(this) size=1> <INPUT name=b5 onclick=clickit(this) type=button> <BR><INPUT name=a6
onfocus=complain(this) size=1> <INPUT name=b6 onclick=clickit(this) type=button> <INPUT name=a7
onfocus=complain(this) size=1> <INPUT name=b7 onclick=clickit(this) type=button> <INPUT name=a8
onfocus=complain(this) size=1> <INPUT name=b8 onclick=clickit(this) type=button> <BR><BR><BR>
<FONT size=3>
Komunikat:
<INPUT name=output><BR><BR>Stopień trudności: <SELECT name=difficulty
onchange=diff=form.difficulty[form.difficulty.selectedIndex].value;
size=1> <OPTION value=1>Very Easy</OPTION> <OPTION value=2>Easy</OPTION>
<OPTION selected value=3>Medium</OPTION> <OPTION
value=4>Hard</OPTION></SELECT> <BR><INPUT onclick="
if (!step++) this.form.a4.value='O';" type=button value="Computer wykonuje ruch jako pierwszy">
<BR><INPUT onclick=clear_all(this.form) type=reset value=Restart>
</font>
</P></CENTER></DIV></FORM> |
Post został pochwalony 0 razy
|
|