JAVA五子棋判断输赢

2024-07-17 23:46:10作者:饭克斯

importjava.awt.*;

importjavax.swing.*;

publicclassChessFrameextendsJFrame{

privateChessPanelcp;

privateChessModelcm;

publicChessFrame(){

this.setTitle(五子棋);

cm=newChessModel();

cp=newChessPanel(cm);

this.add(cp);

}

publicvoidshowMe(){

this.setVisible(true);

this.setSize(480,400);

this.setResizable(false);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

publicstaticvoidmain(String[]args){

newChessFrame().showMe();

}

}

----------------------------------------

importjavax.swing.*;

publicclassChessModel{

privateintwidth,height;

privateintx=0,y=0;

privateint[][]arrMapShow;

privatebooleanisOdd,isExist;

publicChessModel(){

this.isOdd=true;

PaneInit(20,15);

}

privatevoidPaneInit(intwidth,intheight){

this.width=width;

this.height=height;

arrMapShow=newint[width+1][height+1];

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

for(intj=0;j<height+1;j++){

arrMapShow[i][j]=5;

}

}

}

publicintgetWidth(){

returnwidth;

}

publicvoidsetWidth(intwidth){

this.width=width;

}

publicint[][]getArrMapShow(){

returnarrMapShow;

}

publicvoidsetArrMapShow(int[][]arrMapShow){

this.arrMapShow=arrMapShow;

}

publicintgetHeight(){

returnheight;

}

publicvoidsetHeight(intheight){

this.height=height;

}

publicintgetX(){

returnx;

}

publicvoidsetX(intx){

this.x=x;

}

publicintgetY(){

returny;

}

publicvoidsetY(inty){

this.y=y;

}

publicbooleangetisOdd(){

returnisOdd;

}

publicvoidsetisOdd(booleanisOdd){

if(isOdd)

this.isOdd=true;

else

this.isOdd=false;

}

publicbooleanisExist(){

returnisExist;

}

publicvoidsetExist(booleanisExist){

this.isExist=isExist;

}

privatebooleanbadxy(intx,inty){

if(x>=width+20||x<0)

returntrue;

returny>=height+20||y<0;

}

publicbooleanchessExist(inti,intj){

if(this.arrMapShow[i][j]==1||this.arrMapShow[i][j]==2)

returntrue;

returnfalse;

}

publicvoidredyplay(intx,inty){

if(badxy(x,y))

return;

if(chessExist(x,y))

return;

this.arrMapShow[x][y]=3;

}

publicvoidplay(intx,inty){

if(badxy(x,y))

return;

if(chessExist(x,y)){

this.isExist=true;

return;

}else

this.isExist=false;

if(getisOdd()){

setisOdd(false);

this.arrMapShow[x][y]=1;

}else{

setisOdd(true);

this.arrMapShow[x][y]=2;

}

}

//判断胜利的条件

publicbooleanjudgeSuccess(intx,inty,booleanisodd){

intnum=1;

intarrvalue;

intx_temp=x,y_temp=y;

if(isodd)

arrvalue=2;

else

arrvalue=1;

intx_temp1=x_temp,y_temp1=y_temp;

//判断右边

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

x_temp1+=1;

if(x_temp1>this.width)

break;

if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)

num++;

else

break;

}

//判断左边

x_temp1=x_temp;

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

x_temp1-=1;

if(x_temp1<0)

break;

if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)

num++;

else

break;

}

if(num==5)

returntrue;

//判断上方

x_temp1=x_temp;

y_temp1=y_temp;

num=1;

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

y_temp1-=1;

if(y_temp1<0)

break;

if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)

num++;

else

break;

}

//判断下方

y_temp1=y_temp;

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

y_temp1+=1;

if(y_temp1>this.height)

break;

if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)

num++;

else

break;

}

if(num==5)

returntrue;

//判断左上

x_temp1=x_temp;

y_temp1=y_temp;

num=1;

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

x_temp1-=1;

y_temp1-=1;

if(y_temp1<0||x_temp1<0)

break;

if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)

num++;

else

break;

}

//判断右下

x_temp1=x_temp;

y_temp1=y_temp;

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

x_temp1+=1;

y_temp1+=1;

if(y_temp1>this.height||x_temp1>this.width)

break;

if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)

num++;

else

break;

}

if(num==5)

returntrue;

//判断右上

x_temp1=x_temp;

y_temp1=y_temp;

num=1;

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

x_temp1+=1;

y_temp1-=1;

if(y_temp1this.width)

break;

if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)

num++;

else

break;

}

//判断左下

x_temp1=x_temp;

y_temp1=y_temp;

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

x_temp1-=1;

y_temp1+=1;

if(y_temp1>this.height||x_temp1<0)

break;

if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)

num++;

else

break;

}

if(num==5)

returntrue;

returnfalse;

}

//五子成线后的提示

publicvoidshowSuccess(JPaneljp){

JOptionPane.showMessageDialog(jp,你赢了,好厉害!,win,

JOptionPane.INFORMATION_MESSAGE);

newChessFrame().showMe();

}

}

----------------------

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.JPanel;

publicclassChessPanelextendsJPanel{

privateintwidth,height;

privateChessModelcm;

publicChessPanel(ChessModelmm){

cm=mm;

width=cm.getWidth();

height=cm.getHeight();

addMouseListener(newMouseAdapter(){

publicvoidmousePressed(MouseEvente){

intx=(e.getX()-10)/20;

inty=(e.getY()-10)/20;

cm.play(x,y);

repaint();

if(cm.judgeSuccess(x,y,cm.getisOdd())){

cm.showSuccess(null);

}

}

});

}

publicvoidpaintComponent(Graphicsg){

super.paintComponent(g);

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

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

intv=cm.getArrMapShow()[i][j];

draw(g,i,j,v);

}

}

}

publicvoidsetModel(ChessModelmm){

cm=mm;

width=cm.getWidth();

height=cm.getHeight();

}

publicvoiddraw(Graphicsg,inti,intj,intv){

intx=20*i+20;

inty=20*j+20;

if(i!=width&&j!=height){

g.setColor(Color.black);

g.drawRect(x,y,20,20);

}

if(i!=width&&j!=height){

g.setColor(Color.black);

g.drawRect(x,y,20,20);

}

if(v==1){

g.setColor(Color.gray);

g.drawOval(x-8,y-8,16,16);

g.setColor(Color.black);

g.fillOval(x-8,y-8,16,16);

}

if(v==2){

g.setColor(Color.gray);

g.drawOval(x-8,y-8,16,16);

g.setColor(Color.white);

g.fillOval(x-8,y-8,16,16);

}

if(v==3){

g.setColor(Color.cyan);

g.drawOval(x-8,y-8,316,16);

}

}

}

三个类可以直接使用了

展开全文

热门推荐

相关攻略

猜你喜欢