[LIB-9] Separate chesshog-uci module
[chesshog.git] / chesshog-uci / src / main / java / org / hedgecode / chess / uci / ExternalEngine.java
diff --git a/chesshog-uci/src/main/java/org/hedgecode/chess/uci/ExternalEngine.java b/chesshog-uci/src/main/java/org/hedgecode/chess/uci/ExternalEngine.java
new file mode 100644 (file)
index 0000000..fe4a8c9
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * 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;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintWriter;
+
+/**
+ * External UCI Engine.
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class ExternalEngine implements Engine {
+
+    private static final String COMMAND_FORMAT = "%s %s";
+
+    private Transmitter<String> transmitter;
+    private PrintWriter engineWriter;
+
+    ExternalEngine(InputStream input, OutputStream output) {
+        transmitter = new ExternalEngineTransmitter(input);
+        engineWriter = new PrintWriter(output, true);
+    }
+
+    public Transmitter<String> transmitter() {
+        return transmitter;
+    }
+
+    private void write(String command) {
+        engineWriter.println(command);
+    }
+
+    private String format(String name, String params) {
+        return String.format(
+                COMMAND_FORMAT, name, params
+        );
+    }
+
+    @Override
+    public void init() {
+        write(
+                UCIConstants.UCI
+        );
+    }
+
+    @Override
+    public void debug(DebugMode mode) {
+        write(
+                format(
+                        UCIConstants.DEBUG, mode.value()
+                )
+        );
+    }
+
+    @Override
+    public void isReady() {
+        write(
+                UCIConstants.IS_READY
+        );
+    }
+
+    @Override
+    public void go() {
+        write(
+                UCIConstants.GO // todo: + params
+        );
+    }
+
+    @Override
+    public void stop() {
+        write(
+                UCIConstants.STOP
+        );
+    }
+
+    @Override
+    public void ponderhit() {
+        write(
+                UCIConstants.PONDER_HIT
+        );
+    }
+
+    @Override
+    public void quit() {
+        write(
+                UCIConstants.QUIT
+        );
+    }
+
+}