JAVA高手接招—————五子棋双人对弈程序

新建一个chess类,其中代码如下

packagework;

importjava.awt.Color;

publicclassChess{

publicstaticfinalintdiamter=30;

privateintx;//在网格中的索引,0~15

privateinty;//在网格中的索引,0~15

privateColorcolor;

publicColorgetColor(){

returncolor;

}

publicvoidsetColor(Colorcolor){

this.color=color;

}

publicintgetX(){

returnx;

}

publicvoidsetX(intx){

this.x=x;

}

publicintgetY(){

returny;

}

publicvoidsetY(inty){

this.y=y;

}

publicChess(intx,inty,Colorcolor){

super();

this.x=x;

this.y=y;

this.color=color;

}

}

然后在同一个包中新建FirstFrame类。代码如下

packagework;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjavax.swing.JFrame;

importjavax.swing.JMenu;

importjavax.swing.JMenuBar;

importjavax.swing.JMenuItem;

//主函数

publicclassFirstFrame{

publicstaticvoidmain(String[]args){

FirstFrame1f=newFirstFrame1();

f.setVisible(true);

}

}

//窗体函数

classFirstFrame1extendsJFrameimplementsActionListener{

JMenuBarmenuBar;

JMenusysMenu;

JMenuItemstartMenuItem;

JMenuItembackMenuItem;

JMenuItemexitMenuItem;

DrawPanelmyPanel=newDrawPanel();

publicFirstFrame1(){

super(娃哈哈);

add(myPanel);

menuBar=newJMenuBar();

setJMenuBar(menuBar);

sysMenu=newJMenu(系统(s));

sysMenu.setMnemonic('s');

menuBar.add(sysMenu);

startMenuItem=newJMenuItem(开始);

backMenuItem=newJMenuItem(悔棋);

exitMenuItem=newJMenuItem(退出);

sysMenu.add(startMenuItem);

sysMenu.add(backMenuItem);

sysMenu.addSeparator();

sysMenu.add(exitMenuItem);

startMenuItem.addActionListener(this);

backMenuItem.addActionListener(this);

exitMenuItem.addActionListener(this);

super.setSize(600,650);

super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

publicvoidactionPerformed(ActionEvente){

Objectobj=e.getSource();

if(obj==startMenuItem){

myPanel.start();//DrawPanelp=newDrawPanel();是错的;

}elseif(obj==backMenuItem){

myPanel.back();

}else{

System.exit(0);

}

}

}

最后新建一个DrawPanel类代码如下

packagework;

importjava.awt.Color;

importjava.awt.Cursor;

importjava.awt.Graphics;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.awt.event.MouseEvent;

importjava.awt.event.MouseListener;

importjava.awt.event.MouseMotionListener;

importjavax.swing.JFrame;

importjavax.swing.JMenu;

importjavax.swing.JMenuBar;

importjavax.swing.JMenuItem;

importjavax.swing.JOptionPane;

importjavax.swing.JPanel;

importsun.awt.geom.AreaOp.AddOp;

//画柄;

classDrawPanelextendsJPanelimplementsMouseListener,MouseMotionListener{

finalintMARGIN_TOP=60;

finalintMARGIN_LEFT=30;

finalintCELL_WIDTH=35;

finalintCOLS=15;

finalintROWS=15;

Chess[]chessList=newChess[(COLS+1)*(ROWS+1)];

intchessCount=0;

booleanisBlack=true;

booleangameOver=false;

publicDrawPanel(){//构造函数

Colorcolor=newColor(200,250,200);

setBackground(color);

this.addMouseMotionListener(this);

this.addMouseListener(this);

}

publicvoidstart(){

for(inti=0;i<chessList.length;i++)

chessList[i]=null;

isBlack=true;

gameOver=false;

chessCount=0;

repaint();

}

publicvoidback(){

if(chessCount==0||gameOver==true)

return;

chessList[chessCount-1]=null;

chessCount--;

isBlack=!isBlack;

repaint();

}

@Override

protectedvoidpaintComponent(Graphicsg){//方法重写

//TODOAuto-generatedmethodstub

super.paintComponent(g);

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

g.drawLine(MARGIN_LEFT,MARGIN_TOP+i*CELL_WIDTH,MARGIN_LEFT+COLS*CELL_WIDTH,MARGIN_TOP+i*CELL_WIDTH);

}

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

g.drawLine(MARGIN_LEFT+i*CELL_WIDTH,MARGIN_TOP,MARGIN_LEFT+i*CELL_WIDTH,MARGIN_TOP+ROWS*CELL_WIDTH);

}

for(inti=0;i<chessCount;i++){//绘制棋子数组

Chesschess=chessList[i];

intxPos=MARGIN_LEFT+CELL_WIDTH*chess.getX()-Chess.diamter/2;

intyPos=MARGIN_TOP+CELL_WIDTH*chess.getY()-Chess.diamter/2;

g.setColor(chess.getColor());

g.fillOval(xPos,yPos,Chess.diamter,Chess.diamter);

if(i==chessCount-1){

g.setColor(Color.red);

g.drawRect(xPos,yPos,Chess.diamter,Chess.diamter);

}

}

if(isBlack)

g.setColor(Color.black);

else

g.setColor(Color.white);

g.fillOval(8*CELL_WIDTH,CELL_WIDTH/2,Chess.diamter,Chess.diamter);

StringcolorStr=isBlack==false?白方下:黑方下;

g.setColor(Color.blue);

g.drawString(colorStr+第+chessCount/2+步,9*CELL_WIDTH,CELL_WIDTH);

}

publicvoidmouseClicked(MouseEvente){}

publicvoidmouseEntered(MouseEvente){}

publicvoidmouseExited(MouseEvente){}

publicvoidmouseReleased(MouseEvente){}

publicvoidmouseDragged(MouseEvente){}

publicvoidmouseMoved(MouseEvente){

//TODOAuto-generatedmethodstub

super.setCursor(newCursor(Cursor.HAND_CURSOR));

intx=(e.getX()-MARGIN_LEFT+Chess.diamter/2)/CELL_WIDTH;

inty=(e.getY()-MARGIN_TOP+Chess.diamter/2)/CELL_WIDTH;

if(xCOLS||y>ROWS)//超出棋盘边界

super.setCursor(newCursor(Cursor.DEFAULT_CURSOR));

if(findChess(x,y))//下过的地方不能再下

super.setCursor(newCursor(Cursor.DEFAULT_CURSOR));

if(gameOver)

super.setCursor(newCursor(Cursor.DEFAULT_CURSOR));

}

publicvoidmousePressed(MouseEvente){

intx=(e.getX()-MARGIN_LEFT+Chess.diamter/2)/CELL_WIDTH;

inty=(e.getY()-MARGIN_TOP+Chess.diamter/2)/CELL_WIDTH;

if(xCOLS||y>ROWS)//超出棋盘边界

return;

Colorcolor=Color.black;

if(!isBlack)//变颜色

color=Color.white;

if(findChess(x,y))//下过的地方不能再下

return;

if(gameOver)

return;

chessList[chessCount]=newChess(x,y,color);

repaint();//重绘

isBlack=!isBlack;

chessCount++;

StringcolorStr=color==Color.white?白方:黑方;

int[]count={0,0,0,0};

for(inti=x-1;i>=0;i--){

if(findChess(i,y,color)){

count[0]++;

}

else

break;

}

for(inti=x+1;i<=COLS;i++){

if(findChess(i,y,color)){

count[0]++;

}

else

break;

}

for(inti=y-1;i>=0;i--){

if(findChess(x,i,color)){

count[1]++;

}

else

break;

}

for(inti=y+1;i<=ROWS;i++){

if(findChess(x,i,color)){

count[2]++;

}

else

break;

}

for(inti=x+1,j=y+1;i<=COLS&&j<=ROWS;i++,j++){

if(findChess(i,j,color)){

count[3]++;

}

else

break;

}

for(inti=x-1,j=y-1;i>=0&&j>=0;i--,j--){

if(findChess(i,j,color)){

count[3]++;

}

else

break;

}

for(inti=x+1,j=y-1;i=0;i++,j--){

if(findChess(i,j,color)){

count[3]++;

}

else

break;

}

for(inti=x-1,j=y+1;i>=0&&j<=ROWS;i--,j++){

if(findChess(i,j,color)){

count[3]++;

}

else

break;

}

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

if(count[i]>=4){

gameOver=true;

JOptionPane.showMessageDialog(this,恭喜+colorStr+胜利);

return;

}

}

System.out.println(e.getX()+,+e.getY());

}

publicbooleanfindChess(intx,inty){

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

if(chessList[i].getX()==x&&chessList[i].getY()==y)

returntrue;

}

returnfalse;

}

publicbooleanfindChess(intx,inty,Colorcolor){

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

if(chessList[i].getX()==x&&chessList[i].getY()==y&&chessList[i].getColor()==color)

returntrue;

}

returnfalse;

}

}

如果不行就hi我

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