五子棋代码java论文 java五子棋毕业论文

java游戏五子棋论文

%@ 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;imaxsize*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;i15;i++)

{out.print("tr");

for(int j=0;j15;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(tempjmaxsize) //横

{ 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(total4) { return true;}

total=1;

var tempi=i+1; //竖

while(tempimaxsize)

{ 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(total4) { return true;}

total=1;

tempi=i+1; //一三象限斜

tempj=j+1;

while(tempimaxsizetempjmaxsize)

{ if(chese[maxsize*tempi+tempj]==checkword)

{total++; tempi++; tempj++;}

else break;

}

tempi=i-1;

tempj=j-1;

while(tempi=0tempj=0)

{ if(chese[maxsize*tempi+tempj]==checkword)

{total++; tempi--; tempj--;}

else break;

}

if(total4) { return true;}

total=1;

tempi=i+1; //二四象限斜

tempj=j-1;

while(tempimaxsizetempj=0)

{ if(chese[maxsize*tempi+tempj]==checkword)

{total++; tempi++; tempj--;}

else break;

}

tempi=i-1;

tempj=j+1;

while(tempi=0tempjmaxsize)

{ if(chese[maxsize*tempi+tempj]==checkword)

{total++; tempi--; tempj++;}

else break;

}

if(total4) { return true;}

return false;

}

function pushstack()

{

}

function popstack() //悔棋

{

if(_stack.length/21) 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;ttttmaxsize*maxsize;tttt++)

chesevalue[tttt]=-10;//初始化

for(var i=0;imaxsize;i++)

for(var j=0;jmaxsize;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(tempjmaxsize) //横

{ if(chese[maxsize*i+tempj]==checkwordcheckword==2) //是机器的

{total++; tempj++;}

else if(chese[maxsize*i+tempj]==0checkword==2) //对于防守不考虑空格

{ //is empty

spacenumber++; break;

if((++tempj)maxsizechese[maxsize*i+tempj]==checkwordspacenumber2)

total++;

else break;

}

else if(chese[maxsize*i+tempj]==checkwordcheckword==1) //防守

{ total++; tempj++;

}

else break;

}

var endj=tempj;

tempj=j-1;

while(tempj=0)

{ if(chese[maxsize*i+tempj]==checkwordcheckword==2)

{total++; tempj--;}

else if(chese[maxsize*i+tempj]==0checkword==2)

{ //is empty

spacenumber++; break;

if((--tempj)=0chese[maxsize*i+tempj]==checkwordspacenumber2)

total++;

else break;

}

else if(chese[maxsize*i+tempj]==checkwordcheckword==1) //防守

{ total++; tempj--;

}

else break;

}

var startj=tempj;

//由于checkword==2先运行,所以可以直接用befive=1,也可以用befive++;

if(total4) { //能组成五个或以上的

if(checkword==2)

{befive+=2

} //堵住成五

else befive++;

}//进攻要大于防守

else if(total==4) { //能组成四个

if(checkword==2) //表示考虑进攻

{ if(startj=0endjmaxsizechese[maxsize*i+startj]!=1chese[maxsize*i+endj]!=1) //两边为空格

livefour+=2; //放在这里可以形成一个活四

else if((startj=0chese[maxsize*i+startj]!=1)||(endjmaxsizechese[maxsize*i+endj]!=1))

befour+=2; //形成一个冲四

else deadfour+=2;

}

else //考虑防守

{ if(startj=0endjmaxsizechese[maxsize*i+startj]!=2chese[maxsize*i+endj]!=2)

livefour+=1; //放在这里可以形成一个活四

else if((startj=0chese[maxsize*i+startj]!=2)||(endjmaxsizechese[maxsize*i+endj]!=2))

befour+=1; //形成一个冲四

else deadfour+=1;

}

}

else if(total==3){if(checkword==2) //表示考虑进攻

{ if(startj=0endjmaxsizechese[maxsize*i+startj]!=1chese[maxsize*i+endj]!=1) //两边为空格

livethree+=2; //放在这里可以形成一个活三(算做连活三)

else if((startj=0chese[maxsize*i+startj]!=1)||(endjmaxsizechese[maxsize*i+endj]!=1))

bethree+=2; //形成一个眠三,即可以冲四的三

// else deadfour+=2;

//跳三

}

else //考虑防守

{ if(startj=0endjmaxsizechese[maxsize*i+startj]!=2chese[maxsize*i+endj]!=2) //两边为空格

livethree+=1; //放在这里可以形成一个活三(算做连活三)

else if((startj=0chese[maxsize*i+startj]!=2)||(endjmaxsizechese[maxsize*i+endj]!=2))

bethree+=1; //形成一个眠三,即可以冲四的三

}

}

else if(total==2){ if(checkword==2) //表示考虑进攻

{ if(startj=0endjmaxsizechese[maxsize*i+startj]!=1chese[maxsize*i+endj]!=1) //两边为空格

livetwo++; //放在这里可以形成一个活三(算做连活三)

}

else //考虑防守

{ if(startj=0endjmaxsizechese[maxsize*i+startj]!=2chese[maxsize*i+endj]!=2) //两边为空格

livetwo++; //放在这里可以形成一个活三(算做连活三)

}

}

求一个简单的JAVA五子棋代码!! 网上复制的别来了!

以下是现写的 实现了两人对战 自己复制后运行把 没什么难度 类名 Games

import java.util.Scanner;

public class Games {

private String board[][];

private static int SIZE = 17;

private static String roles = "A玩家";

//初始化数组

public void initBoard() {

board = new String[SIZE][SIZE];

for (int i = 0; i SIZE; i++) {

for (int j = 0; j SIZE; j++) {

// if(i==0){

// String str = "";

// str += j+" ";

// board[i][j]= str;

// }else if(i!=0j==0){

// String str = "";

// str += i+" ";

// board[i][j]= str;

// }else{

board[i][j] = "╋";

// }

}

}

}

//输出棋盘

public void printBoard() {

for (int i = 0; i SIZE; i++) {

for (int j = 0; j SIZE; j++) {

System.out.print(board[i][j]);

}

System.out.println();

}

}

//判断所下棋子位置是否合理

public boolean isOk(int x, int y) {

boolean isRight = true;

if (x = 16 || x 1 || y = 16 | y 1) {

//System.out.println("输入错误,请从新输入");

isRight = false;

}

if (board[x][y].equals("●") || board[x][y].equals("○")) {

isRight = false;

}

return isRight;

}

//判断谁赢了

public void whoWin(Games wz) {

// 从数组挨个查找找到某个类型的棋子就从该棋子位置向右,向下,斜向右下 各查找5连续的位置看是否为5个相同的

int xlabel;// 记录第一次找到某个棋子的x坐标

int ylabel;// 记录第一次找到某个棋子的y坐标

// ●○╋

// 判断人是否赢了

for (int i = 0; i SIZE; i++) {

for (int j = 0; j SIZE; j++) {

if (board[i][j].equals("○")) {

xlabel = i;

ylabel = j;

// 横向找 x坐标不变 y坐标以此加1连成字符串

String heng = "";

if (i + 5 SIZE j + 5 SIZE) {

for (int k = j; k j + 5; k++) {

heng += board[i][k];

}

if (heng.equals("○○○○○")) {

System.out.println(roles+"赢了!您输了!");

System.exit(0);

}

// 向下判断y不变 x逐增5 连成字符串

String xia = "";

for (int l = j; l i + 5; l++) {

xia += board[l][j];

// System.out.println(xia);

}

if (xia.equals("○○○○○")) {

System.out.println(roles+"赢了!您输了!");

System.exit(0);

}

// 斜向右下判断

String youxia = "";

for (int a = 1; a = 5; a++) {

youxia += board[xlabel++][ylabel++];

}

if (youxia.equals("○○○○○")) {

System.out.println(roles+"赢了!您输了!");

System.exit(0);

}

}

}

}

}

// 判断电脑是否赢了

for (int i = 0; i SIZE; i++) {

for (int j = 0; j SIZE; j++) {

if (board[i][j].equals("●")) {

xlabel = i;

ylabel = j;

// 横向找 x坐标不变 y坐标以此加1连成字符串

String heng = "";

if (j + 5 SIZE i + 5 SIZE) {

for (int k = j; k j + 5; k++) {

heng += board[i][k];

}

if (heng.equals("●●●●●")) {

System.out.println(roles+"赢输了!您输了!");

System.exit(0);

}

// 向下判断y不变 x逐增5 连成字符串

String xia = "";

for (int l = i; l i + 5; l++) {

xia += board[l][ylabel];

// System.out.println(xia);

}

if (xia.equals("●●●●●")) {

System.out.println(roles+"赢了!您输了!");

System.exit(0);

}

// 斜向右下判断

String youxia = "";

for (int a = 1; a = 5; a++) {

youxia += board[xlabel++][ylabel++];

}

if (youxia.equals("●●●●●")) {

System.out.println(roles+"赢了!您输了!");

System.exit(0);

}

}

}

}

}

}

public static void main(String[] args) {

Games wz = new Games();

Scanner sc = new Scanner(System.in);

wz.initBoard();

wz.printBoard();

while (true) {

System.out.print("请"+roles+"输入X,Y坐标,必须在0-15范围内,xy以空格隔开,输入16 16结束程序");

int x = sc.nextInt();

int y = sc.nextInt();

if (x == SIZE y == SIZE) {

System.out.println("程序结束");

System.exit(0);

}

if (x SIZE || x 0 || y SIZE | y 0) {

System.out.println("输入错误,请从新输入");

continue;

}

//如果roles是A玩家 就让A玩家下棋,否则就让B玩家下棋。

if (wz.board[x][y].equals("╋")roles.equals("A玩家")) {

wz.board[x][y] = "○";

wz.printBoard();

//判断输赢

wz.whoWin(wz);

}else if(wz.board[x][y].equals("╋")roles.equals("B玩家")){

wz.board[x][y] = "●";

wz.printBoard();

//判断输赢

wz.whoWin(wz);

} else {

System.out.println("此处已经有棋子,从新输入");

continue;

}

if(roles.equals("A玩家")){

roles = "B玩家";

}else if(roles.equals("B玩家")){

roles = "A玩家";

}

}

}

}

java五子棋毕业论文

计算机毕业设计论文 要比较全的java游戏五子棋可以到七七计算机论文的题目列表搜寻一下,应该有的

JAVA五子棋程序代码分析(1)

楼主要是觉得看的不舒服可以拷到记事本里看~ import java.applet.*; import java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.awt.Color; //这一段import就不说了,下面要用到的就import进来 public class wuziqi extends Applet implements ActionListener,MouseListener,MouseMotionListener,ItemListener //继承Applet表明是个applet,后面的接口必须要实现每个接口的所有方法 { int color_Qizi=0;//旗子的颜色标识 0:白子 1:黑子 int intGame_Start=0;//游戏开始标志 0未开始 1游戏中 int intGame_Body[][]=new int[16][16]; //设置棋盘棋子状态 0 无子 1 白子 2 黑子 Button b1=new Button("游戏开始"); Button b2=new Button("重置游戏"); //两个按钮 Label lblWin=new Label(" "); //这个label用来显示最后输赢信息的,先留空 Checkbox ckbHB[]=new Checkbox[2]; //用来表明选择黑气或白棋先走的checkbox CheckboxGroup ckgHB=new CheckboxGroup(); //两个checkbox必须放在同一个checkboxgroup里才能做到单选 public void init() //初始化,堆砌界面 { setLayout(null); //不设布局管理器 addMouseListener(this); //将本类作为鼠标事件的接口响应鼠标动作 add(b1); //将事先定义好的第一个按钮添加入界面 b1.setBounds(330,50,80,30); //设置第一个按钮左上角的位置和大小 b1.addActionListener(this); //将本类作为按钮事件的接口响应按钮动作 add(b2); //将事先定义好的第二个按钮添加进去 b2.setBounds(330,90,80,30); /设置第二个按钮左上角的位置和大小 b2.addActionListener(this); //将本类作为按钮事件的接口响应按钮动作 ckbHB[0]=new Checkbox("白子先",ckgHB,false); //new一个checkbox ckbHB[0].setBounds(320,20,60,30); //设置左上角位置和大小 ckbHB[1]=new Checkbox("黑子先",ckgHB,false); //new第二个checkbox ckbHB[1].setBounds(380,20,60,30); //设置左上角位置和大小 add(ckbHB[0]); //将第一个checkbox加入界面 add(ckbHB[1]); //将第二个checkbox加入界面 ckbHB[0].addItemListener(this); //将本类作为其事件接口来响应选中动作 ckbHB[1].addItemListener(this); //将本类作为其事件接口来响应选中动作 add(lblWin); //将标签加入界面 lblWin.setBounds(330,130,80,30); //设置标签的左上角位置和大小 Game_start_csh(); //调用游戏初始化 } public void itemStateChanged(ItemEvent e) //ItemListener接口中的方法,必须要有 { if (ckbHB[0].getState()) //选择黑子先还是白子先 { color_Qizi=0; //白棋先 } else { color_Qizi=1; //黑棋先 } } public void actionPerformed(ActionEvent e) //ActionListener接口中的方法,也是必须的 { Graphics g=getGraphics(); //这句话貌似可以去掉,g是用来画图或者画界面的 if (e.getSource()==b1) //如果动作的来源是第一个按钮 { Game_start(); //游戏开始 } else //否则 { Game_re(); //游戏重新开始 } } public void mousePressed(MouseEvent e){} //MouseListener接口中的方法,用不到所以留个空,但一定要有 public void mouseClicked(MouseEvent e) //鼠标单击时 { Graphics g=getGraphics(); //获得画笔 int x1,y1; x1=e.getX(); //单击处的x坐标 y1=e.getY(); //单击处的y坐标 if (e.getX()20 || e.getX()300 || e.getY()20 || e.getY()300) //在棋盘范围之外 { return; //则这是不能走棋的,直接返回 } //下面这两个if和两个赋值的作用是将x和y坐标根据舍入原则修改成棋盘上格子的坐标 if (x1%2010) { x1+=20; } if(y1%2010) { y1+=20; } x1=x1/20*20; y1=y1/20*20; set_Qizi(x1,y1); //在棋盘上画上一个棋子 } public void mouseEntered(MouseEvent e){} //MouseListener接口中的方法,用不到所以留个空,但一定要有 public void mouseExited(MouseEvent e){} //MouseListener接口中的方法,用不到所以留个空,但一定要有 public void mouseReleased(MouseEvent e){} //MouseListener接口中的方法,用不到所以留个空,但一定要有 public void mouseDragged(MouseEvent e){} //MouseListener接口中的方法,用不到所以留个空,但一定要有 public void mouseMoved(MouseEvent e){} //MouseListener接口中的方法,用不到所以留个空,但一定要有 public void paint(Graphics g) //重绘和applet程序装载的时候会调用这个绘制的过程 { draw_qipan(g); //画棋盘 }


分享文章:五子棋代码java论文 java五子棋毕业论文
网站地址:http://ybzwz.com/article/dosgdgj.html