mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 19:04:49 +00:00
Compare commits
12 Commits
912d25c01f
...
ReversiML
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5501c3893f | ||
|
|
ffdec38e5d | ||
|
|
1ae79daef0 | ||
|
|
cd8eb99559 | ||
|
|
0132981d94 | ||
|
|
322197494c | ||
|
|
a9c99df5d2 | ||
|
|
03dc6130e2 | ||
|
|
ca7f9e8ecf | ||
|
|
f6d90ed439 | ||
|
|
7e913ff50f | ||
|
|
afb4844084 |
80
.github/workflows/checks.yaml
vendored
80
.github/workflows/checks.yaml
vendored
@@ -1,42 +1,42 @@
|
|||||||
name: Checks
|
#name: Checks
|
||||||
|
|
||||||
on:
|
#on:
|
||||||
push:
|
# push:
|
||||||
branches:
|
# branches:
|
||||||
- 'main'
|
# - 'main'
|
||||||
pull_request:
|
# pull_request:
|
||||||
branches:
|
# branches:
|
||||||
- 'main'
|
# - 'main'
|
||||||
|
#
|
||||||
|
#jobs:
|
||||||
|
# formatting-check:
|
||||||
|
# name: Follow Google Formatting Guidelines
|
||||||
|
# runs-on: ubuntu-latest
|
||||||
|
# steps:
|
||||||
|
# - uses: actions/checkout@v5
|
||||||
|
# with:
|
||||||
|
# fetch-depth: 0 # Fix for incremental formatting
|
||||||
|
# - uses: actions/setup-java@v5
|
||||||
|
# with:
|
||||||
|
# java-version: '25'
|
||||||
|
# distribution: 'temurin'
|
||||||
|
# cache: maven
|
||||||
|
# - name: Run Format Check
|
||||||
|
# run: mvn spotless:check
|
||||||
|
|
||||||
jobs:
|
# tests:
|
||||||
formatting-check:
|
# name: Unittests
|
||||||
name: Follow Google Formatting Guidelines
|
# runs-on: ${{ matrix.os }}
|
||||||
runs-on: ubuntu-latest
|
# needs: formatting-check
|
||||||
steps:
|
# strategy:
|
||||||
- uses: actions/checkout@v5
|
# matrix:
|
||||||
with:
|
# os: [ubuntu-latest] #windows-latest, macos-latest
|
||||||
fetch-depth: 0 # Fix for incremental formatting
|
# steps:
|
||||||
- uses: actions/setup-java@v5
|
# - uses: actions/checkout@v5
|
||||||
with:
|
# - uses: actions/setup-java@v5
|
||||||
java-version: '25'
|
# with:
|
||||||
distribution: 'temurin'
|
# java-version: '25'
|
||||||
cache: maven
|
# distribution: 'temurin'
|
||||||
- name: Run Format Check
|
# cache: maven
|
||||||
run: mvn spotless:check
|
# - name: Run Unittests
|
||||||
|
# run: mvn -B test
|
||||||
tests:
|
|
||||||
name: Unittests
|
|
||||||
runs-on: ${{ matrix.os }}
|
|
||||||
needs: formatting-check
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
os: [ubuntu-latest] #windows-latest, macos-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v5
|
|
||||||
- uses: actions/setup-java@v5
|
|
||||||
with:
|
|
||||||
java-version: '25'
|
|
||||||
distribution: 'temurin'
|
|
||||||
cache: maven
|
|
||||||
- name: Run Unittests
|
|
||||||
run: mvn -B test
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package org.toop.app;
|
|||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
import javafx.scene.input.KeyCode;
|
import javafx.scene.input.KeyCode;
|
||||||
import javafx.scene.input.KeyCodeCombination;
|
import javafx.scene.input.KeyCodeCombination;
|
||||||
import javafx.scene.input.KeyCombination;
|
|
||||||
import javafx.scene.input.KeyEvent;
|
import javafx.scene.input.KeyEvent;
|
||||||
|
|
||||||
import org.toop.app.widget.Primitive;
|
import org.toop.app.widget.Primitive;
|
||||||
@@ -17,6 +16,7 @@ import org.toop.framework.audio.*;
|
|||||||
import org.toop.framework.audio.events.AudioEvents;
|
import org.toop.framework.audio.events.AudioEvents;
|
||||||
import org.toop.framework.eventbus.EventFlow;
|
import org.toop.framework.eventbus.EventFlow;
|
||||||
import org.toop.framework.eventbus.GlobalEventBus;
|
import org.toop.framework.eventbus.GlobalEventBus;
|
||||||
|
import org.toop.game.machinelearning.NeuralNetwork;
|
||||||
import org.toop.framework.networking.NetworkingClientEventListener;
|
import org.toop.framework.networking.NetworkingClientEventListener;
|
||||||
import org.toop.framework.networking.NetworkingClientManager;
|
import org.toop.framework.networking.NetworkingClientManager;
|
||||||
import org.toop.framework.resource.ResourceLoader;
|
import org.toop.framework.resource.ResourceLoader;
|
||||||
@@ -112,20 +112,19 @@ public final class App extends Application {
|
|||||||
Platform.runLater(() -> stage.setOpacity(1.0));
|
Platform.runLater(() -> stage.setOpacity(1.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
Platform.runLater(() -> loading.setMaxAmount(e.isLoadingAmount()));
|
|
||||||
|
|
||||||
Platform.runLater(() -> {
|
Platform.runLater(() -> {
|
||||||
|
loading.setMaxAmount(e.isLoadingAmount());
|
||||||
try {
|
try {
|
||||||
loading.setAmount(e.hasLoadedAmount());
|
loading.setAmount(e.hasLoadedAmount());
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
throw new RuntimeException(ex);
|
throw new RuntimeException(ex);
|
||||||
}
|
}
|
||||||
|
if (e.hasLoadedAmount() >= e.isLoadingAmount()-1) {
|
||||||
|
Platform.runLater(loading::triggerSuccess);
|
||||||
|
loadingFlow.unsubscribe("init_loading");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (e.hasLoadedAmount() >= e.isLoadingAmount()) {
|
|
||||||
Platform.runLater(loading::triggerSuccess);
|
|
||||||
loadingFlow.unsubscribe("init_loading");
|
|
||||||
}
|
|
||||||
|
|
||||||
}, false, "init_loading");
|
}, false, "init_loading");
|
||||||
|
|
||||||
@@ -140,8 +139,14 @@ public final class App extends Application {
|
|||||||
|
|
||||||
stage.show();
|
stage.show();
|
||||||
|
|
||||||
|
//startML();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void startML() {
|
||||||
|
NeuralNetwork nn = new NeuralNetwork();
|
||||||
|
nn.init();
|
||||||
|
}
|
||||||
|
|
||||||
private void setKeybinds(StackPane root) {
|
private void setKeybinds(StackPane root) {
|
||||||
root.addEventHandler(KeyEvent.KEY_PRESSED,event -> {
|
root.addEventHandler(KeyEvent.KEY_PRESSED,event -> {
|
||||||
if (event.getCode() == KeyCode.ESCAPE) {
|
if (event.getCode() == KeyCode.ESCAPE) {
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ import org.toop.game.games.reversi.BitboardReversi;
|
|||||||
import org.toop.game.games.tictactoe.BitboardTicTacToe;
|
import org.toop.game.games.tictactoe.BitboardTicTacToe;
|
||||||
import org.toop.game.players.ArtificialPlayer;
|
import org.toop.game.players.ArtificialPlayer;
|
||||||
import org.toop.game.players.OnlinePlayer;
|
import org.toop.game.players.OnlinePlayer;
|
||||||
import org.toop.game.players.RandomAI;
|
import org.toop.game.players.ai.RandomAI;
|
||||||
import org.toop.local.AppContext;
|
import org.toop.local.AppContext;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ public class GenericGameController<T extends TurnBasedGame<T>> implements GameCo
|
|||||||
// Listen to updates
|
// Listen to updates
|
||||||
eventFlow
|
eventFlow
|
||||||
.listen(GUIEvents.GameEnded.class, this::onGameFinish, false)
|
.listen(GUIEvents.GameEnded.class, this::onGameFinish, false)
|
||||||
.listen(GUIEvents.PlayerAttemptedMove.class, event -> {if (getCurrentPlayer() instanceof LocalPlayer<T> lp){lp.setMove(event.move());}}, false);
|
.listen(GUIEvents.PlayerAttemptedMove.class, event -> {if (getCurrentPlayer() instanceof LocalPlayer<T> lp){lp.setLastMove(event.move());}}, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start(){
|
public void start(){
|
||||||
|
|||||||
@@ -2,9 +2,6 @@ package org.toop.app.widget.view;
|
|||||||
|
|
||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
import org.toop.app.GameInformation;
|
import org.toop.app.GameInformation;
|
||||||
import org.toop.app.canvas.ReversiBitCanvas;
|
|
||||||
import org.toop.app.canvas.TicTacToeBitCanvas;
|
|
||||||
import org.toop.app.gameControllers.GenericGameController;
|
|
||||||
import org.toop.app.gameControllers.ReversiBitController;
|
import org.toop.app.gameControllers.ReversiBitController;
|
||||||
import org.toop.app.gameControllers.TicTacToeBitController;
|
import org.toop.app.gameControllers.TicTacToeBitController;
|
||||||
import org.toop.framework.gameFramework.controller.GameController;
|
import org.toop.framework.gameFramework.controller.GameController;
|
||||||
@@ -18,8 +15,8 @@ import org.toop.app.widget.complex.PlayerInfoWidget;
|
|||||||
import org.toop.app.widget.complex.ViewWidget;
|
import org.toop.app.widget.complex.ViewWidget;
|
||||||
import org.toop.app.widget.popup.ErrorPopup;
|
import org.toop.app.widget.popup.ErrorPopup;
|
||||||
import org.toop.app.widget.tutorial.*;
|
import org.toop.app.widget.tutorial.*;
|
||||||
import org.toop.game.players.MiniMaxAI;
|
import org.toop.game.players.ai.MiniMaxAI;
|
||||||
import org.toop.game.players.RandomAI;
|
import org.toop.game.players.ai.RandomAI;
|
||||||
import org.toop.local.AppContext;
|
import org.toop.local.AppContext;
|
||||||
|
|
||||||
import javafx.geometry.Pos;
|
import javafx.geometry.Pos;
|
||||||
@@ -27,9 +24,6 @@ import javafx.scene.control.ScrollPane;
|
|||||||
import javafx.scene.layout.VBox;
|
import javafx.scene.layout.VBox;
|
||||||
import org.toop.local.AppSettings;
|
import org.toop.local.AppSettings;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
public class LocalMultiplayerView extends ViewWidget {
|
public class LocalMultiplayerView extends ViewWidget {
|
||||||
private final GameInformation information;
|
private final GameInformation information;
|
||||||
|
|
||||||
|
|||||||
@@ -146,7 +146,13 @@
|
|||||||
<artifactId>error_prone_annotations</artifactId>
|
<artifactId>error_prone_annotations</artifactId>
|
||||||
<version>2.42.0</version>
|
<version>2.42.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
<dependency>
|
||||||
|
<groupId>org.deeplearning4j</groupId>
|
||||||
|
<artifactId>deeplearning4j-nn</artifactId>
|
||||||
|
<version>1.0.0-M2.1</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
|||||||
@@ -14,7 +14,8 @@ import org.toop.framework.eventbus.events.EventType;
|
|||||||
import org.toop.framework.eventbus.events.ResponseToUniqueEvent;
|
import org.toop.framework.eventbus.events.ResponseToUniqueEvent;
|
||||||
import org.toop.framework.eventbus.events.UniqueEvent;
|
import org.toop.framework.eventbus.events.UniqueEvent;
|
||||||
import org.toop.framework.eventbus.bus.EventBus;
|
import org.toop.framework.eventbus.bus.EventBus;
|
||||||
import org.toop.framework.eventbus.subscriber.DefaultSubscriber;
|
import org.toop.framework.eventbus.subscriber.DefaultNamedSubscriber;
|
||||||
|
import org.toop.framework.eventbus.subscriber.NamedSubscriber;
|
||||||
import org.toop.framework.eventbus.subscriber.Subscriber;
|
import org.toop.framework.eventbus.subscriber.Subscriber;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -43,7 +44,7 @@ public class EventFlow {
|
|||||||
private EventType event = null;
|
private EventType event = null;
|
||||||
|
|
||||||
/** The listener returned by GlobalEventBus subscription. Used for unsubscription. */
|
/** The listener returned by GlobalEventBus subscription. Used for unsubscription. */
|
||||||
private final List<Subscriber<?, ?>> listeners = new ArrayList<>();
|
private final List<NamedSubscriber<?>> listeners = new ArrayList<>();
|
||||||
|
|
||||||
/** Holds the results returned from the subscribed event, if any. */
|
/** Holds the results returned from the subscribed event, if any. */
|
||||||
private Map<String, ?> result = null;
|
private Map<String, ?> result = null;
|
||||||
@@ -161,7 +162,7 @@ public class EventFlow {
|
|||||||
this.result = eventClass.result();
|
this.result = eventClass.result();
|
||||||
};
|
};
|
||||||
|
|
||||||
var subscriber = new DefaultSubscriber<>(
|
var subscriber = new DefaultNamedSubscriber<>(
|
||||||
name,
|
name,
|
||||||
event,
|
event,
|
||||||
newAction
|
newAction
|
||||||
@@ -248,7 +249,7 @@ public class EventFlow {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var listener = new DefaultSubscriber<>(
|
var listener = new DefaultNamedSubscriber<>(
|
||||||
name,
|
name,
|
||||||
(Class<TT>) action.getClass().getDeclaredMethods()[0].getParameterTypes()[0],
|
(Class<TT>) action.getClass().getDeclaredMethods()[0].getParameterTypes()[0],
|
||||||
newAction
|
newAction
|
||||||
@@ -295,7 +296,7 @@ public class EventFlow {
|
|||||||
if (unsubscribeAfterSuccess) unsubscribe(String.valueOf(id));
|
if (unsubscribeAfterSuccess) unsubscribe(String.valueOf(id));
|
||||||
};
|
};
|
||||||
|
|
||||||
var listener = new DefaultSubscriber<>(
|
var listener = new DefaultNamedSubscriber<>(
|
||||||
name,
|
name,
|
||||||
event,
|
event,
|
||||||
newAction
|
newAction
|
||||||
@@ -378,7 +379,7 @@ public class EventFlow {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var listener = new DefaultSubscriber<>(
|
var listener = new DefaultNamedSubscriber<>(
|
||||||
name,
|
name,
|
||||||
eventClass,
|
eventClass,
|
||||||
newAction
|
newAction
|
||||||
@@ -496,7 +497,7 @@ public class EventFlow {
|
|||||||
*
|
*
|
||||||
* @return Copy of the list of listeners.
|
* @return Copy of the list of listeners.
|
||||||
*/
|
*/
|
||||||
public Subscriber<?, ?>[] getListeners() {
|
public Subscriber<?>[] getListeners() {
|
||||||
return listeners.toArray(new Subscriber[0]);
|
return listeners.toArray(new Subscriber[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package org.toop.framework.eventbus;
|
|||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.toop.framework.eventbus.bus.DisruptorEventBus;
|
import org.toop.framework.eventbus.bus.DisruptorEventBus;
|
||||||
import org.toop.framework.eventbus.bus.EventBus;
|
import org.toop.framework.eventbus.bus.EventBus;
|
||||||
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
import org.toop.framework.eventbus.store.DefaultSubscriberStore;
|
import org.toop.framework.eventbus.store.DefaultSubscriberStore;
|
||||||
import org.toop.framework.eventbus.subscriber.Subscriber;
|
import org.toop.framework.eventbus.subscriber.Subscriber;
|
||||||
|
|
||||||
@@ -19,17 +20,17 @@ public class GlobalEventBus implements EventBus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void subscribe(Subscriber<?, ?> listener) {
|
public void subscribe(Subscriber<? extends EventType> listener) {
|
||||||
INSTANCE.subscribe(listener);
|
INSTANCE.subscribe(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void unsubscribe(Subscriber<?, ?> listener) {
|
public void unsubscribe(Subscriber<? extends EventType> listener) {
|
||||||
INSTANCE.unsubscribe(listener);
|
INSTANCE.unsubscribe(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> void post(T event) {
|
public <T extends EventType> void post(T event) {
|
||||||
INSTANCE.post(event);
|
INSTANCE.post(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,22 +17,22 @@ public class DefaultEventBus implements EventBus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void subscribe(Subscriber<?, ?> subscriber) {
|
public void subscribe(Subscriber<? extends EventType> subscriber) {
|
||||||
eventsHolder.add(subscriber);
|
eventsHolder.add(subscriber);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void unsubscribe(Subscriber<?, ?> subscriber) {
|
public void unsubscribe(Subscriber<? extends EventType> subscriber) {
|
||||||
eventsHolder.remove(subscriber);
|
eventsHolder.remove(subscriber);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <T> void post(T event) {
|
public <T extends EventType> void post(T event) {
|
||||||
Class<T> eventType = (Class<T>) event.getClass();
|
Class<T> eventType = (Class<T>) event.getClass();
|
||||||
var subs = eventsHolder.get(eventType);
|
var subs = eventsHolder.get(eventType);
|
||||||
if (subs != null) {
|
if (subs != null) {
|
||||||
for (Subscriber<?, ?> subscriber : subs) {
|
for (Subscriber<?> subscriber : subs) {
|
||||||
Class<T> eventClass = (Class<T>) subscriber.event();
|
Class<T> eventClass = (Class<T>) subscriber.event();
|
||||||
Consumer<EventType> action = (Consumer<EventType>) subscriber.handler();
|
Consumer<EventType> action = (Consumer<EventType>) subscriber.handler();
|
||||||
|
|
||||||
|
|||||||
@@ -21,8 +21,8 @@ public class DisruptorEventBus implements EventBus {
|
|||||||
private final Logger logger;
|
private final Logger logger;
|
||||||
private final SubscriberStore eventsHolder;
|
private final SubscriberStore eventsHolder;
|
||||||
|
|
||||||
private final Disruptor<EventHolder<?>> disruptor;
|
private final Disruptor<EventHolder<? extends EventType>> disruptor;
|
||||||
private final RingBuffer<EventHolder<?>> ringBuffer;
|
private final RingBuffer<EventHolder<? extends EventType>> ringBuffer;
|
||||||
|
|
||||||
public DisruptorEventBus(Logger logger, SubscriberStore eventsHolder) {
|
public DisruptorEventBus(Logger logger, SubscriberStore eventsHolder) {
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
@@ -41,9 +41,9 @@ public class DisruptorEventBus implements EventBus {
|
|||||||
this.ringBuffer = disruptor.getRingBuffer();
|
this.ringBuffer = disruptor.getRingBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Disruptor<EventHolder<?>> getEventHolderDisruptor(ThreadFactory threadFactory) {
|
private Disruptor<EventHolder<? extends EventType>> getEventHolderDisruptor(ThreadFactory threadFactory) {
|
||||||
int RING_BUFFER_SIZE = 1024 * 64;
|
int RING_BUFFER_SIZE = 1024 * 64;
|
||||||
Disruptor<EventHolder<?>> disruptor = new Disruptor<>(
|
Disruptor<EventHolder<? extends EventType>> disruptor = new Disruptor<>(
|
||||||
EventHolder::new,
|
EventHolder::new,
|
||||||
RING_BUFFER_SIZE,
|
RING_BUFFER_SIZE,
|
||||||
threadFactory,
|
threadFactory,
|
||||||
@@ -61,17 +61,17 @@ public class DisruptorEventBus implements EventBus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void subscribe(Subscriber<?, ?> listener) {
|
public void subscribe(Subscriber<? extends EventType> listener) {
|
||||||
eventsHolder.add(listener);
|
eventsHolder.add(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void unsubscribe(Subscriber<?, ?> listener) {
|
public void unsubscribe(Subscriber<? extends EventType> listener) {
|
||||||
eventsHolder.remove(listener);
|
eventsHolder.remove(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> void post(T event) {
|
public <T extends EventType> void post(T event) {
|
||||||
long seq = ringBuffer.next();
|
long seq = ringBuffer.next();
|
||||||
try {
|
try {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@@ -93,10 +93,10 @@ public class DisruptorEventBus implements EventBus {
|
|||||||
eventsHolder.reset();
|
eventsHolder.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> void dispatchEvent(T event) {
|
private <T extends EventType> void dispatchEvent(T event) {
|
||||||
var classListeners = eventsHolder.get(event.getClass());
|
var classListeners = eventsHolder.get(event.getClass());
|
||||||
if (classListeners != null) {
|
if (classListeners != null) {
|
||||||
for (Subscriber<?, ?> listener : classListeners) {
|
for (Subscriber<?> listener : classListeners) {
|
||||||
try {
|
try {
|
||||||
callListener(listener, event);
|
callListener(listener, event);
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
@@ -108,7 +108,7 @@ public class DisruptorEventBus implements EventBus {
|
|||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private <T> void callListener(Subscriber<?, ?> subscriber, T event) {
|
private <T> void callListener(Subscriber<?> subscriber, T event) {
|
||||||
Class<T> eventClass = (Class<T>) subscriber.event();
|
Class<T> eventClass = (Class<T>) subscriber.event();
|
||||||
Consumer<EventType> action = (Consumer<EventType>) subscriber.handler();
|
Consumer<EventType> action = (Consumer<EventType>) subscriber.handler();
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
package org.toop.framework.eventbus.bus;
|
package org.toop.framework.eventbus.bus;
|
||||||
|
|
||||||
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
import org.toop.framework.eventbus.subscriber.Subscriber;
|
import org.toop.framework.eventbus.subscriber.Subscriber;
|
||||||
|
|
||||||
public interface EventBus {
|
public interface EventBus {
|
||||||
void subscribe(Subscriber<?, ?> subscriber);
|
void subscribe(Subscriber<? extends EventType> subscriber);
|
||||||
void unsubscribe(Subscriber<?, ?> subscriber);
|
void unsubscribe(Subscriber<? extends EventType> subscriber);
|
||||||
<T> void post(T event);
|
<T extends EventType> void post(T event);
|
||||||
void shutdown();
|
void shutdown();
|
||||||
void reset();
|
void reset();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +1,24 @@
|
|||||||
package org.toop.framework.eventbus.store;
|
package org.toop.framework.eventbus.store;
|
||||||
|
|
||||||
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
import org.toop.framework.eventbus.subscriber.Subscriber;
|
import org.toop.framework.eventbus.subscriber.Subscriber;
|
||||||
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
|
||||||
public class AsyncSubscriberStore implements SubscriberStore {
|
public class AsyncSubscriberStore implements SubscriberStore {
|
||||||
private final ConcurrentHashMap<Class<?>, ConcurrentLinkedQueue<Subscriber<?, ?>>> queues = new ConcurrentHashMap<>();
|
private final ConcurrentHashMap<Class<? extends EventType>, ConcurrentLinkedQueue<Subscriber<? extends EventType>>> queues = new ConcurrentHashMap<>();
|
||||||
private final ConcurrentHashMap<Class<?>, Subscriber<?, ?>[]> snapshots = new ConcurrentHashMap<>();
|
private final ConcurrentHashMap<Class<? extends EventType>, Subscriber<? extends EventType>[]> snapshots = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void add(Subscriber<?, ?> sub) {
|
public void add(Subscriber<? extends EventType> sub) {
|
||||||
queues.computeIfAbsent(sub.event(), _ -> new ConcurrentLinkedQueue<>()).add(sub);
|
queues.computeIfAbsent(sub.event(), _ -> new ConcurrentLinkedQueue<>()).add(sub);
|
||||||
rebuildSnapshot(sub.event());
|
rebuildSnapshot(sub.event());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove(Subscriber<?, ?> sub) {
|
public void remove(Subscriber<? extends EventType> sub) {
|
||||||
ConcurrentLinkedQueue<Subscriber<?, ?>> queue = queues.get(sub.event());
|
ConcurrentLinkedQueue<Subscriber<?>> queue = queues.get(sub.event());
|
||||||
if (queue != null) {
|
if (queue != null) {
|
||||||
queue.remove(sub);
|
queue.remove(sub);
|
||||||
rebuildSnapshot(sub.event());
|
rebuildSnapshot(sub.event());
|
||||||
@@ -25,8 +26,8 @@ public class AsyncSubscriberStore implements SubscriberStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Subscriber<?, ?>[] get(Class<?> event) {
|
public Subscriber<? extends EventType>[] get(Class<? extends EventType> event) {
|
||||||
return snapshots.getOrDefault(event, new Subscriber[0]);
|
return snapshots.getOrDefault(event, new Subscriber<?>[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -35,12 +36,12 @@ public class AsyncSubscriberStore implements SubscriberStore {
|
|||||||
snapshots.clear();
|
snapshots.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void rebuildSnapshot(Class<?> event) {
|
private void rebuildSnapshot(Class<? extends EventType> event) {
|
||||||
ConcurrentLinkedQueue<Subscriber<?, ?>> queue = queues.get(event);
|
ConcurrentLinkedQueue<Subscriber<?>> queue = queues.get(event);
|
||||||
if (queue != null) {
|
if (queue != null) {
|
||||||
snapshots.put(event, queue.toArray(new Subscriber[0]));
|
snapshots.put(event, queue.toArray(new Subscriber<?>[0]));
|
||||||
} else {
|
} else {
|
||||||
snapshots.put(event, new Subscriber[0]);
|
snapshots.put(event, new Subscriber<?>[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,25 +1,27 @@
|
|||||||
package org.toop.framework.eventbus.store;
|
package org.toop.framework.eventbus.store;
|
||||||
|
|
||||||
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
|
import org.toop.framework.eventbus.subscriber.NamedSubscriber;
|
||||||
import org.toop.framework.eventbus.subscriber.Subscriber;
|
import org.toop.framework.eventbus.subscriber.Subscriber;
|
||||||
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
public class DefaultSubscriberStore implements SubscriberStore {
|
public class DefaultSubscriberStore implements SubscriberStore {
|
||||||
|
|
||||||
private static final Subscriber<?, ?>[] EMPTY = new Subscriber[0];
|
private static final Subscriber<? extends EventType>[] EMPTY = new Subscriber<?>[0];
|
||||||
|
|
||||||
private final ConcurrentHashMap<Class<?>, Subscriber<?, ?>[]> listeners =
|
private final ConcurrentHashMap<Class<? extends EventType>, Subscriber<? extends EventType>[]>
|
||||||
new ConcurrentHashMap<>();
|
listeners = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void add(Subscriber<?, ?> sub) {
|
public void add(Subscriber<? extends EventType> sub) {
|
||||||
listeners.compute(sub.event(), (_, arr) -> {
|
listeners.compute(sub.event(), (_, arr) -> {
|
||||||
if (arr == null || arr.length == 0) {
|
if (arr == null || arr.length == 0) {
|
||||||
return new Subscriber<?, ?>[]{sub};
|
return new Subscriber<?>[]{sub};
|
||||||
}
|
}
|
||||||
|
|
||||||
int len = arr.length;
|
int len = arr.length;
|
||||||
Subscriber<?, ?>[] newArr = new Subscriber[len + 1];
|
Subscriber<?>[] newArr = new Subscriber[len + 1];
|
||||||
System.arraycopy(arr, 0, newArr, 0, len);
|
System.arraycopy(arr, 0, newArr, 0, len);
|
||||||
newArr[len] = sub;
|
newArr[len] = sub;
|
||||||
return newArr;
|
return newArr;
|
||||||
@@ -27,7 +29,7 @@ public class DefaultSubscriberStore implements SubscriberStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove(Subscriber<?, ?> sub) {
|
public void remove(Subscriber<? extends EventType> sub) {
|
||||||
listeners.computeIfPresent(sub.event(), (_, arr) -> {
|
listeners.computeIfPresent(sub.event(), (_, arr) -> {
|
||||||
int len = arr.length;
|
int len = arr.length;
|
||||||
|
|
||||||
@@ -36,7 +38,7 @@ public class DefaultSubscriberStore implements SubscriberStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int keep = 0;
|
int keep = 0;
|
||||||
for (Subscriber<?, ?> s : arr) {
|
for (Subscriber<?> s : arr) {
|
||||||
if (!s.equals(sub)) keep++;
|
if (!s.equals(sub)) keep++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,9 +49,9 @@ public class DefaultSubscriberStore implements SubscriberStore {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Subscriber<?, ?>[] newArr = new Subscriber[keep];
|
Subscriber<?>[] newArr = new Subscriber[keep];
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (Subscriber<?, ?> s : arr) {
|
for (Subscriber<?> s : arr) {
|
||||||
if (!s.equals(sub)) {
|
if (!s.equals(sub)) {
|
||||||
newArr[i++] = s;
|
newArr[i++] = s;
|
||||||
}
|
}
|
||||||
@@ -60,7 +62,7 @@ public class DefaultSubscriberStore implements SubscriberStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Subscriber<?, ?>[] get(Class<?> event) {
|
public Subscriber<? extends EventType>[] get(Class<? extends EventType> event) {
|
||||||
return listeners.getOrDefault(event, EMPTY);
|
return listeners.getOrDefault(event, EMPTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
package org.toop.framework.eventbus.store;
|
package org.toop.framework.eventbus.store;
|
||||||
|
|
||||||
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
import org.toop.framework.eventbus.subscriber.Subscriber;
|
import org.toop.framework.eventbus.subscriber.Subscriber;
|
||||||
|
|
||||||
public interface SubscriberStore {
|
public interface SubscriberStore {
|
||||||
void add(Subscriber<?, ?> subscriber);
|
void add(Subscriber<? extends EventType> subscriber);
|
||||||
void remove(Subscriber<?, ?> subscriber);
|
void remove(Subscriber<? extends EventType> subscriber);
|
||||||
Subscriber<?, ?>[] get(Class<?> event);
|
Subscriber<? extends EventType>[] get(Class<? extends EventType> event);
|
||||||
void reset();
|
void reset();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package org.toop.framework.eventbus.store;
|
package org.toop.framework.eventbus.store;
|
||||||
|
|
||||||
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
import org.toop.framework.eventbus.subscriber.Subscriber;
|
import org.toop.framework.eventbus.subscriber.Subscriber;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -8,23 +9,23 @@ import java.util.Map;
|
|||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
public class SyncSubscriberStore implements SubscriberStore {
|
public class SyncSubscriberStore implements SubscriberStore {
|
||||||
private final Map<Class<?>, List<Subscriber<?, ?>>> LISTENERS = new ConcurrentHashMap<>();
|
private final Map<Class<? extends EventType>, List<Subscriber<? extends EventType>>> LISTENERS = new ConcurrentHashMap<>();
|
||||||
private static final Subscriber<?, ?>[] EMPTY = new Subscriber[0];
|
private static final Subscriber<? extends EventType>[] EMPTY = new Subscriber<?>[0];
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void add(Subscriber<?, ?> sub) {
|
public void add(Subscriber<? extends EventType> sub) {
|
||||||
LISTENERS.computeIfAbsent(sub.event(), _ -> new ArrayList<>()).add(sub);
|
LISTENERS.computeIfAbsent(sub.event(), _ -> new ArrayList<>()).add(sub);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove(Subscriber<?, ?> sub) {
|
public void remove(Subscriber<? extends EventType> sub) {
|
||||||
LISTENERS.getOrDefault(sub.event(), new ArrayList<>()).remove(sub);
|
LISTENERS.getOrDefault(sub.event(), new ArrayList<>()).remove(sub);
|
||||||
LISTENERS.entrySet().removeIf(entry -> entry.getValue().isEmpty());
|
LISTENERS.entrySet().removeIf(entry -> entry.getValue().isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Subscriber<?, ?>[] get(Class<?> event) {
|
public Subscriber<? extends EventType>[] get(Class<? extends EventType> event) {
|
||||||
List<Subscriber<?, ?>> list = LISTENERS.get(event);
|
List<Subscriber<? extends EventType>> list = LISTENERS.get(event);
|
||||||
if (list == null || list.isEmpty()) return EMPTY;
|
if (list == null || list.isEmpty()) return EMPTY;
|
||||||
return list.toArray(EMPTY);
|
return list.toArray(EMPTY);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package org.toop.framework.eventbus.subscriber;
|
||||||
|
|
||||||
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
public record DefaultNamedSubscriber<K extends EventType>(String id, Class<K> event, Consumer<K> handler)
|
||||||
|
implements NamedSubscriber<K> {}
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
package org.toop.framework.eventbus.subscriber;
|
package org.toop.framework.eventbus.subscriber;
|
||||||
|
|
||||||
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public record DefaultSubscriber<K>(String id, Class<K> event, Consumer<K> handler) implements NamedSubscriber<K> {}
|
public record DefaultSubscriber<K extends EventType>(Class<K> event, Consumer<K> handler) implements Subscriber<K> {}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package org.toop.framework.eventbus.subscriber;
|
||||||
|
|
||||||
|
public interface HasId<ID> {
|
||||||
|
ID id();
|
||||||
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
package org.toop.framework.eventbus.subscriber;
|
package org.toop.framework.eventbus.subscriber;
|
||||||
|
|
||||||
public interface IdSubscriber<T> extends Subscriber<Long, T> {}
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
|
|
||||||
|
public interface IdSubscriber<K extends EventType> extends Subscriber<K>, HasId<Long> {}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
package org.toop.framework.eventbus.subscriber;
|
package org.toop.framework.eventbus.subscriber;
|
||||||
|
|
||||||
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public record LongIdSubscriber<K>(Long id, Class<K> event, Consumer<K> handler) implements IdSubscriber<K> {}
|
public record LongIdSubscriber<K extends EventType>(Long id, Class<K> event, Consumer<K> handler)
|
||||||
|
implements IdSubscriber<K> {}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
package org.toop.framework.eventbus.subscriber;
|
package org.toop.framework.eventbus.subscriber;
|
||||||
|
|
||||||
public interface NamedSubscriber<T> extends Subscriber<String, T> {}
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
|
|
||||||
|
public interface NamedSubscriber<K extends EventType> extends Subscriber<K>, HasId<String> {}
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
package org.toop.framework.eventbus.subscriber;
|
package org.toop.framework.eventbus.subscriber;
|
||||||
|
|
||||||
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public interface Subscriber<ID, K> {
|
public interface Subscriber<K extends EventType> {
|
||||||
ID id();
|
|
||||||
Class<K> event();
|
Class<K> event();
|
||||||
Consumer<K> handler();
|
Consumer<K> handler();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,46 +5,66 @@ import org.apache.logging.log4j.Logger;
|
|||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract class representing a player in a game.
|
* Base class for players in a turn-based game.
|
||||||
* <p>
|
*
|
||||||
* Players are entities that can make moves based on the current state of a game.
|
* @param <T> the game type
|
||||||
* player types, such as human players or AI players.
|
|
||||||
* </p>
|
|
||||||
* <p>
|
|
||||||
* Subclasses should override the {@link #getMove(GameR)} method to provide
|
|
||||||
* specific move logic.
|
|
||||||
* </p>
|
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractPlayer<T extends TurnBasedGame<T>> implements Player<T> {
|
public abstract class AbstractPlayer<T extends TurnBasedGame<T>> implements Player<T> {
|
||||||
private final Logger logger = LogManager.getLogger(this.getClass());
|
|
||||||
|
|
||||||
|
private final Logger logger = LogManager.getLogger(this.getClass());
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new player with the given name.
|
||||||
|
*
|
||||||
|
* @param name the player name
|
||||||
|
*/
|
||||||
protected AbstractPlayer(String name) {
|
protected AbstractPlayer(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a copy of another player.
|
||||||
|
*
|
||||||
|
* @param other the player to copy
|
||||||
|
*/
|
||||||
protected AbstractPlayer(AbstractPlayer<T> other) {
|
protected AbstractPlayer(AbstractPlayer<T> other) {
|
||||||
this.name = other.name;
|
this.name = other.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determines the next move based on the provided game state.
|
* Gets the player's move for the given game state.
|
||||||
|
* A deep copy is provided so the player cannot modify the real state.
|
||||||
* <p>
|
* <p>
|
||||||
* The default implementation throws an {@link UnsupportedOperationException},
|
* This method uses the Template Method Pattern: it defines the fixed
|
||||||
* indicating that concrete subclasses must override this method to provide
|
* algorithm and delegates the variable part to {@link #determineMove(T)}.
|
||||||
* actual move logic.
|
|
||||||
* </p>
|
|
||||||
*
|
*
|
||||||
* @param gameCopy a snapshot of the current game state
|
* @param game the current game
|
||||||
* @return an integer representing the chosen move
|
* @return the chosen move
|
||||||
* @throws UnsupportedOperationException if the method is not overridden
|
|
||||||
*/
|
*/
|
||||||
public long getMove(T gameCopy) {
|
public final long getMove(T game) {
|
||||||
logger.error("Method getMove not implemented.");
|
return determineMove(game.deepCopy());
|
||||||
throw new UnsupportedOperationException("Not supported yet.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName(){
|
|
||||||
|
/**
|
||||||
|
* Determines the player's move using a safe copy of the game.
|
||||||
|
* <p>
|
||||||
|
* This method is called by {@link #getMove(T)} and should contain
|
||||||
|
* the player's strategy for choosing a move.
|
||||||
|
*
|
||||||
|
* @param gameCopy a deep copy of the game
|
||||||
|
* @return the chosen move
|
||||||
|
*/
|
||||||
|
protected abstract long determineMove(T gameCopy);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the player's name.
|
||||||
|
*
|
||||||
|
* @return the name
|
||||||
|
*/
|
||||||
|
public String getName() {
|
||||||
return this.name;
|
return this.name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
10
game/pom.xml
10
game/pom.xml
@@ -105,6 +105,16 @@
|
|||||||
<version>0.1</version>
|
<version>0.1</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.deeplearning4j</groupId>
|
||||||
|
<artifactId>deeplearning4j-core</artifactId>
|
||||||
|
<version>1.0.0-M2.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.nd4j</groupId>
|
||||||
|
<artifactId>nd4j-native-platform</artifactId>
|
||||||
|
<version>1.0.0-M2.1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
@@ -167,4 +167,13 @@ public class BitboardReversi extends BitboardGame<BitboardReversi> {
|
|||||||
private long shift(long bit, int shift, long mask) {
|
private long shift(long bit, int shift, long mask) {
|
||||||
return shift > 0 ? (bit << shift) & mask : (bit >>> -shift) & mask;
|
return shift > 0 ? (bit << shift) & mask : (bit >>> -shift) & mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isGameOver(){
|
||||||
|
BitboardReversi copy = this.deepCopy();
|
||||||
|
if (copy.getLegalMoves() == 0){
|
||||||
|
nextTurn();
|
||||||
|
return copy.getLegalMoves() == 0;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,228 @@
|
|||||||
|
package org.toop.game.machinelearning;
|
||||||
|
|
||||||
|
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;
|
||||||
|
import org.deeplearning4j.nn.conf.NeuralNetConfiguration;
|
||||||
|
import org.deeplearning4j.nn.conf.layers.DenseLayer;
|
||||||
|
import org.deeplearning4j.nn.conf.layers.OutputLayer;
|
||||||
|
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
|
||||||
|
import org.deeplearning4j.nn.weights.WeightInit;
|
||||||
|
import org.deeplearning4j.util.ModelSerializer;
|
||||||
|
import org.nd4j.linalg.activations.Activation;
|
||||||
|
import org.nd4j.linalg.api.ndarray.INDArray;
|
||||||
|
import org.nd4j.linalg.dataset.DataSet;
|
||||||
|
import org.nd4j.linalg.factory.Nd4j;
|
||||||
|
import org.nd4j.linalg.learning.config.Adam;
|
||||||
|
import org.nd4j.linalg.lossfunctions.LossFunctions;
|
||||||
|
import org.toop.framework.gameFramework.GameState;
|
||||||
|
import org.toop.framework.gameFramework.model.game.PlayResult;
|
||||||
|
import org.toop.framework.gameFramework.model.player.AbstractAI;
|
||||||
|
import org.toop.framework.gameFramework.model.player.Player;
|
||||||
|
import org.toop.game.games.reversi.BitboardReversi;
|
||||||
|
import org.toop.game.players.ArtificialPlayer;
|
||||||
|
import org.toop.game.players.ai.MiniMaxAI;
|
||||||
|
import org.toop.game.players.ai.RandomAI;
|
||||||
|
import org.toop.game.players.ai.ReversiAIML;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static java.lang.Math.abs;
|
||||||
|
import static java.lang.Math.random;
|
||||||
|
|
||||||
|
public class NeuralNetwork {
|
||||||
|
|
||||||
|
private MultiLayerConfiguration conf;
|
||||||
|
private MultiLayerNetwork model;
|
||||||
|
private AbstractAI<BitboardReversi> opponentAI;
|
||||||
|
private AbstractAI<BitboardReversi> opponentMM = new MiniMaxAI<>(6);
|
||||||
|
private AbstractAI<BitboardReversi> opponentRand = new RandomAI<>();
|
||||||
|
private AbstractAI<BitboardReversi> opponentAIML = new ReversiAIML<>();
|
||||||
|
private Player[] playerSet = new Player[4];
|
||||||
|
|
||||||
|
|
||||||
|
public NeuralNetwork() {}
|
||||||
|
|
||||||
|
public void init(){
|
||||||
|
initPlayers();
|
||||||
|
|
||||||
|
conf = new NeuralNetConfiguration.Builder()
|
||||||
|
.updater(new Adam(0.001))
|
||||||
|
.weightInit(WeightInit.XAVIER) //todo understand
|
||||||
|
.list()
|
||||||
|
.layer(new DenseLayer.Builder()
|
||||||
|
.nIn(64)
|
||||||
|
.nOut(128)
|
||||||
|
.activation(Activation.RELU)
|
||||||
|
.build())
|
||||||
|
.layer(new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
|
||||||
|
.nIn(128)
|
||||||
|
.nOut(64)
|
||||||
|
.activation(Activation.SOFTMAX)
|
||||||
|
.build())
|
||||||
|
.build();
|
||||||
|
model = new MultiLayerNetwork(conf);
|
||||||
|
IO.println(model.params());
|
||||||
|
loadModel();
|
||||||
|
IO.println(model.params());
|
||||||
|
model.init();
|
||||||
|
IO.println(model.summary());
|
||||||
|
|
||||||
|
model.setLearningRate(0.0003);
|
||||||
|
trainingLoop();
|
||||||
|
saveModel();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initPlayers(){
|
||||||
|
playerSet[0] = new ArtificialPlayer<>(new MiniMaxAI<BitboardReversi>(6),"MiniMaxAI");
|
||||||
|
playerSet[1] = new ArtificialPlayer<>(new RandomAI<BitboardReversi>(),"RandomAI");
|
||||||
|
playerSet[2] = new ArtificialPlayer<>(new ReversiAIML<BitboardReversi>(),"MachineLearningAI");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void saveModel(){
|
||||||
|
File modelFile = new File("reversi-model.zip");
|
||||||
|
try {
|
||||||
|
ModelSerializer.writeModel(model, modelFile, true);
|
||||||
|
}catch (Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void loadModel(){
|
||||||
|
File modelFile = new File("reversi-model.zip");
|
||||||
|
try {
|
||||||
|
model = ModelSerializer.restoreMultiLayerNetwork(modelFile);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void trainingLoop(){
|
||||||
|
int totalGames = 5000;
|
||||||
|
double epsilon = 0.05;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
long start = System.nanoTime();
|
||||||
|
|
||||||
|
for (int game = 0; game<totalGames; game++){
|
||||||
|
char modelPlayer = random()<0.5?'B':'W';
|
||||||
|
BitboardReversi reversi = new BitboardReversi(new Player[2]);
|
||||||
|
opponentAI = getOpponentAI();
|
||||||
|
List<StateAction> gameHistory = new ArrayList<>();
|
||||||
|
PlayResult state = new PlayResult(GameState.NORMAL,reversi.getCurrentTurn());
|
||||||
|
|
||||||
|
double reward = 0;
|
||||||
|
|
||||||
|
while (state.state() != GameState.DRAW && state.state() != GameState.WIN){
|
||||||
|
int curr = reversi.getCurrentTurn();
|
||||||
|
long move;
|
||||||
|
if (curr == modelPlayer) {
|
||||||
|
long[] input = reversi.getBoard();
|
||||||
|
if (Math.random() < epsilon) {
|
||||||
|
long moves = reversi.getLegalMoves();
|
||||||
|
move = (long) (Math.random() * Long.bitCount(moves) - .5f);
|
||||||
|
} else {
|
||||||
|
INDArray boardInput = Nd4j.create(new long[][]{input});
|
||||||
|
INDArray prediction = model.output(boardInput);
|
||||||
|
|
||||||
|
int location = pickLegalMove(prediction, reversi);
|
||||||
|
gameHistory.add(new StateAction(input, location));
|
||||||
|
move = location;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
move = opponentAI.getMove(reversi);
|
||||||
|
}
|
||||||
|
state = reversi.play(move);
|
||||||
|
}
|
||||||
|
|
||||||
|
//IO.println(model.params());
|
||||||
|
BitboardReversi.Score score = reversi.getScore();
|
||||||
|
int scoreDif = abs(score.black() - score.white());
|
||||||
|
if (score.black() > score.white()){
|
||||||
|
reward = 1 + ((scoreDif / 64.0) * 0.5);
|
||||||
|
}else if (score.black() < score.white()){
|
||||||
|
reward = -1 - ((scoreDif / 64.0) * 0.5);
|
||||||
|
}else{
|
||||||
|
reward = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (modelPlayer == 'W'){
|
||||||
|
reward = -reward;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
for (StateAction step : gameHistory){
|
||||||
|
trainFromHistory(step, reward);
|
||||||
|
}
|
||||||
|
|
||||||
|
//IO.println("Wr: " + (double)p1wins/(game+1) + " draws: " + draws);
|
||||||
|
if(game % 100 == 0){
|
||||||
|
IO.println("Completed game " + game + " | Reward: " + reward);
|
||||||
|
//IO.println(Arrays.toString(reversi.getBoardDouble()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
long end = System.nanoTime();
|
||||||
|
IO.println((end-start));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private int pickLegalMove(INDArray prediction, BitboardReversi reversi) {
|
||||||
|
double[] logits = prediction.toDoubleVector();
|
||||||
|
long legalMoves = reversi.getLegalMoves();
|
||||||
|
|
||||||
|
if (legalMoves == 0L) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Math.random() < 0.01) {
|
||||||
|
int randomIndex = (int) (Math.random() * Long.bitCount(legalMoves));
|
||||||
|
long moves = legalMoves;
|
||||||
|
for (int i = 0; i < randomIndex; i++) {
|
||||||
|
moves &= moves - 1;
|
||||||
|
}
|
||||||
|
return Long.numberOfTrailingZeros(moves);
|
||||||
|
}
|
||||||
|
|
||||||
|
int bestMove = -1;
|
||||||
|
double bestVal = Double.NEGATIVE_INFINITY;
|
||||||
|
|
||||||
|
long moves = legalMoves;
|
||||||
|
while (moves != 0L) {
|
||||||
|
int move = Long.numberOfTrailingZeros(moves);
|
||||||
|
double value = logits[move];
|
||||||
|
|
||||||
|
if (value > bestVal) {
|
||||||
|
bestVal = value;
|
||||||
|
bestMove = move;
|
||||||
|
}
|
||||||
|
|
||||||
|
moves &= moves - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return bestMove;
|
||||||
|
}
|
||||||
|
|
||||||
|
private AbstractAI<BitboardReversi> getOpponentAI(){
|
||||||
|
return switch ((int) (Math.random() * 4)) {
|
||||||
|
case 0 -> opponentRand;
|
||||||
|
case 1 -> opponentMM;
|
||||||
|
case 2 -> opponentAIML;
|
||||||
|
default -> opponentRand;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private void trainFromHistory(StateAction step, double reward){
|
||||||
|
double[] output = new double[64];
|
||||||
|
output[step.action] = reward;
|
||||||
|
|
||||||
|
DataSet ds = new DataSet(
|
||||||
|
Nd4j.create(new long[][] { step.state }),
|
||||||
|
Nd4j.create(new double[][] { output })
|
||||||
|
);
|
||||||
|
|
||||||
|
model.fit(ds);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package org.toop.game.machinelearning;
|
||||||
|
|
||||||
|
public class StateAction {
|
||||||
|
long[] state;
|
||||||
|
int action;
|
||||||
|
public StateAction(long[] state, int action) {
|
||||||
|
this.state = state;
|
||||||
|
this.action = action;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,52 +4,52 @@ import org.toop.framework.gameFramework.model.player.*;
|
|||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a player controlled by an AI in a game.
|
* Represents a player controlled by an AI.
|
||||||
* <p>
|
|
||||||
* This player uses an {@link AbstractAI} instance to determine its moves. The generic
|
|
||||||
* parameter {@code T} specifies the type of {@link GameR} the AI can handle.
|
|
||||||
* </p>
|
|
||||||
*
|
*
|
||||||
* @param <T> the specific type of game this AI player can play
|
* @param <T> the type of turn-based game
|
||||||
*/
|
*/
|
||||||
public class ArtificialPlayer<T extends TurnBasedGame<T>> extends AbstractPlayer<T> {
|
public class ArtificialPlayer<T extends TurnBasedGame<T>> extends AbstractPlayer<T> {
|
||||||
|
|
||||||
/** The AI instance used to calculate moves. */
|
|
||||||
private final AI<T> ai;
|
private final AI<T> ai;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new ArtificialPlayer using the specified AI.
|
* Creates a new AI-controlled player.
|
||||||
*
|
*
|
||||||
* @param ai the AI instance that determines moves for this player
|
* @param ai the AI controlling this player
|
||||||
|
* @param name the player's name
|
||||||
*/
|
*/
|
||||||
public ArtificialPlayer(AI<T> ai, String name) {
|
public ArtificialPlayer(AI<T> ai, String name) {
|
||||||
super(name);
|
super(name);
|
||||||
this.ai = ai;
|
this.ai = ai;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a copy of another AI-controlled player.
|
||||||
|
*
|
||||||
|
* @param other the player to copy
|
||||||
|
*/
|
||||||
public ArtificialPlayer(ArtificialPlayer<T> other) {
|
public ArtificialPlayer(ArtificialPlayer<T> other) {
|
||||||
super(other);
|
super(other);
|
||||||
this.ai = other.ai.deepCopy();
|
this.ai = other.ai.deepCopy();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determines the next move for this player using its AI.
|
* Determines the player's move using the AI.
|
||||||
* <p>
|
|
||||||
* This method overrides {@link AbstractPlayer#getMove(GameR)}. Because the AI is
|
|
||||||
* typed to {@code T}, a runtime cast is required. It is the caller's
|
|
||||||
* responsibility to ensure that {@code gameCopy} is of type {@code T}.
|
|
||||||
* </p>
|
|
||||||
*
|
*
|
||||||
* @param gameCopy a copy of the current game state
|
* @param gameCopy a copy of the current game
|
||||||
* @return the integer representing the chosen move
|
* @return the move chosen by the AI
|
||||||
* @throws ClassCastException if {@code gameCopy} is not of type {@code T}
|
|
||||||
*/
|
*/
|
||||||
public long getMove(T gameCopy) {
|
protected long determineMove(T gameCopy) {
|
||||||
return ai.getMove(gameCopy);
|
return ai.getMove(gameCopy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a deep copy of this AI player.
|
||||||
|
*
|
||||||
|
* @return a copy of this player
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public ArtificialPlayer<T> deepCopy() {
|
public ArtificialPlayer<T> deepCopy() {
|
||||||
return new ArtificialPlayer<T>(this);
|
return new ArtificialPlayer<>(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,85 +2,86 @@ package org.toop.game.players;
|
|||||||
|
|
||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
import org.toop.framework.gameFramework.model.player.AbstractPlayer;
|
import org.toop.framework.gameFramework.model.player.AbstractPlayer;
|
||||||
import org.toop.framework.gameFramework.model.player.Player;
|
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a local player who provides moves manually.
|
||||||
|
*
|
||||||
|
* @param <T> the type of turn-based game
|
||||||
|
*/
|
||||||
public class LocalPlayer<T extends TurnBasedGame<T>> extends AbstractPlayer<T> {
|
public class LocalPlayer<T extends TurnBasedGame<T>> extends AbstractPlayer<T> {
|
||||||
// Future can be used with event system, IF unsubscribeAfterSuccess works...
|
|
||||||
// private CompletableFuture<Integer> LastMove = new CompletableFuture<>();
|
|
||||||
|
|
||||||
private CompletableFuture<Long> LastMove;
|
private CompletableFuture<Long> LastMove = new CompletableFuture<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new local player with the given name.
|
||||||
|
*
|
||||||
|
* @param name the player's name
|
||||||
|
*/
|
||||||
public LocalPlayer(String name) {
|
public LocalPlayer(String name) {
|
||||||
super(name);
|
super(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a copy of another local player.
|
||||||
|
*
|
||||||
|
* @param other the player to copy
|
||||||
|
*/
|
||||||
public LocalPlayer(LocalPlayer<T> other) {
|
public LocalPlayer(LocalPlayer<T> other) {
|
||||||
super(other);
|
super(other);
|
||||||
|
this.LastMove = other.LastMove;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Waits for and returns the player's next legal move.
|
||||||
|
*
|
||||||
|
* @param gameCopy a copy of the current game
|
||||||
|
* @return the chosen move
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public long getMove(T gameCopy) {
|
protected long determineMove(T gameCopy) {
|
||||||
return getValidMove(gameCopy);
|
long legalMoves = gameCopy.getLegalMoves();
|
||||||
|
long move;
|
||||||
|
|
||||||
|
do {
|
||||||
|
move = getLastMove();
|
||||||
|
} while ((legalMoves & move) == 0);
|
||||||
|
|
||||||
|
return move;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMove(long move) {
|
/**
|
||||||
|
* Sets the player's last move.
|
||||||
|
*
|
||||||
|
* @param move the move to set
|
||||||
|
*/
|
||||||
|
public void setLastMove(long move) {
|
||||||
LastMove.complete(move);
|
LastMove.complete(move);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: helper function, would like to replace to get rid of this method
|
/**
|
||||||
public static boolean contains(int[] array, int value){
|
* Waits for the next move from the player.
|
||||||
for (int i : array) if (i == value) return true;
|
*
|
||||||
return false;
|
* @return the chosen move or 0 if interrupted
|
||||||
}
|
*/
|
||||||
|
private long getLastMove() {
|
||||||
private long getMove2(T gameCopy) {
|
LastMove = new CompletableFuture<>(); // Reset the future
|
||||||
LastMove = new CompletableFuture<>();
|
|
||||||
long move = 0;
|
|
||||||
try {
|
try {
|
||||||
move = LastMove.get();
|
return LastMove.get();
|
||||||
System.out.println(Long.toBinaryString(move));
|
} catch (ExecutionException | InterruptedException e) {
|
||||||
} catch (InterruptedException | ExecutionException e) {
|
return 0;
|
||||||
// TODO: Add proper logging.
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
return move;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected long getValidMove(T gameCopy){
|
|
||||||
// Get this player's valid moves
|
|
||||||
long validMoves = gameCopy.getLegalMoves();
|
|
||||||
// Make sure provided move is valid
|
|
||||||
// TODO: Limit amount of retries?
|
|
||||||
// TODO: Stop copying game so many times
|
|
||||||
long move = getMove2(gameCopy.deepCopy());
|
|
||||||
while ((validMoves & move) == 0) {
|
|
||||||
System.out.println("Not a valid move, try again");
|
|
||||||
move = getMove2(gameCopy.deepCopy());
|
|
||||||
}
|
|
||||||
return move;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a deep copy of this local player.
|
||||||
|
*
|
||||||
|
* @return a copy of this player
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public LocalPlayer<T> deepCopy() {
|
public LocalPlayer<T> deepCopy() {
|
||||||
return new LocalPlayer<T>(this.getName());
|
return new LocalPlayer<>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*public void register() {
|
|
||||||
// Listening to PlayerAttemptedMove
|
|
||||||
new EventFlow().listen(GUIEvents.PlayerAttemptedMove.class, event -> {
|
|
||||||
if (!LastMove.isDone()) {
|
|
||||||
LastMove.complete(event.move()); // complete the future
|
|
||||||
}
|
|
||||||
}, true); // auto-unsubscribe
|
|
||||||
}
|
|
||||||
|
|
||||||
// This blocks until the next move arrives
|
|
||||||
public int take() throws ExecutionException, InterruptedException {
|
|
||||||
int move = LastMove.get(); // blocking
|
|
||||||
LastMove = new CompletableFuture<>(); // reset for next move
|
|
||||||
return move;
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,30 +5,45 @@ import org.toop.framework.gameFramework.model.player.AbstractPlayer;
|
|||||||
import org.toop.framework.gameFramework.model.player.Player;
|
import org.toop.framework.gameFramework.model.player.Player;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a player controlled remotely or over a network.
|
* Represents a player that participates online.
|
||||||
* <p>
|
*
|
||||||
* This class extends {@link AbstractPlayer} and can be used to implement game logic
|
* @param <T> the type of turn-based game
|
||||||
* where moves are provided by an external source (e.g., another user or a server).
|
|
||||||
* Currently, this class is a placeholder and does not implement move logic.
|
|
||||||
* </p>
|
|
||||||
*/
|
*/
|
||||||
public class OnlinePlayer<T extends TurnBasedGame<T>> extends AbstractPlayer<T> {
|
public class OnlinePlayer<T extends TurnBasedGame<T>> extends AbstractPlayer<T> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new OnlinePlayer.
|
* Creates a new online player with the given name.
|
||||||
* <p>
|
*
|
||||||
* Currently, no additional initialization is performed. Subclasses or
|
* @param name the name of the player
|
||||||
* future implementations should provide mechanisms to receive moves from
|
|
||||||
* an external source.
|
|
||||||
*/
|
*/
|
||||||
public OnlinePlayer(String name) {
|
public OnlinePlayer(String name) {
|
||||||
super(name);
|
super(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a copy of another online player.
|
||||||
|
*
|
||||||
|
* @param other the player to copy
|
||||||
|
*/
|
||||||
public OnlinePlayer(OnlinePlayer<T> other) {
|
public OnlinePlayer(OnlinePlayer<T> other) {
|
||||||
super(other);
|
super(other);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
* <p>
|
||||||
|
* This method is not supported for online players.
|
||||||
|
*
|
||||||
|
* @throws UnsupportedOperationException always
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected long determineMove(T gameCopy) {
|
||||||
|
throw new UnsupportedOperationException("An online player does not support determining move");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Player<T> deepCopy() {
|
public Player<T> deepCopy() {
|
||||||
return new OnlinePlayer<>(this);
|
return new OnlinePlayer<>(this);
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.game.players;
|
package org.toop.game.players.ai;
|
||||||
|
|
||||||
import org.toop.framework.gameFramework.GameState;
|
import org.toop.framework.gameFramework.GameState;
|
||||||
import org.toop.framework.gameFramework.model.game.PlayResult;
|
import org.toop.framework.gameFramework.model.game.PlayResult;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.game.players;
|
package org.toop.game.players.ai;
|
||||||
|
|
||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
import org.toop.framework.gameFramework.model.player.AbstractAI;
|
import org.toop.framework.gameFramework.model.player.AbstractAI;
|
||||||
80
game/src/main/java/org/toop/game/players/ai/ReversiAIML.java
Normal file
80
game/src/main/java/org/toop/game/players/ai/ReversiAIML.java
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
package org.toop.game.players.ai;
|
||||||
|
|
||||||
|
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
|
||||||
|
import org.deeplearning4j.util.ModelSerializer;
|
||||||
|
import org.nd4j.linalg.api.ndarray.INDArray;
|
||||||
|
import org.nd4j.linalg.factory.Nd4j;
|
||||||
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
|
import org.toop.framework.gameFramework.model.player.AI;
|
||||||
|
import org.toop.framework.gameFramework.model.player.AbstractAI;
|
||||||
|
import org.toop.game.games.reversi.BitboardReversi;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import static java.lang.Math.random;
|
||||||
|
|
||||||
|
public class ReversiAIML<T extends TurnBasedGame<T>> extends AbstractAI<T> {
|
||||||
|
|
||||||
|
MultiLayerNetwork model;
|
||||||
|
|
||||||
|
public ReversiAIML() {
|
||||||
|
InputStream is = getClass().getResourceAsStream("/reversi-model.zip");
|
||||||
|
try {
|
||||||
|
assert is != null;
|
||||||
|
model = ModelSerializer.restoreMultiLayerNetwork(is);
|
||||||
|
} catch (IOException e) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int pickLegalMove(INDArray prediction, BitboardReversi reversi) {
|
||||||
|
double[] logits = prediction.toDoubleVector();
|
||||||
|
long legalMoves = reversi.getLegalMoves();
|
||||||
|
|
||||||
|
if (legalMoves == 0L) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Math.random() < 0.01) {
|
||||||
|
int randomIndex = (int) (Math.random() * Long.bitCount(legalMoves));
|
||||||
|
long moves = legalMoves;
|
||||||
|
for (int i = 0; i < randomIndex; i++) {
|
||||||
|
moves &= moves - 1;
|
||||||
|
}
|
||||||
|
return Long.numberOfTrailingZeros(moves);
|
||||||
|
}
|
||||||
|
|
||||||
|
int bestMove = -1;
|
||||||
|
double bestVal = Double.NEGATIVE_INFINITY;
|
||||||
|
|
||||||
|
long moves = legalMoves;
|
||||||
|
while (moves != 0L) {
|
||||||
|
int move = Long.numberOfTrailingZeros(moves);
|
||||||
|
double value = logits[move];
|
||||||
|
|
||||||
|
if (value > bestVal) {
|
||||||
|
bestVal = value;
|
||||||
|
bestMove = move;
|
||||||
|
}
|
||||||
|
|
||||||
|
moves &= moves - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return bestMove;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getMove(T game) {
|
||||||
|
long[] input = game.getBoard();
|
||||||
|
|
||||||
|
INDArray boardInput = Nd4j.create(new long[][] { input });
|
||||||
|
INDArray prediction = model.output(boardInput);
|
||||||
|
|
||||||
|
int move = pickLegalMove(prediction,(BitboardReversi) game);
|
||||||
|
return move;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ReversiAIML<T> deepCopy() {
|
||||||
|
return new ReversiAIML();
|
||||||
|
}
|
||||||
|
}
|
||||||
272
game/src/test/java/org/toop/game/tictactoe/ReversiTest.java
Normal file
272
game/src/test/java/org/toop/game/tictactoe/ReversiTest.java
Normal file
@@ -0,0 +1,272 @@
|
|||||||
|
/*//todo fix this mess
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
package org.toop.game.tictactoe;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.toop.framework.gameFramework.model.player.AbstractAI;
|
||||||
|
import org.toop.framework.gameFramework.model.player.Player;
|
||||||
|
import org.toop.game.AI;
|
||||||
|
import org.toop.game.enumerators.GameState;
|
||||||
|
import org.toop.game.games.reversi.ReversiAIR;
|
||||||
|
import org.toop.game.games.reversi.ReversiR;
|
||||||
|
import org.toop.game.records.Move;
|
||||||
|
import org.toop.game.reversi.Reversi;
|
||||||
|
import org.toop.game.reversi.ReversiAI;
|
||||||
|
import org.toop.game.players.ai.ReversiAIML;
|
||||||
|
import org.toop.game.games.reversi.ReversiAISimple;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class ReversiTest {
|
||||||
|
private ReversiR game;
|
||||||
|
private ReversiAIR ai;
|
||||||
|
private ReversiAIML aiml;
|
||||||
|
private ReversiAISimple aiSimple;
|
||||||
|
private AbstractAI<ReversiR> player1;
|
||||||
|
private AbstractAI<ReversiR> player2;
|
||||||
|
private Player[] players = new Player[2];
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setup() {
|
||||||
|
game = new ReversiR(players);
|
||||||
|
ai = new ReversiAIR();
|
||||||
|
aiml = new ReversiAIML();
|
||||||
|
aiSimple = new ReversiAISimple();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCorrectStartPiecesPlaced() {
|
||||||
|
assertNotNull(game);
|
||||||
|
assertEquals('W', game.getBoard()[27]);
|
||||||
|
assertEquals('B', game.getBoard()[28]);
|
||||||
|
assertEquals('B', game.getBoard()[35]);
|
||||||
|
assertEquals('W', game.getBoard()[36]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetLegalMovesAtStart() {
|
||||||
|
Move[] moves = game.getLegalMoves();
|
||||||
|
List<Move> expectedMoves = List.of(
|
||||||
|
new Move(19, 'B'),
|
||||||
|
new Move(26, 'B'),
|
||||||
|
new Move(37, 'B'),
|
||||||
|
new Move(44, 'B')
|
||||||
|
);
|
||||||
|
assertNotNull(moves);
|
||||||
|
assertTrue(moves.length > 0);
|
||||||
|
assertMovesMatchIgnoreOrder(expectedMoves, Arrays.asList(moves));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertMovesMatchIgnoreOrder(List<Move> expected, List<Move> actual) {
|
||||||
|
assertEquals(expected.size(), actual.size());
|
||||||
|
for (int i = 0; i < expected.size(); i++) {
|
||||||
|
assertTrue(actual.contains(expected.get(i)));
|
||||||
|
assertTrue(expected.contains(actual.get(i)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testMakeValidMoveFlipsPieces() {
|
||||||
|
game.play(new Move(19, 'B'));
|
||||||
|
assertEquals('B', game.getBoard()[19]);
|
||||||
|
assertEquals('B', game.getBoard()[27], "Piece should have flipped to B");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testMakeInvalidMoveDoesNothing() {
|
||||||
|
char[] before = game.getBoard().clone();
|
||||||
|
game.play(new Move(0, 'B'));
|
||||||
|
assertArrayEquals(before, game.getBoard(), "Board should not change on invalid move");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testTurnSwitchesAfterValidMove() {
|
||||||
|
char current = game.getCurrentPlayer();
|
||||||
|
game.play(game.getLegalMoves()[0]);
|
||||||
|
assertNotEquals(current, game.getCurrentPlayer(), "Player turn should switch after a valid move");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCountScoreCorrectlyAtStart() {
|
||||||
|
long start = System.nanoTime();
|
||||||
|
Reversi.Score score = game.getScore();
|
||||||
|
assertEquals(2, score.player1Score()); // Black
|
||||||
|
assertEquals(2, score.player2Score()); // White
|
||||||
|
long end = System.nanoTime();
|
||||||
|
IO.println((end - start));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void zLegalMovesInCertainPosition() {
|
||||||
|
game.play(new Move(19, 'B'));
|
||||||
|
game.play(new Move(20, 'W'));
|
||||||
|
Move[] moves = game.getLegalMoves();
|
||||||
|
List<Move> expectedMoves = List.of(
|
||||||
|
new Move(13, 'B'),
|
||||||
|
new Move(21, 'B'),
|
||||||
|
new Move(29, 'B'),
|
||||||
|
new Move(37, 'B'),
|
||||||
|
new Move(45, 'B'));
|
||||||
|
assertNotNull(moves);
|
||||||
|
assertTrue(moves.length > 0);
|
||||||
|
IO.println(Arrays.toString(moves));
|
||||||
|
assertMovesMatchIgnoreOrder(expectedMoves, Arrays.asList(moves));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCountScoreCorrectlyAtEnd() {
|
||||||
|
for (int i = 0; i < 1; i++) {
|
||||||
|
game = new Reversi();
|
||||||
|
Move[] legalMoves = game.getLegalMoves();
|
||||||
|
while (legalMoves.length > 0) {
|
||||||
|
game.play(legalMoves[(int) (Math.random() * legalMoves.length)]);
|
||||||
|
legalMoves = game.getLegalMoves();
|
||||||
|
}
|
||||||
|
Reversi.Score score = game.getScore();
|
||||||
|
IO.println(score.player1Score());
|
||||||
|
IO.println(score.player2Score());
|
||||||
|
|
||||||
|
for (int r = 0; r < game.getRowSize(); r++) {
|
||||||
|
char[] row = Arrays.copyOfRange(game.getBoard(), r * game.getColumnSize(), (r + 1) * game.getColumnSize());
|
||||||
|
IO.println(Arrays.toString(row));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testPlayerMustSkipTurnIfNoValidMoves() {
|
||||||
|
game.play(new Move(19, 'B'));
|
||||||
|
game.play(new Move(34, 'W'));
|
||||||
|
game.play(new Move(45, 'B'));
|
||||||
|
game.play(new Move(11, 'W'));
|
||||||
|
game.play(new Move(42, 'B'));
|
||||||
|
game.play(new Move(54, 'W'));
|
||||||
|
game.play(new Move(37, 'B'));
|
||||||
|
game.play(new Move(46, 'W'));
|
||||||
|
game.play(new Move(63, 'B'));
|
||||||
|
game.play(new Move(62, 'W'));
|
||||||
|
game.play(new Move(29, 'B'));
|
||||||
|
game.play(new Move(50, 'W'));
|
||||||
|
game.play(new Move(55, 'B'));
|
||||||
|
game.play(new Move(30, 'W'));
|
||||||
|
game.play(new Move(53, 'B'));
|
||||||
|
game.play(new Move(38, 'W'));
|
||||||
|
game.play(new Move(61, 'B'));
|
||||||
|
game.play(new Move(52, 'W'));
|
||||||
|
game.play(new Move(51, 'B'));
|
||||||
|
game.play(new Move(60, 'W'));
|
||||||
|
game.play(new Move(59, 'B'));
|
||||||
|
assertEquals('B', game.getCurrentPlayer());
|
||||||
|
game.play(ai.findBestMove(game, 5));
|
||||||
|
game.play(ai.findBestMove(game, 5));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGameShouldEndIfNoValidMoves() {
|
||||||
|
//European Grand Prix Ghent 2017: Replay Hassan - Verstuyft J. (3-17)
|
||||||
|
game.play(new Move(19, 'B'));
|
||||||
|
game.play(new Move(20, 'W'));
|
||||||
|
game.play(new Move(29, 'B'));
|
||||||
|
game.play(new Move(22, 'W'));
|
||||||
|
game.play(new Move(21, 'B'));
|
||||||
|
game.play(new Move(34, 'W'));
|
||||||
|
game.play(new Move(23, 'B'));
|
||||||
|
game.play(new Move(13, 'W'));
|
||||||
|
game.play(new Move(26, 'B'));
|
||||||
|
game.play(new Move(18, 'W'));
|
||||||
|
game.play(new Move(12, 'B'));
|
||||||
|
game.play(new Move(4, 'W'));
|
||||||
|
game.play(new Move(17, 'B'));
|
||||||
|
game.play(new Move(31, 'W'));
|
||||||
|
GameState stateTurn15 = game.play(new Move(39, 'B'));
|
||||||
|
assertEquals(GameState.NORMAL, stateTurn15);
|
||||||
|
GameState stateTurn16 = game.play(new Move(16, 'W'));
|
||||||
|
assertEquals(GameState.WIN, stateTurn16);
|
||||||
|
GameState stateTurn17 = game.play(new Move(5, 'B'));
|
||||||
|
assertNull(stateTurn17);
|
||||||
|
Reversi.Score score = game.getScore();
|
||||||
|
assertEquals(3, score.player1Score());
|
||||||
|
assertEquals(17, score.player2Score());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testAISelectsLegalMove() {
|
||||||
|
Move move = ai.findBestMove(game, 4);
|
||||||
|
assertNotNull(move);
|
||||||
|
assertTrue(containsMove(game.getLegalMoves(), move), "AI should always choose a legal move");
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean containsMove(Move[] moves, Move move) {
|
||||||
|
for (Move m : moves) {
|
||||||
|
if (m.equals(move)) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testAis() {
|
||||||
|
player1 = aiml;
|
||||||
|
player2 = ai;
|
||||||
|
testAIvsAIML();
|
||||||
|
player2 = aiSimple;
|
||||||
|
testAIvsAIML();
|
||||||
|
player1 = ai;
|
||||||
|
testAIvsAIML();
|
||||||
|
player2 = aiml;
|
||||||
|
testAIvsAIML();
|
||||||
|
player1 = aiml;
|
||||||
|
testAIvsAIML();
|
||||||
|
player1 = aiSimple;
|
||||||
|
testAIvsAIML();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testAIvsAIML() {
|
||||||
|
if(player1 == null || player2 == null) {
|
||||||
|
player1 = aiml;
|
||||||
|
player2 = ai;
|
||||||
|
}
|
||||||
|
int totalGames = 2000;
|
||||||
|
IO.println("Testing... " + player1.getClass().getSimpleName() + " vs " + player2.getClass().getSimpleName() + " for " + totalGames + " games");
|
||||||
|
int p1wins = 0;
|
||||||
|
int p2wins = 0;
|
||||||
|
int draws = 0;
|
||||||
|
List<Integer> moves = new ArrayList<>();
|
||||||
|
for (int i = 0; i < totalGames; i++) {
|
||||||
|
game = new ReversiR();
|
||||||
|
while (!game.isGameOver()) {
|
||||||
|
char curr = game.getCurrentPlayer();
|
||||||
|
Move move;
|
||||||
|
if (curr == 'B') {
|
||||||
|
move = player1.findBestMove(game, 5);
|
||||||
|
} else {
|
||||||
|
move = player2.findBestMove(game, 5);
|
||||||
|
}
|
||||||
|
if (i%500 == 0) moves.add(move.position());
|
||||||
|
game.play(move);
|
||||||
|
}
|
||||||
|
if (i%500 == 0) {
|
||||||
|
IO.println(moves);
|
||||||
|
moves.clear();
|
||||||
|
}
|
||||||
|
int winner = game.getWinner();
|
||||||
|
if (winner == 1) {
|
||||||
|
p1wins++;
|
||||||
|
} else if (winner == 2) {
|
||||||
|
p2wins++;
|
||||||
|
} else {
|
||||||
|
draws++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
IO.println("p1 winrate: " + p1wins + "/" + totalGames + " = " + (double) p1wins / totalGames + "\np2wins: " + p2wins + " draws: " + draws);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*/
|
||||||
78
game/src/test/java/org/toop/game/tictactoe/TestReversi.java
Normal file
78
game/src/test/java/org/toop/game/tictactoe/TestReversi.java
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
package org.toop.game.tictactoe;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.toop.framework.gameFramework.model.player.Player;
|
||||||
|
import org.toop.game.games.reversi.BitboardReversi;
|
||||||
|
import org.toop.game.players.ArtificialPlayer;
|
||||||
|
import org.toop.game.players.ai.MiniMaxAI;
|
||||||
|
import org.toop.game.players.ai.RandomAI;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class TestReversi {
|
||||||
|
private BitboardReversi game;
|
||||||
|
private Player[] players;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setup(){
|
||||||
|
players = new Player[2];
|
||||||
|
players[0] = new ArtificialPlayer<BitboardReversi>(new RandomAI<BitboardReversi>(),"randomAI");
|
||||||
|
players[1] = new ArtificialPlayer<BitboardReversi>(new MiniMaxAI<BitboardReversi>(10),"miniMaxAI");
|
||||||
|
game = new BitboardReversi(players);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCorrectStartPiecesPlaced() {
|
||||||
|
assertNotNull(game);
|
||||||
|
long[] board = game.getBoard();
|
||||||
|
IO.println(Long.toBinaryString(board[0]));
|
||||||
|
IO.println(Long.toBinaryString(board[1]));
|
||||||
|
long black = board[0];
|
||||||
|
long white = board[1];
|
||||||
|
assertEquals(1L, ((white >>> 27) & 1L)); //checks if the 27-shifted long has a 1 bit
|
||||||
|
assertEquals(1L, ((black >>> 28) & 1L));
|
||||||
|
assertEquals(1L, ((black >>> 35) & 1L));
|
||||||
|
assertEquals(1L, ((white >>> 36) & 1L));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testPlayGames(){
|
||||||
|
int totalGames = 1;
|
||||||
|
long start = System.nanoTime();
|
||||||
|
long midtime = System.nanoTime();
|
||||||
|
int p1wins = 0;
|
||||||
|
int p2wins = 0;
|
||||||
|
int draws = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < totalGames; i++){
|
||||||
|
game = new BitboardReversi(players);
|
||||||
|
while(!game.isGameOver()){
|
||||||
|
midtime = System.nanoTime();
|
||||||
|
int currentTurn = game.getCurrentTurn();
|
||||||
|
long move = players[currentTurn].getMove(game.deepCopy());
|
||||||
|
game.play(move);
|
||||||
|
IO.println(System.nanoTime() - midtime);
|
||||||
|
}
|
||||||
|
switch (game.getWinner()){
|
||||||
|
case 0:
|
||||||
|
p1wins++;
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
p2wins++;
|
||||||
|
break;
|
||||||
|
case -1:
|
||||||
|
draws++;
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println(System.nanoTime() - start);
|
||||||
|
IO.println(p1wins + " " + p2wins + " " + draws);
|
||||||
|
assertEquals(totalGames, p1wins + p2wins + draws);
|
||||||
|
IO.println("p1 wr: " + p1wins + "/" + totalGames + " = " + (double) p1wins / totalGames);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user