[LIB-9] Add chesshog-qrcode module
[chesshog.git] / src / main / java / org / hedgecode / chess / uci / command / AbstractCommand.java
1 /*
2  * Copyright (c) 2018. Developed by Hedgecode.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.hedgecode.chess.uci.command;
18
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21
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;
26
27 /**
28  * Abstract UCI command with parsing functionality.
29  *
30  * @author Dmitry Samoshin aka gotty
31  */
32 public abstract class AbstractCommand implements UCICommand {
33
34     private static final String NULLMOVE = UCIConstants.NULLMOVE;
35
36     private static final Pattern MOVE_PATTERN = Pattern.compile(UCIConstants.MOVE_REGEX);
37
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;
41
42
43     protected Move parseMove(String move) {
44         if (NULLMOVE.equals(move))
45             return new Move(null, null);
46
47         Matcher matcher = MOVE_PATTERN.matcher(move);
48         if (matcher.find()) {
49             return new Move(
50                     Square.getSquare(matcher.group(SQUARE_FROM)),
51                     Square.getSquare(matcher.group(SQUARE_TO)),
52                     Piece.byLetter(matcher.group(PIECE_PROMOTE))
53             );
54         }
55         return null;
56     }
57
58 }