您好,登錄后才能下訂單哦!
這篇文章主要介紹如何用javafx實現(xiàn)五子棋游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
需求描述
一個五子棋游戲,能實現(xiàn)雙方黑白對決,當一方獲勝時給出提示信息,利用GUI界面實現(xiàn)
項目結構如下圖
一、實體
FiveChess類
package entity; import javafx.scene.control.Alert; public class FiveChess{ public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public double getCellLen() { return cellLen; } public void setCellLen(double cellLen) { this.cellLen = cellLen; } /** * 維度 */ private int n; private double width; private double height; private double cellLen; private char currentSide='B'; public char getFlag() { return flag; } private char flag=' '; private char[][] chess; public char[][] getChess() { return chess; } public void setChess(char[][] chess) { this.chess = chess; } public char getCurrentSide() { return currentSide; } public void setCurrentSide(char currentSide) { this.currentSide = currentSide; } //其他請補充 public FiveChess(double width,double height,double cellLen){ this.width=width; this.height=height; this.cellLen=cellLen; chess=new char[(int)height][(int)width]; for(int i=0;i<height;i++) for(int j=0;j<width;j++) chess[i][j]=' '; } public void play(int x,int y){ //將當前的棋子放置到(x,y) if(chess[x][y]==' '){ chess[x][y]=currentSide; if(!judgeGame(x,y,currentSide)){ //游戲結束彈出信息框 Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle("五子棋游戲"); alert.setHeaderText("提示信息"); alert.setContentText(currentSide+"方取得勝利!"); alert.showAndWait(); } changeSide(); } } public void changeSide(){ //更換下棋方 setCurrentSide(currentSide=='B'?'W':'B'); } public boolean judgeGame(int row, int col, char chessColor){ //判斷游戲是否結束 if(rowJudge(row,col,chessColor)&&colJudge(row,col,chessColor)&&mainDiagonalJudge(row,col,chessColor)&&DeputyDiagonalJudge(row,col,chessColor)) return true; return false; } public boolean rowJudge(int row,int col,char chessColor){ //判斷一行是否五子連線 int count=0; for(int j=col;j<width;j++){ if(chess[row][j]!=chessColor) break; count++; } for(int j=col-1;j>=0;j--){ if(chess[row][j]!=chessColor) break; count++; } if(count>=5) return false; return true; } public boolean colJudge(int row,int col,char chessColor){ //判斷一列是否五子連線 int count=0; for(int i=row;i<height;i++){ if(chess[i][col]!=chessColor) break; count++; } for(int i=row-1;i>=0;i--){ if(chess[i][col]!=chessColor) break; count++; } if(count>=5) return false; return true; } public boolean mainDiagonalJudge(int row,int col,char chessColor){ //判斷主對角線是否五子連線 int count=0; for(int i=row,j=col;i<height&&j<width;i++,j++){ if(chess[i][j]!=chessColor) break; count++; } for(int i=row-1,j=col-1;i>=0&&j>=0;i--,j--){ if(chess[i][j]!=chessColor) break; count++; } if(count>=5) return false; return true; } public boolean DeputyDiagonalJudge(int row,int col,char chessColor){ //判斷副對角線是否五子連線 int count=0; for(int i=row,j=col;i>=0&&j<width;i--,j++){ if(chess[i][j]!=chessColor) break; count++; } for(int i=row+1,j=col-1;i<height&&j>=0;i++,j--){ if(chess[i][j]!=chessColor) break; count++; } if(count>=5) return false; return true; } }
二、視圖
ChessPane類繼承Pane類實現(xiàn)棋盤和五子棋的繪制
package view; import entity.FiveChess; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; public class ChessPane extends Pane { public Canvas getCanvas() { return canvas; } public Canvas canvas; public GraphicsContext getGc() { return gc; } public GraphicsContext gc; public FiveChess getFiveChess() { return fiveChess; } public void setFiveChess(FiveChess fiveChess) { this.fiveChess = fiveChess; } public FiveChess fiveChess; public ChessPane(FiveChess fiveChess){ this.fiveChess=fiveChess; double cell=fiveChess.getCellLen(); drawPane(cell); drawChess(cell); getChildren().add(canvas); } public void drawPane(double cell){ canvas = new Canvas(800,700); gc = canvas.getGraphicsContext2D(); gc.clearRect(0,0,canvas.getWidth(),canvas.getHeight()); //繪制棋盤 gc.setStroke(Color.BLACK); for(int i=0;i<fiveChess.getWidth();i++) for(int j=0;j<fiveChess.getHeight();j++){ gc.strokeRect(100+i*cell,100+cell*j,cell,cell);//清理一個矩形取區(qū)域的內容 } } public void drawChess(double cell){ char[][] chess=fiveChess.getChess(); for(int i=0;i<fiveChess.getHeight();i++) for(int j=0;j<fiveChess.getWidth();j++){ if(chess[i][j]=='B'){ gc.setFill(Color.BLACK);//設置填充色 gc.fillOval(100+i*cell-cell/2,100+j*cell-cell/2,cell,cell); } else if(chess[i][j]=='W'){ gc.setFill(Color.WHITE); gc.fillOval(100+i*cell-cell/2,100+j*cell-cell/2,cell,cell);//填充橢圓 gc.strokeOval(100+i*cell-cell/2,100+j*cell-cell/2,cell,cell);//繪制輪廓 } } } }
三、控制器
playAction類繼承自事件處理器EventHandler并傳遞的參數(shù)是鼠標事件,表示接受鼠標點擊面板事件
package controller; import entity.FiveChess; import javafx.event.EventHandler; import javafx.scene.control.Alert; import javafx.scene.input.MouseEvent; import view.ChessPane; public class PlayAction implements EventHandler<MouseEvent> { /**fiveChess表示五子棋游戲模型*/ private FiveChess fiveChess; /**chessPane表示五子棋顯示面板*/ private ChessPane chessPane; public PlayAction(FiveChess fiveChess,ChessPane chessPane){ this.chessPane=chessPane; this.fiveChess = fiveChess; } @Override public void handle(MouseEvent event) { //處理鼠標點擊事件 double cell=fiveChess.getCellLen(); //event.getX()獲取鼠標點擊x坐標,返回double類型 double x=event.getX(); double y=event.getY(); int i=(int)((x-100+cell/2)/cell); int j=(int)((y-100+cell/2)/cell); System.out.println(i+" "+j); fiveChess.play(i,j); chessPane.drawChess(cell); if(!fiveChess.judgeGame(i,j,fiveChess.getCurrentSide()=='B'?'W':'B')){ Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle("五子棋游戲"); alert.setHeaderText("提示信息"); alert.setContentText((fiveChess.getCurrentSide()=='B'?"白":"黑")+"方取得勝利!"); alert.showAndWait(); } } }
四、測試
import controller.PlayAction; import entity.FiveChess; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.stage.Stage; import view.ChessPane; import javax.print.attribute.standard.Fidelity; public class Test extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { FiveChess fiveChess = new FiveChess(20,20,28.0); ChessPane chesspane=new ChessPane(fiveChess); chesspane.setOnMouseClicked(new PlayAction(fiveChess,chesspane));//事件源綁定處理器 Scene scene=new Scene(chesspane,800,700); primaryStage.setScene(scene); primaryStage.setTitle("五子棋游戲"); primaryStage.show(); } }
效果圖
以上是如何用javafx實現(xiàn)五子棋游戲的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。