Skip to content

Commit

Permalink
Play agains AI:
Browse files Browse the repository at this point in the history
Das TestUI kann mit dem JoinRequest dem Server sagen, es will gegen eine KI spielen.
Dies war der Sinn dieses Branches.
  • Loading branch information
Julian-Wollersberger committed Jun 12, 2018
1 parent 4c4b240 commit 7a84705
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 30 deletions.
27 changes: 16 additions & 11 deletions AI/src/at/dropical/wolliAI/AiMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,26 @@
* localhost server.
*/
public class AiMain {
/** Execute direktly */
public static void main(String[] args) throws InterruptedException {
AI ai = new BestPossibilityAI(new ServerAdapter());
new AiMain(new ServerAdapter()).loop();
}
/** Called from Server. */
public static void newAIconnection(String gameID) throws InterruptedException {
new AiMain(new ServerAdapter(gameID)).loop();
}

//Temp to occupy full game
//AI ai2 = new TryToLoseAI(new ServerAdapter());

while(true) {
Thread.sleep(100);
try {
ai.process();
//ai2.process();
private AI ai;

} catch(IndexOutOfBoundsException e) {
System.err.println(e.getMessage());
}
public AiMain(ServerAdapter adapter) {
ai = new BestPossibilityAI(adapter);
}

private void loop() throws InterruptedException{
while(!Thread.currentThread().isInterrupted()) {
Thread.sleep(100);
ai.process();
}
}

Expand Down
18 changes: 10 additions & 8 deletions AI/src/at/dropical/wolliAI/ServerAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import at.dropical.shared.net.requests.JoinRequest
* in this class.
*/
class ServerAdapter(
player: String = "Wolli AI "+ Math.random(),
hostName: String = "localhost",
port: Int = 45000
player: String = "Wolli AI "+ Math.random(),
hostName: String = "localhost",
port: Int = 45000,
gameName: String? = null
): DropicalListener {

private var newestGameDataContainer: GameDataContainer? = null
Expand All @@ -37,10 +38,11 @@ class ServerAdapter(

init {
/** Auto-queue to a game on the server. */
server.writeToServer(JoinRequest(playerName))
println("gejoined")
server.writeToServer(JoinRequest(gameName, playerName))
println("gejoint")
}

// Needed because Java.
constructor(gameID: String) : this(gameName = gameID)

/* The DropicalProxy calls these functions. */
override fun updateUI(container: GameDataContainer?) {
Expand Down Expand Up @@ -122,9 +124,9 @@ class ServerAdapter(
server.writeToServer(HandleInputRequest(playerName, input))
}

/** For performance and clarity */
/** For performance and readability */
companion object {
// Size: 20*10 TODO
// Size: 20*10
val emptyArena = Array<IntArray>(20, { IntArray(10) })
//4*4
val emptyTetromino = Array<IntArray>(4, { IntArray(4) })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public BestPlace findBestColumn(GameField field) {
field.resetToTop();
} while(field.moveRight());

System.out.println(line);
//System.out.println(line);
return new BestPlace(bestColumn, 0, bestColumnCount);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import at.dropical.wolliAI.ServerAdapter;

/**
* TODO Description
* See Package-info.
*/
public class BestPossibilityAI implements AI {
Expand Down
4 changes: 3 additions & 1 deletion client/src/at/dropical/client/impl/testUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ public void start(Stage primaryStage) throws IOException {
stage.show();

proxy = new DropicalProxy("localhost", 45000, this);
proxy.writeToServer(new JoinRequest(BESCHTER_PLAYERNAME));
//fixme Change that back.
//proxy.writeToServer(new JoinRequest(BESCHTER_PLAYERNAME));
proxy.writeToServer(new JoinRequest(BESCHTER_PLAYERNAME, true));

}

Expand Down
5 changes: 3 additions & 2 deletions server/src/at/dropical/server/GameManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ private void joinGame(@Nullable String gameID, @NotNull String playerName, boole
if(gameID != null && !gameID.equals(""))
game = joinExistingGame(gameID, playerName, trans);
else game = autoJoinOrCreateGame(playerName, trans);
//if(playAgainsAI) //TODO
//joinExistingGame(new AI);

if(playAgainsAI)
Server.startLocalAI(game.getName());
}

private Game joinExistingGame(String gameID, String playerName, Transmitter trans) {
Expand Down
14 changes: 14 additions & 0 deletions server/src/at/dropical/server/Server.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package at.dropical.server;

import at.dropical.server.logging.LoggerSetup;
import at.dropical.wolliAI.AiMain;

import java.io.IOException;
import java.net.ServerSocket;
Expand Down Expand Up @@ -73,4 +74,17 @@ public static void execute(Runnable runnable) {
public GameManager getManager() {
return manager;
}


/** It communicates over a socket instead of localTransmitter. */
static void startLocalAI(String gameID) {
execute(() -> {
try {
AiMain.newAIconnection(gameID);
} catch(InterruptedException e) {
log(Level.INFO, "AI was interrupted.");
}
});

}
}
3 changes: 1 addition & 2 deletions server/src/at/dropical/server/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public Game(int playercount, String name) {
this.necessaryPlayers =playercount;
}

/** TODO Now the game is a own Thread,
* but it doesn't run in the Server executor. */
/** TODO game doesn't run in the Server executor. */
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
Expand Down
2 changes: 0 additions & 2 deletions server/src/at/dropical/server/logging/LoggerSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import java.util.logging.*;

/**
* fixme How do I use this Logger ?
* TODO Replace "System.err.println" with the logger.
*
*/
public class LoggerSetup {
Expand Down
3 changes: 1 addition & 2 deletions shared/src/at/dropical/shared/PlayerAction.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package at.dropical.shared;
/** Up means turn.
*
* TODO We should change that for our version and
* turnLeft and turnRight should be possible
* TODO turnLeft and turnRight should be possible in our version
*/


Expand Down

0 comments on commit 7a84705

Please sign in to comment.