Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.98 KB | None | 0 0
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <HTML>
  3. <HEAD>
  4. <META NAME="description" content="Hex is a PC-Game written in HTML and JavaScript">
  5. <META NAME="author" content="Lutz Tautenhahn">
  6. <META NAME="keywords" content="Game, Hex, JavaScript">
  7. <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
  8. <title>Hex</title>
  9. <style type='text/css'>
  10. span { background-color: #ffffff; border: 1px solid #000000; cursor:default }
  11. .AI {font-size:9px}
  12. </style>
  13. <script language="JavaScript">
  14. var i, j, k, IsOver=true, IsStart0, Start, Start0, Size=11, IsRunning=false, LastEvent="";
  15. var MoveCount, MaxMoveCount, MaxFld=Size*Size, IsSwap, ActiveColor=0;
  16. IsPlayer = new Array(2);
  17. Level = new Array(2);
  18. ImgNum = new Array(Size);
  19. for (i=0; i<Size; i++)
  20. ImgNum[i] = new Array(Size);
  21. Fld = new Array(Size);
  22. for (i=0; i<Size; i++)
  23. Fld[i] = new Array(Size);
  24. Pot = new Array(Size);
  25. for (i=0; i<Size; i++)
  26. Pot[i] = new Array(Size);
  27. for (i=0; i<Size; i++)
  28. { for (j=0; j<Size; j++)
  29. Pot[i][j] = new Array(4);
  30. }
  31. Bridge = new Array(Size);
  32. for (i=0; i<Size; i++)
  33. Bridge[i] = new Array(Size);
  34. for (i=0; i<Size; i++)
  35. { for (j=0; j<Size; j++)
  36. Bridge[i][j] = new Array(4);
  37. }
  38. Upd = new Array(Size);
  39. for (i=0; i<Size; i++)
  40. Upd[i] = new Array(Size);
  41. History = new Array(MaxFld+1);
  42. for (i=0; i<MaxFld+1; i++)
  43. History[i] = new Array(2);
  44. Pic= new Array(3);
  45. Pic[0] = new Image();
  46. Pic[0].src = "hex_r.gif";
  47. Pic[1] = new Image();
  48. Pic[1].src = "hex_t.gif";
  49. Pic[2] = new Image();
  50. Pic[2].src = "hex_b.gif";
  51.  
  52. IsStart0=true;
  53. IsPlayer[0]=true;
  54. IsPlayer[1]=false;
  55. Level[0]=2;
  56. Level[1]=3;
  57.  
  58. function Init()
  59. { if (IsRunning) { LastEvent="Init()"; return; }
  60. var ii, jj;
  61. for (ii=0; ii<Size; ii++)
  62. { for (jj=0; jj<Size; jj++)
  63. Fld[ii][jj]=0;
  64. }
  65. if (IsStart0) Start0=true;
  66. else Start0=false;
  67. MoveCount=0;
  68. MaxMoveCount=0;
  69. RefreshScreen();
  70. WritePot(true);
  71. IsOver=false;
  72. if ((MoveCount+Start0)%2==0) window.document.OptionsForm.Msg.value=" Blue to move.";
  73. else window.document.OptionsForm.Msg.value=" Red to move.";
  74. }
  75.  
  76. function SetOption(nn, mm)
  77. { if (IsRunning) { LastEvent="SetOption("+nn+","+mm+")"; return; }
  78. if (nn<2)
  79. { if (mm==0)
  80. IsPlayer[nn]=true;
  81. else
  82. IsPlayer[nn]=false;
  83. }
  84. else IsStart0=mm;
  85. }
  86.  
  87. function SetLevel(nn, mm)
  88. { if (IsRunning) { LastEvent="SetLevel("+nn+","+mm+")"; return; }
  89. Level[nn]=mm;
  90. }
  91.  
  92. var IsAI=0;
  93. function ShowAI(bb)
  94. { var ww;
  95. IsAI=bb;
  96. if (IsAI)
  97. { WritePot(true);
  98. document.getElementById('AI').style.display='inline';
  99. ww=parseInt(window.top.innerWidth);
  100. if (ww<840) window.top.resizeBy(840-ww, 0);
  101. }
  102. else document.getElementById('AI').style.display='none';
  103. }
  104.  
  105. function Timer()
  106. { if (LastEvent!="")
  107. { eval(LastEvent);
  108. LastEvent="";
  109. return;
  110. }
  111. if (IsOver) return;
  112. if (IsRunning) return;
  113. if (IsPlayer[(MoveCount+Start0+1)%2]) { WritePot(true); return; }
  114. IsRunning=true;
  115. var ll=Level[(MoveCount+Start0+1)%2];
  116. if (SwapTest()) return;
  117. GetPot(ll);
  118. setTimeout("GetBestMove("+eval(((MoveCount+1+Start0)%2)*2-1)+","+ll+")",10);
  119. }
  120.  
  121. function Back()
  122. { if (IsRunning) { LastEvent="Back()"; return; }
  123. if (MoveCount>0)
  124. { IsOver=false;
  125. MoveCount--;
  126. var ii=History[MoveCount][0];
  127. var jj=History[MoveCount][1];
  128. if ((MoveCount==1)&&(IsSwap))
  129. { Fld[jj][ii]=0;
  130. RefreshPic(jj, ii);
  131. Fld[ii][jj]=((MoveCount+Start0)%2)*2-1;
  132. RefreshPic(ii, jj);
  133. }
  134. else
  135. { Fld[ii][jj]=0;
  136. RefreshPic(ii, jj);
  137. }
  138. if (MoveCount<10)
  139. window.document.OptionsForm.Moves.value=" "+eval(MoveCount)+" ";
  140. else
  141. window.document.OptionsForm.Moves.value=MoveCount;
  142. if ((MoveCount+Start0)%2==0) window.document.OptionsForm.Msg.value=" Blue to move.";
  143. else window.document.OptionsForm.Msg.value=" Red to move.";
  144. WritePot(true);
  145. }
  146. }
  147.  
  148. function Replay()
  149. { if (IsRunning) { LastEvent="Replay()"; return; }
  150. if (MoveCount<MaxMoveCount)
  151. { var ii=History[MoveCount][0];
  152. var jj=History[MoveCount][1];
  153. if (MoveCount<MaxMoveCount-1) { MakeMove(ii, jj, false); WritePot(true); }
  154. else MakeMove(ii, jj, true);
  155. }
  156. }
  157.  
  158. function GetMoveList()
  159. { var ii, jj, nn, ss="";
  160. for (nn=0; nn<MaxMoveCount; nn++)
  161. { ii=History[nn][0];
  162. jj=History[nn][1];
  163. if (nn>0) ss+=" ";
  164. ss+=String.fromCharCode(65+jj)+eval(ii+1);
  165. }
  166. window.document.OptionsForm.MoveList.value=ss;
  167. }
  168.  
  169. function ApplyMoveList()
  170. { if (IsRunning) { LastEvent="ApplyMoveList()"; return; }
  171. Init();
  172. var ii, jj, nn, ss=window.document.OptionsForm.MoveList.value;
  173. ss=ss.split(" ");
  174. for (nn=0; nn<ss.length; nn++)
  175. { jj=ss[nn].charCodeAt(0)-65;
  176. ii=parseInt(ss[nn].substr(1,2))-1;
  177. if (isNaN(ii)||isNaN(jj)||(ii<0)||(jj<0)||(ii>=Size)||(jj>=Size)) return;
  178. if (nn<ss.length-1) MakeMove(ii, jj, false);
  179. else MakeMove(ii, jj, true);
  180. }
  181. }
  182.  
  183. function SwapTest()
  184. { if (! window.document.OptionsForm.Swap.checked) return(false);
  185. var ii, jj;
  186. if (MoveCount==0)
  187. { ii=random(4);
  188. jj=random(4-ii);
  189. if (random(2)<1)
  190. { ii=Size-1-ii;
  191. jj=Size-1-jj;
  192. }
  193. MakeMove(ii, jj, false);
  194. WritePot(true);
  195. IsRunning=false;
  196. return(true);
  197. }
  198. if (MoveCount==1)
  199. { for (ii=0; ii<Size; ii++)
  200. { for (jj=0; jj<Size; jj++)
  201. { if (Fld[ii][jj]!=0)
  202. { if ((ii+jj<2)||(ii+jj>2*Size-4)) return(false);
  203. if ((ii+jj==2)||(ii+jj==2*Size-4)) { if (random(2)<1) return(false); }
  204. MakeMove(ii, jj, false);
  205. WritePot(true);
  206. IsRunning=false;
  207. return(true);
  208. }
  209. }
  210. }
  211. }
  212. return(false);
  213. }
  214.  
  215. function MakeMove(ii, jj, oo)
  216. { var ccol, kk, iis=ii, jjs=jj;
  217. if (MoveCount==1)
  218. { if (Fld[ii][jj]!=0)
  219. { Fld[ii][jj]=0;
  220. RefreshPic(ii, jj);
  221. iis=jj;
  222. jjs=ii;
  223. IsSwap=1;
  224. }
  225. else IsSwap=0;
  226. }
  227. ccol=((MoveCount+1+Start0)%2)*2-1;
  228. Fld[iis][jjs]=ccol;
  229. RefreshPic(iis, jjs);
  230. if (History[MoveCount][0]!=ii)
  231. { History[MoveCount][0]=ii;
  232. MaxMoveCount=MoveCount+1;
  233. }
  234. if (History[MoveCount][1]!=jj)
  235. { History[MoveCount][1]=jj;
  236. MaxMoveCount=MoveCount+1;
  237. }
  238. MoveCount++;
  239. if (MaxMoveCount<MoveCount)
  240. MaxMoveCount=MoveCount;
  241. if (MoveCount<10)
  242. window.document.OptionsForm.Moves.value=" "+eval(MoveCount)+" ";
  243. else
  244. window.document.OptionsForm.Moves.value=MoveCount;
  245. if ((MoveCount+Start0)%2==0) window.document.OptionsForm.Msg.value=" Blue to move.";
  246. else window.document.OptionsForm.Msg.value=" Red to move.";
  247. if ((MoveCount==2)&&(IsSwap>0))
  248. window.document.OptionsForm.Msg.value=" Swap."+window.document.OptionsForm.Msg.value;
  249. if (! oo) return;
  250. GetPot(0);
  251. //GetPot(2); ShowPot();
  252. WritePot(true);
  253. if (ccol<0)
  254. { if ((Pot[ii][jj][2]>0)||(Pot[ii][jj][3]>0)) return;
  255. window.document.OptionsForm.Msg.value=" Red has won !";
  256. Blink(0);
  257. }
  258. else
  259. { if ((Pot[ii][jj][0]>0)||(Pot[ii][jj][1]>0)) return;
  260. window.document.OptionsForm.Msg.value=" Blue has won !";
  261. Blink(0);
  262. }
  263. IsOver=true;
  264. }
  265.  
  266. function random(nn)
  267. { return(Math.floor(Math.random()*1000)%nn);
  268. }
  269.  
  270. function ShowPot()
  271. { var ii, jj;
  272. for (ii=0; ii<Size; ii++)
  273. { for (jj=0; jj<Size; jj++)
  274. window.document.images[ImgNum[ii][jj]].title =
  275. Math.round(Pot[ii][jj][2])+"\n"+
  276. Math.round(Pot[ii][jj][0])+"|"+
  277. Math.round(Pot[ii][jj][1])+"->"+
  278. Math.round(Pot[ii][jj][0]+Pot[ii][jj][1])+"\n"+
  279. Math.round(Pot[ii][jj][3])+"->"+
  280. Math.round(Pot[ii][jj][2]+Pot[ii][jj][3])+"\n"+
  281. Math.round(Pot[ii][jj][0]+Pot[ii][jj][1]+Pot[ii][jj][2]+Pot[ii][jj][3])+"\n"+
  282. Math.round(Bridge[ii][jj][2])+"\n"+
  283. Math.round(Bridge[ii][jj][0])+"|"+
  284. Math.round(Bridge[ii][jj][1])+"->"+
  285. Math.round(Bridge[ii][jj][0]+Bridge[ii][jj][1])+"\n"+
  286. Math.round(Bridge[ii][jj][3])+"->"+
  287. Math.round(Bridge[ii][jj][2]+Bridge[ii][jj][3])+"\n"+
  288. Math.round(Bridge[ii][jj][0]+Bridge[ii][jj][1]+Bridge[ii][jj][2]+Bridge[ii][jj][3])+"\n"+
  289. Math.round(Pot[ii][jj][0]+Pot[ii][jj][1]+Pot[ii][jj][2]+Pot[ii][jj][3]-
  290. Bridge[ii][jj][0]-Bridge[ii][jj][1]-Bridge[ii][jj][2]-Bridge[ii][jj][3]);
  291. }
  292. }
  293.  
  294. function RedPotCol(vv)
  295. { var xx=0, hh="0123456789abcdef";
  296. if (vv>0) xx=vv;
  297. var nn=Math.floor(255/(1+xx/255));
  298. return("#"+hh.charAt(Math.floor(nn/16))+hh.charAt(nn%16)+"0000");
  299. }
  300. function BluePotCol(vv)
  301. { var xx=0, hh="0123456789abcdef";
  302. if (vv>0) xx=vv;
  303. var nn=Math.floor(255/(1+xx/255));
  304. return("#0000"+hh.charAt(Math.floor(nn/16))+hh.charAt(nn%16));
  305. }
  306.  
  307. function WritePot(bb)
  308. { var ii, jj;
  309. if (!IsAI) return;
  310. if (bb) GetPot(2);
  311. for (ii=0; ii<Size; ii++)
  312. { for (jj=0; jj<Size; jj++)
  313. { window.document.getElementById("Pot0"+ii+jj).title = Math.round(Pot[ii][jj][0]);
  314. window.document.getElementById("Pot1"+ii+jj).title = Math.round(Pot[ii][jj][1]);
  315. window.document.getElementById("Pot2"+ii+jj).title = Math.round(Pot[ii][jj][2]);
  316. window.document.getElementById("Pot3"+ii+jj).title = Math.round(Pot[ii][jj][3]);
  317. window.document.getElementById("Pot0"+ii+jj).style.backgroundColor = BluePotCol(Pot[ii][jj][0]);
  318. window.document.getElementById("Pot1"+ii+jj).style.backgroundColor = BluePotCol(Pot[ii][jj][1]);
  319. window.document.getElementById("Pot2"+ii+jj).style.backgroundColor = RedPotCol(Pot[ii][jj][2]);
  320. window.document.getElementById("Pot3"+ii+jj).style.backgroundColor = RedPotCol(Pot[ii][jj][3]);
  321. }
  322. }
  323. }
  324.  
  325. function sign(xx)
  326. { if (xx<0) return(-1);
  327. if (xx>0) return(1);
  328. return(0);
  329. }
  330.  
  331. function GetBestMove(theCol, theLevel)
  332. { var ii, jj, kk, ii_b, jj_b, ff=0, ii_q=0, jj_q=0, cc, pp0, pp1;
  333. vv=new Array();
  334. if (MoveCount>0) ff=190/(MoveCount*MoveCount);
  335. mm=20000;
  336. for (ii=0; ii<Size; ii++)
  337. { for (jj=0; jj<Size; jj++)
  338. { if (Fld[ii][jj]!=0)
  339. { ii_q+=2*ii+1-Size;
  340. jj_q+=2*jj+1-Size;
  341. }
  342. }
  343. }
  344. ii_q=sign(ii_q);
  345. jj_q=sign(jj_q);
  346. for (ii=0; ii<Size; ii++)
  347. { for (jj=0; jj<Size; jj++)
  348. { if (Fld[ii][jj]==0)
  349. { mmp=Math.random()*(49-theLevel*16);
  350. mmp+=(Math.abs(ii-5)+Math.abs(jj-5))*ff;
  351. mmp+=8*(ii_q*(ii-5)+jj_q*(jj-5))/(MoveCount+1);
  352. if (theLevel>2)
  353. { for (kk=0; kk<4; kk++)
  354. mmp-=Bridge[ii][jj][kk];
  355. }
  356. pp0=Pot[ii][jj][0]+Pot[ii][jj][1];
  357. pp1=Pot[ii][jj][2]+Pot[ii][jj][3];
  358. mmp+=pp0+pp1;
  359. if ((pp0<=268)||(pp1<=268)) mmp-=400; //140+128
  360. vv[ii*Size+jj]=mmp;
  361. if (mmp<mm)
  362. { mm=mmp;
  363. ii_b=ii;
  364. jj_b=jj;
  365. }
  366. }
  367. }
  368. }
  369. if (theLevel>2)
  370. { mm+=108;
  371. for (ii=0; ii<Size; ii++)
  372. { for (jj=0; jj<Size; jj++)
  373. { if (vv[ii*Size+jj]<mm)
  374. { if (theCol<0)//red
  375. { if ((ii>3)&&(ii<Size-1)&&(jj>0)&&(jj<3))
  376. { if (Fld[ii-1][jj+2]==-theCol)
  377. { cc=CanConnectFarBorder(ii-1,jj+2,-theCol);
  378. if (cc<2)
  379. { ii_b=ii;
  380. if (cc<-1) { ii_b--; cc++; }
  381. jj_b=jj-cc;
  382. mm=vv[ii*Size+jj];
  383. }
  384. }
  385. }
  386. if ((ii>0)&&(ii<Size-1)&&(jj==0))
  387. { if ((Fld[ii-1][jj+2]==-theCol)&&
  388. (Fld[ii-1][jj]==0)&&(Fld[ii-1][jj+1]==0)&&(Fld[ii][jj+1]==0)&&(Fld[ii+1][jj]==0))
  389. { ii_b=ii; jj_b=jj; mm=vv[ii*Size+jj]; }
  390. }
  391. if ((ii>0)&&(ii<Size-4)&&(jj<Size-1)&&(jj>Size-4))
  392. { if (Fld[ii+1][jj-2]==-theCol)
  393. { cc=CanConnectFarBorder(ii+1,jj-2,-theCol);
  394. if (cc<2)
  395. { ii_b=ii;
  396. if (cc<-1) { ii_b++; cc++; }
  397. jj_b=jj+cc;
  398. mm=vv[ii*Size+jj];
  399. }
  400. }
  401. }
  402. if ((ii>0)&&(ii<Size-1)&&(jj==Size-1))
  403. { if ((Fld[ii+1][jj-2]==-theCol)&&
  404. (Fld[ii+1][jj]==0)&&(Fld[ii+1][jj-1]==0)&&(Fld[ii][jj-1]==0)&&(Fld[ii-1][jj]==0))
  405. { ii_b=ii; jj_b=jj; mm=vv[ii*Size+jj]; }
  406. }
  407. }
  408. else
  409. { if ((jj>3)&&(jj<Size-1)&&(ii>0)&&(ii<3))
  410. { if (Fld[ii+2][jj-1]==-theCol)
  411. { cc=CanConnectFarBorder(ii+2,jj-1,-theCol);
  412. if (cc<2)
  413. { jj_b=jj;
  414. if (cc<-1) { jj_b--; cc++; }
  415. ii_b=ii-cc;
  416. mm=vv[ii*Size+jj];
  417. }
  418. }
  419. }
  420. if ((jj>0)&&(jj<Size-1)&&(ii==0))
  421. { if ((Fld[ii+2][jj-1]==-theCol)&&
  422. (Fld[ii][jj-1]==0)&&(Fld[ii+1][jj-1]==0)&&(Fld[ii+1][jj]==0)&&(Fld[ii][jj+1]==0))
  423. { ii_b=ii; jj_b=jj; mm=vv[ii*Size+jj]; }
  424. }
  425. if ((jj>0)&&(jj<Size-4)&&(ii<Size-1)&&(ii>Size-4))
  426. { if (Fld[ii-2][jj+1]==-theCol)
  427. { cc=CanConnectFarBorder(ii-2,jj+1,-theCol);
  428. if (cc<2)
  429. { jj_b=jj;
  430. if (cc<-1) { jj_b++; cc++; }
  431. ii_b=ii+cc;
  432. mm=vv[ii*Size+jj];
  433. }
  434. }
  435. }
  436. if ((jj>0)&&(jj<Size-1)&&(ii==Size-1))
  437. { if ((Fld[ii-2][jj+1]==-theCol)&&
  438. (Fld[ii][jj+1]==0)&&(Fld[ii-1][jj+1]==0)&&(Fld[ii-1][jj]==0)&&(Fld[ii][jj-1]==0))
  439. { ii_b=ii; jj_b=jj; mm=vv[ii*Size+jj]; }
  440. }
  441. }
  442. }
  443. }
  444. }
  445. }
  446. MakeMove(ii_b, jj_b, false);
  447. IsRunning=false;
  448. if (theCol<0)
  449. { if ((Pot[ii_b][jj_b][2]>140)||(Pot[ii_b][jj_b][3]>140)) { WritePot(false); return; }
  450. window.document.OptionsForm.Msg.value=" Red has won !";
  451. Blink(-2);
  452. }
  453. else
  454. { if ((Pot[ii_b][jj_b][0]>140)||(Pot[ii_b][jj_b][1]>140)) { WritePot(false); return; }
  455. window.document.OptionsForm.Msg.value=" Blue has won !";
  456. Blink(-2);
  457. }
  458. IsOver=true;
  459. }
  460.  
  461. function CanConnectFarBorder(nn, mm, cc)
  462. { var ii, jj;
  463. if (cc>0) //blue
  464. { if (2*mm<Size-1)
  465. { for (ii=0; ii<Size; ii++)
  466. { for (jj=0; jj<mm; jj++)
  467. { if ((jj-ii<mm-nn)&&(ii+jj<=nn+mm)&&(Fld[ii][jj]!=0)) return(2);
  468. }
  469. }
  470. if (Fld[nn-1][mm]==-cc) return(0);
  471. if (Fld[nn-1][mm-1]==-cc)
  472. { if (GetFld(nn+2,mm-1)==-cc) return(0);
  473. return(-1);
  474. }
  475. if (GetFld(nn+2,mm-1)==-cc) return(-2);
  476. }
  477. else
  478. { for (ii=0; ii<Size; ii++)
  479. { for (jj=Size-1; jj>mm; jj--)
  480. { if ((jj-ii>mm-nn)&&(ii+jj>=nn+mm)&&(Fld[ii][jj]!=0)) return(2);
  481. }
  482. }
  483. if (Fld[nn+1][mm]==-cc) return(0);
  484. if (Fld[nn+1][mm+1]==-cc)
  485. { if (GetFld(nn-2,mm+1)==-cc) return(0);
  486. return(-1);
  487. }
  488. if (GetFld(nn-2,mm+1)==-cc) return(-2);
  489. }
  490. }
  491. else
  492. { if (2*nn<Size-1)
  493. { for (jj=0; jj<Size; jj++)
  494. { for (ii=0; ii<nn; ii++)
  495. { if ((ii-jj<nn-mm)&&(ii+jj<=nn+mm)&&(Fld[ii][jj]!=0)) return(2);
  496. }
  497. }
  498. if (Fld[nn][mm-1]==-cc) return(0);
  499. if (Fld[nn-1][mm-1]==-cc)
  500. { if (GetFld(nn-1,mm+2)==-cc) return(0);
  501. return(-1);
  502. }
  503. if (GetFld(nn-1,mm+2)==-cc) return(-2);
  504. }
  505. else
  506. { for (jj=0; jj<Size; jj++)
  507. { for (ii=Size-1; ii>nn; ii--)
  508. { if ((ii-jj>nn-mm)&&(ii+jj>=nn+mm)&&(Fld[ii][jj]!=0)) return(2);
  509. }
  510. }
  511. if (Fld[nn][mm+1]==-cc) return(0);
  512. if (Fld[nn+1][mm+1]==-cc)
  513. { if (GetFld(nn+1,mm-2)==-cc) return(0);
  514. return(-1);
  515. }
  516. if (GetFld(nn+1,mm-2)==-cc) return(-2);
  517. }
  518. }
  519. return(1);
  520. }
  521.  
  522. function GetFld(ii, jj)
  523. { if (ii<0) return(-1);
  524. if (jj<0) return(1);
  525. if (ii>=Size) return(-1);
  526. if (jj>=Size) return(1);
  527. return(Fld[ii][jj]);
  528. }
  529.  
  530. function Blink(nn)
  531. { IsRunning=true;
  532. if (nn==-2)
  533. { setTimeout("Blink(-1)",10);
  534. return;
  535. }
  536. if (nn==-1)
  537. { GetPot(0);
  538. WritePot(false);
  539. setTimeout("Blink(0)",10);
  540. return;
  541. }
  542. if (nn==14)
  543. { IsRunning=false;
  544. return;
  545. }
  546. var ii, jj, cc=(nn%2)*(((MoveCount+Start0)%2)*2-1);
  547. for (ii=0; ii<Size; ii++)
  548. { for (jj=0; jj<Size; jj++)
  549. { if ((Pot[ii][jj][0]+Pot[ii][jj][1]<=0)||(Pot[ii][jj][2]+Pot[ii][jj][3]<=0))
  550. { Fld[ii][jj]=cc;
  551. RefreshPic(ii, jj);
  552. }
  553. }
  554. }
  555. setTimeout("Blink("+eval(nn+1)+")",200);
  556. }
  557.  
  558. function GetPot(llevel)
  559. { var ii, jj, kk, mm, mmp, nn, bb, dd=128;
  560. ActiveColor=((MoveCount+1+Start0)%2)*2-1;
  561. for (ii=0; ii<Size; ii++)
  562. { for (jj=0; jj<Size; jj++)
  563. { for (kk=0; kk<4; kk++)
  564. { Pot[ii][jj][kk]=20000;
  565. Bridge[ii][jj][kk]=0;
  566. }
  567. }
  568. }
  569. for (ii=0; ii<Size; ii++)
  570. { if (Fld[ii][0]==0) Pot[ii][0][0]=dd;//blue border
  571. else
  572. { if (Fld[ii][0]>0) Pot[ii][0][0]=0;
  573. }
  574. if (Fld[ii][Size-1]==0) Pot[ii][Size-1][1]=dd;//blue border
  575. else
  576. { if (Fld[ii][Size-1]>0) Pot[ii][Size-1][1]=0;
  577. }
  578. }
  579. for (jj=0; jj<Size; jj++)
  580. { if (Fld[0][jj]==0) Pot[0][jj][2]=dd;//red border
  581. else
  582. { if (Fld[0][jj]<0) Pot[0][jj][2]=0;
  583. }
  584. if (Fld[Size-1][jj]==0) Pot[Size-1][jj][3]=dd;//red border
  585. else
  586. { if (Fld[Size-1][jj]<0) Pot[Size-1][jj][3]=0;
  587. }
  588. }
  589. for (kk=0; kk<2; kk++)//blue potential
  590. { for (ii=0; ii<Size; ii++)
  591. { for (jj=0; jj<Size; jj++)
  592. Upd[ii][jj]=true;
  593. }
  594. nn=0;
  595. do
  596. { nn++;
  597. bb=0;
  598. for (ii=0; ii<Size; ii++)
  599. { for (jj=0; jj<Size; jj++)
  600. { if (Upd[ii][jj]) bb+=SetPot(ii, jj, kk, 1, llevel);
  601. }
  602. }
  603. for (ii=Size-1; ii>=0; ii--)
  604. { for (jj=Size-1; jj>=0; jj--)
  605. { if (Upd[ii][jj]) bb+=SetPot(ii, jj, kk, 1, llevel);
  606. }
  607. }
  608. }
  609. while ((bb>0)&&(nn<12));
  610. }
  611. for (kk=2; kk<4; kk++)//red potential
  612. { for (ii=0; ii<Size; ii++)
  613. { for (jj=0; jj<Size; jj++)
  614. Upd[ii][jj]=true;
  615. }
  616. nn=0;
  617. do
  618. { nn++;
  619. bb=0;
  620. for (ii=0; ii<Size; ii++)
  621. { for (jj=0; jj<Size; jj++)
  622. { if (Upd[ii][jj]) bb+=SetPot(ii, jj, kk, -1, llevel);
  623. }
  624. }
  625. for (ii=Size-1; ii>=0; ii--)
  626. { for (jj=Size-1; jj>=0; jj--)
  627. { if (Upd[ii][jj]) bb+=SetPot(ii, jj, kk, -1, llevel);
  628. }
  629. }
  630. }
  631. while ((bb>0)&&(nn<12));
  632. }
  633. }
  634.  
  635. var vv=new Array(6);
  636. var tt=new Array(6);
  637. function SetPot(ii, jj, kk, cc, llevel)
  638. { Upd[ii][jj]=false;
  639. Bridge[ii][jj][kk]=0;
  640. if (Fld[ii][jj]==-cc) return(0);
  641. var ll, mm, ddb=0, nn, oo=0, dd=140, bb=66;
  642. if (cc!=ActiveColor) bb=52;
  643. vv[0]=PotVal(ii+1,jj,kk,cc);
  644. vv[1]=PotVal(ii,jj+1,kk,cc);
  645. vv[2]=PotVal(ii-1,jj+1,kk,cc);
  646. vv[3]=PotVal(ii-1,jj,kk,cc);
  647. vv[4]=PotVal(ii,jj-1,kk,cc);
  648. vv[5]=PotVal(ii+1,jj-1,kk,cc);
  649. for (ll=0; ll<6; ll++)
  650. { if ((vv[ll]>=30000)&&(vv[(ll+2)%6]>=30000))
  651. { if (vv[(ll+1)%6]<0) ddb=+32;
  652. else vv[(ll+1)%6]+=128; //512;
  653. }
  654. }
  655. for (ll=0; ll<6; ll++)
  656. { if ((vv[ll]>=30000)&&(vv[(ll+3)%6]>=30000))
  657. { ddb+=30;
  658. }
  659. }
  660. mm=30000;
  661. for (ll=0; ll<6; ll++)
  662. { if (vv[ll]<0)
  663. { vv[ll]+=30000;
  664. tt[ll]=10;
  665. }
  666. else tt[ll]=1;
  667. if (mm>vv[ll]) mm=vv[ll];
  668. }
  669. nn=0;
  670. for (ll=0; ll<6; ll++)
  671. { if (vv[ll]==mm) nn+=tt[ll];
  672. }
  673. if (llevel>1)
  674. { Bridge[ii][jj][kk]=nn/5;
  675. if ((nn>=2)&&(nn<10)) { Bridge[ii][jj][kk]=bb+nn-2; mm-=32; }
  676. if (nn<2)
  677. { oo=30000;
  678. for (ll=0; ll<6; ll++)
  679. { if ((vv[ll]>mm)&&(oo>vv[ll])) oo=vv[ll];
  680. }
  681. if (oo<=mm+104) { Bridge[ii][jj][kk]=bb-(oo-mm)/4; mm-=64; }
  682. mm+=oo;
  683. mm/=2;
  684. }
  685. }
  686.  
  687. if ((ii>0)&&(ii<Size-1)&&(jj>0)&&(jj<Size-1)) Bridge[ii][jj][kk]+=ddb;
  688. else Bridge[ii][jj][kk]-=2;
  689. if (((ii==0)||(ii==Size-1))&&((jj==0)||(jj==Size-1))) Bridge[ii][jj][kk]/=2; // /=4
  690. if (Bridge[ii][jj][kk]>68) Bridge[ii][jj][kk]=68; //66
  691.  
  692. if (Fld[ii][jj]==cc)
  693. { if (mm<Pot[ii][jj][kk])
  694. { Pot[ii][jj][kk]=mm;
  695. SetUpd(ii+1,jj,cc);
  696. SetUpd(ii,jj+1,cc);
  697. SetUpd(ii-1,jj+1,cc);
  698. SetUpd(ii-1,jj,cc);
  699. SetUpd(ii,jj-1,cc);
  700. SetUpd(ii+1,jj-1,cc);
  701. return(1);
  702. }
  703. return(0);
  704. }
  705. if (mm+dd<Pot[ii][jj][kk])
  706. { Pot[ii][jj][kk]=mm+dd;
  707. SetUpd(ii+1,jj,cc);
  708. SetUpd(ii,jj+1,cc);
  709. SetUpd(ii-1,jj+1,cc);
  710. SetUpd(ii-1,jj,cc);
  711. SetUpd(ii,jj-1,cc);
  712. SetUpd(ii+1,jj-1,cc);
  713. return(1);
  714. }
  715. return(0);
  716. }
  717.  
  718. function PotVal(ii,jj,kk,cc)
  719. { if (ii<0) return(30000);
  720. if (jj<0) return(30000);
  721. if (ii>=Size) return(30000);
  722. if (jj>=Size) return(30000);
  723. if (Fld[ii][jj]==0) return(Pot[ii][jj][kk]);
  724. if (Fld[ii][jj]==-cc) return(30000);
  725. return(Pot[ii][jj][kk]-30000);
  726. }
  727.  
  728. function SetUpd(ii,jj,cc)
  729. { if (ii<0) return;
  730. if (jj<0) return;
  731. if (ii>=Size) return;
  732. if (jj>=Size) return;
  733. Upd[ii][jj]=true;
  734. }
  735.  
  736. function Clicked(ii, jj)
  737. { if (IsOver) return;
  738. if (IsRunning) { LastEvent="Clicked("+ii+","+jj+")"; return; }
  739. if (Fld[ii][jj]!=0)
  740. { if ((MoveCount==1)&&(window.document.OptionsForm.Swap.checked)) MakeMove(ii,jj,false);
  741. return;
  742. }
  743. if (! IsPlayer[(MoveCount+Start0+1)%2]) return;
  744. MakeMove(ii, jj, true);
  745. window.document.OptionsForm.HelpButton.focus();
  746. window.document.OptionsForm.HelpButton.blur();
  747. }
  748.  
  749. function RefreshPic(ii, jj)
  750. { window.document.images[ImgNum[ii][jj]].src = Pic[1+Fld[ii][jj]].src;
  751. if (MoveCount<10)
  752. window.document.OptionsForm.Moves.value=" "+eval(MoveCount)+" ";
  753. else
  754. window.document.OptionsForm.Moves.value=MoveCount;
  755. }
  756.  
  757. function RefreshScreen()
  758. { for (ii=0; ii<Size; ii++)
  759. { for (jj=0; jj<Size; jj++)
  760. document.images[ImgNum[ii][jj]].src = Pic[1+Fld[ii][jj]].src;
  761. }
  762. if (MoveCount<10)
  763. window.document.OptionsForm.Moves.value=" "+eval(MoveCount)+" ";
  764. else
  765. window.document.OptionsForm.Moves.value=MoveCount;
  766. }
  767.  
  768. function ShowHelp()
  769. { alert("Hex is a board game for two players. It was"+
  770. "\nindependently invented by Piet Hein in 1942"+
  771. "\nand John Nash in 1948 and became popular"+
  772. "\nafter 1950 under the name Hex."+
  773. "\nHex is most commonly played on a board with"+
  774. "\n11x11 cells, but it can also be played on a"+
  775. "\nboard of another size. The red player trys"+
  776. "\nto connect the two red borders with a chain"+
  777. "\nof red cells by coloring empty cells red,"+
  778. "\nwhile the blue player trys the same with the"+
  779. "\nblue borders."+
  780. "\nThe game can never end with a draw:"+
  781. "\nWhen all cells have been colored, there must"+
  782. "\nexists either a red chain or a blue chain."+
  783. "\nThe player who moves first has a big advantage."+
  784. "\nIn order to compense this, there is often used"+
  785. "\nthe so-called 'swap rule': After the first move,"+
  786. "\nthe second player is allowed to swap the sides."+
  787. "\nIn order to apply the swap rule click again on"+
  788. "\nthe cell which was selected in the first move."+
  789. "\nGood luck!");
  790. }
  791.  
  792. function Resize()
  793. { if(navigator.appName == "Netscape") history.go(0);
  794. }
  795. </script>
  796. </head>
  797. <BODY bgcolor=#FFFFA0 text=#000000 onResize="javascript:Resize()">
  798. <DIV ALIGN=center>
  799. <form name="OptionsForm">
  800. <table noborder>
  801. <tr>
  802. <td><table border cellpadding=10 cellspacing=0 bgcolor=#ffffff><tr><td background="hex_bg.gif" align=center><font size=1>
  803. <script language="JavaScript">
  804. k=0;
  805. for (i=0; i < Size; i++)
  806. { document.write("<nobr>");
  807. for (j=0; j <= i; j++)
  808. { document.write("<IMG src=\"hex_t.gif\" border=0 onMouseDown=\"Clicked("+eval(i-j)+","+j+")\" title='"+String.fromCharCode(65+j)+eval(i-j+1)+"' alt='"+String.fromCharCode(65+j)+eval(i-j+1)+"'>");
  809. ImgNum[i-j][j]=k++;
  810. }
  811. document.writeln("</nobr><br>");
  812. }
  813. for (i=Size-2; i >= 0; i--)
  814. { document.write("<nobr>");
  815. for (j=0; j <= i; j++)
  816. { document.write("<IMG src=\"hex_t.gif\" border=0 onMouseDown=\"Clicked("+eval(Size-1-j)+","+eval(Size-1-i+j)+")\" title='"+String.fromCharCode(65+Size-1-i+j)+eval(Size-j)+"' alt='"+String.fromCharCode(65+Size-1-i+j)+eval(Size-j)+"'>");
  817. ImgNum[Size-1-j][Size-1-i+j]=k++;
  818. }
  819. document.writeln("</nobr><br>");
  820. }
  821. </script>
  822. </font></td></tr></table>
  823. </td>
  824. <td>&nbsp;</td>
  825. <td id='AI' style='display:none' >
  826.  
  827. <script language="JavaScript">
  828. document.writeln("<table border cellpadding=4 cellspacing=0 bgcolor=#ffffff><tr><td align=center bgcolor='#ccccff' class='AI'>");
  829. for (i=0; i < Size; i++)
  830. { document.write("<nobr>");
  831. for (j=0; j <= i; j++)
  832. { document.write("<span id='Pot0"+(i-j)+""+j+"'>&nbsp;&nbsp;&nbsp;&nbsp;</span>");
  833. }
  834. document.writeln("</nobr><br>");
  835. }
  836. for (i=Size-2; i >= 0; i--)
  837. { document.write("<nobr>");
  838. for (j=0; j <= i; j++)
  839. { document.write("<span id='Pot0"+(Size-1-j)+""+(Size-1-i+j)+"'>&nbsp;&nbsp;&nbsp;&nbsp;</span>");
  840. }
  841. document.writeln("</nobr><br>");
  842. }
  843. document.writeln("</td><td align=center bgcolor='#ffcccc' class='AI'>");
  844. for (i=0; i < Size; i++)
  845. { document.write("<nobr>");
  846. for (j=0; j <= i; j++)
  847. { document.write("<span id='Pot2"+(i-j)+""+j+"'>&nbsp;&nbsp;&nbsp;&nbsp;</span>");
  848. }
  849. document.writeln("</nobr><br>");
  850. }
  851. for (i=Size-2; i >= 0; i--)
  852. { document.write("<nobr>");
  853. for (j=0; j <= i; j++)
  854. { document.write("<span id='Pot2"+(Size-1-j)+""+(Size-1-i+j)+"'>&nbsp;&nbsp;&nbsp;&nbsp;</span>");
  855. }
  856. document.writeln("</nobr><br>");
  857. }
  858. document.writeln("</td></tr><tr><td align=center bgcolor='#ffcccc' class='AI'>");
  859. for (i=0; i < Size; i++)
  860. { document.write("<nobr>");
  861. for (j=0; j <= i; j++)
  862. { document.write("<span id='Pot3"+(i-j)+""+j+"'>&nbsp;&nbsp;&nbsp;&nbsp;</span>");
  863. }
  864. document.writeln("</nobr><br>");
  865. }
  866. for (i=Size-2; i >= 0; i--)
  867. { document.write("<nobr>");
  868. for (j=0; j <= i; j++)
  869. { document.write("<span id='Pot3"+(Size-1-j)+""+(Size-1-i+j)+"'>&nbsp;&nbsp;&nbsp;&nbsp;</span>");
  870. }
  871. document.writeln("</nobr><br>");
  872. }
  873. document.writeln("</td><td align=center bgcolor='#ccccff' class='AI'>");
  874. for (i=0; i < Size; i++)
  875. { document.write("<nobr>");
  876. for (j=0; j <= i; j++)
  877. { document.write("<span id='Pot1"+(i-j)+""+j+"'>&nbsp;&nbsp;&nbsp;&nbsp;</span>");
  878. }
  879. document.writeln("</nobr><br>");
  880. }
  881. for (i=Size-2; i >= 0; i--)
  882. { document.write("<nobr>");
  883. for (j=0; j <= i; j++)
  884. { document.write("<span id='Pot1"+(Size-1-j)+""+(Size-1-i+j)+"'>&nbsp;&nbsp;&nbsp;&nbsp;</span>");
  885. }
  886. document.writeln("</nobr><br>");
  887. }
  888. document.writeln("</td></tr></table>");
  889. </script>
  890.  
  891.  
  892. </td>
  893. <td>&nbsp;</td>
  894. <td valign=middle>
  895. <table noborder cellpadding=0 cellspacing=0 width=100%>
  896. <tr><td>
  897. <table border cellpadding=1 cellspacing=1 width=100% bgcolor=#DDCC88>
  898. <tr><td><input type=radio name="Red" checked value="Red: Player" onClick="javascript:SetOption(0,0)"> Red: Player</td></tr>
  899. <tr><td><table noborder width=100% cellpadding=0 cellspacing=0>
  900. <tr><td><input type=radio name="Red" value="Red: Computer" onClick="javascript:SetOption(0,1)"> Red: Computer</td></tr>
  901. <tr><td>&nbsp;Level:
  902. <input type=radio name="RedLevel" value="1" onClick="javascript:SetLevel(0,1)">1
  903. <input type=radio name="RedLevel" checked value="2" onClick="javascript:SetLevel(0,2)">2
  904. <input type=radio name="RedLevel" value="3" onClick="javascript:SetLevel(0,3)">3
  905. </td></tr></table>
  906. </td></tr></table>
  907. </td></tr>
  908. <tr><td>&nbsp;</td></tr>
  909. <tr><td>
  910. <table border cellpadding=1 cellspacing=1 width=100% bgcolor=#DDCC88>
  911. <tr><td><input type=radio name="Blue" value="Blue: Player" onClick="javascript:SetOption(1,0)"> Blue: Player</td></tr>
  912. <tr><td><table noborder width=100% cellpadding=0 cellspacing=0>
  913. <tr><td><input type=radio name="Blue" checked value="Blue: Computer" onClick="javascript:SetOption(1,1)"> Blue: Computer</td></tr>
  914. <tr><td>&nbsp;Level:
  915. <input type=radio name="BlueLevel" value="1" onClick="javascript:SetLevel(1,1)">1
  916. <input type=radio name="BlueLevel" value="2" onClick="javascript:SetLevel(1,2)">2
  917. <input type=radio name="BlueLevel" checked value="3" onClick="javascript:SetLevel(1,3)">3
  918. </td></tr></table>
  919. </td></tr></table>
  920. </td></tr>
  921. <tr><td>&nbsp;</td></tr>
  922. <tr><td>
  923. <table border cellpadding=1 cellspacing=1 width=100% bgcolor=#DDCC88>
  924. <tr><td width=100%><input type=radio name="Start" checked value="Red" onClick="javascript:SetOption(2,1)"> Red begins</td></tr>
  925. <tr><td><input type=radio name="Start" value="Blue" onClick="javascript:SetOption(2,0)"> Blue begins</td></tr>
  926. </table>
  927. </td></tr>
  928. <tr><td>&nbsp;</td></tr>
  929. <tr><td>
  930. <table border cellpadding=1 cellspacing=1 width=100% bgcolor=#DDCC88>
  931. <tr><td align=left><input type=checkbox name="Swap" checked>swap rule <input type=checkbox name="AI" onclick="javascript:ShowAI(this.checked)">show AI</td></tr>
  932. </table>
  933. </td></tr>
  934. <tr><td>&nbsp;</td></tr>
  935. <tr><td>
  936. <table border cellpadding=1 cellspacing=1 width=100% bgcolor=#DDCC88>
  937. <tr><td>
  938. <table noborder width=100% cellpadding=0 cellspacing=0><tr>
  939. <td align=left><input type=button value="&nbsp;&nbsp;&nbsp;NEW&nbsp;&nbsp;&nbsp;" style="width:68px;font-size:15px" onClick="javascript:Init()"></td>
  940. <td align=right><input type=button value="&nbsp;&nbsp;HELP&nbsp;&nbsp;&nbsp;" style="width:68px;font-size:15px" onClick="javascript:ShowHelp()" name="HelpButton"></td>
  941. </tr></table>
  942. </td></tr>
  943. <tr><td>
  944. <table noborder width=100% cellpadding=0 cellspacing=0><tr>
  945. <td align=center><input type=button value="&nbsp;&lt;&lt;&nbsp;" style="width:25px;font-size:15px" onClick="javascript:Back();Back()" title="two moves back"></td>
  946. <td align=center><input type=button value="&nbsp;&lt;&nbsp;" style="width:25px;font-size:15px" onClick="javascript:Back()" title="one move back"></td>
  947. <td align=center><input type=button value="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" style="width:32px;font-size:15px;background-color:#FFFFFF;font-weight:bold" disabled name="Moves"></td>
  948. <td align=center><input type=button value="&nbsp;&gt;&nbsp;" style="width:25px;font-size:15px" onClick="javascript:Replay()" title="one move forward"></td>
  949. <td align=center><input type=button value="&nbsp;&gt;&gt;&nbsp;" style="width:25px;font-size:15px" onClick="javascript:Replay();Replay()" title="two moves forward"></td>
  950. </tr></table>
  951. </td></tr>
  952. <tr><td align=center><input size=18 name="Msg" readonly></td></tr>
  953. </table>
  954. </td></tr>
  955. <tr><td>&nbsp;</td></tr>
  956. <tr><td>
  957. <table border cellpadding=1 cellspacing=1 width=100% bgcolor=#DDCC88>
  958. <tr><td align=center>Move list:<BR><textarea cols=14 rows=3 name="MoveList"></textarea></td></tr>
  959. <tr><td>
  960. <table noborder width=100% cellpadding=0 cellspacing=0><tr>
  961. <td align=left><input type=button value="&nbsp;&nbsp;&nbsp;GET&nbsp;&nbsp;&nbsp;" style="width:68px;font-size:15px" onClick="javascript:GetMoveList()"></td>
  962. <td align=right><input type=button value="&nbsp;&nbsp;APPLY&nbsp;&nbsp;" style="width:68px;font-size:15px" onClick="javascript:ApplyMoveList()"></td>
  963. </tr></table>
  964. </td></tr> </table>
  965. </td></tr>
  966. </table>
  967. </td>
  968. </tr>
  969. </table>
  970. </form>
  971. </DIV>
  972. <script language="JavaScript">
  973. Init();
  974. setInterval("Timer()",200);
  975. </script>
  976. </BODY>
  977. </HTML>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement