Skip to content

Commit

Permalink
Fixes endless loop in AI. Again.
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian-Wollersberger committed Jun 6, 2018
1 parent 87f4da8 commit e1b4da3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
3 changes: 3 additions & 0 deletions AI/src/at/dropical/wolliAI/AiMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public class AiMain {
public static void main(String[] args) throws InterruptedException {
AI ai = new BestPossibilityAI(new ServerAdapter());

//Temp
//AI ai2 = new BestPossibilityAI(new ServerAdapter());

while(true) {
Thread.sleep(100);
try {
Expand Down
6 changes: 4 additions & 2 deletions AI/src/at/dropical/wolliAI/bestPossibility/GameField.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,14 @@ public GameField clone() {

/** Move the tetromino as far left as possible. */
void moveToLeftBorder() {
while(arena.checkTetromino(mino, posH, posW -1, true))
//replaced while with for loop and sanity check.
for(int i = 0; i<TetrisArena.width && arena.checkTetromino(mino, posH, posW -1, true); i++)
posW--;
}
/** Does not place the tetromino. */
void moveToBottom() {
while(arena.checkTetromino(mino, posH +1, posW, true))
//replaced while with for loop and sanity check.
for(int i = 0; i<TetrisArena.height && arena.checkTetromino(mino, posH +1, posW, true); i++)
posH++;
}
/** Does as if moveToBottom() was never invoked. */
Expand Down
5 changes: 4 additions & 1 deletion AI/src/at/dropical/wolliAI/gamefieldCopy/TetrisArena.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ private void fromArray(int[][] newArena) throws IllegalArgumentException {
* blocks at those positions. */
public boolean checkTetromino(Tetromino tetromino, int h, int w, boolean overTopAllowed) {
int[][] tetrominoArr = tetromino.toArray();
boolean emptyTetromino = true;
for(int i = 0; i < Tetromino.size; i++) {
for(int j = 0; j < Tetromino.size; j++) {
// Only care if there is a block
if(tetrominoArr[i][j] >= 1) {
emptyTetromino = false;

// If space is occupied Or out of bounds of actual arena
if(arena[marginTop + h + i][marginLeftRight + w + j] >= 1
Expand All @@ -95,7 +97,8 @@ public boolean checkTetromino(Tetromino tetromino, int h, int w, boolean overTop
}
}
// Only when all 4*4 spaces of the Tetromino are ok.
return true;
// Special case when tetromino is empty, then return false. To prevent endless loops.
return !emptyTetromino;
}

/** Only the actual arena. */
Expand Down
6 changes: 4 additions & 2 deletions AI/src/at/dropical/wolliAI/serverAdapter/ServerAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import java.util.*
* in this class.
*/
class ServerAdapter(
player: String = "Wolli AI",
player: String = "Wolli AI "+ Math.random(),
hostName: String = "localhost",
port: Int = 45000
): DropicalListener {
Expand All @@ -37,6 +37,7 @@ class ServerAdapter(

private val server: DropicalProxy = DropicalProxy(hostName, port, this)
private val playerName = player

init {
/** Auto-queue to a game on the server. */
server.writeToServer(JoinRequest(playerName))
Expand All @@ -47,12 +48,13 @@ class ServerAdapter(
/* The DropicalProxy calls these functions. */
override fun updateUI(container: GameDataContainer?) {
if (container != null) {
val i = 0
var i = 0
while (i < container.playernames.size) {
if (container.playernames[i] == playerName) {
index = i
break
}
i++;
}
newestGameDataContainer = container
}
Expand Down

0 comments on commit e1b4da3

Please sign in to comment.