Javafx 实现国际象棋游戏

基本规则
  • 棋子马设计“日”的移动方式
  • 兵设计只能向前直走,每次只能走一格。但走第一步时,可以走一格或两格的移动方式
  • 请为后设计横、直、斜都可以走,步数不受限制,但不能越子的移动方式。
  • 车只能横向或者竖向行走
  • 国王是在以自己为中心的九宫格内行走
  • 骑士只能走对角线
项目目录结构

在这里插入图片描述
获取完整项目

  • 方法一: https://github.com/441712875al/InternationalChess-Game
  • 方法二:下载资源 链接:https://pan.baidu.com/s/1br3FqOT-6zvbVT64VYuqvQ
    提取码:d7mb
UML类图关系

以骑士为例

在这里插入图片描述

实现基本功能
  • 吃子
  • 不能越子
  • 游戏结束提示
  • 基本移动策略
  • 背景音乐
  • 悔棋

效果

在这里插入图片描述

Controller

  1. PressedAction
    这个控制类实现当鼠标点击棋子时使棋子踩的棋盘高亮显示
  2. ReleaseAction
    当鼠标释放时完成的动作
    • 是否可以吃子
    • 游戏结束否,结束则给出提示信息
  3. ResetAction
    记录棋子的信息,将其压入堆栈,悔棋时再弹出来
package com.Exercise3.Controller;

import com.Exercise3.view.ChessBoard;
import com.Exercise3.view.ChessPane;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javafx.scene.media.MediaPlayer;


public class PressedAction implements EventHandler<MouseEvent> {
    private ChessPane chessPane;
    private MediaPlayer mediaPlayer;


    public PressedAction(ChessPane chessPane,MediaPlayer mediaPlayer) {
        this.chessPane = chessPane;
        this.mediaPlayer = mediaPlayer;
    }

    @Override
    public void handle(MouseEvent e) {
        ChessBoard chessBoard = chessPane.getChessBoard();
        int x=(int)((e.getX()-chessBoard.getStartX())/(chessBoard.getCellLength()));
        int y=(int)((e.getY()-chessBoard.getStartY())/(chessBoard.getCellLength()));


        chessPane.getChessPieces().forEach(o->{
            if(o.getCol()==x&&o.getRow()==y){
                o.setSelected(true);
                chessPane.drawPiece();
            }
        });
        mediaPlayer.play();
    }

}



/* --------------------------------------------------------------*/
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<MouseEvent> {
    private ChessPane chessPane;
    static Stack<ChessPiece> 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())){
                        if(judgeGame(x,y)){
                            printTip(o.getSide());
                        }
                        eatPiece(x,y);
                        stack.push((ChessPiece) o.clone());
                        o.setCol(x);
                        o.setRow(y);

                        chessBoard.changeSide();
                    }

                }
                o.setSelected(false);
                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();
    }


}


/*-----------------------------------------------------------*/
package com.Exercise3.Controller;

        import com.Exercise3.entity.Piece.ChessPiece;
        import com.Exercise3.view.ChessPane;
        import javafx.event.ActionEvent;
        import javafx.event.EventHandler;


        import java.util.Stack;

public class ResetAction  implements EventHandler<ActionEvent>{
    private ChessPane chessPane;
    public ResetAction(ChessPane chessPane) {
        this.chessPane = chessPane;
    }

    @Override
    public void handle(ActionEvent e) {
        Stack<ChessPiece> stack = ReleaseAction.stack;
        if(!stack.empty()){
            chessPane.getChessPieces().removeIf(o->o.equals(stack.peek()));//去除原来的棋子
            chessPane.getChessPieces().add(stack.pop());//将以前压入堆栈的棋子重新加入


            chessPane.drawBoard();
            chessPane.drawPiece();
        }
    }
}

entity

Piece

每个棋子都继承自ChessPiece,都有一个移动策略类MoveStrategy类,该类来完成棋子移动的动作

  • id 描述类棋子的编号
  • Type 棋子类型
  • selected 棋子是否被选中
  • side 棋子是黑方还是白方
  • col row 棋子在棋盘的位置

每个棋子对象都包含了该棋子的信息

package com.Exercise3.entity.Piece;

import com.Exercise3.entity.PieceType;
import com.Exercise3.entity.Strategy.CarStrategy;

public class Car extends ChessPiece {
    public Car(PieceType type, int row, int col) {
        super(type, row, col);
        setMoveStrategy(new CarStrategy(getCol(),getRow()));
    }
}


/*-------------------------------------------*/
package com.Exercise3.entity.Piece;

import com.Exercise3.entity.Strategy.MoveStrategy;
import com.Exercise3.entity.PieceType;

import java.util.Objects;

public   class ChessPiece implements Cloneable{
    private int id;
    private PieceType type;
    private MoveStrategy moveStrategy;
    private boolean selected;
    private int row;
    private int col;
    private char side;
    private static int auto_id = 0;


    public ChessPiece(PieceType type,int row,int col) {
        this.type = type;
        this.row = row;
        this.col = col;
        selected = false;
        side = type.getDesc().endsWith("Black")?'B':'W';
        this.id = ++auto_id;
    }

    public Object clone(){
        Object obj = null;
        try{
            obj = super.clone();
        }catch (Exception e){
            e.printStackTrace();
        }

        return obj;
    }
    public PieceType getType() {
        return type;
    }

    public void setMoveStrategy(MoveStrategy moveStrategy){
        this.moveStrategy = moveStrategy;
    }


    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getCol() {
        return col;
    }

    public void setCol(int col) {
        this.col = col;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public MoveStrategy getMoveStrategy() {
        return moveStrategy;
    }

    public char getSide() {
        return side;
    }

    public void setSide(char side) {
        this.side = side;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ChessPiece that = (ChessPiece) o;
        return id == that.id;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public String toString() {
        return "ChessPiece{" +
                "id=" + id +
                ", type=" + type +
                ", row=" + row +
                ", col=" + col +
                ", side=" + side +
                '}';
    }
}


/*---------------------------------------------*/
package com.Exercise3.entity.Piece;

import com.Exercise3.entity.PieceType;
import com.Exercise3.entity.Strategy.HorseStategy;

public class Horse extends ChessPiece{
    public Horse(PieceType type, int row, int col) {
        super(type, row, col);
        setMoveStrategy(new HorseStategy(getCol(),getRow()));
    }
}




/*---------------------------------------------------*/
package com.Exercise3.entity.Piece;

import com.Exercise3.entity.PieceType;
import com.Exercise3.entity.Strategy.KingStrategy;

public class King extends ChessPiece {
    public King(PieceType type, int row, int col) {
        super(type, row, col);
        setMoveStrategy(new KingStrategy(getCol(),getRow()));
    }
}


/*--------------------------------------------*/
package com.Exercise3.entity.Piece;

import com.Exercise3.entity.PieceType;
import com.Exercise3.entity.Strategy.KnightStrategy;

public class Knight extends ChessPiece {
    public Knight(PieceType type, int row, int col) {
        super(type, row, col);
        setMoveStrategy(new KnightStrategy(getCol(),getRow()));
    }
}


/*-------------------------------------------*/
package com.Exercise3.entity.Piece;

import com.Exercise3.entity.PieceType;
import com.Exercise3.entity.Strategy.QueenStrategy;

public class Queen extends ChessPiece {
    public Queen(PieceType type, int row, int col) {
        super(type, row, col);
        setMoveStrategy(new QueenStrategy(getCol(),getRow()));
    }
}

/*-------------------------------------------------------*/
package com.Exercise3.entity.Piece;

import com.Exercise3.entity.PieceType;
import com.Exercise3.entity.Strategy.SoldierStategy;

public class Soldier extends ChessPiece{
    public Soldier(PieceType type, int row, int col) {
        super(type, row, col);
        setMoveStrategy(new SoldierStategy(getCol(),getRow(),getSide()));
    }

}

MoveStrategy

该类有一个个重要的方法move()来实现棋子的移动,有些棋子需要作出越子判断isOverPiece()
以马为例,黑方马的“日”字位的八个位置

  • 若有黑子,则不能移动
  • 若是白子则可以吃子
  • 若无子,则可以直接移动
package com.Exercise3.entity.Strategy;

import com.Exercise3.entity.Piece.ChessPiece;

import java.util.List;
import java.util.Set;

public class CarStrategy implements MoveStrategy {
    private int curX;
    private int curY;

    public CarStrategy() {
    }

    public CarStrategy(int curX, int curY) {
        this.curX = curX;
        this.curY = curY;
    }

    public boolean move(int x, int y, Set<ChessPiece> chessPieces) {
        if(x!=curX&&y!=curY)
            return false;
        if(isOverPiece(Math.min(curX,x),Math.min(curY,y),
                Math.max(curX,x),Math.max(curY,y),chessPieces))
            return false;
        curX = x;
        curY = y;
        return true;
    }

    public  static boolean isOverPiece(int stX,int stY,int edX,int edY,Set<ChessPiece> chessPieces){
        for(ChessPiece e:chessPieces)
            if((e.getRow()>stY&&e.getRow()<edY)&&e.getCol()==stX||
                    (e.getCol()>stX&&e.getCol()<edX&&e.getRow()==stY))
                return true;
        return false;
    }


    public int getCurX() {
        return curX;
    }

    public void setCurX(int curX) {
        this.curX = curX;
    }

    public int getCurY() {
        return curY;
    }

    public void setCurY(int curY) {
        this.curY = curY;
    }
}



/*-----------------------------------------------*/
package com.Exercise3.entity.Strategy;

import com.Exercise3.entity.Piece.ChessPiece;

import java.util.List;
import java.util.Set;

public class HorseStategy implements MoveStrategy{
    private int curX;
    private int curY;

    public HorseStategy(int curX, int curY) {
        this.curX = curX;
        this.curY = curY;
    }


    @Override
    public boolean move(int x, int y, Set<ChessPiece> chessPieces) {
        if((Math.abs(curX-x)==1&&Math.abs(curY-y)==2)||
                (Math.abs(curX-x)==2&&Math.abs(curY-y)==1)){
            curX = x;
            curY = y;
            return true;
        }
        return false;
    }

    public int getCurX() {
        return curX;
    }

    public void setCurX(int curX) {
        this.curX = curX;
    }

    public int getCurY() {
        return curY;
    }

    public void setCurY(int curY) {
        this.curY = curY;
    }
}




/*-------------------------------------------------*/
package com.Exercise3.entity.Strategy;

import com.Exercise3.entity.Piece.ChessPiece;

import java.util.List;
import java.util.Set;

public class KingStrategy implements MoveStrategy {
    private int curX;
    private int curY;

    public KingStrategy(int curX, int cuY) {
        this.curX = curX;
        this.curY = cuY;
    }

    @Override
    public boolean move(int x, int y, Set<ChessPiece> chessPieces) {
        if(Math.abs(curX-x)<=1&&Math.abs(curY-y)<=1){
            curX = x;
            curY = y;
            return true;
        }

        return false;
    }

    public int getCurX() {
        return curX;
    }

    public void setCurX(int curX) {
        this.curX = curX;
    }

    public int getCurY() {
        return curY;
    }

    public void setCurY(int curY) {
        this.curY = curY;
    }
}




/*---------------------------------------------------*/
package com.Exercise3.entity.Strategy;

import com.Exercise3.entity.Piece.ChessPiece;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class KnightStrategy implements MoveStrategy {
    private int curX;
    private int curY;

    public KnightStrategy(int curX, int curY) {
        this.curX = curX;
        this.curY = curY;
    }

    @Override
    public boolean move(int x, int y, Set<ChessPiece> chessPieces) {
        if(Math.abs(x-curX)==Math.abs(y-curY)){
            if(isOverPiece(Math.min(curX,x),Math.min(curY,y),
                    Math.max(curX,x),Math.max(curY,y),chessPieces))
                return false;
            curX=x;
            curY=y;
            return true;
        }
        return false;
    }

    public  static boolean isOverPiece(int stX,int stY,int edX,int edY,Set<ChessPiece> chessPieces){
        for(ChessPiece e:chessPieces){
            if(e.getCol()-stX==edX-e.getCol()&&edY-e.getRow()==e.getRow()-stY){
                System.out.println(e.isSelected()+" "+e.getRow()+" "+e.getCol());
                return true;
            }
        }

        return false;
    }
    public int getCurX() {
        return curX;
    }

    public void setCurX(int curX) {
        this.curX = curX;
    }

    public int getCurY() {
        return curY;
    }

    public void setCurY(int curY) {
        this.curY = curY;
    }
}


/*---------------------------------------------*/
package com.Exercise3.entity.Strategy;

import com.Exercise3.entity.Piece.ChessPiece;

import java.util.Set;

public interface MoveStrategy {
    boolean move(int x, int y, Set<ChessPiece> chessPieces);
}




/*-----------------------------------------------------*/
package com.Exercise3.entity.Strategy;

import com.Exercise3.entity.Piece.ChessPiece;

import java.util.List;
import java.util.Set;


public class QueenStrategy implements MoveStrategy{
    private int curX;
    private int curY;

    public QueenStrategy(int curX, int curY) {
        this.curX = curX;
        this.curY = curY;
    }

    @Override
    public boolean move (int x, int y, Set<ChessPiece> chessPieces) {
        if(Math.abs(x-curX)==Math.abs(y-curY)||!(x!=curX&&y!=curY)){
            if(isOverPiece(Math.min(curX,x),Math.min(curY,y),
                    Math.max(curX,x),Math.max(curY,y),chessPieces))
                return false;
            curX = x;
            curY = y;
            return true;
        }
        return false;
    }

    public boolean isOverPiece (int stX,int stY,int edX,int edY,Set<ChessPiece> chessPieces) {
        for(ChessPiece e:chessPieces){
            if(e.getRow()!=stY&&e.getCol()!=stX){
                return KnightStrategy.isOverPiece(stX,stY,edX,edY,chessPieces);
            }
            else{
                return CarStrategy.isOverPiece(stX,stY,edX,edY,chessPieces);
            }
        }
        return false;
    }



    public int getCurX() {
        return curX;
    }

    public void setCurX(int curX) {
        this.curX = curX;
    }

    public int getCurY() {
        return curY;
    }

    public void setCurY(int curY) {
        this.curY = curY;
    }
}



/*-----------------------------------------------*/
package com.Exercise3.entity.Strategy;

import com.Exercise3.entity.Piece.ChessPiece;

import java.util.Set;

public class SoldierStategy  implements MoveStrategy{
    private int curX;
    private int curY;
    private char side;
    private boolean firstMove = true;

    public SoldierStategy(int curX, int curY,char side) {
        this.curX = curX;
        this.curY = curY;
        this.side = side;
    }



    @Override
    public boolean move(int x, int y, Set<ChessPiece> chessPieces) {
        //直线移动
        if(curY==y){
            if(isOverPiece(x,y,side,chessPieces))
                return false;
            switch (side){
                case 'B': {
                    if(isFirstMove()&&(x==curX+1||curX+2==x)){
                        setFirstMove(false);
                        curY = y;
                        curX = x;
                        return true;
                    }
                    else if(!isFirstMove()&&curX+1==x){
                        curY = y;
                        curX = x;
                        return true;
                    }
                    break;
                }

                case 'W':{
                    if(isFirstMove()&&(x==curX-1||x==curX-2)){
                        setFirstMove(false);
                        curY = y;
                        curX = x;
                        return true;
                    }
                    else if(!isFirstMove()&&curX-1==x){
                        curY = y;
                        curX = x;
                        return true;
                    }
                    break;
                }
            }
        }

        //吃子移动
        for(ChessPiece e:chessPieces){
            if(Math.abs(e.getRow()-curY)==1){
                if(e.getCol()-curX==1&&e.getSide()=='W'||
                curX-e.getCol()==1&&e.getSide()=='B'){
                    curY = y;
                    curX = x;
                    return true;
                }

            }
        }

        return false;
    }

    public boolean isOverPiece(int x, int y,char side,Set<ChessPiece> chessPieces){
        for(ChessPiece e:chessPieces){
            if(e.getCol()==x&&e.getRow()==y&&!e.isSelected()){
                return true;
            }
        }
        return false;
    }



    public boolean isFirstMove() {
        return firstMove;
    }

    public void setFirstMove(boolean firstMove) {
        this.firstMove = firstMove;
    }

    public int getCurX() {
        return curX;
    }

    public void setCurX(int curX) {
        this.curX = curX;
    }

    public int getCurY() {
        return curY;
    }

    public void setCurY(int curY) {
        this.curY = curY;
    }
}

PieceType

package com.Exercise3.entity;

public enum PieceType {
    KINGBLACK("KingBlack","com/Exercise3/img/KingBlack.jpg"),
    QUEENBLACK("QueenBlack","com/Exercise3/img/QueenBlack.jpg"),
    CARBLACK("CarBlack","com/Exercise3/img/CarBlack.jpg"),
    HORSEBLACK("HorseBlack","com/Exercise3/img/HorseBlack.jpg"),
    SOLDIERBLACK("SoldierBlack","com/Exercise3/img/SoldierBlack.jpg"),
    KNIGHTBLACK("KnightBlack","com/Exercise3/img/KnightBlack.jpg"),

    KINGWHITE("KingWhite","com/Exercise3/img/KingWhite.jpg"),
    QUEENWHITE("QueenWhite","com/Exercise3/img/QueenWhite.jpg"),
    CARWHITE("CarWhite","com/Exercise3/img/CarWhite.jpg"),
    HORSEWHITE("HorseWhite","com/Exercise3/img/HorseWhite.jpg"),
    SOLDIERWHITE("SoldierWhite","com/Exercise3/img/SoldierWhite.jpg"),
    KNIGHTWHITE("KnightWhite","com/Exercise3/img/KnightWhite.jpg");


    private String desc;
    private PieceType(String desc,String url ){
        this.desc = desc;
        this.url = url;
    }

    private String url;

    public String getDesc(){
        return desc;
    }

    public String getUrl() {
        return url;
    }
}

view

视图层有两个类,ChessBoard和ChessPane

  • ChessBoard
    描述了棋盘的网格大小,底色和棋盘尺寸等信息
  • ChessPane
    该类继承自Pane,有两个重要的属性
    • ChessPiece列表,装载了所有的棋子
    • ChessBoard对象
      两个重要的方法 drawPiece() 用来画棋子,drawBoard()画棋盘,这两个方法在控制器中使用。
package com.Exercise3.view;

public  class ChessBoard {
    static ChessBoard chessBoard = null;
    private int row;
    private int col;
    private double cellLength;
    private double startX;
    private double startY;
    private char currSide;

    private ChessBoard(double cellLength, double startX, double startY) {
        this.row = 8;
        this.col = 8;
        this.cellLength = cellLength;
        this.startX = startX;
        this.startY = startY;
        this.currSide = 'B';
    }

    public static ChessBoard  getInstance(double cellLength, double startX, double startY){
        if(chessBoard == null)
            return new ChessBoard(cellLength,startX,startY);
        return chessBoard;
    }

    public ChessBoard getInstance(){
        return chessBoard;
    }

    public int getCol() {
        return col;
    }



    public int getRow() {
        return row;
    }

    public double getCellLength() {
        return cellLength;
    }

    public void changeSide(){
        currSide=(currSide=='B'?'W':'B');
    }

    public void setCellLength(double cellLength) {
        this.cellLength = cellLength;
    }

    public double getStartX() {
        return startX;
    }

    public void setStartX(double startX) {
        this.startX = startX;
    }

    public double getStartY() {
        return startY;
    }

    public void setStartY(double startY) {
        this.startY = startY;
    }

    public char getCurrSide() {
        return currSide;
    }
}




/*------------------------------------------------------*/
package com.Exercise3.view;

import com.Exercise3.entity.Piece.*;
import com.Exercise3.entity.PieceType;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.*;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;

import java.util.*;

public class ChessPane extends Pane {
    private Set<ChessPiece> chessPieces;
    private  ChessBoard chessBoard;
    private Canvas canvas;
    private GraphicsContext gc;

    public ChessPane(ChessBoard chessBoard) {
        this.chessBoard = chessBoard;
        setChessPiece();
        canvas = new Canvas(900,900);
        gc = canvas.getGraphicsContext2D();
        draw();
    }



    public void draw(){
        drawBoard();
        drawPiece();
        getChildren().add(canvas);
    }


    public void drawBoard(){
        gc.clearRect(0,0,900,900);
        double x = chessBoard.getStartX();
        double y = chessBoard.getStartY();
        double cell = chessBoard.getCellLength();


        boolean flag = false;
        for(int i=0;i<chessBoard.getRow();i++){
            flag = !flag;
            for(int j=0;j<chessBoard.getCol();j++){
                gc.setFill(flag? Color.valueOf("#EDEDED"):Color.valueOf("CDC5BF"));
                gc.fillRect(x+j*cell,y+i*cell,cell,cell);
                flag = !flag;
            }
        }



        gc.setStroke(Color.GRAY);
        gc.strokeRect(x,y,cell*chessBoard.getCol(),cell*chessBoard.getRow());

    }

    public void drawPiece(){
        double cell = chessBoard.getCellLength();
        chessPieces.forEach( e->{
            if(e.isSelected()){
                gc.setFill(Color.valueOf("#6495ED"));
                gc.fillRect(chessBoard.getStartX()+e.getCol()*cell,
                        chessBoard.getStartY()+e.getRow()*cell,
                        cell,cell);
            }

            Image image = new Image(e.getType().getUrl());
            gc.drawImage(image,
                    chessBoard.getStartX()+10 + e.getCol() * cell,
                    chessBoard.getStartY()+10 + e.getRow() * cell,
                    cell-20, cell-20);
        });
    }



    //加入棋子
    public void setChessPiece() {
        chessPieces = new HashSet<>();
        chessPieces.add(new Car(PieceType.CARBLACK,0,0));
        chessPieces.add(new Horse(PieceType.HORSEBLACK,1,0));
        chessPieces.add(new Knight(PieceType.KNIGHTBLACK,2,0));
        chessPieces.add(new King(PieceType.KINGBLACK,3,0));
        chessPieces.add(new Queen(PieceType.QUEENBLACK,4,0));
        chessPieces.add(new Knight(PieceType.KNIGHTBLACK,5,0));
        chessPieces.add(new Horse(PieceType.HORSEBLACK,6,0));
        chessPieces.add(new Car(PieceType.CARBLACK,7,0));
        for(int i=0;i<8;i++){
            chessPieces.add(new Soldier(PieceType.SOLDIERBLACK,i,1));
        }


        chessPieces.add(new Car(PieceType.CARWHITE,0,7));
        chessPieces.add(new Horse(PieceType.HORSEWHITE,1,7));
        chessPieces.add(new Knight(PieceType.KNIGHTWHITE,2,7));
        chessPieces.add(new King(PieceType.KINGWHITE,3,7));
        chessPieces.add(new Queen(PieceType.QUEENWHITE,4,7));
        chessPieces.add(new Knight(PieceType.KNIGHTWHITE,5,7));
        chessPieces.add(new Horse(PieceType.HORSEWHITE,6,7));
        chessPieces.add(new Car(PieceType.CARWHITE,7,7));
        for(int i=0;i<8;i++){
            chessPieces.add(new Soldier(PieceType.SOLDIERWHITE,i,6));
        }
    }

    public ChessBoard getChessBoard() {
        return chessBoard;
    }

    public void setChessBoard(ChessBoard chessBoard) {
        this.chessBoard = chessBoard;
    }

    public Set<ChessPiece> getChessPieces() {
        return chessPieces;
    }

    public void setChessPieces(Set<ChessPiece> chessPieces) {
        this.chessPieces = chessPieces;
    }

    public Canvas getCanvas() {
        return canvas;
    }

    public void setCanvas(Canvas canvas) {
        this.canvas = canvas;
    }

    public GraphicsContext getGc() {
        return gc;
    }

    public void setGc(GraphicsContext gc) {
        this.gc = gc;
    }
}

  • 9
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值