团队课程设计博客-飞行棋

一.团队介绍

成员姓名 任务分配 团队成员课程设计博客链接
严威(组长) 飞行棋功能的实现,人机功能实现,棋子和骰子的所有操作等 https://www.cnblogs.com/putianliuzhong/p/12174353.html
张伟龙 数据库连接,数据库账户匹配及相关操作,道具实现等 https://www.cnblogs.com/zwl-/p/12174236.html
周秋斌 登录页面GUI,游戏过程GUI实现,素材收集,背景音乐等 https://www.cnblogs.com/zhouqb/p/12174221.html

二.项目git地址

https://gitee.com/ywww1/java

三.项目git提交记录截图




四.前期调查

五.项目功能架构图、主要功能流程图

六.包结构图与主要UML类图


七.运行结果

登录界面:

游戏过程中:

八.关键代码

package model;

import java.awt.*;
import java.util.ArrayList;

import static java.lang.Thread.*;
import static model.ChessPane.*;

/**
 * 棋子的移动实现
 * @author yw
 * @date 2020.1.6
 */
public class Move {
    private DiceData dice;
    private ChessData chess;
    /**
     * 要走的棋子编号
     */
    private int chessNum;
    /**
     * 对哪个玩家的棋子就行操作
     */
    private int playerTurn;
    /**
     * 判断棋子是否已经执行
     */
    private boolean flag = false;
    /**
     * 棋子下一步移动的步数
     */
    private int nextPositionNum = 0;
    /**
     * 骰子点数
     */
    private int moveStep = 0;
    private ArrayList<Point> chessPoint = new ArrayList<>();
    private Prop prop;

    public Move(int chessNum, DiceData dice, ChessData chess, Prop prop) {
        /**
         * 产生6个随机地点、功能的道具,并设置每动十次棋子才更新道具的频率
         */
        this.dice=dice;
        this.chess = chess;
        this.prop = prop;
        this.chessNum = chessNum;
        this.playerTurn = dice.getThisPlayerTurn();
        this.moveStep = dice.getThisNum();
        findMoveWay();
        /**
         * 判断移动完之后是否踩到了道具
         * 根据踩到道具的种类而完成相应功能
         */
        eatProp();
        //道具结束
    }

    /**
     * 移动和起飞的步数
     */
    private void findMoveWay(){
        // 起飞的操作
        for (int i = 0; i < 4; i++) {
            // 棋子的位置在家
            if (homePosition[playerTurn][i].equals(chess.getButtonsPosition(chessNum))) {
                // 起飞状态设为true
                chess.changeStatus(chessNum, true);
                chess.changePosition(chessNum, road[playerTurn][0]);
                chess.getChessButton(chessNum).changeBound(road[playerTurn][0]);
                flag = true;
            }
        }
        if (!flag) {
            for (int i = 0; i < roadLength; i++) {
                if (road[playerTurn][i].equals(chess.getButtonsPosition(chessNum))) {
                    nextPositionNum = 2 * roadLength - i - moveStep - 2;
                    //到终点
                    if(i+moveStep==roadLength-1){
                        move(this.chessNum,road[playerTurn][roadLength-1]);
                        move(this.chessNum,homePosition[this.chessNum/4][this.chessNum%4]);
                        chess.getChessButton(this.chessNum).setEndImage(playerTurn);
                        chess.setAChessEndStatus(this.chessNum,true);
                        chess.changeStatus(chessNum,false);
                        if(chess.isPlayerEnd(playerTurn)){
                            chess.setAPlayerEndStatus(playerTurn,true);
                            dice.addEndComps(playerTurn);
                        }
                    } else if (i + moveStep > roadLength - 1) {
                        // 到终点弹回
                        for (int k = i; k < roadLength; k++) {
                            move(this.chessNum, road[playerTurn][k]);
                        }
                        for (int k = roadLength - 1; k >= nextPositionNum; k--) {
                            move(this.chessNum, road[playerTurn][k]);
                        }
                    } else {
                        // 辅助记录移动到哪里
                        int k;
                        nextPositionNum = i + moveStep;
                        for (k = i; k <= nextPositionNum; k++) {
                            move(this.chessNum, road[playerTurn][k]);
                        }
                        killOtherChess(road[playerTurn][nextPositionNum]);
                        // 是否可以飞
                        for (int j = 0; j < flyPositionNum; j++) {
                            if (road[playerTurn][nextPositionNum].equals
                                    (flyPosition[playerTurn][j])) {
                                nextPositionNum += 4;
                                break;
                            }
                        }
                        //移动操作
                        for (k = k - 1; k <= nextPositionNum; k++) {
                            move(this.chessNum, road[playerTurn][k]);
                        }
                        killOtherChess(road[playerTurn][nextPositionNum]);
                        // 是否可以超远飞
                        if (road[playerTurn][nextPositionNum].equals
                                (specialFlyPosition[playerTurn])) {
                            nextPositionNum += 12;
                        }
                        move(this.chessNum, road[playerTurn][nextPositionNum]);
                        killOtherChess(road[playerTurn][nextPositionNum]);

                    }
                    break;

                }
            }

        }
    }

    /**
     * 控制移动
     * @param chessNum
     * @param point
     */
    private void move(int chessNum, Point point) {
        int i = chess.getButtonsPosition(chessNum).x;
        int j = chess.getButtonsPosition(chessNum).y;
        int x = point.x - i;
        int y = point.y - j;
        int dx = 1;
        int dy = 1;
        if (x != 0) {
            dx = Math.abs(x) / x;
        }
        if (y != 0) {
            dy = Math.abs(y) / y;
        }
        for (; i != point.x && j != point.y; i += dx, j += dy) {
            chess.getChessButton(chessNum).changeBound(new Point(i, j));
            try {
                sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (i != point.x) {
            for (; i != point.x; i += dx) {
                chess.getChessButton(chessNum).changeBound(new Point(i, j));
                try {
                    sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } else {
            for (; j != point.y; j += dy) {
                chess.getChessButton(chessNum).changeBound(new Point(i, j));
                try {
                    sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        chess.getChessButton(chessNum).changeBound(point);
        chess.changePosition(chessNum, point);
    }

    /**
     * 踩死同一位置上的其它棋子
     * @param point
     */
    private void killOtherChess(Point point) {
        for (int i = 0; i < chess.getCompNum() * 4; i++) {
            // 是不同颜色的棋子
            if (i / 4 != playerTurn) {
                if (chess.getButtonsPosition(i).equals(point)) {
                    move(i, homePosition[i / 4][i % 4]);
                    chess.changeStatus(i,false);
                }
            }
        }
    }
    /**
     * 判断移动完之后是否踩到了道具
     * 根据踩到道具的种类而完成相应功能
     */
    private void eatProp(){
        if(prop.collision(chess.getButtonsPosition(chessNum))) {
            Point P;
            switch (prop.getPoint(chess.getButtonsPosition(chessNum))) {
                case 0: {
                    System.out.println("踩到龙卷风,随机跳");
                    P = prop.windFunction(dice.getThisPlayerTurn(), chessPoint);
                    move(chessNum, P);
                    killOtherChess(P);
                    break;
                }
                case 1: {
                    System.out.println("踩到加油站,再摇一次骰子");
                    Prop.setAgain(true);
                    break;
                }
                case 2: {
                    System.out.println("踩到炸弹,退后两格");
                    P = prop.minusTwoFunction(dice.getThisPlayerTurn(), chess.getButtonsPosition(chessNum));
                    move(chessNum, P);
                    killOtherChess(P);
                    break;
                }
                default: {
                    System.out.println("踩到六骰子,前进六格");
                    P = prop.sixPane(dice.getThisPlayerTurn(), chess.getButtonsPosition(chessNum));
                    move(chessNum, P);
                    killOtherChess(P);
                    break;
                }
            }
        }
    }
}

此代码主要功能是进行棋子的移动和走到相应位置产生的效果,如到终点飞回起点并不能再次起飞,前进几格等操作,还有吃到道具产生相应操作的实现,并实现界面上棋子的移动效果。

九.项目代码扫描结果及改正

修正前:

修正后:

十.项目总结

不足:没有实现联网互相对战的功能,缺少人机的算法,导致人机行动比较的单一。
展望:可以将道具的界面进行优化,棋子的移动进行重新设计。

猜你喜欢

转载自www.cnblogs.com/putianliuzhong/p/12174238.html