感觉算法还是有些笨,不过现在已没时间改进它了。
源代码如下:
- <html>
- <!--
- ######################################################
- # Copyright 2002-2003 #
- # #
- # Written by neweroica #
- ######################################################
- -->
- <title>neweroica的抢24游戏</title>
- <!--------------作者:neweroica(aqyw)---------------->
- <h2>24点游戏初解</h2>
- <h4>作者:neweroica(aqyw)</h4>
- <input type=hidden name=a1>
- <input type=hidden name=b1>
- <input type=hidden name=c1>
- <input type=hidden name=d1>
- <table>
- <tr>
- <td>
- <b id=a2 style="width:60;height:100;font-size:25pt;cursor:hand;background:#ff8888;border:solid 1px #000000;" onclick=addN(a1.value)> </b>
- <b id=b2 style="width:60;height:100;font-size:25pt;cursor:hand;background:#ff8888;border:solid 1px #000000;" onclick=addN(b1.value)> </b>
- <b id=c2 style="width:60;height:100;font-size:25pt;cursor:hand;background:#ff8888;border:solid 1px #000000;" onclick=addN(c1.value)> </b>
- <b id=d2 style="width:60;height:100;font-size:25pt;cursor:hand;background:#ff8888;border:solid 1px #000000;" onclick=addN(d1.value)> </b>
- <p>
- <input type=button value=发牌 ōnclick=cards(0)>
- <input type=button value=自定 ōnclick=cards(1)>
- <font color=#00aa00>正确</font><b id=correct style="width:30;height:;border:solid 1px #aaaa88;">0</b>
- <font color=#aa0000>错误</font><b id=wrong style="width:30;height:;border:solid 1px #aaaa88;">0</b>
- <font color=#0000aa>正确率</font><b id=yeild style="width:60;height:;border:solid 1px #aaaa88;">0</b>
- </p>
- </td>
- <td>
- <table cellspacing=2 cellpadding=2>
- <tr>
- <td colspan=4>
- <b id=formula style="color:#000088;width:200;height:25;border:solid 1px #880000;"> </b>
- </td>
- </tr>
- <tr>
- <td>
- <input type=button value=+ style="width:30;height:30" ōnclick=add()>
- </td>
- <td>
- <input type=button value=- style="width:30;height:30" ōnclick=minus()>
- </td>
- <td>
- <input type=button value=* style="width:30;height:30" ōnclick=mult()>
- </td>
- <td>
- <input type=button value=/ style="width:30;height:30" ōnclick=divd()>
- </td>
- </tr>
- <tr>
- <td>
- <input type=button value=( style="width:30;height:30" ōnclick=lbr()>
- </td>
- <td>
- <input type=button value=) style="width:30;height:30" ōnclick=rbr()>
- </td>
- <td>
- <input type=button value== style="width:30;height:30" ōnclick=equ()>
- </td>
- <td>
- <input type=button value=← style="width:30;height:30" ōnclick=bkspc()>
- </td>
- </tr>
- <tr>
- <td>
- <input type=button value=重置 ōnclick=resetit()>
- </td>
- <td>
- <input type=button value=无解 ōnclick=abort()>
- </td>
- <td colspan=2>
- <input type=button value=查看答案 ōnclick=cal()>
- </td>
- </tr>
- </table>
- </td>
- </tr>
- </table>
- <p id=showit style="color:#aa3388;width:300;height:150;border:solid 1px #008800;"> </p>
- <scrīpt>
- function numToChar(n){ //数字的单个字符表示;
- return unescape('%u'+(n+1000));
- }
- function charToNum(c){ //单个字符表示对应的数字;
- return parseInt(escape(c).substring(2,100))-1000;
- }
- var cardInf='',cardInf0;
- var cardS=new Array(0,'A',2,3,4,5,6,7,8,9,10,'J','Q','K');
- var typ=new Array(0,1,2,3);
- var typhtml=new Array(
- '<font color=#000000>'+unescape('%u2660'),
- '<font color=#000000>'+unescape('%u2663'),
- '<font color=#ff0000>'+unescape('%u2665'),
- '<font color=#ff0000>'+unescape('%u2666'));
- for(var i=1;i<=13;i++){
- for(var j=0;j<4;j++){
- cardInf+=numToChar(i)+typ[j];
- }
- }
- cardInf0=cardInf;
- var mode=new Array();
- mode[0]='a+b+c+d';
- mode[1]='a+b+c-d';
- mode[2]='a+b-c-d';
- mode[3]='a*b+c+d';
- mode[4]='a*b*c+d';
- mode[5]='a*b+c*d';
- mode[6]='a*b*c*d';
- mode[7]='a/b+c+d';
- mode[8]='a/b/c+d';
- mode[9]='a/b+c/d';
- mode[10]='a*b*c/d';
- mode[11]='a*b/c/d';
- mode[12]='a*b/c+d';
- mode[13]='a/b+c*d';
- mode[14]='a*b-c-d';
- mode[15]='a*b*c-d';
- mode[16]='a*b-c*d';
- mode[17]='a*b/c-d';
- mode[18]='a*b+c-d';
- mode[19]='(a+b+c)*d';
- mode[20]='(a+b-c)*d';
- mode[21]='(a*b+c)*d';
- mode[22]='(a*b-c)*d';
- mode[23]='(a/b+c)*d';
- mode[24]='(a/b-c)*d';
- mode[25]='(a+b+c)/d';
- mode[26]='(a+b-c)/d';
- mode[27]='(a*b+c)/d';
- mode[28]='(a*b-c)/d';
- mode[29]='(a/b+c)/d';
- mode[30]='(a+b)*c+d';
- mode[31]='(a-b)*c+d';
- mode[32]='(a+b)/c+d';
- mode[33]='(a-b)/c+d';
- mode[34]='(a+b)*c-d';
- mode[35]='(a-b)*c-d';
- mode[36]='(a+b)/c-d';
- mode[37]='(a+b)*c*d';
- mode[38]='(a-b)*c*d';
- mode[33]='(a+b)/c*d';
- mode[40]='(a-b)/c*d';
- mode[41]='(a+b)/c/d';
- mode[42]='(a+b)*(c+d)';
- mode[43]='(a+b)*(c-d)';
- mode[44]='(a-b)*(c-d)';
- mode[45]='(a+b)/(c+d)';
- mode[46]='(a+b)/(c-d)';
- mode[47]='a/b+c-d';
- mode[48]='a+b/(c+d)';
- mode[49]='a*b/(c+d)';
- mode[50]='a+b/(c-d)';
- mode[51]='a*b/(c-d)';
- mode[52]='a/(b-c/d)';
- var vari=new Array(
- '0123',
- '0132',
- '0213',
- '0231',
- '0312',
- '0321',
- '1023',
- '1032',
- '1203',
- '1230',
- '1302',
- '1320',
- '2013',
- '2031',
- '2103',
- '2130',
- '2301',
- '2310',
- '3012',
- '3021',
- '3102',
- '3120',
- '3201',
- '3210');
- var z=new Array();
- var zt=new Array();
- function cards(ifmine){
- formula.innerHTML='';
- showit.innerHTML='';
- if(ifmine){
- z[0]=prompt('第一张牌的数字:','');
- z[1]=prompt('第二张牌的数字:','');
- z[2]=prompt('第三张牌的数字:','');
- z[3]=prompt('第四张牌的数字:','');
- for(var i=0;i<4;i++){
- zt[i]='<font color=#000088>';
- }
- }
- else{
- for(var i=0;i<4;i++){
- var index=2*Math.floor(cardInf.length/2*Math.random());
- var cardN=cardInf.charAt(index);
- z[i]=charToNum(cardN);
- zt[i]=typhtml[parseInt(cardInf.charAt(index+1))];
- cardInf=cardInf.substring(0,index)+cardInf.substring(index+2,cardInf.length);
- }
- if(cardInf==''){
- alert('52张牌已全部发完!nn正确:'+correct.innerHTML+';错误:'+wrong.innerHTML+';正确率:'+yeild.innerHTML+'。nnn再来一局!');
- cardInf=cardInf0;
- a2.innerHTML='';
- b2.innerHTML='';
- c2.innerHTML='';
- d2.innerHTML='';
- a2.style.backgroundColor='#ff8888';
- b2.style.backgroundColor='#ff8888';
- c2.style.backgroundColor='#ff8888';
- d2.style.backgroundColor='#ff8888';
- a1.value='';
- b1.value='';
- c1.value='';
- d1.value='';
- correct.innerHTML=0;
- wrong.innerHTML=0;
- yeild.innerHTML=0;
- return 0;
- }
- }
- a1.value=z[0];
- b1.value=z[1];
- c1.value=z[2];
- d1.value=z[3];
- var m1='<br><center>',m2='</center></font>';
- a2.innerHTML=zt[0]+m1+cardS[z[0]]+m2;
- b2.innerHTML=zt[1]+m1+cardS[z[1]]+m2;
- c2.innerHTML=zt[2]+m1+cardS[z[2]]+m2;
- d2.innerHTML=zt[3]+m1+cardS[z[3]]+m2;
- a2.style.backgroundColor='#ffffee';
- b2.style.backgroundColor='#ffffee';
- c2.style.backgroundColor='#ffffee';
- d2.style.backgroundColor='#ffffee';
- }
- function cal(){
- showit.innerHTML='<br>标准答案:<br>';
- var p=0;
- for(i=0;i<mode.length;i++){
- for(j=0;j<24;j++){
- a=parseInt(z[vari[j].charAt(0)]);
- b=parseInt(z[vari[j].charAt(1)]);
- c=parseInt(z[vari[j].charAt(2)]);
- d=parseInt(z[vari[j].charAt(3)]);
- var s=mode[i];
- var result=eval(s);
- if(Math.abs(result-24)<1E-5){
- var ina=mode[i].indexOf('a');
- var inb=mode[i].indexOf('b');
- var inc=mode[i].indexOf('c');
- var ind=mode[i].indexOf('d');
- showit.innerHTML+=mode[i].substring(0,ina)+a+mode[i].substring(ina+1,inb)+b+mode[i].substring(inb+1,inc)+c+mode[i].substring(inc+1,ind)+d+mode[i].substring(ind+1,mode[i].length)+'=24n';
- showit.innerHTML+='<br>';
- p=1;
- break;
- }
- }
- }
- if(p) return 0;
- else showit.innerHTML='无解';
- }
- function addN(v,n){
- formula.innerHTML+=v;
- }
- function add(){
- formula.innerHTML+='+';
- }
- function minus(){
- formula.innerHTML+='-';
- }
- function mult(){
- formula.innerHTML+='*';
- }
- function divd(){
- formula.innerHTML+='/';
- }
- function lbr(){
- formula.innerHTML+='(';
- }
- function rbr(){
- formula.innerHTML+=')';
- }
- function equ(){
- var v=eval(formula.innerHTML);
- if(v-Math.floor(v)<1E-5||v-Math.floor(v)>9E-6)
- v=Math.round(v);
- formula.innerHTML+='='+v;
- cal();
- var temp=showit.innerHTML;
- var rt=parseInt(correct.innerHTML);
- var wg=parseInt(wrong.innerHTML);
- if(v==24){
- showit.innerHTML='正确!'+temp;
- correct.innerHTML=rt+1;
- }
- else{
- showit.innerHTML='错误!'+temp;
- wrong.innerHTML=wg+1;
- }
- rt=parseInt(correct.innerHTML);
- wg=parseInt(wrong.innerHTML);
- yeild.innerHTML=Math.floor(rt/(rt+wg)*10000)/100+'%';
- }
- function bkspc(){
- var s=formula.innerHTML;
- formula.innerHTML=s.substring(0,s.length-1);
- }
- function resetit(){
- formula.innerHTML='';
- }
- function abort(){
- cal();
- var temp=showit.innerHTML;
- var rt=parseInt(correct.innerHTML);
- var wg=parseInt(wrong.innerHTML);
- if(showit.innerHTML=='无解'){
- showit.innerHTML='正确!<br>确实无解。'
- correct.innerHTML=rt+1;
- }
- else{
- showit.innerHTML='错误!'+temp;
- wrong.innerHTML=wg+1;
- }
- rt=parseInt(correct.innerHTML);
- wg=parseInt(wrong.innerHTML);
- yeild.innerHTML=Math.floor(rt/(rt+wg)*10000)/100+'%';
- }
- </scrīpt>
- </html>
评论