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

一、實體
FiveChess類
- 提供五子棋實體包含的所有信息
- 判斷游戲是否結(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;
}
//其他請補充
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)){
//游戲結(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類實現(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ū)域的內(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ù)是鼠標事件,表示接受鼠標點擊面板事件
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();
}
}
效果圖

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springAI結(jié)合ollama簡單實現(xiàn)小結(jié)
本文主要介紹了springAI結(jié)合ollama簡單實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03
Java Socket設(shè)置timeout的幾種常用方式說明
這篇文章主要介紹了Java Socket設(shè)置timeout的幾種常用方式說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
全網(wǎng)最精細詳解二叉樹,2萬字帶你進入算法領(lǐng)域
大家好,我是哪吒,一個熱愛編碼的Java工程師,本著"欲速則不達,欲達則欲速"的學(xué)習(xí)態(tài)度,在程序猿這條不歸路上不斷成長,所謂成長,不過是用時間慢慢擦亮你的眼睛,少時看重的,年長后卻視若鴻毛,少時看輕的,年長后卻視若泰山,成長之路,亦是漸漸放下執(zhí)念,內(nèi)心歸于平靜的旅程2021-08-08
Spring Boot集成ShedLock分布式定時任務(wù)的實現(xiàn)示例
ShedLock確保您計劃的任務(wù)最多同時執(zhí)行一次。如果一個任務(wù)正在一個節(jié)點上執(zhí)行,則它會獲得一個鎖,該鎖將阻止從另一個節(jié)點(或線程)執(zhí)行同一任務(wù)。2021-05-05

