java游戏五子棋论文
发布网友
发布时间:2022-04-23 09:29
我来回答
共1个回答
热心网友
时间:2022-04-23 10:59
<%@ page contentType="text/html;charset=utf-8"%>
<html>
<head>
<title>五子棋</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
body{ word-break:break-word;
}
#aboutstep{
position:absolute;
right:45px;
top:300px;
width:200px;
vertical-align:bottom;
}
</style>
<script language="javascript">
//五子棋棋子亦称“棋石”分黑、白两色,形状为扁圆形,有一面凸起或两面凸起等形状,厚度不超过0.8厘米,直径为2.0~2.3厘米;
//一副棋子总数为225枚,其中黑子113枚,白子112枚。
var presentperson=false; //true表示人先下,否则表示机器先下
//谁先只需要改为true或false就可以了,不需要改其它东西
var maxsize=15;
var personstep=0;
var machinestep=0;
var _stack=new Array();//needn't to allocate space
var chese=new Array(maxsize*maxsize);
var chesevalue=new Array(maxsize*maxsize);
for(var i=0;i<maxsize*maxsize;i++)//初始化棋盘
{ chese[i]=0;
}
</script>
</head>
<body onload="startup()">
<table id="chesetable" border="1px" width="auto" align="center" height="600px" style="background-color:#EED090;text-align:center;">
<%
for(int i=0;i<15;i++)
{out.print("<tr>");
for(int j=0;j<15;j++)
{out.print("<td onclick='putone(this,"+i+","+j+")' style='width:45px;height:45px;cursor:pointer;'> </td>");
}
out.print("</tr>");
}
%>
</table>
<div id="aboutstep"><!-- 加计时器会降低速度,这里就不加了 -->
人 <img src="images/white_.gif" alt="white" width="50px" height="50px" style="vertical-align:middle;"/><span id="aboutsteppeople" style="position:inline;">0</span>
<span style="display:inline;"><input type="text" id="peopleusedtime" size=4 maxlength="8" /></span>
<br />
机器 <img src="images/black_.gif" alt="black" width="50px" height="50px" style="vertical-align:middle;"s/><span id="aboutstepmachine" style="position:inline;">0</span>
<span style="display:inline;"><input type="text" id="machineusedtime" size=4 maxlength="8" /></span>
</div>
<div align="center">
<input type="button" name="restart" value="重新开始" onclick="reload()"/><input type="button" name="turnout" value="交换顺序" />
<input type="button" name="undo" value="悔棋" onclick="popstack()"/>
</div>
<script language="JavaScript">
function startup()
{
if(!presentperson)//机器先下
{ var obj=document.getElementById("chesetable").rows[7].cells[7];
putone(obj,7,7);
}
}
function putone(obj,i,j)
{ //if(obj.firstChild.nodeValue=="◎"||obj.firstChild.nodeValue=="●")
if(obj.firstChild.nodeType==1) //表示放了棋子
{ return false;}
var node=null;
if(presentperson)
{ //node=document.createTextNode("◎"); //人的棋子颜色
if(document.uniqueID) //IE
node=document.createElement("<img src='images/white_.gif' alt='white' width='40px' height='40px' />");
else { //for Firefox,Opera
node=document.createElement("img");
node.setAttribute("src","images/white_.gif");
node.setAttribute("alt","white");
node.setAttribute("width","40px");
node.setAttribute("height","40px");
}
presentperson=!presentperson;
chese[maxsize*i+j]=1;
personstep++;
document.getElementById("aboutsteppeople").innerHTML=personstep;
}
else {//node=document.createTextNode("●"); //机器的棋子颜色
if(document.uniqueID) //IE
node=document.createElement("<img src='images/black_.gif' alt='black' width='40px' height='40px' />");
else { node=document.createElement("img");
node.setAttribute("src","images/black_.gif");
node.setAttribute("alt","black");
node.setAttribute("width","40px");
node.setAttribute("height","40px");
}presentperson=!presentperson;
chese[maxsize*i+j]=2;
machinestep++;
document.getElementById("aboutstepmachine").innerHTML=machinestep;
}
obj.replaceChild(node,obj.firstChild);
_stack.push(i);
_stack.push(j); //避免数据冗余,就不存储presentperson
var result=checkfinish(!presentperson,i,j);
if(result)
{ if(!presentperson) alert("Congratulation!You win.");
else alert("I am sorry that the machine wins,try your best to win next time.");
setTimeout(reload,4000); //刷新页面
}
if(!presentperson) {machinedecideposition();} //机器走。如果选择全部由人走就去掉这个
}
function checkfinish(presentperson,i,j)
{ //persentperson为true时表示刚才人下了一步
var checkword;
var result=false; //nobody wins
if(presentperson) checkword=1;
else checkword=2;
var total=1;
var tempj=j+1;
while(tempj<maxsize) //横
{ if(chese[maxsize*i+tempj]==checkword)
{total++; tempj++;}
else break;
}
tempj=j-1;
while(tempj>=0)
{ if(chese[maxsize*i+tempj]==checkword)
{total++; tempj--;}
else break;
}
if(total>4) { return true;}
total=1;
var tempi=i+1; //竖
while(tempi<maxsize)
{ if(chese[maxsize*tempi+j]==checkword)
{total++; tempi++;}
else break;
}
tempi=i-1;
while(tempi>=0)
{ if(chese[maxsize*tempi+j]==checkword)
{total++; tempi--;}
else break;
}
if(total>4) { return true;}
total=1;
tempi=i+1; //一三象限斜
tempj=j+1;
while(tempi<maxsize&&tempj<maxsize)
{ if(chese[maxsize*tempi+tempj]==checkword)
{total++; tempi++; tempj++;}
else break;
}
tempi=i-1;
tempj=j-1;
while(tempi>=0&&tempj>=0)
{ if(chese[maxsize*tempi+tempj]==checkword)
{total++; tempi--; tempj--;}
else break;
}
if(total>4) { return true;}
total=1;
tempi=i+1; //二四象限斜
tempj=j-1;
while(tempi<maxsize&&tempj>=0)
{ if(chese[maxsize*tempi+tempj]==checkword)
{total++; tempi++; tempj--;}
else break;
}
tempi=i-1;
tempj=j+1;
while(tempi>=0&&tempj<maxsize)
{ if(chese[maxsize*tempi+tempj]==checkword)
{total++; tempi--; tempj++;}
else break;
}
if(total>4) { return true;}
return false;
}
function pushstack()
{
}
function popstack() //悔棋
{
if(_stack.length/2<1) return;
//var ispresentperson=(_stack.length/2)%2?true:false;//这个跟谁先下有关
var ispresentperson=presentperson; //now it has not matter with the length of _stack
var j=_stack.pop();
var i=_stack.pop();
removeone(ispresentperson,i,j);
}
function removeone(ispresentperson,i,j)
{ var obj=document.getElementById("chesetable");
//text,tbody
var tbody=obj.firstChild.nextSibling;
tbody.rows[i].cells[j].innerHTML=" ";
chese[maxsize*i+j]=0; //也要初始化那个棋子所在的位置
presentperson=!presentperson;//也要回滚该谁走,who's turn now?
if(!ispresentperson) {personstep--;
document.getElementById("aboutsteppeople").innerHTML=personstep;
}
else {machinestep--;
document.getElementById("aboutstepmachine").innerHTML=machinestep;
}
}
function machinedecideposition() //机器决定下一步的位置
{ //在第一层节点选择的时候采用贪婪算法,直接找出相对分数比较高的几个形成第一层节点,目的是为了提高搜索速度和防止堆栈溢出。
if(machinestep==0) //机器走第一步
{ var obj=document.getElementById("chesetable").rows[7].cells[7];
if(putone(obj,7,7)==false)
{ obj=document.getElementById("chesetable").rows[6].cells[6];
putone(obj,6,6);
}
}
else if(machinestep==1) //机器走第二步
{ var obj=document.getElementById("chesetable").rows[7].cells[7];
if(putone(obj,7,7)==false) //7,7位置被占用
{ obj=document.getElementById("chesetable").rows[6].cells[6];
if(putone(obj,6,6)==false)//6,6位置被占用
{obj=document.getElementById("chesetable").rows[6].cells[7];
putone(obj,6,7);
}
}
}
else { var checkword=2; //先判断机器的,在设置为1判断这个位置对人的贡献
for(var tttt=0;tttt<maxsize*maxsize;tttt++)
chesevalue[tttt]=-10;//初始化
for(var i=0;i<maxsize;i++)
for(var j=0;j<maxsize;j++)
{ if(chese[maxsize*i+j]!=0) //the position is occupied
continue;
var total=1;
var tempj=j+1;
var spacenumber=0; //中间空多少个
var befive=0; //成5,包括五连和长连
var livefour=0; //活四,有两个点可以成5地四
var befour=0; //冲四,只有一个点可以成为5的四
var deadfour=0; //死四,不能成为5的四
var livethree=0; //活三,再走一步就可以成为活四的三
//活三包括 连活三和跳活三
var bethree=0;
var livetwo=0;
var betwo=0;
//三三,一子落下同时形成两个活三
//四四,一子落下同时形成两个冲四
//四三,一子落下同时形成一个冲四和 活三
for(var tt=0;;tt++)
{ if(tt==0)
checkword=2; //machine
else checkword=1; //people
while(tempj<maxsize) //横
{ if(chese[maxsize*i+tempj]==checkword&&checkword==2) //是机器的
{total++; tempj++;}
else if(chese[maxsize*i+tempj]==0&&checkword==2) //对于防守不考虑空格
{ //is empty
spacenumber++; break;
if((++tempj)<maxsize&&chese[maxsize*i+tempj]==checkword&&spacenumber<2)
total++;
else break;
}
else if(chese[maxsize*i+tempj]==checkword&&checkword==1) //防守
{ total++; tempj++;
}
else break;
}
var endj=tempj;
tempj=j-1;
while(tempj>=0)
{ if(chese[maxsize*i+tempj]==checkword&&checkword==2)
{total++; tempj--;}
else if(chese[maxsize*i+tempj]==0&&checkword==2)
{ //is empty
spacenumber++; break;
if((--tempj)>=0&&chese[maxsize*i+tempj]==checkword&&spacenumber<2)
total++;
else break;
}
else if(chese[maxsize*i+tempj]==checkword&&checkword==1) //防守
{ total++; tempj--;
}
else break;
}
var startj=tempj;
//由于checkword==2先运行,所以可以直接用befive=1,也可以用befive++;
if(total>4) { //能组成五个或以上的
if(checkword==2)
{befive+=2
} //堵住成五
else befive++;
}//进攻要大于防守
else if(total==4) { //能组成四个
if(checkword==2) //表示考虑进攻
{ if(startj>=0&&endj<maxsize&&chese[maxsize*i+startj]!=1&&chese[maxsize*i+endj]!=1) //两边为空格
livefour+=2; //放在这里可以形成一个活四
else if((startj>=0&&chese[maxsize*i+startj]!=1)||(endj<maxsize&&chese[maxsize*i+endj]!=1))
befour+=2; //形成一个冲四
else deadfour+=2;
}
else //考虑防守
{ if(startj>=0&&endj<maxsize&&chese[maxsize*i+startj]!=2&&chese[maxsize*i+endj]!=2)
livefour+=1; //放在这里可以形成一个活四
else if((startj>=0&&chese[maxsize*i+startj]!=2)||(endj<maxsize&&chese[maxsize*i+endj]!=2))
befour+=1; //形成一个冲四
else deadfour+=1;
}
}
else if(total==3){if(checkword==2) //表示考虑进攻
{ if(startj>=0&&endj<maxsize&&chese[maxsize*i+startj]!=1&&chese[maxsize*i+endj]!=1) //两边为空格
livethree+=2; //放在这里可以形成一个活三(算做连活三)
else if((startj>=0&&chese[maxsize*i+startj]!=1)||(endj<maxsize&&chese[maxsize*i+endj]!=1))
bethree+=2; //形成一个眠三,即可以冲四的三
// else deadfour+=2;
//跳三
}
else //考虑防守
{ if(startj>=0&&endj<maxsize&&chese[maxsize*i+startj]!=2&&chese[maxsize*i+endj]!=2) //两边为空格
livethree+=1; //放在这里可以形成一个活三(算做连活三)
else if((startj>=0&&chese[maxsize*i+startj]!=2)||(endj<maxsize&&chese[maxsize*i+endj]!=2))
bethree+=1; //形成一个眠三,即可以冲四的三
}
}
else if(total==2){ if(checkword==2) //表示考虑进攻
{ if(startj>=0&&endj<maxsize&&chese[maxsize*i+startj]!=1&&chese[maxsize*i+endj]!=1) //两边为空格
livetwo++; //放在这里可以形成一个活三(算做连活三)
}
else //考虑防守
{ if(startj>=0&&endj<maxsize&&chese[maxsize*i+startj]!=2&&chese[maxsize*i+endj]!=2) //两边为空格
livetwo++; //放在这里可以形成一个活三(算做连活三)
}
}