mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 19:04:49 +00:00
Compare commits
1 Commits
TimerSongD
...
7e913ff50f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7e913ff50f |
@@ -2,6 +2,7 @@ package org.toop;
|
||||
|
||||
import org.toop.app.App;
|
||||
import org.toop.framework.audio.*;
|
||||
import org.toop.framework.machinelearning.NeuralNetwork;
|
||||
import org.toop.framework.networking.NetworkingClientEventListener;
|
||||
import org.toop.framework.networking.NetworkingClientManager;
|
||||
import org.toop.framework.resource.ResourceLoader;
|
||||
@@ -35,6 +36,10 @@ public final class Main {
|
||||
).initListeners("medium-button-click.wav");
|
||||
|
||||
}).start();
|
||||
|
||||
|
||||
//NeuralNetwork nn = new NeuralNetwork();
|
||||
//nn.init();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import org.toop.game.reversi.ReversiAI;
|
||||
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.paint.Color;
|
||||
import org.toop.game.reversi.ReversiAIML;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
@@ -30,7 +31,7 @@ public final class ReversiGame {
|
||||
private final BlockingQueue<Move> moveQueue;
|
||||
|
||||
private final Reversi game;
|
||||
private final ReversiAI ai;
|
||||
private final ReversiAIML ai;
|
||||
|
||||
private final GameView primary;
|
||||
private final ReversiCanvas canvas;
|
||||
@@ -46,7 +47,7 @@ public final class ReversiGame {
|
||||
moveQueue = new LinkedBlockingQueue<>();
|
||||
|
||||
game = new Reversi();
|
||||
ai = new ReversiAI();
|
||||
ai = new ReversiAIML();
|
||||
|
||||
isRunning = new AtomicBoolean(true);
|
||||
isPaused = new AtomicBoolean(false);
|
||||
@@ -324,7 +325,7 @@ public final class ReversiGame {
|
||||
if (isLegalMove) {
|
||||
moves = game.getFlipsForPotentialMove(
|
||||
new Point(cellEntered%game.getColumnSize(),cellEntered/game.getRowSize()),
|
||||
game.getCurrentPlayer());
|
||||
game.getCurrentPlayer(), game.getCurrentPlayer() == 'B'?'W':'B',game.makeBoardAGrid());
|
||||
}
|
||||
canvas.drawHighlightDots(moves);
|
||||
}
|
||||
|
||||
400
app/src/main/java/org/toop/app/view/View.java
Normal file
400
app/src/main/java/org/toop/app/view/View.java
Normal file
@@ -0,0 +1,400 @@
|
||||
package org.toop.app.view;
|
||||
|
||||
import org.toop.framework.audio.events.AudioEvents;
|
||||
import org.toop.framework.eventbus.EventFlow;
|
||||
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.ComboBox;
|
||||
import javafx.scene.control.ScrollPane;
|
||||
import javafx.scene.control.Separator;
|
||||
import javafx.scene.control.Slider;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.HBox;
|
||||
import javafx.scene.layout.Region;
|
||||
import javafx.scene.layout.StackPane;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.scene.text.Text;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class View {
|
||||
private final boolean mainView;
|
||||
|
||||
private final StackPane view;
|
||||
private final Map<String, Node> nodeMap;
|
||||
|
||||
protected View(boolean mainView, String cssClass) {
|
||||
this.mainView = mainView;
|
||||
|
||||
view = new StackPane();
|
||||
view.getStyleClass().add(cssClass);
|
||||
|
||||
nodeMap = new HashMap<String, Node>();
|
||||
}
|
||||
|
||||
public void add(Pos position, Node node) {
|
||||
assert node != null;
|
||||
|
||||
StackPane.setAlignment(node, position);
|
||||
view.getChildren().add(node);
|
||||
}
|
||||
|
||||
protected Region hspacer() {
|
||||
final Region hspacer = new Region();
|
||||
hspacer.getStyleClass().add("hspacer");
|
||||
|
||||
return hspacer;
|
||||
}
|
||||
|
||||
protected Region vspacer() {
|
||||
final Region vspacer = new Region();
|
||||
vspacer.getStyleClass().add("vspacer");
|
||||
|
||||
return vspacer;
|
||||
}
|
||||
|
||||
protected ScrollPane fit(String identifier, String cssClass, Node node) {
|
||||
assert node != null;
|
||||
|
||||
final ScrollPane fit = new ScrollPane(node);
|
||||
fit.getStyleClass().add(cssClass);
|
||||
|
||||
fit.setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
|
||||
fit.setFitToWidth(true);
|
||||
fit.setFitToHeight(true);
|
||||
|
||||
if (!identifier.isEmpty()) {
|
||||
nodeMap.put(identifier, fit);
|
||||
}
|
||||
|
||||
return fit;
|
||||
}
|
||||
|
||||
protected ScrollPane fit(String identifier, Node node) {
|
||||
return fit(identifier, "fit", node);
|
||||
}
|
||||
|
||||
protected ScrollPane fit(Node node) {
|
||||
return fit("", node);
|
||||
}
|
||||
|
||||
protected HBox hbox(String identifier, String cssClass, Node... nodes) {
|
||||
assert !nodeMap.containsKey(identifier);
|
||||
|
||||
final HBox hbox = new HBox();
|
||||
hbox.getStyleClass().add(cssClass);
|
||||
hbox.setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
|
||||
|
||||
for (final Node node : nodes) {
|
||||
if (node != null) {
|
||||
hbox.getChildren().add(node);
|
||||
}
|
||||
}
|
||||
|
||||
if (!identifier.isEmpty()) {
|
||||
nodeMap.put(identifier, hbox);
|
||||
}
|
||||
|
||||
return hbox;
|
||||
}
|
||||
|
||||
protected HBox hbox(String identifier, Node... nodes) {
|
||||
return hbox(identifier, "container", nodes);
|
||||
}
|
||||
|
||||
protected HBox hbox(Node... nodes) {
|
||||
return hbox("", nodes);
|
||||
}
|
||||
|
||||
protected HBox hboxFill(String identifier, String cssClass, Node... nodes) {
|
||||
final HBox hbox = hbox(identifier, cssClass, nodes);
|
||||
|
||||
for (final Node node : hbox.getChildren()) {
|
||||
if (node instanceof Region) {
|
||||
((Region)node).setMaxHeight(Double.MAX_VALUE);
|
||||
}
|
||||
}
|
||||
|
||||
return hbox;
|
||||
}
|
||||
|
||||
protected HBox hboxFill(String identifier, Node... nodes) {
|
||||
final HBox hbox = hbox(identifier, nodes);
|
||||
|
||||
for (final Node node : hbox.getChildren()) {
|
||||
if (node instanceof Region) {
|
||||
((Region)node).setMaxHeight(Double.MAX_VALUE);
|
||||
}
|
||||
}
|
||||
|
||||
return hbox;
|
||||
}
|
||||
|
||||
protected HBox hboxFill(Node... nodes) {
|
||||
final HBox hbox = hbox(nodes);
|
||||
|
||||
for (final Node node : hbox.getChildren()) {
|
||||
if (node instanceof Region) {
|
||||
((Region)node).setMaxHeight(Double.MAX_VALUE);
|
||||
}
|
||||
}
|
||||
|
||||
return hbox;
|
||||
}
|
||||
|
||||
protected VBox vbox(String identifier, String cssClass, Node... nodes) {
|
||||
assert !nodeMap.containsKey(identifier);
|
||||
|
||||
final VBox vbox = new VBox();
|
||||
vbox.getStyleClass().add(cssClass);
|
||||
vbox.setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
|
||||
|
||||
for (final Node node : nodes) {
|
||||
if (node != null) {
|
||||
vbox.getChildren().add(node);
|
||||
}
|
||||
}
|
||||
|
||||
if (!identifier.isEmpty()) {
|
||||
nodeMap.put(identifier, vbox);
|
||||
}
|
||||
|
||||
return vbox;
|
||||
}
|
||||
|
||||
protected VBox vbox(String identifier, Node... nodes) {
|
||||
return vbox(identifier, "container", nodes);
|
||||
}
|
||||
|
||||
protected VBox vbox(Node... nodes) {
|
||||
return vbox("", nodes);
|
||||
}
|
||||
|
||||
protected VBox vboxFill(String identifier, String cssClass, Node... nodes) {
|
||||
final VBox vbox = vbox(identifier, cssClass, nodes);
|
||||
|
||||
for (final Node node : vbox.getChildren()) {
|
||||
if (node instanceof Region) {
|
||||
((Region)node).setMaxWidth(Double.MAX_VALUE);
|
||||
}
|
||||
}
|
||||
|
||||
return vbox;
|
||||
}
|
||||
|
||||
protected VBox vboxFill(String identifier, Node... nodes) {
|
||||
final VBox vbox = vbox(identifier, nodes);
|
||||
|
||||
for (final Node node : vbox.getChildren()) {
|
||||
if (node instanceof Region) {
|
||||
((Region)node).setMaxWidth(Double.MAX_VALUE);
|
||||
}
|
||||
}
|
||||
|
||||
return vbox;
|
||||
}
|
||||
|
||||
protected VBox vboxFill(Node... nodes) {
|
||||
final VBox vbox = vbox(nodes);
|
||||
|
||||
for (final Node node : vbox.getChildren()) {
|
||||
if (node instanceof Region) {
|
||||
((Region)node).setMaxWidth(Double.MAX_VALUE);
|
||||
}
|
||||
}
|
||||
|
||||
return vbox;
|
||||
}
|
||||
|
||||
protected Separator separator(String identifier, String cssClass) {
|
||||
assert !nodeMap.containsKey(identifier);
|
||||
|
||||
final Separator separator = new Separator();
|
||||
separator.getStyleClass().add(cssClass);
|
||||
|
||||
if (!identifier.isEmpty()) {
|
||||
nodeMap.put(identifier, separator);
|
||||
}
|
||||
|
||||
return separator;
|
||||
}
|
||||
|
||||
protected Separator separator(String identifier) {
|
||||
return separator(identifier, "separator");
|
||||
}
|
||||
|
||||
protected Separator separator() {
|
||||
return separator("");
|
||||
}
|
||||
|
||||
protected Text header(String identifier, String cssClass) {
|
||||
assert !nodeMap.containsKey(identifier);
|
||||
|
||||
final Text header = new Text();
|
||||
header.getStyleClass().add(cssClass);
|
||||
|
||||
if (!identifier.isEmpty()) {
|
||||
nodeMap.put(identifier, header);
|
||||
}
|
||||
|
||||
return header;
|
||||
}
|
||||
|
||||
protected Text header(String identifier) {
|
||||
return header(identifier, "header");
|
||||
}
|
||||
|
||||
protected Text header() {
|
||||
return header("");
|
||||
}
|
||||
|
||||
protected Text text(String identifier, String cssClass) {
|
||||
assert !nodeMap.containsKey(identifier);
|
||||
|
||||
final Text text = new Text();
|
||||
text.getStyleClass().add(cssClass);
|
||||
|
||||
if (!identifier.isEmpty()) {
|
||||
nodeMap.put(identifier, text);
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
protected Text text(String identifier) {
|
||||
return text(identifier, "text");
|
||||
}
|
||||
|
||||
protected Text text() {
|
||||
return text("");
|
||||
}
|
||||
|
||||
protected Button button(String identifier, String cssClass) {
|
||||
assert !nodeMap.containsKey(identifier);
|
||||
|
||||
final Button button = new Button();
|
||||
button.getStyleClass().add(cssClass);
|
||||
|
||||
button.setOnMouseClicked(_ -> {
|
||||
new EventFlow().addPostEvent(new AudioEvents.ClickButton()).asyncPostEvent();
|
||||
});
|
||||
|
||||
if (!identifier.isEmpty()) {
|
||||
nodeMap.put(identifier, button);
|
||||
}
|
||||
|
||||
return button;
|
||||
}
|
||||
|
||||
protected Button button(String identifier) {
|
||||
return button(identifier, "button");
|
||||
}
|
||||
|
||||
protected Button button() {
|
||||
return button("");
|
||||
}
|
||||
|
||||
protected Slider slider(String identifier, String cssClass) {
|
||||
assert !nodeMap.containsKey(identifier);
|
||||
|
||||
final Slider slider = new Slider();
|
||||
slider.getStyleClass().add(cssClass);
|
||||
|
||||
slider.setMinorTickCount(0);
|
||||
slider.setMajorTickUnit(1);
|
||||
slider.setBlockIncrement(1);
|
||||
|
||||
slider.setSnapToTicks(true);
|
||||
slider.setShowTickLabels(true);
|
||||
|
||||
slider.setOnMouseClicked(_ -> {
|
||||
new EventFlow().addPostEvent(new AudioEvents.ClickButton()).asyncPostEvent();
|
||||
});
|
||||
|
||||
if (!identifier.isEmpty()) {
|
||||
nodeMap.put(identifier, slider);
|
||||
}
|
||||
|
||||
return slider;
|
||||
}
|
||||
|
||||
protected Slider slider(String identifier) {
|
||||
return slider(identifier, "slider");
|
||||
}
|
||||
|
||||
protected Slider slider() {
|
||||
return slider("");
|
||||
}
|
||||
|
||||
protected TextField input(String identifier, String cssClass) {
|
||||
assert !nodeMap.containsKey(identifier);
|
||||
|
||||
final TextField input = new TextField();
|
||||
input.getStyleClass().add(cssClass);
|
||||
|
||||
input.setOnMouseClicked(_ -> {
|
||||
new EventFlow().addPostEvent(new AudioEvents.ClickButton()).asyncPostEvent();
|
||||
});
|
||||
|
||||
if (!identifier.isEmpty()) {
|
||||
nodeMap.put(identifier, input);
|
||||
}
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
protected TextField input(String identifier) {
|
||||
return input(identifier, "input");
|
||||
}
|
||||
|
||||
protected TextField input() {
|
||||
return input("");
|
||||
}
|
||||
|
||||
protected <T> ComboBox<T> combobox(String identifier, String cssClass) {
|
||||
assert !nodeMap.containsKey(identifier);
|
||||
|
||||
final ComboBox<T> combobox = new ComboBox<T>();
|
||||
combobox.getStyleClass().add(cssClass);
|
||||
|
||||
combobox.setOnMouseClicked(_ -> {
|
||||
new EventFlow().addPostEvent(new AudioEvents.ClickButton()).asyncPostEvent();
|
||||
});
|
||||
|
||||
if (!identifier.isEmpty()) {
|
||||
nodeMap.put(identifier, combobox);
|
||||
}
|
||||
|
||||
return combobox;
|
||||
}
|
||||
|
||||
protected <T> ComboBox<T> combobox(String identifier) {
|
||||
return combobox(identifier, "combo-box");
|
||||
}
|
||||
|
||||
protected <T> ComboBox<T> combobox() {
|
||||
return combobox("");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected <T extends Node> T get(String identifier) {
|
||||
assert nodeMap.containsKey(identifier);
|
||||
return (T) nodeMap.get(identifier);
|
||||
}
|
||||
|
||||
protected void clear() {
|
||||
view.getChildren().clear();
|
||||
nodeMap.clear();
|
||||
}
|
||||
|
||||
public boolean isMainView() { return mainView; }
|
||||
public Region getView() { return view; }
|
||||
|
||||
public abstract void setup();
|
||||
public void cleanup() {
|
||||
clear();
|
||||
}
|
||||
}
|
||||
105
app/src/main/java/org/toop/app/view/ViewStack.java
Normal file
105
app/src/main/java/org/toop/app/view/ViewStack.java
Normal file
@@ -0,0 +1,105 @@
|
||||
package org.toop.app.view;
|
||||
|
||||
import javafx.application.Platform;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.layout.StackPane;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
public final class ViewStack {
|
||||
private static boolean setup = false;
|
||||
|
||||
private static StackPane root;
|
||||
|
||||
private static View active;
|
||||
private static Stack<View> stack;
|
||||
|
||||
public static void setup(Scene scene) {
|
||||
assert scene != null;
|
||||
|
||||
if (setup) {
|
||||
return;
|
||||
}
|
||||
|
||||
root = new StackPane();
|
||||
|
||||
active = null;
|
||||
stack = new Stack<View>();
|
||||
|
||||
scene.setRoot(root);
|
||||
|
||||
setup = true;
|
||||
}
|
||||
|
||||
public static void cleanup() {
|
||||
assert setup;
|
||||
|
||||
final var count = stack.size();
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
pop();
|
||||
}
|
||||
|
||||
if (active != null) {
|
||||
active.cleanup();
|
||||
}
|
||||
|
||||
setup = false;
|
||||
}
|
||||
|
||||
public static void reload() {
|
||||
assert setup;
|
||||
|
||||
for (final var view : stack) {
|
||||
view.cleanup();
|
||||
}
|
||||
|
||||
if (active != null) {
|
||||
active.cleanup();
|
||||
active.setup();
|
||||
}
|
||||
|
||||
for (final var view : stack) {
|
||||
view.setup();
|
||||
}
|
||||
}
|
||||
|
||||
public static void push(View view) {
|
||||
assert setup;
|
||||
assert view != null;
|
||||
|
||||
if (view.isMainView()) {
|
||||
Platform.runLater(() -> {
|
||||
if (active != null) {
|
||||
root.getChildren().removeFirst();
|
||||
active.cleanup();
|
||||
}
|
||||
|
||||
root.getChildren().addFirst(view.getView());
|
||||
view.setup();
|
||||
|
||||
active = view;
|
||||
});
|
||||
} else {
|
||||
Platform.runLater(() -> {
|
||||
stack.push(view);
|
||||
root.getChildren().addLast(view.getView());
|
||||
view.setup();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static void pop() {
|
||||
assert setup;
|
||||
|
||||
if (stack.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Platform.runLater(() -> {
|
||||
final var last = stack.pop();
|
||||
root.getChildren().removeLast();
|
||||
last.cleanup();
|
||||
});
|
||||
}
|
||||
}
|
||||
128
app/src/main/java/org/toop/app/view/displays/SongDisplay.java
Normal file
128
app/src/main/java/org/toop/app/view/displays/SongDisplay.java
Normal file
@@ -0,0 +1,128 @@
|
||||
package org.toop.app.view.displays;
|
||||
|
||||
import javafx.application.Platform;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.ProgressBar;
|
||||
import javafx.scene.layout.HBox;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.scene.paint.Color;
|
||||
import org.toop.app.widget.Widget;
|
||||
import org.toop.framework.audio.AudioEventListener;
|
||||
import org.toop.framework.audio.events.AudioEvents;
|
||||
import org.toop.framework.eventbus.EventFlow;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.text.Text;
|
||||
import org.toop.framework.eventbus.GlobalEventBus;
|
||||
|
||||
public class SongDisplay extends VBox implements Widget {
|
||||
|
||||
private final Text songTitle;
|
||||
private final ProgressBar progressBar;
|
||||
private final Text progressText;
|
||||
private boolean paused = false;
|
||||
|
||||
public SongDisplay() {
|
||||
new EventFlow()
|
||||
.listen(this::updateTheSong);
|
||||
|
||||
setAlignment(Pos.CENTER);
|
||||
getStyleClass().add("song-display");
|
||||
|
||||
songTitle = new Text("song playing");
|
||||
songTitle.getStyleClass().add("song-title");
|
||||
|
||||
progressBar = new ProgressBar(0);
|
||||
progressBar.getStyleClass().add("progress-bar");
|
||||
|
||||
progressText = new Text("0:00/0:00");
|
||||
progressText.getStyleClass().add("progress-text");
|
||||
|
||||
Button skipButton = new Button(">>");
|
||||
Button pauseButton = new Button("⏸");
|
||||
Button previousButton = new Button("<<");
|
||||
|
||||
skipButton.getStyleClass().setAll("skip-button");
|
||||
pauseButton.getStyleClass().setAll("pause-button");
|
||||
previousButton.getStyleClass().setAll("previous-button");
|
||||
|
||||
skipButton.setOnAction( event -> {
|
||||
GlobalEventBus.post(new AudioEvents.SkipMusic());
|
||||
paused = false;
|
||||
pauseButton.setText(getPlayString(paused));
|
||||
});
|
||||
|
||||
pauseButton.setOnAction(event -> {
|
||||
GlobalEventBus.post(new AudioEvents.PauseMusic());
|
||||
paused = !paused;
|
||||
pauseButton.setText(getPlayString(paused));
|
||||
});
|
||||
|
||||
previousButton.setOnAction( event -> {
|
||||
GlobalEventBus.post(new AudioEvents.PreviousMusic());
|
||||
paused = false;
|
||||
pauseButton.setText(getPlayString(paused));
|
||||
});
|
||||
|
||||
HBox control = new HBox(10, previousButton, pauseButton, skipButton);
|
||||
control.setAlignment(Pos.CENTER);
|
||||
control.getStyleClass().add("controls");
|
||||
|
||||
getChildren().addAll(songTitle, progressBar, progressText, control);
|
||||
}
|
||||
|
||||
private void updateTheSong(AudioEvents.PlayingMusic event) {
|
||||
Platform.runLater(() -> {
|
||||
String text = event.name();
|
||||
text = text.substring(0, text.length() - 4);
|
||||
songTitle.setText(text);
|
||||
double currentPos = event.currentPosition();
|
||||
double duration = event.duration();
|
||||
if (currentPos / duration > 0.05) {
|
||||
double progress = currentPos / duration;
|
||||
progressBar.setProgress(progress);
|
||||
}
|
||||
else if (currentPos / duration < 0.05) {
|
||||
progressBar.setProgress(0.05);
|
||||
}
|
||||
progressText.setText(getTimeString(event.currentPosition(), event.duration()));
|
||||
});
|
||||
}
|
||||
|
||||
private String getTimeString(long position, long duration) {
|
||||
long positionMinutes = position / 60;
|
||||
long durationMinutes = duration / 60;
|
||||
long positionSeconds = position % 60;
|
||||
long durationSeconds = duration % 60;
|
||||
String positionSecondsStr = String.valueOf(positionSeconds);
|
||||
String durationSecondsStr = String.valueOf(durationSeconds);
|
||||
|
||||
if (positionSeconds < 10) {
|
||||
positionSecondsStr = "0" + positionSeconds;
|
||||
}
|
||||
if (durationSeconds < 10) {
|
||||
durationSecondsStr = "0" + durationSeconds;
|
||||
}
|
||||
|
||||
String time = positionMinutes + ":" + positionSecondsStr + " / " + durationMinutes + ":" + durationSecondsStr;
|
||||
return time;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Node getNode() {
|
||||
return this;
|
||||
}
|
||||
private String getPlayString(boolean paused) {
|
||||
if (paused) {
|
||||
return "▶";
|
||||
}
|
||||
else {
|
||||
return "⏸";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
127
app/src/main/java/org/toop/app/view/views/ChallengeView.java
Normal file
127
app/src/main/java/org/toop/app/view/views/ChallengeView.java
Normal file
@@ -0,0 +1,127 @@
|
||||
package org.toop.app.view.views;
|
||||
|
||||
import org.toop.app.GameInformation;
|
||||
import org.toop.app.Server;
|
||||
import org.toop.app.view.View;
|
||||
import org.toop.app.view.ViewStack;
|
||||
import org.toop.app.view.displays.SongDisplay;
|
||||
import org.toop.local.AppContext;
|
||||
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Slider;
|
||||
import javafx.scene.text.Text;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public final class ChallengeView extends View {
|
||||
private final GameInformation.Player playerInformation;
|
||||
|
||||
private final String challenger;
|
||||
private final String game;
|
||||
|
||||
private final Consumer<GameInformation.Player> onAccept;
|
||||
|
||||
public ChallengeView(String challenger, String game, Consumer<GameInformation.Player> onAccept) {
|
||||
super(false, "bg-popup");
|
||||
|
||||
playerInformation = new GameInformation.Player();
|
||||
|
||||
this.challenger = challenger;
|
||||
this.game = game;
|
||||
|
||||
this.onAccept = onAccept;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setup() {
|
||||
final Text challengeText = text();
|
||||
challengeText.setText(AppContext.getString("you-were-challenged-by"));
|
||||
|
||||
final Text challengerHeader = header();
|
||||
challengerHeader.setText(challenger);
|
||||
|
||||
final Text gameText = text();
|
||||
gameText.setText(AppContext.getString("to-a-game-of") + " " + game);
|
||||
|
||||
final Button acceptButton = button();
|
||||
acceptButton.setText(AppContext.getString("accept"));
|
||||
acceptButton.setOnAction(_ -> {
|
||||
onAccept.accept(playerInformation);
|
||||
});
|
||||
|
||||
final Button denyButton = button();
|
||||
denyButton.setText(AppContext.getString("deny"));
|
||||
denyButton.setOnAction(_ -> {
|
||||
ViewStack.pop();
|
||||
});
|
||||
|
||||
final List<Node> nodes = new ArrayList<>();
|
||||
|
||||
if (playerInformation.isHuman) {
|
||||
final Button playerToggle = button();
|
||||
playerToggle.setText(AppContext.getString("player"));
|
||||
playerToggle.setOnAction(_ -> {
|
||||
playerInformation.isHuman = false;
|
||||
cleanup();
|
||||
setup();
|
||||
});
|
||||
|
||||
nodes.add(vbox(playerToggle));
|
||||
} else {
|
||||
final Button computerToggle = button();
|
||||
computerToggle.setText(AppContext.getString("computer"));
|
||||
computerToggle.setOnAction(_ -> {
|
||||
playerInformation.isHuman = true;
|
||||
cleanup();
|
||||
setup();
|
||||
});
|
||||
|
||||
nodes.add(vbox(computerToggle));
|
||||
|
||||
final Text computerDifficultyText = text();
|
||||
computerDifficultyText.setText(AppContext.getString("computer-difficulty"));
|
||||
|
||||
final Slider computerDifficultySlider = slider();
|
||||
computerDifficultySlider.setMin(0);
|
||||
computerDifficultySlider.setMax(Server.gameToType(game).getMaxDepth());
|
||||
computerDifficultySlider.setValue(playerInformation.computerDifficulty);
|
||||
computerDifficultySlider.valueProperty().addListener((_, _, newValue) -> {
|
||||
playerInformation.computerDifficulty = newValue.intValue();
|
||||
});
|
||||
|
||||
nodes.add(vbox(computerDifficultyText, computerDifficultySlider));
|
||||
}
|
||||
|
||||
final SongDisplay songdisplay = new SongDisplay();
|
||||
|
||||
|
||||
add(Pos.BOTTOM_RIGHT,
|
||||
fit(vboxFill(
|
||||
songdisplay
|
||||
)));
|
||||
|
||||
add(Pos.CENTER,
|
||||
fit(hboxFill(
|
||||
vboxFill(
|
||||
challengeText,
|
||||
challengerHeader,
|
||||
gameText,
|
||||
separator(),
|
||||
|
||||
hboxFill(
|
||||
acceptButton,
|
||||
denyButton
|
||||
)
|
||||
),
|
||||
|
||||
vboxFill(
|
||||
nodes.toArray(new Node[0])
|
||||
)
|
||||
))
|
||||
);
|
||||
}
|
||||
}
|
||||
110
app/src/main/java/org/toop/app/view/views/CreditsView.java
Normal file
110
app/src/main/java/org/toop/app/view/views/CreditsView.java
Normal file
@@ -0,0 +1,110 @@
|
||||
package org.toop.app.view.views;
|
||||
|
||||
import org.toop.app.App;
|
||||
import org.toop.app.view.View;
|
||||
import org.toop.app.view.ViewStack;
|
||||
import org.toop.app.view.displays.SongDisplay;
|
||||
import org.toop.local.AppContext;
|
||||
|
||||
import javafx.animation.KeyFrame;
|
||||
import javafx.animation.KeyValue;
|
||||
import javafx.animation.Timeline;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.ScrollPane;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.scene.text.Text;
|
||||
import javafx.util.Duration;
|
||||
|
||||
public final class CreditsView extends View {
|
||||
public CreditsView() {
|
||||
super(false, "bg-primary");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setup() {
|
||||
final Text scrumMasterHeader = header();
|
||||
scrumMasterHeader.setText(AppContext.getString("scrum-master") + ": Stef");
|
||||
|
||||
final Text productOwnerHeader = header();
|
||||
productOwnerHeader.setText(AppContext.getString("product-owner") + ": Omar");
|
||||
|
||||
final Text mergeCommanderHeader = header();
|
||||
mergeCommanderHeader.setText(AppContext.getString("merge-commander") + ": Bas");
|
||||
|
||||
final Text localizationHeader = header();
|
||||
localizationHeader.setText(AppContext.getString("localization") + ": Ticho");
|
||||
|
||||
final Text aiHeader = header();
|
||||
aiHeader.setText(AppContext.getString("ai") + ": Michiel");
|
||||
|
||||
final Text developersHeader = header();
|
||||
developersHeader.setText(AppContext.getString("developers") + ": Michiel, Bas, Stef, Omar, Ticho");
|
||||
|
||||
final Text moralSupportHeader = header();
|
||||
moralSupportHeader.setText(AppContext.getString("moral-support") + ": Wesley");
|
||||
|
||||
final Text openglHeader = header();
|
||||
openglHeader.setText(AppContext.getString("opengl") + ": Omar");
|
||||
|
||||
final SongDisplay songdisplay = new SongDisplay();
|
||||
|
||||
|
||||
add(Pos.BOTTOM_RIGHT,
|
||||
fit(vboxFill(
|
||||
songdisplay
|
||||
)));
|
||||
|
||||
add(Pos.CENTER,
|
||||
fit("credits-fit", vboxFill("credits-container", "credits-container",
|
||||
vbox("credits-spacer-top", ""),
|
||||
|
||||
scrumMasterHeader,
|
||||
productOwnerHeader,
|
||||
mergeCommanderHeader,
|
||||
localizationHeader,
|
||||
aiHeader,
|
||||
developersHeader,
|
||||
moralSupportHeader,
|
||||
openglHeader,
|
||||
|
||||
vbox("credits-spacer-bottom", "")
|
||||
))
|
||||
);
|
||||
|
||||
final Button backButton = button();
|
||||
backButton.setText(AppContext.getString("back"));
|
||||
backButton.setOnAction(_ -> { ViewStack.pop(); });
|
||||
|
||||
add(Pos.BOTTOM_LEFT,
|
||||
vboxFill(
|
||||
backButton
|
||||
)
|
||||
);
|
||||
|
||||
playCredits(100, 20);
|
||||
}
|
||||
|
||||
private void playCredits(int lineHeight, int length) {
|
||||
final ScrollPane creditsFit = get("credits-fit");
|
||||
creditsFit.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
|
||||
creditsFit.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
|
||||
|
||||
final VBox creditsContainer = get("credits-container");
|
||||
creditsContainer.setSpacing(lineHeight);
|
||||
|
||||
final VBox creditsSpacerTop = get("credits-spacer-top");
|
||||
creditsSpacerTop.setMinHeight(App.getHeight() - lineHeight);
|
||||
|
||||
final VBox creditsSpacerBottom = get("credits-spacer-bottom");
|
||||
creditsSpacerBottom.setMinHeight(App.getHeight() - lineHeight);
|
||||
|
||||
final Timeline timeline = new Timeline(
|
||||
new KeyFrame(Duration.seconds(0), new KeyValue(creditsFit.vvalueProperty(), 0.0)),
|
||||
new KeyFrame(Duration.seconds(length), new KeyValue(creditsFit.vvalueProperty(), 1.0))
|
||||
);
|
||||
|
||||
timeline.setCycleCount(Timeline.INDEFINITE);
|
||||
timeline.play();
|
||||
}
|
||||
}
|
||||
45
app/src/main/java/org/toop/app/view/views/ErrorView.java
Normal file
45
app/src/main/java/org/toop/app/view/views/ErrorView.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package org.toop.app.view.views;
|
||||
|
||||
import org.toop.app.view.View;
|
||||
import org.toop.app.view.ViewStack;
|
||||
import org.toop.local.AppContext;
|
||||
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.text.Text;
|
||||
|
||||
public final class ErrorView extends View {
|
||||
private final String error;
|
||||
|
||||
public ErrorView(String error) {
|
||||
super(false, "bg-popup");
|
||||
this.error = error;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setup() {
|
||||
final Text errorHeader = header();
|
||||
errorHeader.setText(AppContext.getString("error"));
|
||||
|
||||
final Text errorText = text();
|
||||
errorText.setText(error);
|
||||
|
||||
final Button okButton = button();
|
||||
okButton.setText(AppContext.getString("ok"));
|
||||
okButton.setOnAction(_ -> { ViewStack.pop(); });
|
||||
|
||||
add(Pos.CENTER,
|
||||
vboxFill(
|
||||
errorHeader,
|
||||
separator(),
|
||||
|
||||
vspacer(),
|
||||
errorText,
|
||||
vspacer(),
|
||||
|
||||
separator(),
|
||||
okButton
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
179
app/src/main/java/org/toop/app/view/views/GameView.java
Normal file
179
app/src/main/java/org/toop/app/view/views/GameView.java
Normal file
@@ -0,0 +1,179 @@
|
||||
package org.toop.app.view.views;
|
||||
|
||||
import javafx.application.Platform;
|
||||
import org.toop.app.view.View;
|
||||
import org.toop.app.view.ViewStack;
|
||||
import org.toop.app.view.displays.SongDisplay;
|
||||
import org.toop.local.AppContext;
|
||||
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.ListView;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.text.Text;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public final class GameView extends View {
|
||||
// TODO: This should be it's own file...
|
||||
private static class GameOverView extends View {
|
||||
private final boolean iWon;
|
||||
private final String winner;
|
||||
|
||||
// TODO: Make winner generic, there is no "I won" unless you play online or against bot. Should be a generic "... won" to simplify
|
||||
public GameOverView(boolean iWon, String winner) {
|
||||
super(false, "bg-popup");
|
||||
|
||||
this.iWon = iWon;
|
||||
this.winner = winner;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setup() {
|
||||
final Text gameOverHeader = header();
|
||||
gameOverHeader.setText(AppContext.getString("game-over"));
|
||||
|
||||
final Button okButton = button();
|
||||
okButton.setText(AppContext.getString("ok"));
|
||||
okButton.setOnAction(_ -> { ViewStack.pop(); });
|
||||
|
||||
Text gameOverText = text();
|
||||
|
||||
if (winner.isEmpty()) {
|
||||
gameOverText.setText(AppContext.getString("the-game-ended-in-a-draw"));
|
||||
} else {
|
||||
if (iWon) {
|
||||
gameOverText.setText(AppContext.getString("you-win") + " " + winner);
|
||||
} else {
|
||||
gameOverText.setText(AppContext.getString("you-lost-against") + " " + winner);
|
||||
}
|
||||
}
|
||||
|
||||
add(Pos.CENTER,
|
||||
fit(vboxFill(
|
||||
gameOverHeader,
|
||||
separator(),
|
||||
|
||||
vspacer(),
|
||||
gameOverText,
|
||||
vspacer(),
|
||||
|
||||
separator(),
|
||||
okButton
|
||||
))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private final Button forfeitButton;
|
||||
private final Button exitButton;
|
||||
|
||||
private final Text currentPlayerHeader;
|
||||
private final Text currentMoveHeader;
|
||||
|
||||
private final Text nextPlayerHeader;
|
||||
|
||||
private final Text gameStateFeedback = text();
|
||||
|
||||
private final ListView<Text> chatListView;
|
||||
private final TextField chatInput;
|
||||
|
||||
public GameView(Runnable onForfeit, Runnable onExit, Consumer<String> onMessage) {
|
||||
assert onExit != null;
|
||||
|
||||
super(true, "bg-primary");
|
||||
|
||||
if (onForfeit != null) {
|
||||
forfeitButton = button();
|
||||
forfeitButton.setText(AppContext.getString("forfeit"));
|
||||
forfeitButton.setOnAction(_ -> onForfeit.run());
|
||||
} else {
|
||||
forfeitButton = null;
|
||||
}
|
||||
|
||||
final SongDisplay songdisplay = new SongDisplay();
|
||||
|
||||
|
||||
add(Pos.BOTTOM_RIGHT,
|
||||
fit(vboxFill(
|
||||
songdisplay
|
||||
)));
|
||||
|
||||
if (onMessage != null) {
|
||||
chatListView = new ListView<Text>();
|
||||
|
||||
chatInput = input();
|
||||
chatInput.setOnAction(_ -> {
|
||||
onMessage.accept(chatInput.getText());
|
||||
chatInput.setText("");
|
||||
});
|
||||
} else {
|
||||
chatListView = null;
|
||||
chatInput = null;
|
||||
}
|
||||
|
||||
exitButton = button();
|
||||
exitButton.setText(AppContext.getString("exit"));
|
||||
exitButton.setOnAction(_ -> onExit.run());
|
||||
|
||||
currentPlayerHeader = header("", "header");
|
||||
currentMoveHeader = header();
|
||||
nextPlayerHeader = header();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setup() {
|
||||
add(
|
||||
Pos.TOP_CENTER,
|
||||
gameStateFeedback
|
||||
);
|
||||
|
||||
add(Pos.BOTTOM_LEFT,
|
||||
vboxFill(
|
||||
forfeitButton,
|
||||
exitButton
|
||||
)
|
||||
);
|
||||
|
||||
if (chatListView != null) {
|
||||
add(Pos.BOTTOM_RIGHT,
|
||||
fit(vboxFill(
|
||||
chatListView,
|
||||
chatInput
|
||||
)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
public void nextPlayer(boolean isMe, String currentPlayer, String currentMove, String nextPlayer) {
|
||||
Platform.runLater(() -> {
|
||||
gameStateFeedback.setText("Waiting on " + currentPlayer + " to make their move.");
|
||||
currentPlayerHeader.setText(currentPlayer);
|
||||
currentMoveHeader.setText(currentMove);
|
||||
|
||||
nextPlayerHeader.setText(nextPlayer);
|
||||
|
||||
if (isMe) {
|
||||
currentPlayerHeader.getStyleClass().add("my-turn");
|
||||
} else {
|
||||
currentPlayerHeader.getStyleClass().remove("my-turn");
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public void updateChat(String message) {
|
||||
if (chatListView == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final Text messageText = text();
|
||||
messageText.setText(message);
|
||||
|
||||
chatListView.getItems().add(messageText);
|
||||
}
|
||||
|
||||
public void gameOver(boolean iWon, String winner) {
|
||||
ViewStack.push(new GameOverView(iWon, winner));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
package org.toop.app.view.views;
|
||||
|
||||
import org.toop.app.GameInformation;
|
||||
import org.toop.app.game.Connect4Game;
|
||||
import org.toop.app.game.ReversiGame;
|
||||
import org.toop.app.game.TicTacToeGameThread;
|
||||
import org.toop.app.view.View;
|
||||
import org.toop.app.view.ViewStack;
|
||||
import org.toop.app.view.displays.SongDisplay;
|
||||
import org.toop.app.widget.Primitive;
|
||||
import org.toop.app.widget.tutorial.BaseTutorialWidget;
|
||||
import org.toop.app.widget.tutorial.TicTacToeTutorialWidget;
|
||||
import org.toop.local.AppContext;
|
||||
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Slider;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.scene.text.Text;
|
||||
import org.toop.local.AppSettings;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public final class LocalMultiplayerView extends View {
|
||||
private final GameInformation information;
|
||||
|
||||
public LocalMultiplayerView(GameInformation information) {
|
||||
super(true, "bg-primary");
|
||||
this.information = information;
|
||||
}
|
||||
|
||||
public LocalMultiplayerView(GameInformation.Type type) {
|
||||
this(new GameInformation(type));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setup() {
|
||||
final Button playButton = button();
|
||||
playButton.setText(AppContext.getString("play"));
|
||||
playButton.setOnAction(_ -> {
|
||||
for (final GameInformation.Player player : information.players) {
|
||||
if (player.name.isEmpty()) {
|
||||
ViewStack.push(new ErrorView(AppContext.getString("please-enter-your-name")));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
switch (information.type) {
|
||||
case TICTACTOE: new TicTacToeGameThread(information); break;
|
||||
case REVERSI: new ReversiGame(information); break;
|
||||
case CONNECT4: new Connect4Game(information); break;
|
||||
// case BATTLESHIP: new BattleshipGame(information); break;
|
||||
}
|
||||
});
|
||||
|
||||
final SongDisplay songdisplay = new SongDisplay();
|
||||
|
||||
|
||||
add(Pos.BOTTOM_RIGHT,
|
||||
fit(vboxFill(
|
||||
songdisplay
|
||||
)));
|
||||
|
||||
add(Pos.CENTER,
|
||||
fit(vboxFill(
|
||||
hbox(
|
||||
setupPlayers()
|
||||
),
|
||||
|
||||
separator(),
|
||||
playButton
|
||||
))
|
||||
);
|
||||
|
||||
final Button backButton = button();
|
||||
backButton.setText(AppContext.getString("back"));
|
||||
backButton.setOnAction(_ -> { ViewStack.push(new MainView()); });
|
||||
|
||||
add(Pos.BOTTOM_LEFT,
|
||||
vboxFill(
|
||||
backButton
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private VBox[] setupPlayers() {
|
||||
final VBox[] playerBoxes = new VBox[information.type.getPlayerCount()];
|
||||
|
||||
for (int i = 0; i < playerBoxes.length; i++) {
|
||||
final int index = i;
|
||||
|
||||
List<Node> nodes = new ArrayList<>();
|
||||
|
||||
final Text playerHeader = header();
|
||||
playerHeader.setText(AppContext.getString("player") + " #" + (i + 1));
|
||||
|
||||
nodes.add(playerHeader);
|
||||
nodes.add(separator());
|
||||
|
||||
final Text nameText = text();
|
||||
nameText.setText(AppContext.getString("name"));
|
||||
|
||||
if (information.players[i].isHuman) {
|
||||
final Button playerToggle = button();
|
||||
playerToggle.setText(AppContext.getString("player"));
|
||||
playerToggle.setOnAction(_ -> {
|
||||
information.players[index].isHuman = false;
|
||||
cleanup();
|
||||
setup();
|
||||
});
|
||||
|
||||
nodes.add(vboxFill(playerToggle));
|
||||
|
||||
final TextField playerNameInput = input();
|
||||
playerNameInput.setPromptText(AppContext.getString("enter-your-name"));
|
||||
playerNameInput.setText(information.players[i].name);
|
||||
playerNameInput.textProperty().addListener((_, _, newValue) -> {
|
||||
information.players[index].name = newValue;
|
||||
});
|
||||
|
||||
nodes.add(vboxFill(nameText, playerNameInput));
|
||||
} else {
|
||||
final Button computerToggle = button();
|
||||
computerToggle.setText(AppContext.getString("computer"));
|
||||
computerToggle.setOnAction(_ -> {
|
||||
information.players[index].isHuman = true;
|
||||
cleanup();
|
||||
setup();
|
||||
});
|
||||
|
||||
nodes.add(vboxFill(computerToggle));
|
||||
|
||||
information.players[i].name = "Pism Bot V" + i;
|
||||
|
||||
final Text computerNameText = text();
|
||||
computerNameText.setText(information.players[index].name);
|
||||
|
||||
nodes.add(vboxFill(nameText, computerNameText));
|
||||
|
||||
final Text computerDifficultyText = text();
|
||||
computerDifficultyText.setText(AppContext.getString("computer-difficulty"));
|
||||
|
||||
final Slider computerDifficultySlider = slider();
|
||||
computerDifficultySlider.setMin(0);
|
||||
computerDifficultySlider.setMax(information.type.getMaxDepth());
|
||||
computerDifficultySlider.setValue(information.players[i].computerDifficulty);
|
||||
computerDifficultySlider.valueProperty().addListener((_, _, newValue) -> {
|
||||
information.players[index].computerDifficulty = newValue.intValue();
|
||||
});
|
||||
|
||||
nodes.add(vboxFill(computerDifficultyText, computerDifficultySlider));
|
||||
|
||||
final Text computerThinkTimeText = text();
|
||||
computerThinkTimeText.setText(AppContext.getString("computer-think-time"));
|
||||
|
||||
final Slider computerThinkTimeSlider = slider();
|
||||
computerThinkTimeSlider.setMin(0);
|
||||
computerThinkTimeSlider.setMax(5);
|
||||
computerThinkTimeSlider.setValue(information.players[i].computerThinkTime);
|
||||
computerThinkTimeSlider.valueProperty().addListener((_, _, newValue) -> {
|
||||
information.players[index].computerThinkTime = newValue.intValue();
|
||||
});
|
||||
|
||||
nodes.add(vboxFill(computerThinkTimeText, computerThinkTimeSlider));
|
||||
}
|
||||
|
||||
playerBoxes[i] = vboxFill(nodes.toArray(new Node[0]));
|
||||
}
|
||||
|
||||
return playerBoxes;
|
||||
}
|
||||
}
|
||||
57
app/src/main/java/org/toop/app/view/views/LocalView.java
Normal file
57
app/src/main/java/org/toop/app/view/views/LocalView.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package org.toop.app.view.views;
|
||||
|
||||
import org.toop.app.GameInformation;
|
||||
import org.toop.app.view.View;
|
||||
import org.toop.app.view.ViewStack;
|
||||
import org.toop.app.view.displays.SongDisplay;
|
||||
import org.toop.local.AppContext;
|
||||
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.control.Button;
|
||||
|
||||
public final class LocalView extends View {
|
||||
public LocalView() {
|
||||
super(true, "bg-primary");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setup() {
|
||||
final Button ticTacToeButton = button();
|
||||
ticTacToeButton.setText(AppContext.getString("tic-tac-toe"));
|
||||
ticTacToeButton.setOnAction(_ -> { ViewStack.push(new LocalMultiplayerView(GameInformation.Type.TICTACTOE)); });
|
||||
|
||||
final Button reversiButton = button();
|
||||
reversiButton.setText(AppContext.getString("reversi"));
|
||||
reversiButton.setOnAction(_ -> { ViewStack.push(new LocalMultiplayerView(GameInformation.Type.REVERSI)); });
|
||||
|
||||
final Button connect4Button = button();
|
||||
connect4Button.setText(AppContext.getString("connect4"));
|
||||
connect4Button.setOnAction(_ -> { ViewStack.push(new LocalMultiplayerView(GameInformation.Type.CONNECT4)); });
|
||||
|
||||
add(Pos.CENTER,
|
||||
fit(vboxFill(
|
||||
ticTacToeButton,
|
||||
reversiButton,
|
||||
connect4Button
|
||||
))
|
||||
);
|
||||
|
||||
final Button backButton = button();
|
||||
backButton.setText(AppContext.getString("back"));
|
||||
backButton.setOnAction(_ -> { ViewStack.push(new MainView()); });
|
||||
|
||||
final SongDisplay songdisplay = new SongDisplay();
|
||||
|
||||
|
||||
add(Pos.BOTTOM_RIGHT,
|
||||
fit(vboxFill(
|
||||
songdisplay
|
||||
)));
|
||||
|
||||
add(Pos.BOTTOM_LEFT,
|
||||
vboxFill(
|
||||
backButton
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
55
app/src/main/java/org/toop/app/view/views/MainView.java
Normal file
55
app/src/main/java/org/toop/app/view/views/MainView.java
Normal file
@@ -0,0 +1,55 @@
|
||||
package org.toop.app.view.views;
|
||||
|
||||
import org.toop.app.App;
|
||||
import org.toop.app.view.View;
|
||||
import org.toop.app.view.ViewStack;
|
||||
import org.toop.local.AppContext;
|
||||
import org.toop.app.view.displays.SongDisplay;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.control.Button;
|
||||
|
||||
public final class MainView extends View {
|
||||
public MainView() {
|
||||
super(true, "bg-primary");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setup() {
|
||||
final Button localButton = button();
|
||||
localButton.setText(AppContext.getString("local"));
|
||||
localButton.setOnAction(_ -> { ViewStack.push(new LocalView()); });
|
||||
|
||||
final Button onlineButton = button();
|
||||
onlineButton.setText(AppContext.getString("online"));
|
||||
onlineButton.setOnAction(_ -> { ViewStack.push(new OnlineView()); });
|
||||
|
||||
final Button creditsButton = button();
|
||||
creditsButton.setText(AppContext.getString("credits"));
|
||||
creditsButton.setOnAction(_ -> { ViewStack.push(new CreditsView()); });
|
||||
|
||||
final Button optionsButton = button();
|
||||
optionsButton.setText(AppContext.getString("options"));
|
||||
optionsButton.setOnAction(_ -> { ViewStack.push(new OptionsView()); });
|
||||
|
||||
final Button quitButton = button();
|
||||
quitButton.setText(AppContext.getString("quit"));
|
||||
quitButton.setOnAction(_ -> { App.startQuit(); });
|
||||
|
||||
final SongDisplay songdisplay = new SongDisplay();
|
||||
|
||||
add(Pos.BOTTOM_RIGHT,
|
||||
fit(vboxFill(
|
||||
songdisplay
|
||||
)));
|
||||
|
||||
add(Pos.CENTER,
|
||||
fit(vboxFill(
|
||||
localButton,
|
||||
onlineButton,
|
||||
creditsButton,
|
||||
optionsButton,
|
||||
quitButton
|
||||
))
|
||||
);
|
||||
}
|
||||
}
|
||||
91
app/src/main/java/org/toop/app/view/views/OnlineView.java
Normal file
91
app/src/main/java/org/toop/app/view/views/OnlineView.java
Normal file
@@ -0,0 +1,91 @@
|
||||
package org.toop.app.view.views;
|
||||
|
||||
import org.toop.app.Server;
|
||||
import org.toop.app.view.View;
|
||||
import org.toop.app.view.ViewStack;
|
||||
import org.toop.app.view.displays.SongDisplay;
|
||||
import org.toop.local.AppContext;
|
||||
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.text.Text;
|
||||
|
||||
public class OnlineView extends View {
|
||||
public OnlineView() {
|
||||
super(true, "bg-primary");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setup() {
|
||||
final Text serverInformationHeader = header();
|
||||
serverInformationHeader.setText(AppContext.getString("server-information"));
|
||||
|
||||
final Text serverIPText = text();
|
||||
serverIPText.setText(AppContext.getString("ip-address"));
|
||||
|
||||
final TextField serverIPInput = input();
|
||||
serverIPInput.setPromptText(AppContext.getString("enter-the-server-ip"));
|
||||
|
||||
final Text serverPortText = text();
|
||||
serverPortText.setText(AppContext.getString("port"));
|
||||
|
||||
final TextField serverPortInput = input();
|
||||
serverPortInput.setPromptText(AppContext.getString("enter-the-server-port"));
|
||||
|
||||
final Text playerNameText = text();
|
||||
playerNameText.setText(AppContext.getString("player-name"));
|
||||
|
||||
final TextField playerNameInput = input();
|
||||
playerNameInput.setPromptText(AppContext.getString("enter-your-name"));
|
||||
|
||||
final Button connectButton = button();
|
||||
connectButton.setText(AppContext.getString("connect"));
|
||||
connectButton.setOnAction(_ -> {
|
||||
new Server(serverIPInput.getText(), serverPortInput.getText(), playerNameInput.getText());
|
||||
});
|
||||
|
||||
final SongDisplay songdisplay = new SongDisplay();
|
||||
|
||||
add(Pos.BOTTOM_RIGHT,
|
||||
fit(vboxFill(
|
||||
songdisplay
|
||||
)));
|
||||
|
||||
add(Pos.CENTER,
|
||||
fit(vboxFill(
|
||||
serverInformationHeader,
|
||||
separator(),
|
||||
|
||||
vboxFill(
|
||||
serverIPText,
|
||||
serverIPInput
|
||||
),
|
||||
|
||||
vboxFill(
|
||||
serverPortText,
|
||||
serverPortInput
|
||||
),
|
||||
|
||||
vboxFill(
|
||||
playerNameText,
|
||||
playerNameInput
|
||||
),
|
||||
|
||||
vboxFill(
|
||||
connectButton
|
||||
)
|
||||
))
|
||||
);
|
||||
|
||||
final Button backButton = button();
|
||||
backButton.setText(AppContext.getString("back"));
|
||||
backButton.setOnAction(_ -> { ViewStack.push(new MainView()); });
|
||||
|
||||
add(Pos.BOTTOM_LEFT,
|
||||
vboxFill(
|
||||
backButton
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
258
app/src/main/java/org/toop/app/view/views/OptionsView.java
Normal file
258
app/src/main/java/org/toop/app/view/views/OptionsView.java
Normal file
@@ -0,0 +1,258 @@
|
||||
package org.toop.app.view.views;
|
||||
|
||||
import org.toop.app.App;
|
||||
import org.toop.app.view.View;
|
||||
import org.toop.app.view.ViewStack;
|
||||
import org.toop.app.view.displays.SongDisplay;
|
||||
import org.toop.framework.audio.VolumeControl;
|
||||
import org.toop.framework.audio.events.AudioEvents;
|
||||
import org.toop.framework.eventbus.EventFlow;
|
||||
import org.toop.local.AppContext;
|
||||
import org.toop.local.AppSettings;
|
||||
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.ComboBox;
|
||||
import javafx.scene.control.Slider;
|
||||
import javafx.scene.text.Text;
|
||||
import javafx.util.StringConverter;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public final class OptionsView extends View {
|
||||
public OptionsView() {
|
||||
super(false, "bg-secondary");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setup() {
|
||||
final Text generalHeader = header();
|
||||
generalHeader.setText(AppContext.getString("general"));
|
||||
|
||||
final Text volumeHeader = header();
|
||||
volumeHeader.setText(AppContext.getString("volume"));
|
||||
|
||||
final Text styleHeader = header();
|
||||
styleHeader.setText(AppContext.getString("style"));
|
||||
|
||||
add(Pos.CENTER,
|
||||
fit(hboxFill(
|
||||
vboxFill(
|
||||
generalHeader,
|
||||
separator(),
|
||||
|
||||
vboxFill(
|
||||
text("language-text"),
|
||||
combobox("language-combobox")
|
||||
),
|
||||
|
||||
vboxFill(
|
||||
button("fullscreen-button")
|
||||
)
|
||||
),
|
||||
|
||||
vboxFill(
|
||||
volumeHeader,
|
||||
separator(),
|
||||
|
||||
vboxFill(
|
||||
text("master-volume-text"),
|
||||
slider("master-volume-slider")
|
||||
),
|
||||
|
||||
vboxFill(
|
||||
text("effects-volume-text"),
|
||||
slider("effects-volume-slider")
|
||||
),
|
||||
|
||||
vboxFill(
|
||||
text("music-volume-text"),
|
||||
slider("music-volume-slider")
|
||||
)
|
||||
),
|
||||
|
||||
vboxFill(
|
||||
styleHeader,
|
||||
separator(),
|
||||
|
||||
vboxFill(
|
||||
text("theme-text"),
|
||||
combobox("theme-combobox")
|
||||
),
|
||||
|
||||
vboxFill(
|
||||
text("layout-text"),
|
||||
combobox("layout-combobox")
|
||||
)
|
||||
)
|
||||
))
|
||||
);
|
||||
|
||||
setupLanguageOption();
|
||||
setupMasterVolumeOption();
|
||||
setupEffectsVolumeOption();
|
||||
setupMusicVolumeOption();
|
||||
setupThemeOption();
|
||||
setupLayoutOption();
|
||||
setupFullscreenOption();
|
||||
|
||||
final Button backButton = button();
|
||||
backButton.setText(AppContext.getString("back"));
|
||||
backButton.setOnAction(_ -> { ViewStack.pop(); });
|
||||
|
||||
final SongDisplay songdisplay = new SongDisplay();
|
||||
|
||||
|
||||
add(Pos.BOTTOM_RIGHT,
|
||||
fit(vboxFill(
|
||||
songdisplay
|
||||
)));
|
||||
|
||||
add(Pos.BOTTOM_LEFT,
|
||||
vboxFill(
|
||||
backButton
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private void setupLanguageOption() {
|
||||
final Text languageText = get("language-text");
|
||||
languageText.setText(AppContext.getString("language"));
|
||||
|
||||
final ComboBox<Locale> languageCombobox = get("language-combobox");
|
||||
languageCombobox.getItems().addAll(AppContext.getLocalization().getAvailableLocales());
|
||||
languageCombobox.setValue(AppContext.getLocale());
|
||||
|
||||
languageCombobox.getSelectionModel().selectedItemProperty().addListener((_, _, newValue) -> {
|
||||
AppSettings.getSettings().setLocale(newValue.toString());
|
||||
AppContext.setLocale(newValue);
|
||||
});
|
||||
|
||||
languageCombobox.setConverter(new StringConverter<>() {
|
||||
@Override
|
||||
public String toString(Locale locale) {
|
||||
return AppContext.getString(locale.getDisplayName().toLowerCase());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Locale fromString(String s) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void setupMasterVolumeOption() {
|
||||
final Text masterVolumeText = get("master-volume-text");
|
||||
masterVolumeText.setText(AppContext.getString("master-volume"));
|
||||
|
||||
final Slider masterVolumeSlider = get("master-volume-slider");
|
||||
masterVolumeSlider.setMin(0);
|
||||
masterVolumeSlider.setMax(100);
|
||||
masterVolumeSlider.setValue(AppSettings.getSettings().getVolume());
|
||||
|
||||
masterVolumeSlider.valueProperty().addListener((_, _, newValue) -> {
|
||||
AppSettings.getSettings().setVolume(newValue.intValue());
|
||||
new EventFlow().addPostEvent(new AudioEvents.ChangeVolume(newValue.doubleValue(), VolumeControl.MASTERVOLUME)).asyncPostEvent();
|
||||
});
|
||||
}
|
||||
|
||||
private void setupEffectsVolumeOption() {
|
||||
final Text effectsVolumeText = get("effects-volume-text");
|
||||
effectsVolumeText.setText(AppContext.getString("effects-volume"));
|
||||
|
||||
final Slider effectsVolumeSlider = get("effects-volume-slider");
|
||||
effectsVolumeSlider.setMin(0);
|
||||
effectsVolumeSlider.setMax(100);
|
||||
effectsVolumeSlider.setValue(AppSettings.getSettings().getFxVolume());
|
||||
|
||||
effectsVolumeSlider.valueProperty().addListener((_, _, newValue) -> {
|
||||
AppSettings.getSettings().setFxVolume(newValue.intValue());
|
||||
new EventFlow().addPostEvent(new AudioEvents.ChangeVolume(newValue.doubleValue(), VolumeControl.FX)).asyncPostEvent();
|
||||
});
|
||||
}
|
||||
|
||||
private void setupMusicVolumeOption() {
|
||||
final Text musicVolumeText = get("music-volume-text");
|
||||
musicVolumeText.setText(AppContext.getString("music-volume"));
|
||||
|
||||
final Slider musicVolumeSlider = get("music-volume-slider");
|
||||
musicVolumeSlider.setMin(0);
|
||||
musicVolumeSlider.setMax(100);
|
||||
musicVolumeSlider.setValue(AppSettings.getSettings().getMusicVolume());
|
||||
|
||||
musicVolumeSlider.valueProperty().addListener((_, _, newValue) -> {
|
||||
AppSettings.getSettings().setMusicVolume(newValue.intValue());
|
||||
new EventFlow().addPostEvent(new AudioEvents.ChangeVolume(newValue.doubleValue(), VolumeControl.MUSIC)).asyncPostEvent();
|
||||
});
|
||||
}
|
||||
|
||||
private void setupThemeOption() {
|
||||
final Text themeText = get("theme-text");
|
||||
themeText.setText(AppContext.getString("theme"));
|
||||
|
||||
final ComboBox<String> themeCombobox = get("theme-combobox");
|
||||
themeCombobox.getItems().addAll("dark", "light", "high-contrast");
|
||||
themeCombobox.setValue(AppSettings.getSettings().getTheme());
|
||||
|
||||
themeCombobox.getSelectionModel().selectedItemProperty().addListener((_, _, newValue) -> {
|
||||
AppSettings.getSettings().setTheme(newValue);
|
||||
App.setStyle(newValue, AppSettings.getSettings().getLayoutSize());
|
||||
});
|
||||
|
||||
themeCombobox.setConverter(new StringConverter<>() {
|
||||
@Override
|
||||
public String toString(String theme) {
|
||||
return AppContext.getString(theme);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String fromString(String s) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void setupLayoutOption() {
|
||||
final Text layoutText = get("layout-text");
|
||||
layoutText.setText(AppContext.getString("layout-size"));
|
||||
|
||||
final ComboBox<String> layoutCombobox = get("layout-combobox");
|
||||
layoutCombobox.getItems().addAll("small", "medium", "large");
|
||||
layoutCombobox.setValue(AppSettings.getSettings().getLayoutSize());
|
||||
|
||||
layoutCombobox.getSelectionModel().selectedItemProperty().addListener((_, _, newValue) -> {
|
||||
AppSettings.getSettings().setLayoutSize(newValue);
|
||||
App.setStyle(AppSettings.getSettings().getTheme(), newValue);
|
||||
});
|
||||
|
||||
layoutCombobox.setConverter(new StringConverter<>() {
|
||||
@Override
|
||||
public String toString(String layout) {
|
||||
return AppContext.getString(layout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String fromString(String s) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void setupFullscreenOption() {
|
||||
final Button fullscreenButton = get("fullscreen-button");
|
||||
|
||||
if (AppSettings.getSettings().getFullscreen()) {
|
||||
fullscreenButton.setText(AppContext.getString("windowed"));
|
||||
fullscreenButton.setOnAction(_ -> {
|
||||
AppSettings.getSettings().setFullscreen(false);
|
||||
App.setFullscreen(false);
|
||||
});
|
||||
} else {
|
||||
fullscreenButton.setText(AppContext.getString("fullscreen"));
|
||||
fullscreenButton.setOnAction(_ -> {
|
||||
AppSettings.getSettings().setFullscreen(true);
|
||||
App.setFullscreen(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
40
app/src/main/java/org/toop/app/view/views/QuitView.java
Normal file
40
app/src/main/java/org/toop/app/view/views/QuitView.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package org.toop.app.view.views;
|
||||
|
||||
import org.toop.app.App;
|
||||
import org.toop.app.view.View;
|
||||
import org.toop.local.AppContext;
|
||||
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.text.Text;
|
||||
|
||||
public final class QuitView extends View {
|
||||
public QuitView() {
|
||||
super(false, "bg-popup");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setup() {
|
||||
final Text sureHeader = header();
|
||||
sureHeader.setText(AppContext.getString("are-you-sure"));
|
||||
|
||||
final Button yesButton = button();
|
||||
yesButton.setText(AppContext.getString("yes"));
|
||||
yesButton.setOnAction(_ -> { App.quit(); });
|
||||
|
||||
final Button noButton = button();
|
||||
noButton.setText(AppContext.getString("no"));
|
||||
noButton.setOnAction(_ -> { App.stopQuit(); });
|
||||
|
||||
add(Pos.CENTER,
|
||||
fit(vbox(
|
||||
sureHeader,
|
||||
|
||||
hbox(
|
||||
yesButton,
|
||||
noButton
|
||||
)
|
||||
))
|
||||
);
|
||||
}
|
||||
}
|
||||
119
app/src/main/java/org/toop/app/view/views/SendChallengeView.java
Normal file
119
app/src/main/java/org/toop/app/view/views/SendChallengeView.java
Normal file
@@ -0,0 +1,119 @@
|
||||
package org.toop.app.view.views;
|
||||
|
||||
import org.toop.app.GameInformation;
|
||||
import org.toop.app.Server;
|
||||
import org.toop.app.view.View;
|
||||
import org.toop.app.view.ViewStack;
|
||||
import org.toop.local.AppContext;
|
||||
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.ComboBox;
|
||||
import javafx.scene.control.Slider;
|
||||
import javafx.scene.text.Text;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public final class SendChallengeView extends View {
|
||||
private final Server server;
|
||||
private final String opponent;
|
||||
private final BiConsumer<GameInformation.Player, String> onSend;
|
||||
|
||||
private final GameInformation.Player playerInformation;
|
||||
|
||||
public SendChallengeView(Server server, String opponent, BiConsumer<GameInformation.Player, String> onSend) {
|
||||
super(false, "bg-popup");
|
||||
|
||||
this.server = server;
|
||||
this.opponent = opponent;
|
||||
this.onSend = onSend;
|
||||
|
||||
playerInformation = new GameInformation.Player();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setup() {
|
||||
final Text challengeText = text();
|
||||
challengeText.setText(AppContext.getString("challenge"));
|
||||
|
||||
final Text opponentHeader = header();
|
||||
opponentHeader.setText(opponent);
|
||||
|
||||
final Text gameText = text();
|
||||
gameText.setText(AppContext.getString("to-a-game-of"));
|
||||
|
||||
final ComboBox<String> gamesCombobox = combobox();
|
||||
gamesCombobox.getItems().addAll(server.getGameList());
|
||||
gamesCombobox.setValue(gamesCombobox.getItems().getFirst());
|
||||
|
||||
final Button sendButton = button();
|
||||
sendButton.setText(AppContext.getString("send"));
|
||||
sendButton.setOnAction(_ -> { onSend.accept(playerInformation, gamesCombobox.getValue()); });
|
||||
|
||||
final Button cancelButton = button();
|
||||
cancelButton.setText(AppContext.getString("cancel"));
|
||||
cancelButton.setOnAction(_ -> {
|
||||
ViewStack.pop(); });
|
||||
|
||||
final List<Node> nodes = new ArrayList<>();
|
||||
|
||||
if (playerInformation.isHuman) {
|
||||
final Button playerToggle = button();
|
||||
playerToggle.setText(AppContext.getString("player"));
|
||||
playerToggle.setOnAction(_ -> {
|
||||
playerInformation.isHuman = false;
|
||||
cleanup();
|
||||
setup();
|
||||
});
|
||||
|
||||
nodes.add(vbox(playerToggle));
|
||||
} else {
|
||||
final Button computerToggle = button();
|
||||
computerToggle.setText(AppContext.getString("computer"));
|
||||
computerToggle.setOnAction(_ -> {
|
||||
playerInformation.isHuman = true;
|
||||
cleanup();
|
||||
setup();
|
||||
});
|
||||
|
||||
nodes.add(vbox(computerToggle));
|
||||
|
||||
final Text computerDifficultyText = text();
|
||||
computerDifficultyText.setText(AppContext.getString("computer-difficulty"));
|
||||
|
||||
final Slider computerDifficultySlider = slider();
|
||||
computerDifficultySlider.setMin(0);
|
||||
computerDifficultySlider.setMax(Server.gameToType(gamesCombobox.getValue()).getMaxDepth());
|
||||
computerDifficultySlider.setValue(playerInformation.computerDifficulty);
|
||||
computerDifficultySlider.valueProperty().addListener((_, _, newValue) -> {
|
||||
playerInformation.computerDifficulty = newValue.intValue();
|
||||
});
|
||||
|
||||
nodes.add(vbox(computerDifficultyText, computerDifficultySlider));
|
||||
}
|
||||
|
||||
add(Pos.CENTER,
|
||||
fit(hboxFill(
|
||||
vboxFill(
|
||||
challengeText,
|
||||
opponentHeader,
|
||||
gameText,
|
||||
gamesCombobox,
|
||||
separator(),
|
||||
|
||||
hboxFill(
|
||||
sendButton,
|
||||
cancelButton
|
||||
)
|
||||
),
|
||||
|
||||
vboxFill(
|
||||
nodes.toArray(new Node[0])
|
||||
)
|
||||
))
|
||||
);
|
||||
}
|
||||
}
|
||||
89
app/src/main/java/org/toop/app/view/views/ServerView.java
Normal file
89
app/src/main/java/org/toop/app/view/views/ServerView.java
Normal file
@@ -0,0 +1,89 @@
|
||||
package org.toop.app.view.views;
|
||||
|
||||
import org.toop.app.view.View;
|
||||
import org.toop.app.view.ViewStack;
|
||||
import org.toop.app.view.displays.SongDisplay;
|
||||
import org.toop.local.AppContext;
|
||||
|
||||
import javafx.application.Platform;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.ListView;
|
||||
import javafx.scene.text.Text;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public final class ServerView extends View {
|
||||
private final String user;
|
||||
private final Consumer<String> onPlayerClicked;
|
||||
private final Runnable onDisconnect;
|
||||
|
||||
private ListView<Button> listView;
|
||||
|
||||
public ServerView(String user, Consumer<String> onPlayerClicked, Runnable onDisconnect) {
|
||||
super(true, "bg-primary");
|
||||
|
||||
this.user = user;
|
||||
this.onPlayerClicked = onPlayerClicked;
|
||||
this.onDisconnect = onDisconnect;
|
||||
}
|
||||
|
||||
public void update(List<String> players) {
|
||||
Platform.runLater(() -> {
|
||||
listView.getItems().clear();
|
||||
|
||||
for (int i = 0; i < players.size(); i++) {
|
||||
final int finalI = i;
|
||||
|
||||
final Button button = button();
|
||||
button.setText(players.get(i));
|
||||
button.setOnAction(_ -> {
|
||||
onPlayerClicked.accept(players.get(finalI));
|
||||
});
|
||||
|
||||
listView.getItems().add(button);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setup() {
|
||||
final Text playerHeader = header();
|
||||
playerHeader.setText(user);
|
||||
|
||||
listView = new ListView<Button>();
|
||||
|
||||
final SongDisplay songdisplay = new SongDisplay();
|
||||
|
||||
|
||||
add(Pos.BOTTOM_RIGHT,
|
||||
fit(vboxFill(
|
||||
songdisplay
|
||||
)));
|
||||
|
||||
add(Pos.CENTER,
|
||||
fit(vboxFill(
|
||||
vbox(
|
||||
playerHeader,
|
||||
separator()
|
||||
),
|
||||
|
||||
listView
|
||||
))
|
||||
);
|
||||
|
||||
final Button disconnectButton = button();
|
||||
disconnectButton.setText(AppContext.getString("disconnect"));
|
||||
disconnectButton.setOnAction(_ -> {
|
||||
onDisconnect.run();
|
||||
ViewStack.push(new OnlineView());
|
||||
});
|
||||
|
||||
add(Pos.BOTTOM_LEFT,
|
||||
vboxFill(
|
||||
disconnectButton
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -47,7 +47,8 @@ public final class Primitive {
|
||||
return text;
|
||||
}
|
||||
|
||||
public static ImageView image(ImageAsset imageAsset) {
|
||||
public static ImageView image(File file) {
|
||||
ImageAsset imageAsset = new ImageAsset(file);
|
||||
ImageView imageView = new ImageView(imageAsset.getImage());
|
||||
imageView.getStyleClass().add("image");
|
||||
imageView.setPreserveRatio(true);
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
package org.toop.app.widget;
|
||||
|
||||
public interface Updatable {
|
||||
void update();
|
||||
}
|
||||
@@ -1,21 +1,7 @@
|
||||
package org.toop.app.widget.complex;
|
||||
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.control.Button;
|
||||
|
||||
public abstract class PopupWidget extends StackWidget {
|
||||
private final Button popButton;
|
||||
|
||||
public PopupWidget() {
|
||||
super("bg-popup");
|
||||
|
||||
popButton = new Button("X");
|
||||
popButton.setOnAction(_ -> hide());
|
||||
|
||||
add(Pos.TOP_RIGHT, popButton);
|
||||
}
|
||||
|
||||
protected void setOnPop(Runnable onPop) {
|
||||
popButton.setOnAction(_ -> onPop.run());
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,5 @@
|
||||
package org.toop.app.widget.display;
|
||||
|
||||
import javafx.animation.KeyFrame;
|
||||
import javafx.animation.Timeline;
|
||||
import javafx.util.Duration;
|
||||
import org.toop.app.widget.Widget;
|
||||
import org.toop.framework.audio.events.AudioEvents;
|
||||
import org.toop.framework.eventbus.EventFlow;
|
||||
@@ -18,13 +15,10 @@ import javafx.scene.layout.Region;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.scene.text.Text;
|
||||
|
||||
import java.util.Timer;
|
||||
|
||||
public class SongDisplay extends VBox implements Widget {
|
||||
private final Text songTitle;
|
||||
private final ProgressBar progressBar;
|
||||
private final Text progressText;
|
||||
private boolean canClick = true;
|
||||
|
||||
public SongDisplay() {
|
||||
new EventFlow()
|
||||
@@ -55,13 +49,10 @@ public class SongDisplay extends VBox implements Widget {
|
||||
previousButton.getStyleClass().setAll("previous-button");
|
||||
|
||||
skipButton.setOnAction( event -> {
|
||||
if (!canClick) { return; }
|
||||
GlobalEventBus.post(new AudioEvents.SkipMusic());
|
||||
doCooldown();
|
||||
});
|
||||
|
||||
pauseButton.setOnAction(event -> {
|
||||
if (!canClick) { return; }
|
||||
GlobalEventBus.post(new AudioEvents.PauseMusic());
|
||||
if (pauseButton.getText().equals("⏸")) {
|
||||
pauseButton.setText("▶");
|
||||
@@ -69,13 +60,10 @@ public class SongDisplay extends VBox implements Widget {
|
||||
else if (pauseButton.getText().equals("▶")) {
|
||||
pauseButton.setText("⏸");
|
||||
}
|
||||
doCooldown();
|
||||
});
|
||||
|
||||
previousButton.setOnAction( event -> {
|
||||
if (!canClick) { return; }
|
||||
GlobalEventBus.post(new AudioEvents.PreviousMusic());
|
||||
doCooldown();
|
||||
});
|
||||
|
||||
HBox control = new HBox(10, previousButton, pauseButton, skipButton);
|
||||
@@ -122,16 +110,6 @@ public class SongDisplay extends VBox implements Widget {
|
||||
return time;
|
||||
}
|
||||
|
||||
private void doCooldown() {
|
||||
canClick = false;
|
||||
Timeline cooldown = new Timeline(
|
||||
new KeyFrame(Duration.millis(300), event -> canClick = true)
|
||||
);
|
||||
cooldown.setCycleCount(1);
|
||||
cooldown.play();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Node getNode() {
|
||||
return this;
|
||||
|
||||
@@ -20,10 +20,5 @@ public class QuitPopup extends PopupWidget {
|
||||
});
|
||||
|
||||
add(Pos.CENTER, confirmWidget);
|
||||
|
||||
setOnPop(() -> {
|
||||
App.stopQuit();
|
||||
hide();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,116 +1,60 @@
|
||||
package org.toop.app.widget.tutorial;
|
||||
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.scene.text.Text;
|
||||
import org.apache.maven.surefire.shared.lang3.tuple.ImmutablePair;
|
||||
import org.toop.app.widget.Primitive;
|
||||
import org.toop.app.widget.Updatable;
|
||||
import org.toop.app.widget.WidgetContainer;
|
||||
import org.toop.app.widget.complex.PopupWidget;
|
||||
import org.toop.app.widget.complex.ViewWidget;
|
||||
|
||||
import org.toop.framework.resource.resources.ImageAsset;
|
||||
import javafx.scene.control.Button;
|
||||
import org.toop.local.AppContext;
|
||||
|
||||
import java.util.List;
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* A widget base for all the tutorial widgets.
|
||||
*
|
||||
* <p>Usage example:
|
||||
*
|
||||
* <pre>{@code
|
||||
* public class Connect4TutorialWidget extends BaseTutorialWidget {
|
||||
* public Connect4TutorialWidget(Runnable nextScreen) {
|
||||
* super(List.of(
|
||||
* new ImmutablePair<>("connect4.1", ResourceManager.get("connect41.png")),
|
||||
* new ImmutablePair<>("connect4.2", ResourceManager.get("connect42.png"))
|
||||
* ), nextScreen);
|
||||
* }
|
||||
* }</pre>
|
||||
*/
|
||||
public class BaseTutorialWidget extends PopupWidget implements Updatable {
|
||||
public class BaseTutorialWidget extends ViewWidget {
|
||||
|
||||
private final Text tutorialText;
|
||||
private final ImageView imagery;
|
||||
private final Button previousButton;
|
||||
private final Button nextButton;
|
||||
private final List<ImmutablePair<String, ImageAsset>> pages;
|
||||
private final Runnable nextScreen;
|
||||
private TState state;
|
||||
private Text tutorialText;
|
||||
private Button previousButton;
|
||||
private Button nextButton;
|
||||
private Button noButton;
|
||||
private Button yesButton;
|
||||
private Button neverButton;
|
||||
private ImageView imagery;
|
||||
|
||||
private int pageIndex = 0;
|
||||
public BaseTutorialWidget(String key, Runnable onNo, Runnable onYes, Runnable onNever) {
|
||||
System.out.println("Trying to initialize...");
|
||||
this.tutorialText = Primitive.text(key);
|
||||
this.yesButton = Primitive.button("ok", () -> onYes.run());
|
||||
this.noButton = Primitive.button("no", () -> onNo.run());
|
||||
this.neverButton = Primitive.button("never", () -> onNever.run());
|
||||
var a = Primitive.hbox(yesButton, noButton, neverButton);
|
||||
add(Pos.CENTER, Primitive.vbox(tutorialText, a));
|
||||
}
|
||||
|
||||
public BaseTutorialWidget(List<ImmutablePair<String, ImageAsset>> pages, Runnable nextScreen) {
|
||||
this.tutorialText = Primitive.text(pages.getFirst().getKey());
|
||||
this.imagery = Primitive.image(pages.getFirst().getValue());
|
||||
|
||||
this.pages = pages;
|
||||
this.nextScreen = nextScreen;
|
||||
|
||||
previousButton = Primitive.button("goback", () -> { update(false); this.hide(); });
|
||||
nextButton = Primitive.button(">", () -> update(true));
|
||||
|
||||
var w = Primitive.hbox(
|
||||
previousButton,
|
||||
nextButton
|
||||
);
|
||||
|
||||
var x = Primitive.vbox(imagery, tutorialText);
|
||||
public BaseTutorialWidget(TState state, String key, Runnable onPrevious, Runnable onNext) {
|
||||
this.state = state;
|
||||
this.tutorialText = Primitive.text(key);
|
||||
this.previousButton = Primitive.button("<", () -> onPrevious.run());
|
||||
this.nextButton = Primitive.button(">", () -> onNext.run());
|
||||
var w = Primitive.hbox(previousButton, nextButton);
|
||||
add(Pos.CENTER, Primitive.vbox(tutorialText, w));
|
||||
}
|
||||
|
||||
public BaseTutorialWidget(TState state, String key, File image, Runnable onPrevious, Runnable onNext) {
|
||||
this.state = state;
|
||||
this.imagery = Primitive.image(image);
|
||||
this.tutorialText = Primitive.text(key);
|
||||
this.previousButton = Primitive.button("<", () -> onPrevious.run());
|
||||
this.nextButton = Primitive.button(">", () -> onNext.run());
|
||||
var w = Primitive.hbox(previousButton, nextButton);
|
||||
var x = Primitive.vbox(imagery, tutorialText);
|
||||
add(Pos.CENTER, Primitive.vbox(x, w));
|
||||
|
||||
WidgetContainer.add(Pos.CENTER, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
update(true);
|
||||
}
|
||||
|
||||
// TODO Refactor if statements to make code easier to read.
|
||||
public void update(boolean next) {
|
||||
pageIndex = next ? pageIndex + 1 : pageIndex - 1;
|
||||
|
||||
if (pageIndex >= pages.size()) {
|
||||
pageIndex--;
|
||||
return;
|
||||
} else if (pageIndex < 0) {
|
||||
pageIndex++;
|
||||
return;
|
||||
}
|
||||
|
||||
if (pageIndex == pages.size()-1) {
|
||||
nextButton.textProperty().unbind();
|
||||
nextButton.setText(AppContext.getString("startgame"));
|
||||
nextButton.setOnAction((_) -> {
|
||||
this.hide();
|
||||
nextScreen.run();
|
||||
});
|
||||
|
||||
} else {
|
||||
nextButton.textProperty().unbind();
|
||||
nextButton.setText(AppContext.getString(">"));
|
||||
nextButton.setOnAction((_) -> this.update(true));
|
||||
}
|
||||
|
||||
if (pageIndex == 0) {
|
||||
previousButton.textProperty().unbind();
|
||||
previousButton.setText(AppContext.getString("goback"));
|
||||
previousButton.setOnAction((_) -> this.hide());
|
||||
} else {
|
||||
previousButton.textProperty().unbind();
|
||||
previousButton.setText(AppContext.getString("<"));
|
||||
previousButton.setOnAction((_) -> this.update(false));
|
||||
}
|
||||
|
||||
var currentPage = pages.get(pageIndex);
|
||||
|
||||
var text = currentPage.getKey();
|
||||
var image = currentPage.getValue();
|
||||
|
||||
public void update(String key, File image) {
|
||||
tutorialText.textProperty().unbind();
|
||||
tutorialText.setText(AppContext.getString(text));
|
||||
tutorialText.setText(AppContext.getString(key));
|
||||
imagery.setImage(Primitive.image(image).getImage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,39 @@
|
||||
package org.toop.app.widget.tutorial;
|
||||
|
||||
import org.apache.maven.surefire.shared.lang3.tuple.ImmutablePair;
|
||||
import org.toop.framework.resource.ResourceManager;
|
||||
import javafx.geometry.Pos;
|
||||
import org.toop.app.widget.complex.ViewWidget;
|
||||
|
||||
import java.util.List;
|
||||
import java.io.File;
|
||||
|
||||
public class Connect4TutorialWidget extends BaseTutorialWidget {
|
||||
public Connect4TutorialWidget(Runnable nextScreen) {
|
||||
super(List.of(
|
||||
new ImmutablePair<>("connect4.1", ResourceManager.get("connect41.png")),
|
||||
new ImmutablePair<>("connect4.2", ResourceManager.get("connect42.png"))
|
||||
), nextScreen);
|
||||
public class Connect4TutorialWidget extends ViewWidget {
|
||||
private TState state;
|
||||
private String[] keys = {"connect4.1", "connect4.2"};
|
||||
private File[] images = {new File("app/src/main/resources/assets/images/connect41.png"), new File("app/src/main/resources/assets/images/connect42.png")};
|
||||
private BaseTutorialWidget tutorialWidget;
|
||||
|
||||
public Connect4TutorialWidget() {
|
||||
this.state = new TState(keys.length);
|
||||
tutorialWidget = new BaseTutorialWidget(
|
||||
state,
|
||||
keys[state.getCurrent()],
|
||||
images[state.getCurrent()],
|
||||
() -> {
|
||||
if (state.hasPrevious()) {
|
||||
state.previous();
|
||||
update();
|
||||
}
|
||||
},
|
||||
() -> {
|
||||
if (state.hasNext()) {
|
||||
state.next();
|
||||
update();
|
||||
}
|
||||
}
|
||||
);
|
||||
add(Pos.CENTER, tutorialWidget);
|
||||
}
|
||||
|
||||
private void update() {
|
||||
tutorialWidget.update(keys[state.getCurrent()], images[state.getCurrent()]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,39 @@
|
||||
package org.toop.app.widget.tutorial;
|
||||
|
||||
import org.apache.maven.surefire.shared.lang3.tuple.ImmutablePair;
|
||||
import org.toop.framework.resource.ResourceManager;
|
||||
import javafx.geometry.Pos;
|
||||
import org.toop.app.widget.complex.ViewWidget;
|
||||
|
||||
import java.util.List;
|
||||
import java.io.File;
|
||||
|
||||
public class ReversiTutorialWidget extends BaseTutorialWidget {
|
||||
public ReversiTutorialWidget(Runnable nextScreen) {
|
||||
super(List.of(
|
||||
new ImmutablePair<>("reversi1", ResourceManager.get("reversi1.png")),
|
||||
new ImmutablePair<>("reversi2", ResourceManager.get("reversi2.png")),
|
||||
new ImmutablePair<>("reversi3", ResourceManager.get("cat.jpg")),
|
||||
new ImmutablePair<>("reversi4", ResourceManager.get("cat.jpg"))
|
||||
), nextScreen);
|
||||
public class ReversiTutorialWidget extends ViewWidget {
|
||||
private TState state;
|
||||
private String[] keys = {"reversi1", "reversi2", "reversi3", "reversi4"};
|
||||
private File[] images = {new File("app/src/main/resources/assets/images/reversi1.png"), new File("app/src/main/resources/assets/images/reversi2.png"), new File("app/src/main/resources/assets/images/cat.jpg"), new File("app/src/main/resources/assets/images/cat.jpg")};
|
||||
private BaseTutorialWidget tutorialWidget;
|
||||
|
||||
public ReversiTutorialWidget() {
|
||||
this.state = new TState(keys.length);
|
||||
tutorialWidget = new BaseTutorialWidget(
|
||||
state,
|
||||
keys[state.getCurrent()],
|
||||
images[state.getCurrent()],
|
||||
() -> {
|
||||
if (state.hasPrevious()) {
|
||||
state.previous();
|
||||
update();
|
||||
}
|
||||
},
|
||||
() -> {
|
||||
if (state.hasNext()) {
|
||||
state.next();
|
||||
update();
|
||||
}
|
||||
}
|
||||
);
|
||||
add(Pos.CENTER, tutorialWidget);
|
||||
}
|
||||
|
||||
private void update() {
|
||||
tutorialWidget.update(keys[state.getCurrent()], images[state.getCurrent()]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
package org.toop.app.widget.tutorial;
|
||||
|
||||
import javafx.geometry.Pos;
|
||||
import org.toop.app.widget.Primitive;
|
||||
import org.toop.app.widget.WidgetContainer;
|
||||
import org.toop.app.widget.complex.PopupWidget;
|
||||
import org.toop.local.AppSettings;
|
||||
|
||||
public class ShowEnableTutorialWidget extends PopupWidget {
|
||||
|
||||
public ShowEnableTutorialWidget(Runnable tutorial, Runnable nextScreen, Runnable appSettingsSetter) {
|
||||
var a = Primitive.hbox(
|
||||
Primitive.button("ok", () -> { appSettingsSetter.run(); tutorial.run(); this.hide(); }),
|
||||
Primitive.button("no", () -> { appSettingsSetter.run(); nextScreen.run(); this.hide(); }),
|
||||
Primitive.button("never", () -> { AppSettings.getSettings().setTutorialFlag(false); nextScreen.run(); this.hide(); })
|
||||
);
|
||||
|
||||
var txt = Primitive.text("tutorial");
|
||||
add(Pos.CENTER, Primitive.vbox(txt, a));
|
||||
WidgetContainer.add(Pos.CENTER, this);
|
||||
}
|
||||
}
|
||||
44
app/src/main/java/org/toop/app/widget/tutorial/TState.java
Normal file
44
app/src/main/java/org/toop/app/widget/tutorial/TState.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package org.toop.app.widget.tutorial;
|
||||
|
||||
public class TState {
|
||||
|
||||
private int current;
|
||||
private int total;
|
||||
|
||||
public TState(int total) {
|
||||
this.total = total;
|
||||
this.current = 0;
|
||||
}
|
||||
|
||||
public int getCurrent() {
|
||||
return current;
|
||||
}
|
||||
|
||||
public void setCurrent(int current) {
|
||||
this.current = current;
|
||||
}
|
||||
|
||||
public int getTotal() {
|
||||
return total;
|
||||
}
|
||||
|
||||
public void setTotal(int total) {
|
||||
this.total = total;
|
||||
}
|
||||
|
||||
public void next() {
|
||||
current = current + 1;
|
||||
}
|
||||
|
||||
public void previous() {
|
||||
current = current - 1;
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return current < total - 1;
|
||||
}
|
||||
|
||||
public boolean hasPrevious() {
|
||||
return current > 0;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,42 @@
|
||||
package org.toop.app.widget.tutorial;
|
||||
|
||||
import org.apache.maven.surefire.shared.lang3.tuple.ImmutablePair;
|
||||
import org.toop.framework.resource.ResourceManager;
|
||||
import javafx.geometry.Pos;
|
||||
import org.toop.app.widget.complex.ViewWidget;
|
||||
import java.io.File;
|
||||
|
||||
import java.util.List;
|
||||
public class TicTacToeTutorialWidget extends ViewWidget {
|
||||
|
||||
public class TicTacToeTutorialWidget extends BaseTutorialWidget {
|
||||
public TicTacToeTutorialWidget(Runnable nextScreen) {
|
||||
super(List.of(
|
||||
new ImmutablePair<>("tictactoe1", ResourceManager.get("tictactoe1.png")),
|
||||
new ImmutablePair<>("tictactoe2", ResourceManager.get("tictactoe2.png"))
|
||||
), nextScreen);
|
||||
private TState state;
|
||||
private String[] keys = {"tictactoe1", "tictactoe2"};
|
||||
private File[] images = {
|
||||
new File("app/src/main/resources/assets/images/tictactoe1.png"),
|
||||
new File("app/src/main/resources/assets/images/tictactoe2.png")
|
||||
};
|
||||
private BaseTutorialWidget tutorialWidget;
|
||||
|
||||
public TicTacToeTutorialWidget() {
|
||||
this.state = new TState(keys.length);
|
||||
tutorialWidget = new BaseTutorialWidget(
|
||||
state,
|
||||
keys[state.getCurrent()],
|
||||
images[state.getCurrent()],
|
||||
() -> {
|
||||
if (state.hasPrevious()) {
|
||||
state.previous();
|
||||
update();
|
||||
}
|
||||
},
|
||||
() -> {
|
||||
if (state.hasNext()) {
|
||||
state.next();
|
||||
update();
|
||||
}
|
||||
}
|
||||
);
|
||||
add(Pos.CENTER, tutorialWidget);
|
||||
}
|
||||
|
||||
private void update() {
|
||||
tutorialWidget.update(keys[state.getCurrent()], images[state.getCurrent()]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,13 +53,23 @@ public final class GameView extends ViewWidget {
|
||||
|
||||
switch(gameType) {
|
||||
case "TicTacToe":
|
||||
this.tutorialButton = Primitive.button("tutorialstring", () -> new TicTacToeTutorialWidget(() -> {})); break;
|
||||
this.tutorialButton = Primitive.button("tutorialstring", () -> {
|
||||
transitionNext(new TicTacToeTutorialWidget());
|
||||
});
|
||||
break;
|
||||
case "Reversi":
|
||||
this.tutorialButton = Primitive.button("tutorialstring", () -> new ReversiTutorialWidget(() -> {})); break;
|
||||
this.tutorialButton = Primitive.button("tutorialstring", () -> {
|
||||
transitionNext(new ReversiTutorialWidget());
|
||||
});
|
||||
break;
|
||||
case "Connect4":
|
||||
this.tutorialButton = Primitive.button("tutorialstring", () -> new Connect4TutorialWidget(() -> {})); break;
|
||||
this.tutorialButton = Primitive.button("tutorialstring", () -> {
|
||||
transitionNext(new Connect4TutorialWidget());
|
||||
});
|
||||
break;
|
||||
default:
|
||||
this.tutorialButton = null; break;
|
||||
this.tutorialButton = null;
|
||||
break;
|
||||
}
|
||||
|
||||
setupLayout();
|
||||
|
||||
@@ -10,7 +10,10 @@ import org.toop.app.widget.WidgetContainer;
|
||||
import org.toop.app.widget.complex.PlayerInfoWidget;
|
||||
import org.toop.app.widget.complex.ViewWidget;
|
||||
import org.toop.app.widget.popup.ErrorPopup;
|
||||
import org.toop.app.widget.tutorial.*;
|
||||
import org.toop.app.widget.tutorial.BaseTutorialWidget;
|
||||
import org.toop.app.widget.tutorial.Connect4TutorialWidget;
|
||||
import org.toop.app.widget.tutorial.ReversiTutorialWidget;
|
||||
import org.toop.app.widget.tutorial.TicTacToeTutorialWidget;
|
||||
import org.toop.local.AppContext;
|
||||
|
||||
import javafx.geometry.Pos;
|
||||
@@ -37,40 +40,91 @@ public class LocalMultiplayerView extends ViewWidget {
|
||||
switch (information.type) {
|
||||
case TICTACTOE:
|
||||
if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstTTT()) {
|
||||
new ShowEnableTutorialWidget(
|
||||
() -> new TicTacToeTutorialWidget(() -> new TicTacToeGameThread(information)),
|
||||
() -> Platform.runLater(() -> new TicTacToeGameThread(information)),
|
||||
() -> AppSettings.getSettings().setFirstTTT(false)
|
||||
);
|
||||
} else {
|
||||
new TicTacToeGameThread(information);
|
||||
BaseTutorialWidget a = new BaseTutorialWidget(
|
||||
"tutorial",
|
||||
() -> {
|
||||
AppSettings.getSettings().setFirstTTT(false);
|
||||
Platform.runLater(() -> {
|
||||
new TicTacToeGameThread(information);
|
||||
});
|
||||
},
|
||||
() -> {
|
||||
ViewWidget c = new TicTacToeTutorialWidget();
|
||||
transitionNext(c);
|
||||
WidgetContainer.setCurrentView(c);
|
||||
AppSettings.getSettings().setFirstTTT(false);
|
||||
},
|
||||
() -> {
|
||||
AppSettings.getSettings().setTutorialFlag(false);
|
||||
Platform.runLater(() -> {
|
||||
new TicTacToeGameThread(information);
|
||||
});
|
||||
}
|
||||
);
|
||||
transitionNext(a);
|
||||
break;
|
||||
}
|
||||
new TicTacToeGameThread(information);
|
||||
break;
|
||||
case REVERSI:
|
||||
if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstReversi()) {
|
||||
new ShowEnableTutorialWidget(
|
||||
() -> new ReversiTutorialWidget(() -> new ReversiGame(information)),
|
||||
() -> Platform.runLater(() -> new ReversiGame(information)),
|
||||
() -> AppSettings.getSettings().setFirstReversi(false)
|
||||
);
|
||||
} else {
|
||||
new ReversiGame(information);
|
||||
BaseTutorialWidget a = new BaseTutorialWidget(
|
||||
"tutorial",
|
||||
() -> { Platform.runLater(() -> {
|
||||
AppSettings.getSettings().setFirstReversi(false);
|
||||
new ReversiGame(information);
|
||||
});
|
||||
},
|
||||
() -> {
|
||||
Platform.runLater(() -> {
|
||||
ViewWidget c = new ReversiTutorialWidget();
|
||||
transitionNext(c);
|
||||
WidgetContainer.setCurrentView(c);
|
||||
AppSettings.getSettings().setFirstReversi(false);
|
||||
});
|
||||
},
|
||||
() -> {
|
||||
Platform.runLater(() -> {
|
||||
AppSettings.getSettings().setTutorialFlag(false);
|
||||
new ReversiGame(information);
|
||||
});
|
||||
});
|
||||
transitionNext(a);
|
||||
break;
|
||||
}
|
||||
new ReversiGame(information);
|
||||
break;
|
||||
case CONNECT4:
|
||||
if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstConnect4()) {
|
||||
new ShowEnableTutorialWidget(
|
||||
() -> new Connect4TutorialWidget(() -> new Connect4Game(information)),
|
||||
() -> Platform.runLater(() -> new Connect4Game(information)),
|
||||
() -> AppSettings.getSettings().setFirstConnect4(false)
|
||||
);
|
||||
} else {
|
||||
new Connect4Game(information);
|
||||
BaseTutorialWidget a = new BaseTutorialWidget(
|
||||
"tutorial",
|
||||
() -> { Platform.runLater(() -> {
|
||||
AppSettings.getSettings().setFirstConnect4(false);
|
||||
new Connect4Game(information);
|
||||
});
|
||||
},
|
||||
() -> {
|
||||
Platform.runLater(() -> {
|
||||
ViewWidget c = new Connect4TutorialWidget();
|
||||
transitionNext(c);
|
||||
WidgetContainer.setCurrentView(c);
|
||||
AppSettings.getSettings().setFirstConnect4(false);
|
||||
});
|
||||
},
|
||||
() -> {
|
||||
Platform.runLater(() -> {
|
||||
AppSettings.getSettings().setTutorialFlag(false);
|
||||
new Connect4Game(information);
|
||||
});
|
||||
});
|
||||
transitionNext(a);
|
||||
break;
|
||||
}
|
||||
new Connect4Game(information);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// case BATTLESHIP -> new BattleshipGame(information);
|
||||
});
|
||||
});
|
||||
|
||||
var playerSection = setupPlayerSections();
|
||||
|
||||
|
||||
@@ -84,8 +84,6 @@ reversi2=Clicking on a dot will flip all the moves between where you place the d
|
||||
reversi3=Your turn may be skipped if there is no legal move. This will let your opponent play again until you get an opportunity at a legal move.
|
||||
reversi4=The player who wins at the end of the game is the one who has the most pieces on the board.
|
||||
tutorialstring=Tutorial
|
||||
startgame=Start game!
|
||||
goback=Go back
|
||||
|
||||
|
||||
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629 (Arabic)
|
||||
|
||||
@@ -146,7 +146,19 @@
|
||||
<artifactId>error_prone_annotations</artifactId>
|
||||
<version>2.42.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependency>
|
||||
<groupId>org.deeplearning4j</groupId>
|
||||
<artifactId>deeplearning4j-nn</artifactId>
|
||||
<version>1.0.0-M2.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.toop</groupId>
|
||||
<artifactId>game</artifactId>
|
||||
<version>0.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
|
||||
@@ -0,0 +1,182 @@
|
||||
package org.toop.framework.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.game.AI;
|
||||
import org.toop.game.enumerators.GameState;
|
||||
import org.toop.game.records.Move;
|
||||
import org.toop.game.reversi.Reversi;
|
||||
import org.toop.game.reversi.ReversiAI;
|
||||
import org.toop.game.reversi.ReversiAISimple;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static java.lang.Math.abs;
|
||||
|
||||
public class NeuralNetwork {
|
||||
|
||||
private MultiLayerConfiguration conf;
|
||||
private MultiLayerNetwork model;
|
||||
private ReversiAI reversiAI = new ReversiAI();
|
||||
private AI<Reversi> opponentRand = new ReversiAI();
|
||||
private AI<Reversi> opponentSimple = new ReversiAISimple();
|
||||
|
||||
|
||||
public NeuralNetwork() {}
|
||||
|
||||
public void init(){
|
||||
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.001);
|
||||
trainingLoop();
|
||||
saveModel();
|
||||
}
|
||||
|
||||
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.1;
|
||||
|
||||
long start = System.nanoTime();
|
||||
|
||||
for (int game = 0; game<totalGames; game++){
|
||||
Reversi reversi = new Reversi();
|
||||
List<StateAction> gameHistory = new ArrayList<>();
|
||||
GameState state = GameState.NORMAL;
|
||||
|
||||
double reward = 0;
|
||||
|
||||
while (state != GameState.DRAW && state != GameState.WIN){
|
||||
char curr = reversi.getCurrentPlayer();
|
||||
Move move;
|
||||
if (curr == 'B') {
|
||||
int[] input = reversi.getBoardInt();
|
||||
if (Math.random() < epsilon) {
|
||||
Move[] moves = reversi.getLegalMoves();
|
||||
move = moves[(int) (Math.random() * moves.length - .5f)];
|
||||
} else {
|
||||
INDArray boardInput = Nd4j.create(new int[][]{input});
|
||||
INDArray prediction = model.output(boardInput);
|
||||
|
||||
int location = pickLegalMove(prediction, reversi);
|
||||
gameHistory.add(new StateAction(input, location));
|
||||
move = new Move(location, reversi.getCurrentPlayer());
|
||||
}
|
||||
}else{
|
||||
move = reversiAI.findBestMove(reversi,5);
|
||||
}
|
||||
state = reversi.play(move);
|
||||
}
|
||||
|
||||
//IO.println(model.params());
|
||||
Reversi.Score score = reversi.getScore();
|
||||
int scoreDif = abs(score.player1Score() - score.player2Score());
|
||||
if (score.player1Score() > score.player2Score()){
|
||||
reward = 1 + ((scoreDif / 64.0) * 0.5);
|
||||
}else if (score.player1Score() < score.player2Score()){
|
||||
reward = -1 - ((scoreDif / 64.0) * 0.5);
|
||||
}else{
|
||||
reward = 0;
|
||||
}
|
||||
|
||||
|
||||
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 boolean isInCorner(Move move){
|
||||
return move.position() == 0 || move.position() == 7 || move.position() == 56 || move.position() == 63;
|
||||
}
|
||||
|
||||
private int pickLegalMove(INDArray prediction, Reversi reversi){
|
||||
double[] probs = prediction.toDoubleVector();
|
||||
Move[] legalMoves = reversi.getLegalMoves();
|
||||
|
||||
if (legalMoves.length == 0) return -1;
|
||||
|
||||
int bestMove = legalMoves[0].position();
|
||||
double bestVal = probs[bestMove];
|
||||
|
||||
for (Move move : legalMoves){
|
||||
if (probs[move.position()] > bestVal){
|
||||
bestMove = move.position();
|
||||
bestVal = probs[bestMove];
|
||||
}
|
||||
}
|
||||
return bestMove;
|
||||
}
|
||||
|
||||
private void trainFromHistory(StateAction step, double reward){
|
||||
double[] output = new double[64];
|
||||
output[step.action] = reward;
|
||||
|
||||
DataSet ds = new DataSet(
|
||||
Nd4j.create(new int[][] { step.state }),
|
||||
Nd4j.create(new double[][] { output })
|
||||
);
|
||||
|
||||
model.fit(ds);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package org.toop.framework.machinelearning;
|
||||
|
||||
public class StateAction {
|
||||
int[] state;
|
||||
int action;
|
||||
public StateAction(int[] state, int action) {
|
||||
this.state = state;
|
||||
this.action = action;
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import org.toop.framework.resource.types.LoadableResource;
|
||||
|
||||
@FileExtension({"png", "jpg", "jpeg"})
|
||||
public class ImageAsset extends BaseResource implements LoadableResource {
|
||||
private Image image = null;
|
||||
private Image image;
|
||||
|
||||
public ImageAsset(final File file) {
|
||||
super(file);
|
||||
@@ -40,7 +40,8 @@ public class ImageAsset extends BaseResource implements LoadableResource {
|
||||
public Image getImage() {
|
||||
if (!this.isLoaded) {
|
||||
this.load();
|
||||
return image;
|
||||
}
|
||||
return image;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
10
game/pom.xml
10
game/pom.xml
@@ -99,6 +99,16 @@
|
||||
<artifactId>error_prone_annotations</artifactId>
|
||||
<version>2.42.0</version>
|
||||
</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>
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ public final class Reversi extends TurnBasedGame {
|
||||
private int movesTaken;
|
||||
private Set<Point> filledCells = new HashSet<>();
|
||||
private Move[] mostRecentlyFlippedPieces;
|
||||
private char[][] cachedBoard;
|
||||
|
||||
public record Score(int player1Score, int player2Score) {}
|
||||
|
||||
@@ -37,6 +38,7 @@ public final class Reversi extends TurnBasedGame {
|
||||
this.setBoard(new Move(35, 'B'));
|
||||
this.setBoard(new Move(36, 'W'));
|
||||
updateFilledCellsSet();
|
||||
cachedBoard = makeBoardAGrid();
|
||||
}
|
||||
private void updateFilledCellsSet() {
|
||||
for (int i = 0; i < 64; i++) {
|
||||
@@ -49,11 +51,13 @@ public final class Reversi extends TurnBasedGame {
|
||||
@Override
|
||||
public Move[] getLegalMoves() {
|
||||
final ArrayList<Move> legalMoves = new ArrayList<>();
|
||||
char[][] boardGrid = makeBoardAGrid();
|
||||
char[][] boardGrid = cachedBoard;
|
||||
char currentPlayer = (this.getCurrentTurn()==0) ? 'B' : 'W';
|
||||
Set<Point> adjCell = getAdjacentCells(boardGrid);
|
||||
char opponent = (currentPlayer=='W') ? 'B' : 'W';
|
||||
|
||||
Set<Point> adjCell = getAdjacentCells(boardGrid, opponent);
|
||||
for (Point point : adjCell){
|
||||
Move[] moves = getFlipsForPotentialMove(point,currentPlayer);
|
||||
Move[] moves = getFlipsForPotentialMove(point, currentPlayer, opponent, boardGrid);
|
||||
int score = moves.length;
|
||||
if (score > 0){
|
||||
legalMoves.add(new Move(point.x + point.y * this.getRowSize(), currentPlayer));
|
||||
@@ -62,18 +66,20 @@ public final class Reversi extends TurnBasedGame {
|
||||
return legalMoves.toArray(new Move[0]);
|
||||
}
|
||||
|
||||
private Set<Point> getAdjacentCells(char[][] boardGrid) {
|
||||
private Set<Point> getAdjacentCells(char[][] boardGrid, char opponent) {
|
||||
Set<Point> possibleCells = new HashSet<>();
|
||||
for (Point point : filledCells) { //for every filled cell
|
||||
for (int deltaColumn = -1; deltaColumn <= 1; deltaColumn++){ //check adjacent cells
|
||||
for (int deltaRow = -1; deltaRow <= 1; deltaRow++){ //orthogonally and diagonally
|
||||
int newX = point.x + deltaColumn, newY = point.y + deltaRow;
|
||||
if (deltaColumn == 0 && deltaRow == 0 //continue if out of bounds
|
||||
|| !isOnBoard(newX, newY)) {
|
||||
continue;
|
||||
}
|
||||
if (boardGrid[newY][newX] == EMPTY) { //check if the cell is empty
|
||||
possibleCells.add(new Point(newX, newY)); //and then add it to the set of possible moves
|
||||
if (boardGrid[point.x][point.y] == opponent) {
|
||||
for (int deltaColumn = -1; deltaColumn <= 1; deltaColumn++) { //check adjacent cells
|
||||
for (int deltaRow = -1; deltaRow <= 1; deltaRow++) { //orthogonally and diagonally
|
||||
int newX = point.x + deltaColumn, newY = point.y + deltaRow;
|
||||
if (deltaColumn == 0 && deltaRow == 0 //continue if out of bounds
|
||||
|| !isOnBoard(newX, newY)) {
|
||||
continue;
|
||||
}
|
||||
if (boardGrid[newY][newX] == EMPTY) { //check if the cell is empty
|
||||
possibleCells.add(new Point(newX, newY)); //and then add it to the set of possible moves
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -81,14 +87,14 @@ public final class Reversi extends TurnBasedGame {
|
||||
return possibleCells;
|
||||
}
|
||||
|
||||
public Move[] getFlipsForPotentialMove(Point point, char currentPlayer) {
|
||||
public Move[] getFlipsForPotentialMove(Point point, char currentPlayer, char opponent, char[][] boardGrid) {
|
||||
final ArrayList<Move> movesToFlip = new ArrayList<>();
|
||||
for (int deltaColumn = -1; deltaColumn <= 1; deltaColumn++) { //for all directions
|
||||
for (int deltaRow = -1; deltaRow <= 1; deltaRow++) {
|
||||
if (deltaColumn == 0 && deltaRow == 0){
|
||||
continue;
|
||||
}
|
||||
Move[] moves = getFlipsInDirection(point,makeBoardAGrid(),currentPlayer,deltaColumn,deltaRow);
|
||||
Move[] moves = getFlipsInDirection(point, boardGrid, currentPlayer, opponent, deltaColumn, deltaRow);
|
||||
if (moves != null) { //getFlipsInDirection
|
||||
movesToFlip.addAll(Arrays.asList(moves));
|
||||
}
|
||||
@@ -97,8 +103,14 @@ public final class Reversi extends TurnBasedGame {
|
||||
return movesToFlip.toArray(new Move[0]);
|
||||
}
|
||||
|
||||
private Move[] getFlipsInDirection(Point point, char[][] boardGrid, char currentPlayer, int dirX, int dirY) {
|
||||
char opponent = getOpponent(currentPlayer);
|
||||
public Move[] getFlipsForPotentialMove(Move move) {
|
||||
char curr = getCurrentPlayer();
|
||||
char opp = getOpponent(curr);
|
||||
Point point = new Point(move.position() % this.getRowSize(), move.position() / this.getColumnSize());
|
||||
return getFlipsForPotentialMove(point, curr, opp, cachedBoard);
|
||||
}
|
||||
|
||||
private Move[] getFlipsInDirection(Point point, char[][] boardGrid, char currentPlayer, char opponent, int dirX, int dirY) {
|
||||
final ArrayList<Move> movesToFlip = new ArrayList<>();
|
||||
int x = point.x + dirX;
|
||||
int y = point.y + dirY;
|
||||
@@ -123,7 +135,7 @@ public final class Reversi extends TurnBasedGame {
|
||||
return x >= 0 && x < this.getColumnSize() && y >= 0 && y < this.getRowSize();
|
||||
}
|
||||
|
||||
private char[][] makeBoardAGrid() {
|
||||
public char[][] makeBoardAGrid() {
|
||||
char[][] boardGrid = new char[this.getRowSize()][this.getColumnSize()];
|
||||
for (int i = 0; i < 64; i++) {
|
||||
boardGrid[i / this.getRowSize()][i % this.getColumnSize()] = this.getBoard()[i]; //boardGrid[y -> row] [x -> column]
|
||||
@@ -133,6 +145,9 @@ public final class Reversi extends TurnBasedGame {
|
||||
|
||||
@Override
|
||||
public GameState play(Move move) {
|
||||
if (cachedBoard == null) {
|
||||
cachedBoard = makeBoardAGrid();
|
||||
}
|
||||
Move[] legalMoves = getLegalMoves();
|
||||
boolean moveIsLegal = false;
|
||||
for (Move legalMove : legalMoves) { //check if the move is legal
|
||||
@@ -145,13 +160,14 @@ public final class Reversi extends TurnBasedGame {
|
||||
return null;
|
||||
}
|
||||
|
||||
Move[] moves = sortMovesFromCenter(getFlipsForPotentialMove(new Point(move.position()%this.getColumnSize(),move.position()/this.getRowSize()), move.value()),move);
|
||||
Move[] moves = sortMovesFromCenter(getFlipsForPotentialMove(new Point(move.position()%this.getColumnSize(),move.position()/this.getRowSize()), move.value(),move.value() == 'B'? 'W': 'B',makeBoardAGrid()),move);
|
||||
mostRecentlyFlippedPieces = moves;
|
||||
this.setBoard(move); //place the move on the board
|
||||
for (Move m : moves) {
|
||||
this.setBoard(m); //flip the correct pieces on the board
|
||||
}
|
||||
filledCells.add(new Point(move.position() % this.getRowSize(), move.position() / this.getColumnSize()));
|
||||
cachedBoard = makeBoardAGrid();
|
||||
nextTurn();
|
||||
if (getLegalMoves().length == 0) { //skip the players turn when there are no legal moves
|
||||
skipMyTurn();
|
||||
@@ -172,7 +188,7 @@ public final class Reversi extends TurnBasedGame {
|
||||
}
|
||||
|
||||
private void skipMyTurn(){
|
||||
IO.println("TURN " + getCurrentPlayer() + " SKIPPED");
|
||||
//IO.println("TURN " + getCurrentPlayer() + " SKIPPED");
|
||||
//TODO: notify user that a turn has been skipped
|
||||
nextTurn();
|
||||
}
|
||||
@@ -207,6 +223,32 @@ public final class Reversi extends TurnBasedGame {
|
||||
}
|
||||
return new Score(player1Score, player2Score);
|
||||
}
|
||||
|
||||
public boolean isGameOver(){
|
||||
Move[] legalMovesW = getLegalMoves();
|
||||
nextTurn();
|
||||
Move[] legalMovesB = getLegalMoves();
|
||||
nextTurn();
|
||||
if (legalMovesW.length + legalMovesB.length == 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getWinner(){
|
||||
if (!isGameOver()) {
|
||||
return 0;
|
||||
}
|
||||
Score score = getScore();
|
||||
if (score.player1Score() > score.player2Score()) {
|
||||
return 1;
|
||||
}
|
||||
else if (score.player1Score() < score.player2Score()) {
|
||||
return 2;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private Move[] sortMovesFromCenter(Move[] moves, Move center) { //sorts the pieces to be flipped for animation purposes
|
||||
int centerX = center.position()%this.getColumnSize();
|
||||
int centerY = center.position()/this.getRowSize();
|
||||
@@ -226,4 +268,34 @@ public final class Reversi extends TurnBasedGame {
|
||||
public Move[] getMostRecentlyFlippedPieces() {
|
||||
return mostRecentlyFlippedPieces;
|
||||
}
|
||||
|
||||
public int[] getBoardInt(){
|
||||
char[] input = getBoard();
|
||||
int[] result = new int[input.length];
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
switch (input[i]) {
|
||||
case 'W':
|
||||
result[i] = -1;
|
||||
break;
|
||||
case 'B':
|
||||
result[i] = 1;
|
||||
break;
|
||||
case ' ':
|
||||
default:
|
||||
result[i] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Point moveToPoint(Move move){
|
||||
return new Point(move.position()%this.getColumnSize(),move.position()/this.getRowSize());
|
||||
}
|
||||
|
||||
public void printBoard(){
|
||||
for (int row = 0; row < this.getRowSize(); row++) {
|
||||
IO.println(Arrays.toString(cachedBoard[row]));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import org.toop.game.AI;
|
||||
import org.toop.game.records.Move;
|
||||
|
||||
public final class ReversiAI extends AI<Reversi> {
|
||||
|
||||
@Override
|
||||
public Move findBestMove(Reversi game, int depth) {
|
||||
Move[] moves = game.getLegalMoves();
|
||||
|
||||
52
game/src/main/java/org/toop/game/reversi/ReversiAIML.java
Normal file
52
game/src/main/java/org/toop/game/reversi/ReversiAIML.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package org.toop.game.reversi;
|
||||
|
||||
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.game.AI;
|
||||
import org.toop.game.records.Move;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class ReversiAIML extends AI<Reversi>{
|
||||
|
||||
MultiLayerNetwork model;
|
||||
|
||||
public ReversiAIML() {
|
||||
InputStream is = getClass().getResourceAsStream("/reversi-model.zip");
|
||||
try {
|
||||
assert is != null;
|
||||
model = ModelSerializer.restoreMultiLayerNetwork(is);
|
||||
} catch (IOException e) {}
|
||||
}
|
||||
|
||||
public Move findBestMove(Reversi reversi, int depth){
|
||||
int[] input = reversi.getBoardInt();
|
||||
|
||||
INDArray boardInput = Nd4j.create(new int[][] { input });
|
||||
INDArray prediction = model.output(boardInput);
|
||||
|
||||
int move = pickLegalMove(prediction, reversi);
|
||||
return new Move(move, reversi.getCurrentPlayer());
|
||||
}
|
||||
|
||||
private int pickLegalMove(INDArray prediction, Reversi reversi){
|
||||
double[] probs = prediction.toDoubleVector();
|
||||
Move[] legalMoves = reversi.getLegalMoves();
|
||||
|
||||
if (legalMoves.length == 0) return -1;
|
||||
|
||||
int bestMove = legalMoves[0].position();
|
||||
double bestVal = probs[bestMove];
|
||||
|
||||
for (Move move : legalMoves){
|
||||
if (probs[move.position()] > bestVal){
|
||||
bestMove = move.position();
|
||||
bestVal = probs[bestMove];
|
||||
}
|
||||
}
|
||||
return bestMove;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package org.toop.game.reversi;
|
||||
|
||||
import org.toop.game.AI;
|
||||
import org.toop.game.records.Move;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class ReversiAISimple extends AI<Reversi> {
|
||||
|
||||
@Override
|
||||
public Move findBestMove(Reversi game, int depth) {
|
||||
//IO.println("****START FIND BEST MOVE****");
|
||||
|
||||
Move[] moves = game.getLegalMoves();
|
||||
|
||||
|
||||
//game.printBoard();
|
||||
//IO.println("Legal moves: " + Arrays.toString(moves));
|
||||
|
||||
Move bestMove;
|
||||
Move bestMoveScore = moves[0];
|
||||
Move bestMoveOptions = moves[0];
|
||||
int bestScore = -1;
|
||||
int bestOptions = -1;
|
||||
for (Move move : moves){
|
||||
int numOpt = getNumberOfOptions(game, move);
|
||||
if (numOpt > bestOptions) {
|
||||
bestOptions = numOpt;
|
||||
bestMoveOptions = move;
|
||||
}
|
||||
int numSco = getScore(game, move);
|
||||
if (numSco > bestScore) {
|
||||
bestScore = numSco;
|
||||
bestMoveScore = move;
|
||||
}
|
||||
|
||||
//IO.println("Move: " + move.position() + ". Options: " + numOpt + ". Score: " + numSco);
|
||||
}
|
||||
if (bestScore > bestOptions) {
|
||||
bestMove = bestMoveScore;
|
||||
}
|
||||
else{
|
||||
bestMove = bestMoveOptions;
|
||||
}
|
||||
return bestMove;
|
||||
}
|
||||
|
||||
private int getNumberOfOptions(Reversi game, Move move){
|
||||
Reversi copy = new Reversi(game);
|
||||
copy.play(move);
|
||||
return copy.getLegalMoves().length;
|
||||
}
|
||||
|
||||
private int getScore(Reversi game, Move move){
|
||||
return game.getFlipsForPotentialMove(move).length;
|
||||
}
|
||||
}
|
||||
@@ -8,17 +8,24 @@ import org.toop.game.enumerators.GameState;
|
||||
import org.toop.game.records.Move;
|
||||
import org.toop.game.reversi.Reversi;
|
||||
import org.toop.game.reversi.ReversiAI;
|
||||
import org.toop.game.reversi.ReversiAIML;
|
||||
import org.toop.game.reversi.ReversiAISimple;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ReversiTest {
|
||||
private Reversi game;
|
||||
private ReversiAI ai;
|
||||
private ReversiAIML aiml;
|
||||
private ReversiAISimple aiSimple;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
game = new Reversi();
|
||||
ai = new ReversiAI();
|
||||
aiml = new ReversiAIML();
|
||||
aiSimple = new ReversiAISimple();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -190,4 +197,35 @@ class ReversiTest {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAIvsAIML(){
|
||||
IO.println("Testing AI simple ...");
|
||||
int totalGames = 5000;
|
||||
int p1wins = 0;
|
||||
int p2wins = 0;
|
||||
int draws = 0;
|
||||
for (int i = 0; i < totalGames; i++) {
|
||||
game = new Reversi();
|
||||
while (!game.isGameOver()) {
|
||||
char curr = game.getCurrentPlayer();
|
||||
if (curr == 'W') {
|
||||
game.play(ai.findBestMove(game,5));
|
||||
}
|
||||
else {
|
||||
game.play(ai.findBestMove(game,5));
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user