Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [JavaScript]Gra O i X. Problem z zaznaczeniem pola w roztawieniu 4x4
karollo92
post 12.06.2010, 15:15:49
Post #1





Grupa: Zarejestrowani
Postów: 1
Pomógł: 0
Dołączył: 12.06.2010

Ostrzeżenie: (0%)
-----


Mam do przerobienie grę kółko i krzyżyk z 9 polami na tą samą grę tylko ze z 16 polami. Większość udało już mi się zrobić tylko mam problem z 5 ostatnimi przyciskami które wstawiają znak do pola. otóż kiedy je wcisnę one zaznaczają mi zawsze pole a1. Gdzie jest błąd questionmark.gif?

Kod
<html>

<head>
</head>

<body>








<script language=JavaScript>
<!--
step = 0;
diff=3;
function clear_all(form) {
step = 0;
for (i=0;i<16; ++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ęzcy.";
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.a1.value && form.a0.value==form.a2.value && form.a0.value==form.a3.value)||
(form.a0.value!="" && form.a0.value==form.a4.value && form.a0.value==form.a8.value && form.a0.value==form.a12.value) ||
(form.a0.value!="" && form.a0.value==form.a5.value && form.a0.value==form.a10.value && form.a0.value==form.a15.value) ||
(form.a1.value!="" && form.a1.value==form.a5.value && form.a1.value==form.a9.value && form.a1.value==form.a13.value) ||
(form.a2.value!="" && form.a2.value==form.a6.value && form.a2.value==form.a10.value && form.a2.value==form.a14.value) ||
(form.a3.value!="" && form.a3.value==form.a7.value && form.a3.value==form.a11.value && form.a3.value==form.a15.value) ||
(form.a3.value!="" && form.a3.value==form.a6.value && form.a3.value==form.a9.value && form.a3.value==form.a12.value) ||
(form.a4.value!="" && form.a4.value==form.a5.value && form.a4.value==form.a6.value && form.a4.value==form.a7.value) ||
(form.a8.value!="" && form.a8.value==form.a9.value && form.a8.value==form.a10.value && form.a8.value==form.a11.value) ||
(form.f2.value!="" && form.a12.value==form.a13.value && form.a12.value==form.a14.value && form.a12.value==form.a15.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<16; ++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=16;
}
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<16; ++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=16;
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>
          <INPUT name=a3  onfocus=complain(this) size=1> <INPUT name=b3 onclick=clickit(this) type=button>
      <BR>
        <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>
        <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>
      <BR>
        <INPUT name=a8 onfocus=complain(this) size=1> <INPUT name=b8 onclick=clickit(this) type=button>
        <INPUT name=a9 onfocus=complain(this) size=1> <INPUT name=b9 onclick=clickit(this) type=button>
        <INPUT name=a10 onfocus=complain(this) size=1> <INPUT name=b10 onclick=clickit(this) type=button>
        <INPUT name=a11 onfocus=complain(this) size=1> <INPUT name=b11 onclick=clickit(this) type=button>
      <BR>
        <INPUT name=a12 onfocus=complain(this) size=1> <INPUT name=b12 onclick=clickit(this) type=button>
        <INPUT name=a13 onfocus=complain(this) size=1> <INPUT name=b13 onclick=clickit(this) type=button>
        <INPUT name=a14 onfocus=complain(this) size=1> <INPUT name=b14 onclick=clickit(this) type=button>
        <INPUT name=a15 onfocus=complain(this) size=1> <INPUT name=b15 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>Bardz łatwy</OPTION> <OPTION
            value=2>Łatwy</OPTION>  <OPTION
selected value=3>Normalny</OPTION> <OPTION
          value=4>Ciężki</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>






</body>
</html>



Chciałbym podkreślić iż dopiero zaczynam przygodę z JS więc licze na dużą wyrozumiałość sciana.gif

Ten post edytował karollo92 12.06.2010, 15:19:59
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 14.08.2025 - 04:21