java模拟国际象棋游戏_Javafx实现国际象棋游戏

本文展示了如何利用JavaFX实现一个国际象棋游戏,包括棋子的基本规则、移动策略,如马的“日”字走法、兵的特殊移动等。此外,文章还涵盖了游戏结束提示、背景音乐等功能的实现,并提供了控制器、事件处理和棋盘绘制的代码示例。
摘要由CSDN通过智能技术生成

本文实例为大家分享了Javafx实现国际象棋游戏的具体代码,供大家参考,具体内容如下

基本规则

棋子马设计“日”的移动方式

兵设计只能向前直走,每次只能走一格。但走第一步时,可以走一格或两格的移动方式

请为后设计横、直、斜都可以走,步数不受限制,但不能越子的移动方式。

车只能横向或者竖向行走

国王是在以自己为中心的九宫格内行走

骑士只能走对角线

项目目录结构

29adc3f179b8b78ff4a7bff623ddde5a.png

UML类图关系

以骑士为例

1fe1d7832be9d4976bfdff639377b21c.png

实现基本功能

吃子

不能越子

游戏结束提示

基本移动策略

背景音乐

效果

95ec32b34bfc09243416d8d2b68e0d16.png

控制器

PressedAction

package com.Exercise3;

import com.Exercise3.Controller.PressedAction;

import com.Exercise3.Controller.ReleaseAction;

import com.Exercise3.Controller.ResetAction;

import com.Exercise3.view.ChessBoard;

import com.Exercise3.view.ChessPane;

import javafx.application.Application;

import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.layout.BorderPane;

import javafx.scene.layout.HBox;

import javafx.scene.media.Media;

import javafx.scene.media.MediaPlayer;

import javafx.stage.Stage;

public class Test extends Application {

public static void main(String[] args) {

launch(args);

}

@Override

public void start(Stage primaryStage) {

String MEDIA_URL = "file:/E:/IdeaProjects/Experiment/src/com/Exercise3/music/BackgroundMusic.mp3";

ChessBoard chessBoard = ChessBoard.getInstance(100,40,40);

//添加媒体资源

Media media = new Media(MEDIA_URL);

MediaPlayer mediaPlayer = new MediaPlayer(media);

mediaPlayer.setAutoPlay(true);

mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);

mediaPlayer.play();

ChessPane pane = new ChessPane(chessBoard);

pane.setOnMousePressed(new PressedAction(pane,mediaPlayer));

pane.setOnMouseReleased(new ReleaseAction(pane));

BorderPane borderPane = new BorderPane();

borderPane.setCenter(pane);

HBox hBox = new HBox();

hBox.setAlignment(Pos.TOP_CENTER);

Button button = new Button("悔棋");

button.setOnAction(new ResetAction(pane));

hBox.getChildren().add(button);

borderPane.setBottom(hBox);

Scene scene = new Scene(borderPane,900,900);

primaryStage.setScene(scene);

primaryStage.setTitle("国际象棋");

primaryStage.show();

}

}

ReleasedAction

package com.Exercise3.Controller;

import com.Exercise3.entity.Piece.ChessPiece;

import com.Exercise3.entity.PieceType;

import com.Exercise3.view.ChessBoard;

import com.Exercise3.view.ChessPane;

import javafx.event.EventHandler;

import javafx.scene.control.Alert;

import javafx.scene.input.MouseEvent;

import java.util.Stack;

public class ReleaseAction implements EventHandler {

private ChessPane chessPane;

static Stack stack = new Stack<>();

public ReleaseAction(ChessPane chessPane) {

this.chessPane = chessPane;

}

@Override

public void handle(MouseEvent e) {

chessPane.drawBoard();

ChessBoard chessBoard = chessPane.getChessBoard();

int x = (int) ((e.getX() - chessBoard.getStartX()) / (chessBoard.getCellLength()));

int y = (int) ((e.getY() - chessBoard.getStartY()) / (chessBoard.getCellLength()));

for (ChessPiece o : chessPane.getChessPieces()) {

if (o.isSelected()) {

System.out.println(o.isSelected()+" "+o.getRow()+" "+o.getCol());

if (chessBoard.getCurrSide()==o.getSide()){

if(o.getMoveStrategy().move(x, y,chessPane.getChessPieces())){

o.setSelected(false);

if(judgeGame(x,y)){

printTip(o.getSide());

}

eatPiece(x,y);

stack.push((ChessPiece) o.clone());

o.setCol(x);

o.setRow(y);

chessBoard.changeSide();

}

}

break;

}

}

chessPane.drawPiece();

}

public void eatPiece(int x,int y){

chessPane.getChessPieces().removeIf(e->{

if(e.getCol()==x&&e.getRow()==y){

stack.push(e);

return true;

}

return false;

});

}

public boolean judgeGame(int x,int y){

for(ChessPiece e:chessPane.getChessPieces()){

if(e.getCol()==x&&e.getRow()==y&&(

e.getType()== PieceType.KINGBLACK||e.getType()==PieceType.KINGWHITE))

return true;

}

return false;

}

public void printTip(char side){

Alert alert = new Alert(Alert.AlertType.INFORMATION);

alert.setContentText((side=='B'?"黑":"白")+"方取得胜利");

alert.setTitle("游戏结束");

alert.showAndWait();

}

}

ResetAction

package com.Exercise3.Controller;

import com.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值