本文实例为大家分享了Javafx实现国际象棋游戏的具体代码,供大家参考,具体内容如下
基本规则
棋子马设计“日”的移动方式
兵设计只能向前直走,每次只能走一格。但走第一步时,可以走一格或两格的移动方式
请为后设计横、直、斜都可以走,步数不受限制,但不能越子的移动方式。
车只能横向或者竖向行走
国王是在以自己为中心的九宫格内行走
骑士只能走对角线
项目目录结构
UML类图关系
以骑士为例
实现基本功能
吃子
不能越子
游戏结束提示
基本移动策略
背景音乐
效果
控制器
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.