java五子棋清除棋子

//楼主参考一下.

importjava.awt.*;

importjava.awt.event.*;

importjava.applet.Applet;

importjava.awt.Color;

publicclassWuZhiqiextendsAppletimplementsActionListener,MouseListener,

MouseMotionListener,ItemListener{

intcolor=0;//旗子的颜色标识0:白子1:黑子

booleanisStart=false;//游戏开始标志

intbodyArray[][]=newint[16][16];//设置棋盘棋子状态0无子1白子2黑子

Buttonb1=newButton(游戏开始);

Buttonb2=newButton(重置游戏);

LabellblWin=newLabel();

CheckboxckbHB[]=newCheckbox[2];

CheckboxGroupckgHB=newCheckboxGroup();

publicvoidinit(){

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]=newCheckbox(白子先,ckgHB,false);

ckbHB[0].setBounds(320,20,60,30);

ckbHB[1]=newCheckbox(黑子先,ckgHB,false);

ckbHB[1].setBounds(380,20,60,30);

add(ckbHB[0]);

add(ckbHB[1]);

ckbHB[0].addItemListener(this);

ckbHB[1].addItemListener(this);

add(lblWin);

lblWin.setBounds(330,130,80,30);

gameInit();

this.resize(newDimension(450,350));

}

publicvoiditemStateChanged(ItemEvente){

if(ckbHB[0].getState())//选择黑子先还是白子先

{

color=0;

}else{

color=1;

}

}

publicvoidactionPerformed(ActionEvente){

if(e.getSource()==b1){

gameStart();

}else{

reStart();

}

}

publicvoidmousePressed(MouseEvente){

}

publicvoidmouseClicked(MouseEvente){

intx1,y1;

x1=e.getX();

y1=e.getY();

if(e.getX()300||e.getY()300){

return;

}

if(x1%20>10){

x1+=20;

}

if(y1%20>10){

y1+=20;

}

x1=x1/20*20;

y1=y1/20*20;

setDown(x1,y1);

}

publicvoidmouseEntered(MouseEvente){

}

publicvoidmouseExited(MouseEvente){

}

publicvoidmouseReleased(MouseEvente){

}

publicvoidmouseDragged(MouseEvente){

}

publicvoidmouseMoved(MouseEvente){

}

publicvoidpaint(Graphicsg){

g.setColor(Color.lightGray);

g.fill3DRect(10,10,300,300,true);

g.setColor(Color.black);

for(inti=1;i<16;i++){

g.drawLine(20,20*i,300,20*i);

g.drawLine(20*i,20,20*i,300);

}

}

publicvoidsetDown(intx,inty)//落子

{

if(!isStart)//判断游戏未开始

{

return;

}

if(bodyArray[x/20][y/20]!=0){

return;

}

Graphicsg=getGraphics();

if(color==1)//判断黑子还是白子

{

g.setColor(Color.black);

color=0;

}else{

g.setColor(Color.white);

color=1;

}

g.fillOval(x-10,y-10,20,20);

bodyArray[x/20][y/20]=color+1;

if(gameWin1(x/20,y/20))//判断输赢

{

lblWin.setText(startColor(color)+赢了!);

isStart=false;

}

if(gameWin2(x/20,y/20))//判断输赢

{

lblWin.setText(startColor(color)+赢了!);

isStart=false;

}

if(gameWin3(x/20,y/20))//判断输赢

{

lblWin.setText(startColor(color)+赢了!);

isStart=false;

}

if(gameWin4(x/20,y/20))//判断输赢

{

lblWin.setText(startColor(color)+赢了!);

isStart=false;

}

}

publicStringstartColor(intx){

if(x==0){

return黑子;

}else{

return白子;

}

}

publicvoidgameStart()//游戏开始

{

isStart=true;

enableGame(false);

b2.setEnabled(true);

}

publicvoidgameInit()//游戏开始初始化

{

isStart=false;

enableGame(true);

b2.setEnabled(false);

ckbHB[0].setState(true);

for(inti=0;i<16;i++){

for(intj=0;j<16;j++){

bodyArray[i][j]=0;

}

}

lblWin.setText();

}

publicvoidreStart()//游戏重新开始

{

repaint();

gameInit();

}

publicvoidenableGame(booleane)//设置组件状态

{

b1.setEnabled(e);

b2.setEnabled(e);

ckbHB[0].setEnabled(e);

ckbHB[1].setEnabled(e);

}

publicbooleangameWin1(intx,inty)//判断输赢横

{

intx1,y1,t=1;

x1=x;

y1=y;

for(inti=1;i<5;i++){

if(x1>15){

break;

}

if(bodyArray[x1+i][y1]==bodyArray[x][y]){

t+=1;

}else{

break;

}

}

for(inti=1;i<5;i++){

if(x1<1){

break;

}

if(bodyArray[x1-i][y1]==bodyArray[x][y]){

t+=1;

}else{

break;

}

}

if(t>4){

returntrue;

}else{

returnfalse;

}

}

publicbooleangameWin2(intx,inty)//判断输赢竖

{

intx1,y1,t=1;

x1=x;

y1=y;

for(inti=1;i<5;i++){

if(x1>15){

break;

}

if(bodyArray[x1][y1+i]==bodyArray[x][y]){

t+=1;

}else{

break;

}

}

for(inti=1;i<5;i++){

if(x1<1){

break;

}

if(bodyArray[x1][y1-i]==bodyArray[x][y]){

t+=1;

}else{

break;

}

}

if(t>4){

returntrue;

}else{

returnfalse;

}

}

publicbooleangameWin3(intx,inty)//判断输赢左斜

{

intx1,y1,t=1;

x1=x;

y1=y;

for(inti=1;i<5;i++){

if(x1>15){

break;

}

if(bodyArray[x1+i][y1-i]==bodyArray[x][y]){

t+=1;

}else{

break;

}

}

for(inti=1;i<5;i++){

if(x1<1){

break;

}

if(bodyArray[x1-i][y1+i]==bodyArray[x][y]){

t+=1;

}else{

break;

}

}

if(t>4){

returntrue;

}else{

returnfalse;

}

}

publicbooleangameWin4(intx,inty)//判断输赢左斜

{

intx1,y1,t=1;

x1=x;

y1=y;

for(inti=1;i<5;i++){

if(x1>15){

break;

}

if(bodyArray[x1+i][y1+i]==bodyArray[x][y]){

t+=1;

}else{

break;

}

}

for(inti=1;i<5;i++){

if(x1<1){

break;

}

if(bodyArray[x1-i][y1-i]==bodyArray[x][y]){

t+=1;

}else{

break;

}

}

if(t>4){

returntrue;

}else{

returnfalse;

}

}

}

免责声明:本站发布的游戏攻略(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场。
如果本文侵犯了您的权益,请联系站长邮箱进行举报反馈,一经查实,我们将在第一时间处理,感谢您对本站的关注!