<html> <head> <title>Игра крестики-нолики</title> </head> <body> <p align=center><br> <SCRIPT language=JavaScript> <!-- step = 0; diff=3; // change board when button is clicked function clear_all(form) { step = 0; for (i=0;i<9; ++i) { position="a"+i; form[position].value=""; } } // change board when button is clicked function clickit(field) { if (step == -1) {alert("Reset to play again"); return;} position=field.name.substring(1,2,1); position = 'a'+position; if (field.form[position].value !="") { alert("Can't go there"); return;} field.form[position].value="X"; if (eval_pos(field.form)) { field.form.output.value="Вы победили!"; step = -1; return; } position=get_move(field.form); field.form.output.value='I moved to ' + position.substring(1,2,1); if (position=="") { field.form.output.value="Ничья."; step = -1; return; } field.form[position].value="O"; if (eval_pos(field.form)) { field.form.output.value="Вы проиграли!"; step = -1; } } // see if there is a winner 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; } // get position for move. 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); } // get position for move. function get_move(form) { var cost; var bestcost=-2; bestmove=""; // don't think about first move. 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; } // complain if user attempts to change board function complain(field) { field.form.output.focus(); // put focus eleswhere alert("Don't change the game fields directly!"); } // the end // --> </SCRIPT> <form> <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> Message: <input name=output><br><br> Сложность игры : <SELECT name=difficulty onchange=diff=form.difficulty[form.difficulty.selectedIndex].value; size=1> <option value=1>Очень лёгкая</option> <option value=2>Просто</option> <option selected value=3>Нормально</option> <option value=4>Сложно (Компьютер долго думает)</option></SELECT><br> <input onclick="if (!step++) this.form.a4.value='O';" type=button value="Компьютер ходит первым"><br> <input onclick=clear_all(this.form) type=reset value=Рестарт> </P></form> </body> </html>
|