rest van de tutorials toegevoegd

This commit is contained in:
michiel
2025-11-27 11:42:38 +01:00
parent 8c69453506
commit c14b66e892
28 changed files with 314 additions and 52 deletions

View File

@@ -22,6 +22,17 @@ public class GameInformation {
public int getMaxDepth() {
return maxDepth;
}
public String getTypeToString() {
String name = this.name();
return switch (name) {
case "TICTACTOE" -> "TicTacToe";
case "REVERSI" -> "Reversi";
case "CONNECT4" -> "Connect4";
case "BATTLESHIP" -> "Battleship";
default -> name;
};
}
}
public static class Player {

View File

@@ -47,16 +47,17 @@ public abstract class BaseGameThread<TGame extends Game, TAI, TCanvas> {
this.game = gameSupplier.get();
this.ai = aiSupplier.get();
if (onForfeit == null || onExit == null) {
primary = new GameView(null, () -> {
isRunning.set(false);
WidgetContainer.getCurrentView().transitionPrevious();
}, null);
String type = information.type.getTypeToString();
if (onForfeit == null || onExit == null) {
primary = new GameView(null, () -> {
isRunning.set(false);
WidgetContainer.getCurrentView().transitionPrevious();
}, null, type);
} else {
primary = new GameView(onForfeit, () -> {
isRunning.set(false);
onExit.run();
}, onMessage);
}, onMessage, type);
}
this.canvas = canvasFactory.apply(this::onCellClicked);

View File

@@ -5,9 +5,8 @@ import javafx.scene.paint.Color;
import org.toop.app.App;
import org.toop.app.GameInformation;
import org.toop.app.canvas.Connect4Canvas;
import org.toop.app.view.ViewStack;
import org.toop.app.view.views.GameView;
import org.toop.app.view.views.LocalMultiplayerView;
import org.toop.app.widget.view.GameView;
import org.toop.app.widget.WidgetContainer;
import org.toop.framework.eventbus.EventFlow;
import org.toop.framework.networking.events.NetworkEvents;
import org.toop.game.Connect4.Connect4;
@@ -31,7 +30,7 @@ public class Connect4Game {
private final int columnSize = 7;
private final int rowSize = 6;
private final GameView view;
private final GameView primary;
private final Connect4Canvas canvas;
private final AtomicBoolean isRunning;
@@ -49,15 +48,15 @@ public class Connect4Game {
isRunning = new AtomicBoolean(true);
if (onForfeit == null || onExit == null) {
view = new GameView(null, () -> {
primary = new GameView(null, () -> {
isRunning.set(false);
ViewStack.push(new LocalMultiplayerView(information));
}, null);
WidgetContainer.getCurrentView().transitionPrevious();
}, null, "Connect4");
} else {
view = new GameView(onForfeit, () -> {
primary = new GameView(onForfeit, () -> {
isRunning.set(false);
onExit.run();
}, onMessage);
}, onMessage, "Connect4");
}
canvas = new Connect4Canvas(Color.GRAY,
@@ -82,8 +81,8 @@ public class Connect4Game {
}
});
view.add(Pos.CENTER, canvas.getCanvas());
ViewStack.push(view);
primary.add(Pos.CENTER, canvas.getCanvas());
WidgetContainer.getCurrentView().transitionNext(primary);
if (onForfeit == null || onExit == null) {
new Thread(this::localGameThread).start();
@@ -91,8 +90,7 @@ public class Connect4Game {
} else {
new EventFlow()
.listen(NetworkEvents.GameMoveResponse.class, this::onMoveResponse)
.listen(NetworkEvents.YourTurnResponse.class, this::onYourTurnResponse)
.listen(NetworkEvents.ReceivedMessage.class, this::onReceivedMessage);
.listen(NetworkEvents.YourTurnResponse.class, this::onYourTurnResponse);
setGameLabels(myTurn == 0);
}
@@ -108,7 +106,7 @@ public class Connect4Game {
final String currentValue = currentTurn == 0? "RED" : "BLUE";
final int nextTurn = (currentTurn + 1) % information.type.getPlayerCount();
view.nextPlayer(information.players[currentTurn].isHuman,
primary.nextPlayer(information.players[currentTurn].isHuman,
information.players[currentTurn].name,
currentValue,
information.players[nextTurn].name);
@@ -158,9 +156,9 @@ public class Connect4Game {
*/
if (state != Game.State.NORMAL) {
if (state == Game.State.WIN) {
view.gameOver(true, information.players[currentTurn].name);
primary.gameOver(true, information.players[currentTurn].name);
} else if (state == Game.State.DRAW) {
view.gameOver(false, "");
primary.gameOver(false, "");
}
isRunning.set(false);
@@ -187,14 +185,14 @@ public class Connect4Game {
if (state != Game.State.NORMAL) {
if (state == Game.State.WIN) {
if (response.player().equalsIgnoreCase(information.players[0].name)) {
view.gameOver(true, information.players[0].name);
primary.gameOver(true, information.players[0].name);
gameOver();
} else {
view.gameOver(false, information.players[1].name);
primary.gameOver(false, information.players[1].name);
gameOver();
}
} else if (state == Game.State.DRAW) {
view.gameOver(false, "");
primary.gameOver(false, "");
gameOver();
}
}
@@ -242,14 +240,6 @@ public class Connect4Game {
.postEvent();
}
private void onReceivedMessage(NetworkEvents.ReceivedMessage msg) {
if (!isRunning.get()) {
return;
}
view.updateChat(msg.message());
}
private void updateCanvas() {
canvas.clearAll();
@@ -266,7 +256,7 @@ public class Connect4Game {
final int currentTurn = game.getCurrentTurn();
final String currentValue = currentTurn == 0? "RED" : "BLUE";
view.nextPlayer(isMe,
primary.nextPlayer(isMe,
information.players[isMe? 0 : 1].name,
currentValue,
information.players[isMe? 1 : 0].name);

View File

@@ -53,12 +53,12 @@ public final class ReversiGame {
primary = new GameView(null, () -> {
isRunning.set(false);
WidgetContainer.getCurrentView().transitionPrevious();
}, null);
}, null, "Reversi");
} else {
primary = new GameView(onForfeit, () -> {
isRunning.set(false);
onExit.run();
}, onMessage);
}, onMessage, "Reversi");
}
canvas = new ReversiCanvas(Color.BLACK,

View File

@@ -50,12 +50,12 @@ public final class TicTacToeGame {
primary = new GameView(null, () -> {
isRunning.set(false);
WidgetContainer.getCurrentView().transitionPrevious();
}, null);
}, null, "TicTacToe");
} else {
primary = new GameView(onForfeit, () -> {
isRunning.set(false);
onExit.run();
}, onMessage);
}, onMessage, "TicTacToe");
}
canvas = new TicTacToeCanvas(Color.GRAY,

View File

@@ -0,0 +1,39 @@
package org.toop.app.widget.tutorial;
import javafx.geometry.Pos;
import org.toop.app.widget.complex.ViewWidget;
import java.io.File;
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()]);
}
}

View File

@@ -0,0 +1,39 @@
package org.toop.app.widget.tutorial;
import javafx.geometry.Pos;
import org.toop.app.widget.complex.ViewWidget;
import java.io.File;
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()]);
}
}

View File

@@ -15,7 +15,6 @@ public class TicTacToeTutorialWidget extends ViewWidget {
private BaseTutorialWidget tutorialWidget;
public TicTacToeTutorialWidget() {
System.out.println("Hi, I got here!");
this.state = new TState(keys.length);
tutorialWidget = new BaseTutorialWidget(
state,
@@ -34,7 +33,6 @@ public class TicTacToeTutorialWidget extends ViewWidget {
}
}
);
System.out.println("Hi, I got to the end!");
add(Pos.CENTER, tutorialWidget);
}

View File

@@ -11,6 +11,10 @@ import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
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;
public final class GameView extends ViewWidget {
private final Text currentPlayerHeader;
@@ -18,10 +22,10 @@ public final class GameView extends ViewWidget {
private final Text nextPlayerHeader;
private final Button forfeitButton;
private final Button exitButton;
private final Button tutorialButton;
private final TextField chatInput;
public GameView(Runnable onForfeit, Runnable onExit, Consumer<String> onMessage) {
public GameView(Runnable onForfeit, Runnable onExit, Consumer<String> onMessage, String gameType) {
currentPlayerHeader = Primitive.header("");
currentMoveHeader = Primitive.header("");
nextPlayerHeader = Primitive.header("");
@@ -47,6 +51,27 @@ public final class GameView extends ViewWidget {
chatInput = null;
}
switch(gameType) {
case "TicTacToe":
this.tutorialButton = Primitive.button("tutorialstring", () -> {
transitionNext(new TicTacToeTutorialWidget());
});
break;
case "Reversi":
this.tutorialButton = Primitive.button("tutorialstring", () -> {
transitionNext(new ReversiTutorialWidget());
});
break;
case "Connect4":
this.tutorialButton = Primitive.button("tutorialstring", () -> {
transitionNext(new Connect4TutorialWidget());
});
break;
default:
this.tutorialButton = null;
break;
}
setupLayout();
}
@@ -73,6 +98,10 @@ public final class GameView extends ViewWidget {
if (chatInput != null) {
add(Pos.BOTTOM_RIGHT, Primitive.vbox(chatInput));
}
if (tutorialButton != null) {
add(Pos.TOP_LEFT, tutorialButton);
}
}
public void nextPlayer(boolean isMe, String currentPlayer, String currentMove, String nextPlayer) {

View File

@@ -11,6 +11,8 @@ 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.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;
@@ -37,8 +39,6 @@ public class LocalMultiplayerView extends ViewWidget {
switch (information.type) {
case TICTACTOE:
System.out.println(AppSettings.getSettings().getTutorialFlag());
System.out.println(AppSettings.getSettings().getFirstTTT());
if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstTTT()) {
BaseTutorialWidget a = new BaseTutorialWidget(
"tutorial",
@@ -49,11 +49,10 @@ public class LocalMultiplayerView extends ViewWidget {
});
},
() -> {
Platform.runLater(() -> {
ViewWidget c = new TicTacToeTutorialWidget();
transitionNext(c);
WidgetContainer.setCurrentView(c);
});
AppSettings.getSettings().setFirstTTT(false);
},
() -> {
AppSettings.getSettings().setTutorialFlag(false);
@@ -67,11 +66,65 @@ public class LocalMultiplayerView extends ViewWidget {
}
new TicTacToeGameThread(information);
break;
case REVERSI: new ReversiGame(information);
case CONNECT4: new Connect4Game(information);
case REVERSI:
if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstReversi()) {
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()) {
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();

View File

@@ -21,7 +21,7 @@ public class AppSettings {
settingsAsset.load();
}
doDefaultSettings();
checkSettings();
Settings settingsData = settingsAsset.getContent();