javafx實(shí)現(xiàn)五子棋游戲
需求描述
一個(gè)五子棋游戲,能實(shí)現(xiàn)雙方黑白對決,當(dāng)一方獲勝時(shí)給出提示信息,利用GUI界面實(shí)現(xiàn)
項(xiàng)目結(jié)構(gòu)如下圖

一、實(shí)體
FiveChess類
- 提供五子棋實(shí)體包含的所有信息
- 判斷游戲是否結(jié)束
- play方法改變chess[][]棋盤中的數(shù)據(jù)
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;
}
//其他請補(bǔ)充
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){
//將當(dāng)前的棋子放置到(x,y)
if(chess[x][y]==' '){
chess[x][y]=currentSide;
if(!judgeGame(x,y,currentSide)){
//游戲結(jié)束彈出信息框
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){
//判斷游戲是否結(jié)束
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類實(shí)現(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);//清理一個(gè)矩形取區(qū)域的內(nèi)容
}
}
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);//設(shè)置填充色
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ù)是鼠標(biāo)事件,表示接受鼠標(biāo)點(diǎn)擊面板事件
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) {
//處理鼠標(biāo)點(diǎn)擊事件
double cell=fiveChess.getCellLen();
//event.getX()獲取鼠標(biāo)點(diǎn)擊x坐標(biāo),返回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();
}
}
效果圖

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot應(yīng)用啟動(dòng)過程分析
這篇文章主要介紹了SpringBoot應(yīng)用啟動(dòng)過程分析,需要的朋友可以參考下2017-08-08
java中timer的schedule和scheduleAtFixedRate方法區(qū)別詳解
這篇文章主要為大家詳細(xì)介紹了java中timer的schedule和scheduleAtFixedRate方法區(qū)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
IO密集型任務(wù)設(shè)置線程池線程數(shù)實(shí)現(xiàn)方式
這篇文章主要介紹了IO密集型任務(wù)設(shè)置線程池線程數(shù)實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
SpringBoot+Redis Bitmap實(shí)現(xiàn)活躍用戶統(tǒng)計(jì)
Redis的Bitmap數(shù)據(jù)結(jié)構(gòu)是一種緊湊的位圖,它可以用于實(shí)現(xiàn)各種場景,其中統(tǒng)計(jì)活躍用戶是一種經(jīng)典的業(yè)務(wù)場景,下面我們就來學(xué)習(xí)一下SpringBoot如何利用Redis中的Bitmap實(shí)現(xiàn)活躍用戶統(tǒng)計(jì)吧2023-11-11
Java兩個(gè)變量的互換(不借助第3個(gè)變量)具體實(shí)現(xiàn)方法
這篇文章主要介紹了Java兩個(gè)變量的互換(不借助第3個(gè)變量)具體實(shí)現(xiàn)方法,需要的朋友可以參考下2014-02-02
java中BigDecimal進(jìn)行加減乘除的基本用法
大家應(yīng)該對于不需要任何準(zhǔn)確計(jì)算精度的數(shù)字可以直接使用float或double運(yùn)算,但是如果需要精確計(jì)算的結(jié)果,則必須使用BigDecimal類,而且使用BigDecimal類也可以進(jìn)行大數(shù)的操作。下面這篇文章就給大家介紹介紹關(guān)于java中BigDecimal進(jìn)行加減乘除的基本用法。2016-12-12
Java?8?Stream?處理數(shù)據(jù)方法匯總
這篇文章主要介紹了Java?8?Stream處理數(shù)據(jù),Stream是Java?8?新引入的一個(gè)包它讓我們能用聲明式的方式處理數(shù)據(jù),Stream流式處理相較于傳統(tǒng)方法簡潔高效,也便于進(jìn)行并發(fā)編程,更多相關(guān)內(nèi)容需要的小伙伴可以參考下面文章內(nèi)容2022-06-06
SpringBoot整合log4j日志與HashMap的底層原理解析
這篇文章主要介紹了SpringBoot整合log4j日志與HashMap的底層原理,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01

