本文實例講述了java實現的五子棋游戲代碼,分享給大家供大家參考,具體代碼如下
一、實踐目標
1.掌握JavaGUI界面設計
2.掌握鼠標事件的監聽(MouseListener,MouseMotionListener)
二、實踐內容
設計一個簡單的五子棋程序,能夠實現五子棋下棋過程。如下圖所示
1.五子棋棋盤類
package cn.edu.ouc.fiveChess; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RadialGradientPaint; import java.awt.RenderingHints; import java.awt.Toolkit; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.geom.Ellipse2D; import javax.swing.*; /** * 五子棋--棋盤類 */ public class ChessBoard extends JPanel implements MouseListener { public static final int MARGIN=30;//邊距 public static final int GRID_SPAN=35;//網格間距 public static final int ROWS=15;//棋盤行數 public static final int COLS=15;//棋盤列數 Point[] chessList=new Point[(ROWS+1)*(COLS+1)];//初始每個數組元素為null boolean isBlack=true;//默認開始是黑棋先 boolean gameOver=false;//游戲是否結束 int chessCount;//當前棋盤棋子的個數 int xIndex,yIndex;//當前剛下棋子的索引 Image img; Image shadows; Color colortemp; public ChessBoard(){ // setBackground(Color.blue);//設置背景色為橘黃色 img=Toolkit.getDefaultToolkit().getImage("board.jpg"); shadows=Toolkit.getDefaultToolkit().getImage("shadows.jpg"); addMouseListener(this); addMouseMotionListener(new MouseMotionListener(){ public void mouseDragged(MouseEvent e){ } public void mouseMoved(MouseEvent e){ int x1=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN; //將鼠標點擊的坐標位置轉成網格索引 int y1=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN; //游戲已經結束不能下 //落在棋盤外不能下 //x,y位置已經有棋子存在,不能下 if(x1<0||x1>ROWS||y1<0||y1>COLS||gameOver||findChess(x1,y1)) setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); //設置成默認狀態 else setCursor(new Cursor(Cursor.HAND_CURSOR)); } }); } //繪制 public void paintComponent(Graphics g){ super.paintComponent(g);//畫棋盤 int imgWidth= img.getWidth(this); int imgHeight=img.getHeight(this);//獲得圖片的寬度與高度 int FWidth=getWidth(); int FHeight=getHeight();//獲得窗口的寬度與高度 int x=(FWidth-imgWidth)/2; int y=(FHeight-imgHeight)/2; g.drawImage(img, x, y, null); for(int i=0;i<=ROWS;i++){//畫橫線 g.drawLine(MARGIN, MARGIN+i*GRID_SPAN, MARGIN+COLS*GRID_SPAN, MARGIN+i*GRID_SPAN); } for(int i=0;i<=COLS;i++){//畫豎線 g.drawLine(MARGIN+i*GRID_SPAN, MARGIN, MARGIN+i*GRID_SPAN, MARGIN+ROWS*GRID_SPAN); } //畫棋子 for(int i=0;i<chessCount;i++){ //網格交叉點x,y坐標 int xPos=chessList[i].getX()*GRID_SPAN+MARGIN; int yPos=chessList[i].getY()*GRID_SPAN+MARGIN; g.setColor(chessList[i].getColor());//設置顏色 // g.fillOval(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2, //Point.DIAMETER, Point.DIAMETER); //g.drawImage(shadows, xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2, Point.DIAMETER, Point.DIAMETER, null); colortemp=chessList[i].getColor(); if(colortemp==Color.black){ RadialGradientPaint paint = new RadialGradientPaint(xPos-Point.DIAMETER/2+25, yPos-Point.DIAMETER/2+10, 20, new float[]{0f, 1f} , new Color[]{Color.WHITE, Color.BLACK}); ((Graphics2D) g).setPaint(paint); ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT); } else if(colortemp==Color.white){ RadialGradientPaint paint = new RadialGradientPaint(xPos-Point.DIAMETER/2+25, yPos-Point.DIAMETER/2+10, 70, new float[]{0f, 1f} , new Color[]{Color.WHITE, Color.BLACK}); ((Graphics2D) g).setPaint(paint); ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT); } Ellipse2D e = new Ellipse2D.Float(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2, 34, 35); ((Graphics2D) g).fill(e); //標記最后一個棋子的紅矩形框 if(i==chessCount-1){//如果是最后一個棋子 g.setColor(Color.red); g.drawRect(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2, 34, 35); } } } public void mousePressed(MouseEvent e){//鼠標在組件上按下時調用 //游戲結束時,不再能下 if(gameOver) return; String colorName=isBlack?"黑棋":"白棋"; //將鼠標點擊的坐標位置轉換成網格索引 xIndex=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN; yIndex=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN; //落在棋盤外不能下 if(xIndex<0||xIndex>ROWS||yIndex<0||yIndex>COLS) return; //如果x,y位置已經有棋子存在,不能下 if(findChess(xIndex,yIndex))return; //可以進行時的處理 Point ch=new Point(xIndex,yIndex,isBlack?Color.black:Color.white); chessList[chessCount++]=ch; repaint();//通知系統重新繪制 //如果勝出則給出提示信息,不能繼續下棋 if(isWin()){ String msg=String.format("恭喜,%s贏了!", colorName); JOptionPane.showMessageDialog(this, msg); gameOver=true; } isBlack=!isBlack; } //覆蓋mouseListener的方法 public void mouseClicked(MouseEvent e){ //鼠標按鍵在組件上單擊時調用 } public void mouseEntered(MouseEvent e){ //鼠標進入到組件上時調用 } public void mouseExited(MouseEvent e){ //鼠標離開組件時調用 } public void mouseReleased(MouseEvent e){ //鼠標按鈕在組件上釋放時調用 } //在棋子數組中查找是否有索引為x,y的棋子存在 private boolean findChess(int x,int y){ for(Point c:chessList){ if(c!=null&&c.getX()==x&&c.getY()==y) return true; } return false; } private boolean isWin(){ int continueCount=1;//連續棋子的個數 //橫向向西尋找 for(int x=xIndex-1;x>=0;x--){ Color c=isBlack?Color.black:Color.white; if(getChess(x,yIndex,c)!=null){ continueCount++; }else break; } //橫向向東尋找 for(int x=xIndex+1;x<=COLS;x++){ Color c=isBlack?Color.black:Color.white; if(getChess(x,yIndex,c)!=null){ continueCount++; }else break; } if(continueCount>=5){ return true; }else continueCount=1; //繼續另一種搜索縱向 //向上搜索 for(int y=yIndex-1;y>=0;y--){ Color c=isBlack?Color.black:Color.white; if(getChess(xIndex,y,c)!=null){ continueCount++; }else break; } //縱向向下尋找 for(int y=yIndex+1;y<=ROWS;y++){ Color c=isBlack?Color.black:Color.white; if(getChess(xIndex,y,c)!=null) continueCount++; else break; } if(continueCount>=5) return true; else continueCount=1; //繼續另一種情況的搜索:斜向 //東北尋找 for(int x=xIndex+1,y=yIndex-1;y>=0&&x<=COLS;x++,y--){ Color c=isBlack?Color.black:Color.white; if(getChess(x,y,c)!=null){ continueCount++; } else break; } //西南尋找 for(int x=xIndex-1,y=yIndex+1;x>=0&&y<=ROWS;x--,y++){ Color c=isBlack?Color.black:Color.white; if(getChess(x,y,c)!=null){ continueCount++; } else break; } if(continueCount>=5) return true; else continueCount=1; //繼續另一種情況的搜索:斜向 //西北尋找 for(int x=xIndex-1,y=yIndex-1;x>=0&&y>=0;x--,y--){ Color c=isBlack?Color.black:Color.white; if(getChess(x,y,c)!=null) continueCount++; else break; } //東南尋找 for(int x=xIndex+1,y=yIndex+1;x<=COLS&&y<=ROWS;x++,y++){ Color c=isBlack?Color.black:Color.white; if(getChess(x,y,c)!=null) continueCount++; else break; } if(continueCount>=5) return true; else continueCount=1; return false; } private Point getChess(int xIndex,int yIndex,Color color){ for(Point p:chessList){ if(p!=null&&p.getX()==xIndex&&p.getY()==yIndex &&p.getColor()==color) return p; } return null; } public void restartGame(){ //清除棋子 for(int i=0;i<chessList.length;i++){ chessList[i]=null; } //恢復游戲相關的變量值 isBlack=true; gameOver=false; //游戲是否結束 chessCount =0; //當前棋盤棋子個數 repaint(); } //悔棋 public void goback(){ if(chessCount==0) return ; chessList[chessCount-1]=null; chessCount--; if(chessCount>0){ xIndex=chessList[chessCount-1].getX(); yIndex=chessList[chessCount-1].getY(); } isBlack=!isBlack; repaint(); } //矩形Dimension public Dimension getPreferredSize(){ return new Dimension(MARGIN*2+GRID_SPAN*COLS,MARGIN*2 +GRID_SPAN*ROWS); } } 2.棋子類
package cn.edu.ouc.fiveChess; import java.awt.Color; /** * 棋子類 */ public class Point { private int x;//棋盤中的x索引 private int y;//棋盤中的y索引 private Color color;//顏色 public static final int DIAMETER=30;//直徑 public Point(int x,int y,Color color){ this.x=x; this.y=y; this.color=color; } public int getX(){//拿到棋盤中x的索引 return x; } public int getY(){ return y; } public Color getColor(){//獲得棋子的顏色 return color; } } 3.五子棋主框架類
package cn.edu.ouc.fiveChess; import java.awt.event.*; import java.awt.*; import javax.swing.*; /* 五子棋主框架 主站蜘蛛池模板: 望都县| 泸西县| 卫辉市| 含山县| 定边县| 峨眉山市| 洞头县| 黔江区| 斗六市| 会理县| 眉山市| 囊谦县| 德惠市| 昌江| 扎赉特旗| 永和县| 财经| 台湾省| 博野县| 洪湖市| 马山县| 清水河县| 吉木萨尔县| 延边| 襄樊市| 正阳县| 庄浪县| 吴桥县| 新乐市| 昌吉市| 泗水县| 城步| 兴文县| 谷城县| 蓝山县| 昔阳县| 原平市| 原平市| 东城区| 焉耆| 永清县|