001    package rules;
002    import core.*;
003    
004    /**
005     * Go from a piece name to a piece instance.
006     */
007    public class PieceFactory {
008    
009        /**
010         * Copy a piece, with a new lastMove.
011         * @requires p != null
012         */
013        public static Piece newPiece(Piece p, int num) {
014            return pieceFromChar(p.toString().charAt(0), num);
015        }
016    
017        private static final Piece[] king = {new King(Color.WHITE, 1),
018                                             new King(Color.BLACK, 1)};
019        private static final Piece[] queen = {new Queen(Color.WHITE, 1),
020                                             new Queen(Color.BLACK, 1)};
021        private static final Piece[] rook = {new Rook(Color.WHITE, 1),
022                                             new Rook(Color.BLACK, 1)};
023        private static final Piece[] bishop = {new Bishop(Color.WHITE, 1),
024                                             new Bishop(Color.BLACK, 1)};
025        private static final Piece[] pawn = {new Pawn(Color.WHITE, 1),
026                                             new Pawn(Color.BLACK, 1)};
027        private static final Piece[] knight = {new Knight(Color.WHITE, 1),
028                                             new Knight(Color.BLACK, 1)};
029        private static final Piece[] empty = {new Empty()};
030        private static final Piece[] destroy = {new Destroy()};
031        private static final Piece[] spawn = {new Spawn()};
032        private static final Piece[] upgrade = {new Upgrade()};
033    
034        public static Piece newPiece(Piece p) {
035            int index = p.getColor() == Color.BLACK ? 1 : 0; //NONE = 0
036            if (p instanceof King)
037                return king[index];
038            else if (p instanceof Queen)
039                return queen[index];
040            else if (p instanceof Rook)
041                return rook[index];
042            else if (p instanceof Pawn)
043                return pawn[index];
044            else if (p instanceof Knight)
045                return knight[index];
046            else if (p instanceof Bishop)
047                return bishop[index];
048            else if (p instanceof Empty)
049                return empty[index];
050            else if (p instanceof Destroy)
051                return destroy[index];
052            else if (p instanceof Spawn)
053                return spawn[index];
054            else if (p instanceof Upgrade)
055                return upgrade[index];
056            else
057                throw new RuntimeException("Unknown piece " + p);
058        }
059    
060    
061        /**
062         * Go from a short name to a piece, lowercase = black.  Lastmove
063         * is set to 0.
064         */
065        public static Piece pieceFromChar(char s) {
066            return pieceFromChar(s, 0);
067        }
068    
069        /**
070         * Go from a short name to a piece, lowercase = black.  Lastmove
071         * is specified.
072         */
073        public static Piece pieceFromChar(char s, int num)
074        {
075            Color p;
076            if (Character.isLowerCase(s))
077                p = Color.BLACK;
078            else
079                p = Color.WHITE;
080            switch (Character.toLowerCase(s)) {
081            case ' ':
082                return new Empty();
083            case 'p':
084                return new Pawn(p, num);
085            case 'r':
086                return new Rook(p, num);
087            case 'n':
088                return new Knight(p, num);
089            case 'b':
090                return new Bishop(p, num);
091            case 'k':
092                return new King(p, num);
093            case 'q':
094                return new Queen(p, num);
095            case 'd':
096                return new Destroy();
097            case 's':
098                return new Spawn();
099            case 'u':
100                return new Upgrade();
101            default:
102                throw new RuntimeException("Unknown character: '" + s + "'");
103            }
104        }
105    
106    
107        /**
108         * Go from a long name to a piece.  Lastmove is set to 0.
109         * @requires piece != null && p != null
110         */
111        public static Piece pieceFromString(String piece, Color p) {
112            return pieceFromString(piece, p, 0);
113        }
114    
115        /**
116         * Go from a long name to a piece.  Lastmove is specified.
117         * @requires piece != null && p != null
118         */
119        public static Piece pieceFromString(String piece, Color p, int num)
120        {
121            if (piece.equals("pawn"))
122                return new Pawn(p, num);
123            if (piece.equals("rook"))
124                return new Rook(p, num);
125            if (piece.equals("knight"))
126                return new Knight(p, num);
127            if (piece.equals("bishop"))
128                return new Bishop(p, num);
129            if (piece.equals("king"))
130                return new King(p, num);
131            if (piece.equals("queen"))
132                return new Queen(p, num);
133            if (piece.equals("powerup_destroy"))
134                return new Destroy();
135            if (piece.equals("powerup_spawn"))
136                return new Spawn();
137            if (piece.equals("powerup_upgrade"))
138                return new Upgrade();
139            throw new RuntimeException("Unknown string: '" + piece + "'");
140        }
141    }