五子棋双人对战Word文档下载推荐.docx
《五子棋双人对战Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《五子棋双人对战Word文档下载推荐.docx(14页珍藏版)》请在冰豆网上搜索。
![五子棋双人对战Word文档下载推荐.docx](https://file1.bdocx.com/fileroot1/2022-12/7/893ae55d-865f-4430-9b99-eead000d4a43/893ae55d-865f-4430-9b99-eead000d4a431.gif)
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