<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title> 24点游戏 </title> <style> body, td { font-family: 宋体; font-size: 9pt; background-color:#6699FF; color: #FFFFCC; } .tht { font-family: 黑体; font-size: 24pt; color: #000066; font-weight:bold } input { border: #C6FFFF 1px solid; background-color: #003399; color: #FFFFFF; } .ipk {width: 39; height: 30; font-size: 14pt; font-weight:bold } .tit { font-size: 14pt; border: #333399 2px solid; background-color: #ffffff; color: #333399; font-weight:bold } .bg { width: 70; height: 32; } .xg { width: 75; height: 30; } #pkbox { width: 320; height: 110; } #pkbox span { border: #660099 3px solid; background-color: #ffffff; width: 68; height: 100; color: #000000; font-size: 50px; text-align: center; cursor:hand; padding: 16 0; font-weight:bold; } #sskgxx { border: #000066 2px solid; background-color: #6666FF; width: 130; height: 180; color: #ffCCFF; overflow: auto; padding: 4 7 } #k_jg { color: #FF6666; font-size: 14pt; font-weight:bold; width: 130; } </style> <script language="JavaScript"> // 程序作者:海浪 // 日期:2005-8-13 function lop(j) { if(j>0) { var ss = lop(j-1); var px = ""; var oo = "$1"+j+"$2"; for(var k=0; k<=j; k++) { var re = new RegExp("(\\d{"+k+"})(.*?;)","g"); px += ss.replace(re,oo); } return px; } else return "0;"; } var gt = lop(3).slice(0,-1).split(";"); for(var i=0; i<gt.length; i++) gt[i] = gt[i].split(""); var ot = []; var op = ["+","-","*","/"]; for(var x=0; x<4; x++) for(var y=0; y<4; y++) for(var z=0; z<4; z++) { ot[ot.length] = "( (a"+op[x]+"b)"+op[y]+"c) "+op[z]+"d"; ot[ot.length] = "( a"+op[x]+"(b"+op[y]+"c)) "+op[z]+"d"; ot[ot.length] = "a"+op[x]+"( (b"+op[y]+"c)"+op[z]+"d) "; ot[ot.length] = "a"+op[x]+"( b"+op[y]+"(c"+op[z]+"d)) "; ot[ot.length] = "( a"+op[x]+"b) "+op[y]+"(c"+op[z]+"d)"; } /////////////////////////////////////////////////////////////// var ggcs; function ggcsclass() { this.pjys = 0; this.zq = 0; this.cw = 0; this.hl = 0; this.gx(); } function ggcsclass.prototype.gx() { k_pjys.innerText = this.zq==0?0:Math.round(this.pjys/this.zq*100)/100; k_zq.innerText = this.zq; k_cw.innerText = this.cw; k_hl.innerText = this.hl; } function ggcsclass.prototype.phl() { this.hl++; this.gx(); k_jg.innerText = "此题被忽略!!"; } function ggcsclass.prototype.pcw() { this.cw++; this.gx(); k_jg.innerText = "错误!!"; } function ggcsclass.prototype.pzq() { var d = jcsl.endDate(); this.pjys += d/1000; this.zq++; this.gx(); k_jg.innerText = "正确!用时"+Math.round(d/10)/100+"秒!"; } window.onload = function() { ggcs = new ggcsclass(); } /////////////////////////////////////////////////////////////// var jcsl=null; function jclass() { this.xpa = []; this.ssjg = ""; this.wx = 1E-10; this.date = null; } function jclass.prototype.rnd() { for(var ii=0; ii<4; ii++) this.xpa[ii] = Math.floor(Math.random()*10)+1; } function jclass.prototype.fapai() { for(var ii=0; ii<4; ii++) document.getElementById("pk"+ii).innerText = this.xpa[ii]; this.date = new Date(); } function jclass.prototype.endDate() { return new Date()-this.date; } function jclass.prototype.jw() { var a,b,c,d; this.ssjg= ""; for(var i=0; i<gt.length; i++) { a=this.xpa[gt[i][0]]-0; b=this.xpa[gt[i][1]]-0; c=this.xpa[gt[i][2]]-0; d=this.xpa[gt[i][3]]-0; for(var h=0; h<ot.length; h++) if(Math.abs(eval(ot[h])-24)<=this.wx) this.jcf(ot[h].replace("a",a).replace("b",b).replace("c",c).replace("d",d)); } } function jclass.prototype.jcf(t) { var str = t; var zz1 = t.replace(/[\(\)]/g,""); var zz2 = t.replace(/[\(\)] /g,""); var zz3 = t.replace(/[\(\)](?! )/g,""); if(Math.abs(eval(zz1)-24)<=this.wx) str = zz1; else if(Math.abs(eval(zz2)-24)<=this.wx) str = zz2; else if(Math.abs(eval(zz3)-24)<=this.wx) str = zz3; var res = str.replace(/ /g,"")+"=24\n"; if(this.ssjg.indexOf(res)==-1) this.ssjg += res; } function jclass.prototype.scda() { sskgxx.innerText = "全部答案:\n"+this.ssjg; } function jclass.prototype.gx(ss) { var sw1 = ss.match(/\(/g), sw2 = ss.match(/\)/g); sw1 = (sw1==null)?0:sw1.length; sw2 = (sw2==null)?0:sw2.length; if(sw1!=sw2) return "左右括号个数不一样"; if(!/^\(*(\d+)\)*[\+\-\*\/]\(*(\d+)\)*[\+\-\*\/]\(*(\d+)\)*[\+\-\*\/]\(*(\d+)\)*$/.test(ss)) return "算术表达式格式不正确"; var str = "#"+RegExp.$1+"|#"+RegExp.$2+"|#"+RegExp.$3+"|#"+RegExp.$4+"|"; for(var ii=0; ii<4; ii++) { var lok = "#"+this.xpa[ii]+"|"; if(str.indexOf(lok)!=-1) str = str.replace(lok,""); else return "算术表达式中数字不对"; } return "yes"; } ////////////////////////////////////////////////////////////// function fpaaSystem() { if(jcsl!=null) ggcs.phl(); jcsl = new jclass(); do { jcsl.rnd(); jcsl.jw(); } while(jcsl.ssjg==""); k_jg.innerText = ""; sskgxx.innerText = ""; gstext.value = ""; jcsl.fapai(); } function fpaaInput() { var ins = prompt("请输入四个数字(如: 5,4,7,10)",""); if(ins==null) return; if(!/^\d{1,2},\d{1,2},\d{1,2},\d{1,2}$/.test(ins)) { alert("输入的内容不正确!");return; } var vf = jcsl!=null; jcsl = new jclass(); jcsl.xpa = ins.split(","); jcsl.jw(); if(jcsl.ssjg=="") { alert("此题无解!!!");return; } if(vf) ggcs.phl(); k_jg.innerText = ""; sskgxx.innerText = ""; gstext.value = ""; jcsl.fapai(); } function ckda() { if(jcsl==null) return; ggcs.phl(); jcsl.scda(); jcsl=null; } function okaa() { if(jcsl==null) return; var gs = gstext.value; var str = jcsl.gx(gs); if(str!="yes") { alert("算式错误:"+str+"!"); return; } if(Math.abs(eval(gs)-24)<=jcsl.wx) ggcs.pzq(); else ggcs.pcw(); jcsl.scda(); jcsl=null; } function tocs(n) { if(jcsl==null) return; gstext.value+=jcsl.xpa[n]; } </script> </head> <body> <span class="tht">24点游戏</span> 程序作者:海浪 日期:2005-8-13 <hr> <table style="float:left;"> <tr> <td width="330" height="20" colspan="2"> 平均用时:<span id="k_pjys"></span>秒 正确:<span id="k_zq"></span> 错误:<span id="k_cw"></span> 忽略:<span id="k_hl"></span> </td> </tr> <tr> <td width="330" height="110" colspan="2"> <div id="pkbox"> <span id="pk0" onclick="tocs(0)"></span> <span id="pk1" onclick="tocs(1)"></span> <span id="pk2" onclick="tocs(2)"></span> <span id="pk3" onclick="tocs(3)"></span> </div> </td> </tr> <tr> <td width="80" valign="top" height="110"> <input type="button" value="系统发牌" onclick="fpaaSystem()" class="xg"><br> <input type="button" value="玩家选牌" onclick="fpaaInput()" class="xg"><br> <input type="button" value="查看答案" onclick="ckda()" class="xg"> </td> <td width="250" valign="top" height="110"> <input type="text" size="20 " name="gstext" class="tit"><br> <input type="button" value="+" onclick="gstext.value+=this.value" class="ipk"><input type="button" value="-" onclick="gstext.value+=this.value" class="ipk"><input type="button" value="*" onclick="gstext.value+=this.value" class="ipk"><input type="button" value="/" onclick="gstext.value+=this.value" class="ipk"><input type="button" value="(" onclick="gstext.value+=this.value" class="ipk"><input type="button" value=")" onclick="gstext.value+=this.value" class="ipk"><br> <input type="button" value="后退" onclick="gstext.value=gstext.value.slice(0,-1)" class="bg"><input type="button" value="清除" onclick="gstext.value=''" class="bg"> <input type="button" value="确定" onclick="okaa()" class="bg"> </td> </tr> </table> <div id="sskgxx"></div> <div id="k_jg"></div> </body> </html>