From: gotty Date: Thu, 20 Dec 2018 16:46:12 +0000 (+0300) Subject: [LIB-9] Chessboard squares sorting X-Git-Url: https://git.hedgecode.org/?a=commitdiff_plain;h=c30c1d1230ec2771527b172a4f2ee8a44e7d4bda;p=chesshog.git [LIB-9] Chessboard squares sorting --- diff --git a/src/main/java/org/hedgecode/chess/ChessHogConstants.java b/src/main/java/org/hedgecode/chess/ChessHogConstants.java index 77d1d95..f465291 100644 --- a/src/main/java/org/hedgecode/chess/ChessHogConstants.java +++ b/src/main/java/org/hedgecode/chess/ChessHogConstants.java @@ -18,7 +18,6 @@ package org.hedgecode.chess; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; -import java.util.ResourceBundle; /** * Store of library constants. diff --git a/src/main/java/org/hedgecode/chess/position/DiagramPosition.java b/src/main/java/org/hedgecode/chess/position/DiagramPosition.java index e12b477..1d28759 100644 --- a/src/main/java/org/hedgecode/chess/position/DiagramPosition.java +++ b/src/main/java/org/hedgecode/chess/position/DiagramPosition.java @@ -18,6 +18,7 @@ package org.hedgecode.chess.position; import java.util.HashMap; import java.util.Map; +import java.util.TreeMap; /** * @@ -55,6 +56,19 @@ public class DiagramPosition implements Position { } @Override + public Map getSquares(SquareSort sorting) { + if (sorting == null) + sorting = SquareSort.defaultSort(); + + TreeMap sortedSquares = new TreeMap<>( + sorting.comparator() + ); + sortedSquares.putAll(squares); + + return sortedSquares; + } + + @Override public Map getSquarePieces(Color color) { return getSquarePieces(color, null); } diff --git a/src/main/java/org/hedgecode/chess/position/Position.java b/src/main/java/org/hedgecode/chess/position/Position.java index 6fd9713..c094da9 100644 --- a/src/main/java/org/hedgecode/chess/position/Position.java +++ b/src/main/java/org/hedgecode/chess/position/Position.java @@ -27,6 +27,8 @@ public interface Position { Map getSquares(); + Map getSquares(SquareSort sorting); + Map getSquarePieces(Color color); Map getSquarePieces(Color color, Piece piece); diff --git a/src/main/java/org/hedgecode/chess/position/SquareSort.java b/src/main/java/org/hedgecode/chess/position/SquareSort.java new file mode 100644 index 0000000..6c460b9 --- /dev/null +++ b/src/main/java/org/hedgecode/chess/position/SquareSort.java @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2018. Developed by Hedgecode. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.hedgecode.chess.position; + +import java.util.Comparator; + +/** + * + * + * @author Dmitry Samoshin aka gotty + */ +public enum SquareSort { + + A1H8 ( + new A1H8Comparator() + ), + + H8A1 ( + new H8A1Comparator() + ), + + A8H1 ( + new A8H1Comparator() + ); + + Comparator comparator; + + SquareSort(Comparator comparator) { + this.comparator = comparator; + } + + public Comparator comparator() { + return comparator; + } + + public static SquareSort defaultSort() { + return SquareSort.A1H8; + } + + + static class A1H8Comparator implements Comparator { + + @Override + public int compare(Square square1, Square square2) { + return square1.getH() == square2.getH() + ? square1.getV() == square2.getV() ? 0 + : square1.getV() > square2.getV() ? 1 : -1 + : square1.getH() > square2.getH() ? 1 : -1; + } + } + + static class H8A1Comparator implements Comparator { + + @Override + public int compare(Square square1, Square square2) { + return square1.getH() == square2.getH() + ? square1.getV() == square2.getV() ? 0 + : square1.getV() < square2.getV() ? 1 : -1 + : square1.getH() < square2.getH() ? 1 : -1; + } + } + + static class A8H1Comparator implements Comparator { + + @Override + public int compare(Square square1, Square square2) { + return square1.getH() == square2.getH() + ? square1.getV() == square2.getV() ? 0 + : square1.getV() > square2.getV() ? 1 : -1 + : square1.getH() < square2.getH() ? 1 : -1; + } + } + +} diff --git a/src/main/java/org/hedgecode/chess/uci/ConsoleAcceptorRunner.java b/src/main/java/org/hedgecode/chess/uci/ConsoleAcceptorRunner.java index e5a448f..173b634 100644 --- a/src/main/java/org/hedgecode/chess/uci/ConsoleAcceptorRunner.java +++ b/src/main/java/org/hedgecode/chess/uci/ConsoleAcceptorRunner.java @@ -27,7 +27,7 @@ import org.hedgecode.chess.uci.command.CommandParams; * * @author Dmitry Samoshin aka gotty */ -public class ConsoleAcceptorRunner implements AcceptorRunner { +public final class ConsoleAcceptorRunner implements AcceptorRunner { private ConsoleAcceptor acceptor; diff --git a/src/main/java/org/hedgecode/chess/uci/command/GoParams.java b/src/main/java/org/hedgecode/chess/uci/command/GoParams.java new file mode 100644 index 0000000..5505471 --- /dev/null +++ b/src/main/java/org/hedgecode/chess/uci/command/GoParams.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2018. Developed by Hedgecode. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.hedgecode.chess.uci.command; + +/** + * Container class for values of UCI command "go". + * + * @author Dmitry Samoshin aka gotty + */ +public final class GoParams { + + + +}