Commit 0c246544 authored by lynn's avatar lynn
Browse files

Adding attempts to conclude all chess components into one class...

Adding attempts to conclude all chess components into one class AllChessComponents by using enum and switch. There are still things to be fixed.
The method has not been completed yet.
parent f3f2350a
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@ import model.ChessboardPoint;

import java.awt.*;

public class AdvisorChessComponent extends ChessComponent{
public class AdvisorChessComponent extends AllChessComponents{
    protected AdvisorChessComponent(ChessboardPoint chessboardPoint, Point location,
                                    ChessColor chessColor, ClickController clickController, int size) {
        super(chessboardPoint, location, chessColor, clickController, size);
+73 −0
Original line number Diff line number Diff line
package chessComponent;

import controller.ClickController;
import model.ChessColor;
import model.ChessboardPoint;
import java.awt.*;

// todo: use polymorphism to write all chess components
/*  思路:用 0~13 分别代表各个棋子
 *   创建 enum 类
 *   问题:enum 不能调用ChessComponent的constructor
 *       enum 静态构造可行吗?
 */
    public class AllChessComponents extends ChessComponent {

/*          用 enum 列出棋子种类,
*           chessNum 对应每种棋子,
*           便于在 Chessboard 用 random 随机初始化棋子位置
* */
       public enum allChessComponent {Soldier, Horse, Chariot, Minister, Advisor, General, Cannon}

    public int getChessNum() {
        return chessNum;
    }

    public void setChessNum(int chessNum) {
        this.chessNum = chessNum;
    }

    private int chessNum;

//      todo:  确定输入 chessNum 的位置

        public static allChessComponent setChess (int chessNum) {
            // 或许可以在ChessComponent 和 SquareComponent 里写空的 constructor
           allChessComponent chess = allChessComponent.Soldier;
            switch (chessNum){
                case 0:
                    // enum 不能向下转型
                        chess = allChessComponent.Soldier;
                        break;
                case 1:
                    chess = allChessComponent.Horse;
                    break;
                case 2:
                    chess = allChessComponent.Chariot;
                    break;
                case 3:
                    chess = allChessComponent.Minister;
                    break;
                case 4:
                    chess = allChessComponent.Advisor;
                    break;
                case 5:
                    chess = allChessComponent.General;
                    break;
                case 6:
                    chess = allChessComponent.Cannon;
                    break;
            }
            return chess;
        }
        public AllChessComponents(ChessboardPoint chessboardPoint, Point location, ChessColor chessColor,
                                  ClickController clickController, int size) {
            super(chessboardPoint, location, chessColor, clickController, size);
            // todo: 确定 enum 出的每个棋子都关联到各自的类
            this.name = setChess(getChessNum()).name();
        }
        // void constructor
//        public AllChessComponents(){}
    }

+1 −1
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@ import model.ChessboardPoint;

import java.awt.*;

public class CannonChessComponent extends  ChessComponent{
public class CannonChessComponent extends  AllChessComponents{
    protected CannonChessComponent(ChessboardPoint chessboardPoint, Point location,
                                   ChessColor chessColor, ClickController clickController, int size) {
        super(chessboardPoint, location, chessColor, clickController, size);
+6 −1
Original line number Diff line number Diff line
@@ -11,14 +11,19 @@ import java.awt.*;
 */
public class ChariotChessComponent extends ChessComponent {

//    ChariotChessComponent chariot = new ChariotChessComponent();
    public ChariotChessComponent(ChessboardPoint chessboardPoint, Point location,
                                 ChessColor chessColor, ClickController clickController, int size) {
        super(chessboardPoint, location, chessColor, clickController, size);
    }
//    public ChariotChessComponent (){}

    public String setName() {
        if (this.getChessColor() == ChessColor.RED) {
            name = "俥";
        } else {
            name = "車";
        }
        return name;
    }

}
+2 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@ public class ChessComponent extends SquareComponent{
                             ChessColor chessColor, ClickController clickController, int size) {
        super(chessboardPoint, location, chessColor, clickController, size);
    }
//    public ChessComponent(){}

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
Loading