From c30c1d1230ec2771527b172a4f2ee8a44e7d4bda Mon Sep 17 00:00:00 2001 From: gotty Date: Thu, 20 Dec 2018 19:46:12 +0300 Subject: [PATCH] [LIB-9] Chessboard squares sorting --- .../org/hedgecode/chess/ChessHogConstants.java | 1 - .../hedgecode/chess/position/DiagramPosition.java | 14 ++++ .../org/hedgecode/chess/position/Position.java | 2 + .../org/hedgecode/chess/position/SquareSort.java | 88 ++++++++++++++++++++++ .../hedgecode/chess/uci/ConsoleAcceptorRunner.java | 2 +- .../org/hedgecode/chess/uci/command/GoParams.java | 28 +++++++ 6 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/hedgecode/chess/position/SquareSort.java create mode 100644 src/main/java/org/hedgecode/chess/uci/command/GoParams.java 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 { + + + +} -- 2.10.0