HRA: CONNECT4

ID
Nazov / Name:
Popis / Description:
Dlzka / Length:
Testovane pod / Tested under:

 

173
p173.html / p173.zip
Pokuste sa zistit, ze o co ide v tejto hre. :-)
9146b
NN 4.04

UKAZKA / CLICK HERE

Zdrojovy tvar / Source code:

<html> <head><title>CONNECT 4</title></head> <script language="JavaScript"> <!-- function GetTop(col) { return this.top[col]; } function GetElem(row,col) { return this.table[row][col]; } function DropElem(col,n) { var row = this.GetTop(col); this.table[row][col] = n; this.top[col]--; } function RemoveElem(col) { var row = this.GetTop(col) + 1; this.table[row][col] = 0; this.top[col]++; } function Clear() { for (var col = 0; col < 12; col++) { for (var row = 0; row < 12; row++) this.table[row][col] = 0; this.top[col] = 11; } } function MakeTop(n) { this.length = 12; for (var i = 0; i < 12; i++) this[i] = n; return this; } function MakeTable() { this.length = 12; for (var i = 0; i < 12; i++) this[i] = new MakeTop(0); return this; } function DoClick(col) { var str = "?"; if (col < 10) str+= "0" + col; else str+= col; return str; } function Chek(num,col,elem,dir,k) { var cnt=1,x,y,c,r; var stop = false; c = col; r = this.GetTop(c) + k; if ((r == 12) || ((k==1)&&(this.GetElem(r,c)!=elem))) return false; if (dir == 1) { x=0; y=1; } else if (dir == 2) { x=1; y=1; } else if (dir == 3) { x=1; y=0; } else {x=1; y=-1; } while (!stop) { c+=x; r+=y; stop = ((c<1)||(c>11)||(r>11)||(r<this.GetTop(c))||(this.GetElem(r,c)!=elem)); if (!stop) cnt++; } c = col; r = this.GetTop(c) + k; x=-x; y=-y; stop=false; while (!stop) { c+=x; r+=y; stop = ((c<1)||(c>11)||(r>11)||(r<this.GetTop(c))||(this.GetElem(r,c)!=elem)); if (!stop) cnt++; } return (cnt == num); } function ChekN(n,col,elem,k) { var chk=false,dir=1; while (!chk && (dir <=4)){ chk = this.Chek(n,col,elem,dir,k); dir++; } return chk; } function Chek3n(elem) { var tcol1=1,tcol2,idx; var chk = false; while ((tcol1 < 12) && (this.GetTop(tcol1) == 0)) tcol1++; while (!chk && (tcol1 < 12)) { this.DropElem(tcol1,elem); tcol2 = 1; while ((tcol2 < 12) && (this.GetTop(tcol2) == 0)) tcol2++; idx = 0; while ((tcol2 < 12) && (idx < 2)) { if (this.ChekN(4,tcol2,elem,0)) idx++; tcol2++; while (!chk && (tcol2 < 12) && (this.GetTop(tcol2) == 0)) tcol2++; } if (idx == 2) { chk = true; gcol = tcol1; } this.RemoveElem(tcol1); tcol1++; while (!chk && (tcol1 < 12) && (this.GetTop(tcol1) == 0)) tcol1++; } return chk; } function Cheknx(n,elem) { var tcol=1,dr,idx; var chk = false; while ((tcol < 12) && (this.GetTop(tcol) == 0)) tcol++; while (!chk && (tcol < 12)) { this.DropElem(tcol,elem); dr = 1; idx = 0; while ((dr <= 4) && !this.Chek(n,tcol,elem,dr,1)) dr++; if (dr <=4) { if ((elem==2) && !this.ChekN(4,tcol,2,0)) { chk = true; gcol = tcol; } else if ((elem==1) && !this.Chek3n(2) && !this.ChekN(4,tcol,2,0)) { chk = true; gcol = tcol; } } this.RemoveElem(tcol); tcol++; while (!chk && (tcol < 12) && (this.GetTop(tcol) == 0)) tcol++; } return chk; } function Chek2n(elem) { var tcol=1,dr,idx; var chk = false; while ((tcol < 12) && (this.GetTop(tcol) == 0)) tcol++; while (!chk && (tcol < 12)) { this.DropElem(tcol,elem); dr = 1; idx = 0; while ((dr <= 4) && (idx < 2)) { if (this.Chek(2,tcol,elem,dr,1)) idx++; dr++; } if (idx == 2) { if ((elem==2) && !this.ChekN(4,tcol,2,0)) { chk = true; gcol = tcol; } else if ((elem==1) && !this.Chek3n(2) && !this.ChekN(4,tcol,2,0)) { chk = true; gcol = tcol; } } this.RemoveElem(tcol); tcol++; while (!chk && (tcol < 12) && (this.GetTop(tcol) == 0)) tcol++; } return chk; } function Move(str) { var col = (- -str.substring(1,2)) * 10 + (- -str.substring(2,3)); this.DropElem(col,2); for (col=1;col<12;col++) if (this.ChekN(4,col,2,1)) return 2; for (col=1;col<12;col++) if ((this.GetTop(col)>0) && this.ChekN(4,col,1,0)) { this.DropElem(col,1); return 1; } for (col=1;col<12;col++) if ((this.GetTop(col)>0) && this.ChekN(4,col,2,0)) { this.DropElem(col,1); return 0; } if (this.Chek3n(1)) this.DropElem(gcol,1); else if (this.Chek3n(2)) { this.DropElem(gcol,1); if (this.ChekN(4,gcol,2,0)) { this.RemoveElem(gcol); this.DropElem(gcol,2); for(col=1;(col<12) && !this.ChekN(4,col,2,0);col++); this.DropElem(col,1); this.RemoveElem(gcol); } } else if (this.Chek2n(1)) this.DropElem(gcol,1); else if (this.Cheknx(3,2)) this.DropElem(gcol,1); else if (this.Cheknx(2,1)) this.DropElem(gcol,1); else if (this.Cheknx(3,1)) this.DropElem(gcol,1); else if (this.Cheknx(2,2)) this.DropElem(gcol,1); else { col = 1; while ((col<12) && (!this.GetTop(col)==0)) col++; if (col == 12) return 3; else this.DropElem(col,1); } return 0; } function NewBoard() { this.table = new MakeTable(); this.top = new MakeTop(11); this.GetTop = GetTop; this.GetElem = GetElem; this.DropElem = DropElem; this.RemoveElem = RemoveElem; this.DoClick = DoClick; this.Move = Move; this.Chek = Chek; this.ChekN = ChekN; this.Clear = Clear; this.Chek3n = Chek3n; this.Cheknx = Cheknx; this.Chek2n = Chek2n; return this; } var board = new NewBoard(); var gcol; // --> </script> <frameset cols = "0,*"> <frame src="p173-2.html" name=left noresize> <frame src="p173-1.html" name=game noresize> </frameset> </html> </frame> </html>
[ Navrat / BACK ]