五子棋双人对战Word文档下载推荐.docx

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

五子棋双人对战Word文档下载推荐.docx

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

五子棋双人对战Word文档下载推荐.docx

publicstaticfinalintCOLS=15;

publicstaticfinalintSIZE=35;

}

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

packageui;

publicclassMain{

publicstaticvoidmain(String[]args){

ChessBoardChessBoard=newChessBoard();

ChessPanelChessPanel=newChessPanel();

ChessPanel.init();

ChessBoard.setContentPane(ChessPanel);

ChessBoard.setVisible(true);

}

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

importjavax.swing.JFrame;

importcommon.Constant;

publicclassChessBoardextendsJFrame{

publicChessBoard(){

 

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

this.setLocationRelativeTo(null);

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

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<

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;

Chess[i][j]=ChessButton;

this.add(ChessButton);

else{

src/imgs/top.png"

;

Chess[i][j]=ChessButton;

}

elseif(i==14){

src/imgs/leftBottom.png"

ChessButton.chessX=i;

ChessButton.chessY=j;

src/imgs/rightBottom.png"

src/imgs/bottom.png"

else{

src/imgs/left.png"

src/imgs/right.png"

src/imgs/chessPosition.png"

}

}

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

importjava.awt.Graphics;

importjava.awt.event.MouseAdapter;

importjava.awt.event.MouseEvent;

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(){

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();

White=ImageIO.read(newFile("

src/imgs/white.gif"

publicvoidpaint(Graphicsg){

super.paint(g);

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

switch(this.chessState)

{

caseNONE:

break;

caseBLACK:

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

caseWHITE:

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

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

packageutil;

importui.Chess;

importui.ChessPanel;

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(){

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<

Constant.COLS;

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

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

numHorizontal++;

j>

0;

j--){

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

returnnumHorizontal;

publicstaticintjudgeVertical(){

numVertical=1;

for(inti=X,j=Y;

Constant.ROWS;

i++){

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

numVertical++;

i-1>

i--){

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

returnnumVertical;

publicstaticintjudgeUpperleftAndLowerright(){

numUpperleftAndLowerright=1;

for(inti 

=X,j=Y;

i+1<

Constant.ROWS&

&

j+1<

Constant.COLS;

i++,j++){

=Ches

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

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

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

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