除了数组中的所有其他方法,还有其他更好的方法吗?

时间:2019-10-08 12:40:24

标签: java

我是一个初学者,我想做一个刚开始的国际象棋游戏,我认为有一种更好的方法可以将所有棋子排列成一个数组,而不必像这样写每个棋子 我的代码又长又丑,请帮助我

public class Chess {
    public static void main(String[] args) {
        int [][] grid = new int[8][8];

        Pieces PB1 = new Pieces(); PB1.Pieces = 6;
        Pieces PB2 = new Pieces(); PB2.Pieces = 6;
        Pieces PB3 = new Pieces(); PB3.Pieces = 6;
        Pieces PB4 = new Pieces(); PB4.Pieces = 6;
        Pieces PB5 = new Pieces(); PB5.Pieces = 6;
        Pieces PB6 = new Pieces(); PB6.Pieces = 6;
        Pieces PB7 = new Pieces(); PB7.Pieces = 6;
        Pieces PB8 = new Pieces(); PB8.Pieces = 6;
        Pieces PW1 = new Pieces(); PW1.Pieces = 6;
        Pieces PW2 = new Pieces(); PW2.Pieces = 6;
        Pieces PW3 = new Pieces(); PW3.Pieces = 6;
        Pieces PW4 = new Pieces(); PW4.Pieces = 6;
        Pieces PW5 = new Pieces(); PW5.Pieces = 6;
        Pieces PW6 = new Pieces(); PW6.Pieces = 6;
        Pieces PW7 = new Pieces(); PW7.Pieces = 6;
        Pieces PW8 = new Pieces(); PW8.Pieces = 6;
        Pieces KB1 = new Pieces(); KB1.Pieces = 2;
        Pieces KW1 = new Pieces(); KW1.Pieces = 2;
        Pieces QB1 = new Pieces(); QB1.Pieces = 1;
        Pieces QW1 = new Pieces(); QW1.Pieces = 1;
        Pieces BB1 = new Pieces(); BB1.Pieces = 3;
        Pieces BB2 = new Pieces(); BB2.Pieces = 3;
        Pieces BW1 = new Pieces(); BW1.Pieces = 3;
        Pieces BW2 = new Pieces(); BW2.Pieces = 3;
        Pieces NB1 = new Pieces(); NB1.Pieces = 4;
        Pieces NB2 = new Pieces(); NB2.Pieces = 4;
        Pieces NW1 = new Pieces(); NW1.Pieces = 4;
        Pieces NW2 = new Pieces(); NW2.Pieces = 4;
        Pieces RB1 = new Pieces(); RB1.Pieces = 5;
        Pieces RB2 = new Pieces(); RB2.Pieces = 5;
        Pieces RW1 = new Pieces(); RW1.Pieces = 5;
        Pieces RW2 = new Pieces(); RW2.Pieces = 5;
    }
}

public class Pieces{
    int type;     // |1 = Queen|   |2 = King|   |3 = Bishop|   |4 = Knight|   |5 = Rook|   |6 = Pawn|
    int movement; // |1 = Pawn Step|   |2 = Pawn Two Steps|   |3 = Pawn Kill|   |4 = Knight|   |5 = Rook|   |6 = Bishop|   |7 = King|
} 

1 个答案:

答案 0 :(得分:0)

您应该先学习面向对象的原理,然后再去学习。只是想知道您可以做什么

package stackoverflow;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Test;

public class Chess {

    enum Rank {
        // this time lower case even if these are constants
        a, b, c, d, e, f, g, h
    }

    enum File {
        one, two, three, four, five, six, seven, eight
    }

    enum FigureColor {
        BLACK, WHITE
    }

    interface Figure {

        FigureColor getFigureColor();

        Collection<FigurePosition> getMovements();

        Figure move(FigurePosition figurePosition);

        FigurePosition getCurrentPosition();

    }

    class FigurePosition {

        Rank rank;
        File file;

        public FigurePosition(Rank rank, File file) {
            this.rank = rank;
            this.file = file;
        }

        @Override
        public String toString() {
            return "FigurePosition{" +
                    "rank=" + rank +
                    ", file=" + file +
                    '}';
        }
    }

    abstract class BasicFigure implements Figure {

        private FigureColor figureColor;
        private List<FigurePosition> movements = new ArrayList<FigurePosition>();

        public BasicFigure(FigureColor figureColor, FigurePosition initialFigurePosition) {
            this.figureColor = figureColor;
            move(initialFigurePosition);
        }

        public FigureColor getFigureColor() {
            return figureColor;
        }

        public Collection<FigurePosition> getMovements() {
            return movements;
        }

        public Figure move(FigurePosition figurePosition) {
            movements.add(figurePosition);
            return this;
        }

        public FigurePosition getCurrentPosition() {
            return movements.get(movements.size()-1);
        }

        @Override
        public String toString() {
            return getClass().getSimpleName() + "{" +
                    "figureColor=" + figureColor +
                    ", currentPosition=" + getCurrentPosition() +
                    ", movements=[" + getMovements().stream().map(FigurePosition::toString).collect(Collectors.joining(",")) + "]'}'";
        }
    }

    class Queen extends BasicFigure {

        public Queen(FigureColor figureColor, FigurePosition initialFigurePosition) {
            super(figureColor, initialFigurePosition);
        }

        @Override
        public Figure move(FigurePosition figurePosition) {
            // you can for example validate movements here
            return super.move(figurePosition);
        }
    }

    class King extends BasicFigure {

        public King(FigureColor figureColor, FigurePosition initialFigurePosition) {
            super(figureColor, initialFigurePosition);
        }
    }

    class Pawn extends BasicFigure {

        public Pawn(FigureColor figureColor, FigurePosition initialFigurePosition) {
            super(figureColor, initialFigurePosition);
        }
    }

    /** add other figure classes here */

    @Test
    public void test() {
        Figure whiteQueen = new Queen(FigureColor.WHITE, new FigurePosition(Rank.d, File.one));
        System.out.println(whiteQueen);
        Figure blackPawn1 = new Pawn(FigureColor.BLACK, new FigurePosition(Rank.a, File.seven));
        blackPawn1.move(new FigurePosition(Rank.a, File.six));
        System.out.println(blackPawn1);
    }

}