五子棋双人对战.docx

上传人:b****4 文档编号:4625747 上传时间:2022-12-07 格式:DOCX 页数:14 大小:64.01KB
下载 相关 举报
五子棋双人对战.docx_第1页
第1页 / 共14页
五子棋双人对战.docx_第2页
第2页 / 共14页
五子棋双人对战.docx_第3页
第3页 / 共14页
五子棋双人对战.docx_第4页
第4页 / 共14页
五子棋双人对战.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

五子棋双人对战.docx

《五子棋双人对战.docx》由会员分享,可在线阅读,更多相关《五子棋双人对战.docx(14页珍藏版)》请在冰豆网上搜索。

五子棋双人对战.docx

五子棋双人对战

black.gif

chessPostion.png

bottom.png

left.png

leftTop.png

leftBottom.png

right.png

rightTop.png

rightBottom.png

selected.gif

top.png

white.gif

----------------------------------------------------------Constant接口--------------------------------------------------

packagecommon;

/**

 *常量类

 *@authorermao

 *

 */

publicclassConstant{

publicstaticfinalintROWS=15;

publicstaticfinalintCOLS=15;

publicstaticfinalintSIZE=35;

}

-----------------------------------------------------------Main类---------------------------------------------------

packageui;

publicclassMain{

publicstaticvoidmain(String[]args){

ChessBoardChessBoard=newChessBoard();

ChessPanelChessPanel=newChessPanel();

ChessPanel.init();

ChessBoard.setContentPane(ChessPanel);

ChessBoard.setVisible(true);

}

}

----------------------------------------------------ChessBoard类------------------------------------------------

packageui;

importjavax.swing.JFrame;

importcommon.Constant;

publicclassChessBoardextendsJFrame{

publicChessBoard(){

   this.setSize(Constant.ROWS*Constant.SIZE,Constant.COLS*Constant.SIZE);

   this.setLocationRelativeTo(null);

  }

}

------------------------------------------------------ChessPanel类--------------------------------------------------

packageui;

importjava.awt.GridLayout;

importjava.awt.image.BufferedImage;

importjava.io.File;

importjava.io.IOException;

importjavax.imageio.ImageIO;

importjavax.swing.JButton;

importjavax.swing.JPanel;

publicclassChessPanelextendsJPanel{

publicstaticfinalChessChess[][]=newChess[15][15];

staticintChessState=0;

publicstaticintgetChessState(){

returnChessState;

}

publicChessPanel(){

}

publicvoidinit(){

this.setLayout(newGridLayout(15,15));

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

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

BufferedImageBgImage=null;

if(i==0){

if(j==0){

   try{

   BgImage=ImageIO.read(newFile("src/imgs/leftTop.png"));

   }catch(IOExceptione){

   e.printStackTrace();

   }

   ChessChessButton=newChess();

   ChessButton.getImage(BgImage);

   ChessButton.chessX=j;

   ChessButton.chessY=i;

   Chess[i][j]=ChessButton;

   this.add(ChessButton);

}

elseif(j==14){

try{

BgImage=ImageIO.read(newFile("src/imgs/rightTop.png"));

}catch(IOExceptione){

e.printStackTrace();

}

ChessChessButton=newChess();

ChessButton.getImage(BgImage);

ChessButton.chessX=j;

   ChessButton.chessY=i;

Chess[i][j]=ChessButton;

this.add(ChessButton);

}

else{

try{

BgImage=ImageIO.read(newFile("src/imgs/top.png"));

}catch(IOExceptione){

e.printStackTrace();

}

ChessChessButton=newChess();

ChessButton.getImage(BgImage);;

ChessButton.chessX=j;

   ChessButton.chessY=i;

   Chess[i][j]=ChessButton;

this.add(ChessButton);

}

}

elseif(i==14){

if(j==0){

try{

BgImage=ImageIO.read(newFile("src/imgs/leftBottom.png"));

}catch(IOExceptione){

e.printStackTrace();

}

ChessChessButton=newChess();

ChessButton.getImage(BgImage);

ChessButton.chessX=i;

   ChessButton.chessY=j;

   Chess[i][j]=ChessButton;

this.add(ChessButton);

}

elseif(j==14){

try{

BgImage=ImageIO.read(newFile("src/imgs/rightBottom.png"));

}catch(IOExceptione){

e.printStackTrace();

}

ChessChessButton=newChess();

ChessButton.getImage(BgImage);

ChessButton.chessX=i;

   ChessButton.chessY=j;

   Chess[i][j]=ChessButton;

this.add(ChessButton);

}

else{

try{

BgImage=ImageIO.read(newFile("src/imgs/bottom.png"));

}catch(IOExceptione){

e.printStackTrace();

}

ChessChessButton=newChess();

ChessButton.getImage(BgImage);

ChessButton.chessX=i;

   ChessButton.chessY=j;

   Chess[i][j]=ChessButton;

this.add(ChessButton);

}

}

else{

if(j==0){

try{

BgImage=ImageIO.read(newFile("src/imgs/left.png"));

}catch(IOExceptione){

e.printStackTrace();

}

ChessChessButton=newChess();

ChessButton.getImage(BgImage);

ChessButton.chessX=i;

   ChessButton.chessY=j;

   Chess[i][j]=ChessButton;

this.add(ChessButton);

}

elseif(j==14){

try{

BgImage=ImageIO.read(newFile("src/imgs/right.png"));

}catch(IOExceptione){

e.printStackTrace();

}

ChessChessButton=newChess();

ChessButton.getImage(BgImage);

ChessButton.chessX=i;

   ChessButton.chessY=j;

   Chess[i][j]=ChessButton;

this.add(ChessButton);

}

else{

try{

BgImage=ImageIO.read(newFile("src/imgs/chessPosition.png"));

}catch(IOExceptione){

e.printStackTrace();

}

ChessChessButton=newChess();

ChessButton.getImage(BgImage);;

ChessButton.chessX=i;

   ChessButton.chessY=j;

   Chess[i][j]=ChessButton;

this.add(ChessButton);

}

}

}

}

}

}

---------------------------------------------------------------Chess类--------------------------------------------------------

packageui;

importjava.awt.Graphics;

importjava.awt.event.MouseAdapter;

importjava.awt.event.MouseEvent;

importjava.awt.image.BufferedImage;

importjava.io.File;

importjava.io.IOException;

importjavax.imageio.ImageIO;

importjavax.swing.JButton;

importjavax.swing.JOptionPane;

importutil.GameManager;

publicclassChessextendsJButton{

publicstaticfinalintWHITE=2,BLACK=1,NONE=0;

publicstaticintCurrentChess=BLACK;

publicintchessState=NONE;

publicintchessX,chessY;

  BufferedImageBgImage=null,Black=null,White=null;

  publicintgetChessState(){

   returnchessState;

  }

  publicintsetChessState(){

   returnchessState;

  }

  publicChess(){

   this.setBorder(null);

   this.setContentAreaFilled(false);

   this.addMouseListener(newMouseAdapter(){

   

   publicvoidmouseClicked(MouseEvente){

   if(chessState==NONE){

   chessState=CurrentChess;

   CurrentChess=chessState==WHITE?

BLACK:

WHITE;

   Chess.this.repaint();

   playGame();

   }

   }

   

   publicvoidmouseEntered(MouseEvente){

   

   }

   

   publicvoidmouseExited(MouseEvente){

   

   }

   

   });

  }

  publicvoidplayGame(){

   if(GameManager.getXY(this)){

   System.out.print("youwin");

   intr=JOptionPane.showConfirmDialog(null,"获胜,是否继续","提示框",JOptionPane.YES_NO_OPTION);

   }

  }

publicvoidgetImage(BufferedImageImage){

BgImage=Image;

try{

Black=ImageIO.read(newFile("src/imgs/black.gif"));

}catch(IOExceptione){

e.printStackTrace();

}

try{

White=ImageIO.read(newFile("src/imgs/white.gif"));

}catch(IOExceptione){

e.printStackTrace();

}

}

publicvoidpaint(Graphicsg){

super.paint(g);

g.drawImage(BgImage,0,0,null);

switch(this.chessState)

{

caseNONE:

;break;

caseBLACK:

g.drawImage(Black,0,0,null);break;

caseWHITE:

g.drawImage(White,0,0,null);break;

}

}

}

 

--------------------------------------------------------GameManager类----------------------------------------------------------------

packageutil;

importui.Chess;

importui.ChessPanel;

importcommon.Constant;

publicclassGameManager{

 staticintX=0;

   staticintY=0;

 staticintnextChessState=0;

 staticChessnextChess=newChess();

 staticintnumHorizontal=1,numVertical=1,numUpperleftAndLowerright=1,numUpperrightAndLowerleft=1;

    publicstaticbooleangetXY(Chessch){

     booleanres=false;

     X=ch.chessX;

     Y=ch.chessY;

     returnjudge();

    }

    publicstaticbooleanjudge(){

     booleanres=false;

     judgeHorizontal();

     judgeVertical();

     judgeUpperleftAndLowerright();

     judgeUpperrightAndLowerleft();

     if(numHorizontal>=5||numVertical>=5||numUpperleftAndLowerright>=5||numUpperrightAndLowerleft>=5){

     res=true;

     }

     returnres;

    }

    publicstaticintjudgeHorizontal(){

     numHorizontal=1;

     for(inti=X,j=Y;i+1

     if(ChessPanel.Chess[i][j].chessState!

=ChessPanel.Chess[i][j+1].chessState)break;

     numHorizontal++;

     }

     for(inti=X,j=Y;j>0;j--){

     if(ChessPanel.Chess[i][j].chessState!

=ChessPanel.Chess[i][j-1].chessState)break;

     numHorizontal++;

     }

     returnnumHorizontal;

    }

    publicstaticintjudgeVertical(){

     numVertical=1;

     for(inti=X,j=Y;i+1

     if(ChessPanel.Chess[i][j].chessState!

=ChessPanel.Chess[i+1][j].chessState)break;

     numVertical++;

     }

     for(inti=X,j=Y;i-1>0;i--){

     if(ChessPanel.Chess[i][j].chessState!

=ChessPanel.Chess[i-1][j].chessState)break;

     numVertical++;

     }

     returnnumVertical;

    }

    publicstaticintjudgeUpperleftAndLowerright(){

     numUpperleftAndLowerright=1;

     for(inti =X,j=Y;i+1

     if(ChessPanel.Chess[i][j].chessState!

=Ches

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1