2 * Copyright (c) 2018. Developed by Hedgecode.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.hedgecode.chess.uci.command;
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
22 import org.hedgecode.chess.position.Piece;
23 import org.hedgecode.chess.position.Square;
24 import org.hedgecode.chess.uci.UCICommand;
25 import org.hedgecode.chess.uci.UCIConstants;
28 * Abstract UCI command with parsing functionality.
30 * @author Dmitry Samoshin aka gotty
32 public abstract class AbstractCommand implements UCICommand {
34 private static final String NULLMOVE = UCIConstants.NULLMOVE;
36 private static final Pattern MOVE_PATTERN = Pattern.compile(UCIConstants.MOVE_REGEX);
38 private static final int SQUARE_FROM = UCIConstants.MOVE_REGEX_FROM;
39 private static final int SQUARE_TO = UCIConstants.MOVE_REGEX_TO;
40 private static final int PIECE_PROMOTE = UCIConstants.MOVE_REGEX_PROMOTE;
43 protected Move parseMove(String move) {
44 if (NULLMOVE.equals(move))
45 return new Move(null, null);
47 Matcher matcher = MOVE_PATTERN.matcher(move);
50 Square.getSquare(matcher.group(SQUARE_FROM)),
51 Square.getSquare(matcher.group(SQUARE_TO)),
52 Piece.byLetter(matcher.group(PIECE_PROMOTE))