mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
add: cli ui connection with server
This commit is contained in:
@@ -7,6 +7,10 @@ import org.toop.eventbus.GlobalEventBus;
|
|||||||
import org.toop.game.*;
|
import org.toop.game.*;
|
||||||
import org.toop.game.tictactoe.*;
|
import org.toop.game.tictactoe.*;
|
||||||
|
|
||||||
|
import com.google.errorprone.annotations.Keep;
|
||||||
|
|
||||||
|
import org.toop.eventbus.*;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
@@ -20,8 +24,12 @@ public class ConsoleGui {
|
|||||||
private TicTacToe game;
|
private TicTacToe game;
|
||||||
private MinMaxTicTacToe ai;
|
private MinMaxTicTacToe ai;
|
||||||
|
|
||||||
String ai1 = null;
|
private String ai1 = null;
|
||||||
String ai2 = null;
|
private String ai2 = null;
|
||||||
|
|
||||||
|
private String serverId = null;
|
||||||
|
private String connectionId = null;
|
||||||
|
private String ticTacToeGameId = null;
|
||||||
|
|
||||||
public ConsoleGui() throws ExecutionException, InterruptedException {
|
public ConsoleGui() throws ExecutionException, InterruptedException {
|
||||||
scanner = new Scanner(System.in);
|
scanner = new Scanner(System.in);
|
||||||
@@ -94,6 +102,19 @@ public class ConsoleGui {
|
|||||||
|
|
||||||
game = new TicTacToe(player1, player2);
|
game = new TicTacToe(player1, player2);
|
||||||
ai = new MinMaxTicTacToe();
|
ai = new MinMaxTicTacToe();
|
||||||
|
|
||||||
|
CompletableFuture<String> serverIdFuture = new CompletableFuture<>();
|
||||||
|
GlobalEventBus.post(new Events.ServerEvents.StartServerRequest("5001", "tictactoe", serverIdFuture));
|
||||||
|
serverId = serverIdFuture.get();
|
||||||
|
|
||||||
|
CompletableFuture<String> connectionIdFuture = new CompletableFuture<>();
|
||||||
|
GlobalEventBus.post(new Events.ServerEvents.StartConnectionRequest("127.0.0.1", "5001", connectionIdFuture));
|
||||||
|
connectionId = connectionIdFuture.get();
|
||||||
|
|
||||||
|
CompletableFuture<String> ticTacToeGame = new CompletableFuture<>();
|
||||||
|
GlobalEventBus.post(new Events.ServerEvents.CreateTicTacToeGameRequest(serverId, player1, player2, ticTacToeGame));
|
||||||
|
ticTacToeGameId = ticTacToeGame.get();
|
||||||
|
GlobalEventBus.post(new Events.ServerEvents.RunTicTacToeGame(serverId, ticTacToeGameId));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void print() {
|
public void print() {
|
||||||
@@ -134,6 +155,7 @@ public class ConsoleGui {
|
|||||||
}
|
}
|
||||||
|
|
||||||
GameBase.State state = game.play(move);
|
GameBase.State state = game.play(move);
|
||||||
|
boolean keepRunning = true;
|
||||||
|
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case INVALID: {
|
case INVALID: {
|
||||||
@@ -143,18 +165,34 @@ public class ConsoleGui {
|
|||||||
|
|
||||||
case DRAW: {
|
case DRAW: {
|
||||||
System.out.println("Game ended in a draw.");
|
System.out.println("Game ended in a draw.");
|
||||||
return false;
|
keepRunning = false;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case WIN: {
|
case WIN: {
|
||||||
System.out.printf("%s has won the game.\n", game.getCurrentPlayer().name());
|
System.out.printf("%s has won the game.\n", current.name());
|
||||||
return false;
|
keepRunning = false;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case NORMAL:
|
case NORMAL:
|
||||||
default: return true;
|
default: {
|
||||||
|
keepRunning = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GlobalEventBus.post(new Events.ServerEvents.SendCommand(
|
||||||
|
connectionId,
|
||||||
|
"gameid " + ticTacToeGameId, current.name(), "MOVE", String.valueOf(move)
|
||||||
|
));
|
||||||
|
|
||||||
|
if (!keepRunning) {
|
||||||
|
GlobalEventBus.post(new Events.ServerEvents.EndTicTacToeGame(serverId, ticTacToeGameId));
|
||||||
|
}
|
||||||
|
|
||||||
|
return keepRunning;
|
||||||
|
}
|
||||||
|
|
||||||
public GameBase getGame() { return game; }
|
public GameBase getGame() { return game; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,41 +20,9 @@ public class Main {
|
|||||||
private static final Logger logger = LogManager.getLogger(Main.class);
|
private static final Logger logger = LogManager.getLogger(Main.class);
|
||||||
|
|
||||||
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
||||||
|
|
||||||
initSystems();
|
initSystems();
|
||||||
|
|
||||||
CompletableFuture<String> serverIdFuture = new CompletableFuture<>();
|
|
||||||
GlobalEventBus.post(new Events.ServerEvents.StartServerRequest("5001", "tictactoe", serverIdFuture));
|
|
||||||
String serverId = serverIdFuture.get();
|
|
||||||
|
|
||||||
CompletableFuture<String> connectionIdFuture = new CompletableFuture<>();
|
|
||||||
GlobalEventBus.post(new Events.ServerEvents.StartConnectionRequest("127.0.0.1", "5001", connectionIdFuture));
|
|
||||||
String connectionId = connectionIdFuture.get();
|
|
||||||
|
|
||||||
CompletableFuture<String> ticTacToeGame = new CompletableFuture<>();
|
|
||||||
GlobalEventBus.post(new Events.ServerEvents.CreateTicTacToeGameRequest(serverId, "J", "P", ticTacToeGame));
|
|
||||||
String ticTacToeGameId = ticTacToeGame.get();
|
|
||||||
GlobalEventBus.post(new Events.ServerEvents.RunTicTacToeGame(serverId, ticTacToeGameId));
|
|
||||||
|
|
||||||
GlobalEventBus.post(new Events.ServerEvents.SendCommand(
|
|
||||||
connectionId,
|
|
||||||
"gameid " + ticTacToeGameId, "player J", "MOVE", "0"
|
|
||||||
));
|
|
||||||
GlobalEventBus.post(new Events.ServerEvents.SendCommand(
|
|
||||||
connectionId,
|
|
||||||
"gameid " + ticTacToeGameId, "player P", "MOVE", "1"
|
|
||||||
));
|
|
||||||
|
|
||||||
// for (int x = 0; x < 20000000; x++) {
|
|
||||||
// CompletableFuture<String> ticTacToeGame = new CompletableFuture<>();
|
|
||||||
// GlobalEventBus.post(new Events.ServerEvents.CreateTicTacToeGameRequest(serverId, "J"+x, "P"+x, ticTacToeGame));
|
|
||||||
// String ticTacToeGameId = ticTacToeGame.get();
|
|
||||||
// GlobalEventBus.post(new Events.ServerEvents.RunTicTacToeGame(serverId, ticTacToeGameId));
|
|
||||||
// GlobalEventBus.post(new Events.ServerEvents.Command(connectionId, "MOVE", "" + x));
|
|
||||||
// }
|
|
||||||
|
|
||||||
ConsoleGui console = new ConsoleGui();
|
ConsoleGui console = new ConsoleGui();
|
||||||
GameBase.State state = GameBase.State.INVALID;
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
console.print();
|
console.print();
|
||||||
|
|||||||
Reference in New Issue
Block a user