Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Main processor #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions main-processor/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.project
/.settings
/.classpath
/target
20 changes: 20 additions & 0 deletions main-processor/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.uni-leipzig.life</groupId>
<artifactId>icfp2014-main</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ICFP Contest 2014 - Main Processor Simulator</name>
<description>Simulating the main processor for testing the LambdaManAI.</description>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>17.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package de.uni_leipzig.life.icfp2014.ghost;


public class DumbGhostAI implements GhostAI {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package de.uni_leipzig.life.icfp2014.ghost;

public interface GhostAI {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package de.uni_leipzig.life.icfp2014.main;

import static com.google.common.base.Preconditions.checkArgument;

enum CellContent {
WALL(true, false, '#'),
SPACE(true, true, ' '),
PILL(true, true, '.'),
POWER_PILL(true, false, 'o'),
FRUIT(true, false, '%'),
LAMBDA_MAN(true, false, '/'),
GHOST(false, false, '=');

private final boolean isSafe;
private final boolean isFree;
private char symbol;

private CellContent(boolean isSafe, boolean isFree, char symbol) {
this.isSafe = isSafe;
this.isFree = isFree;
this.symbol = symbol;

}

static CellContent valueOf(int ordinal) {
checkArgument(ordinal >= 0 && ordinal < values().length, "Given ordinal %s is not in range [0,%s]",
ordinal, values().length - 1);
return values()[ordinal];
}

boolean isSafe() {
return isSafe;
}

boolean isFree() {
return isFree;
}

public char toSymbol() {
return symbol;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package de.uni_leipzig.life.icfp2014.main;

import java.util.List;


public interface LambdaManAI {
List<Object> main (List<Object> initialWorldState, Object undocumentedThing);

List<Object> step (List<Object> currentAIState, List<Object> currentWorldState);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package de.uni_leipzig.life.icfp2014.main;

import de.uni_leipzig.life.icfp2014.ghost.DumbGhostAI;

import com.google.common.collect.Lists;
import de.uni_leipzig.life.icfp2014.ghost.GhostAI;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;
import static java.util.Collections.singletonList;

public class Main {

private static LambdaManAI lambdaManAI;

public static void main(String[] args) {
try {
boolean won = true;
int level = 1;
LambdaManAI lambdaManAI = getLambdaManAI();
List<GhostAI> ghostAIs = getGhostAIs();
MainProcessor mainProcessor = new MainProcessor(lambdaManAI, ghostAIs);

while(won) {
won = mainProcessor.runLevel(getRandomMap(level), level);
level++;
}
} catch (Exception e) {
e.printStackTrace();
}
}

private static MazeMap getRandomMap(int level) {
return new MazeMap(level);
}

private static LambdaManAI getLambdaManAI() {
return lambdaManAI;
}

private static List<GhostAI> getGhostAIs() {
List<GhostAI> ghostAIs = newArrayList();

ghostAIs.add(new DumbGhostAI());

return ghostAIs;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package de.uni_leipzig.life.icfp2014.main;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import de.uni_leipzig.life.icfp2014.ghost.GhostAI;

import java.util.List;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkArgument;

public class MainProcessor {

private final LambdaManAI lambdaManAI;
private List<GhostAI> ghostAIs;
private int level;

public MainProcessor(LambdaManAI lambdaManAI, List<GhostAI> ghostAIs) {
checkNotNull(lambdaManAI);
checkArgument(!ghostAIs.isEmpty(), "No ghost ai given.");
this.ghostAIs = ghostAIs;
this.lambdaManAI = lambdaManAI;
}

public boolean runLevel(MazeMap map, int level) {
checkNotNull(map);
this.level = level;
LevelState state = initializeState(map);
while(!state.isFinished()) {

}

return false;
}

private LevelState initializeState(MazeMap map) {
return new LevelState(map);
}

public class LevelState {

public LevelState(MazeMap map) {

}

public boolean isFinished() {
// TODO Auto-generated method stub
return false;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package de.uni_leipzig.life.icfp2014.main;

import sun.security.util.Length;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;

import java.util.List;
import java.util.Random;

import static de.uni_leipzig.life.icfp2014.main.CellContent.POWER_PILL;
import static de.uni_leipzig.life.icfp2014.main.CellContent.GHOST;
import static java.lang.String.format;
import static de.uni_leipzig.life.icfp2014.main.CellContent.FRUIT;
import static com.google.common.base.Preconditions.checkState;
import static de.uni_leipzig.life.icfp2014.main.CellContent.LAMBDA_MAN;
import static de.uni_leipzig.life.icfp2014.main.CellContent.WALL;
import static de.uni_leipzig.life.icfp2014.main.CellContent.PILL;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static com.google.common.collect.Lists.newArrayList;

public class MazeMap {
private final int level;
private final List<List<Integer>> map;

public MazeMap(int level) {
this.level = level;
map = generateMap();
}

private List<List<Integer>> generateMap() {
Random rand = new Random();
int width = rand.nextInt(level * 100 / 2) + 1;
int height = level * 100 / width;
List<List<Integer>> map = simpleMap(width, height);
insertItem(map, width, height, LAMBDA_MAN, true, "No lambda man starting position found.");
insertItem(map, width, height, FRUIT, false, "No fruit starting position found.");
insertItems(map, width, height, GHOST, rand.nextInt(min(level * 2, 8)) + 1);
insertItems(map, width, height, POWER_PILL, max(2, 10 / (rand.nextInt(level) + 1)));

return map;
}


private void insertItems(List<List<Integer>> map, int width, int height, CellContent itemType, int itemCount) {
for (int i = 0; i < itemCount; i++) {
insertItem(map, width, height, itemType,
true, format("No %s #%d starting position found.", itemType.toString(), i + 1));
}
}

private void insertItem(List<List<Integer>> map, int width, int height, CellContent cellContent, boolean checkSafety, String errorMessage) {
Random rand = new Random();
int initialX = rand.nextInt(width);
int initialY = rand.nextInt(height);
boolean found = false;

for (int i = 0; i < height; i++) {
int currentY = (initialY + i) % height;

for (int j = 0; j < width; j++) {
int currentX = (initialX + j) % width;
if (CellContent.valueOf(map.get(currentY).get(currentX)).isFree()
&& (!checkSafety || areAdjacentCellsSafe(map, currentX, currentY, width, height))) {
map.get(currentY).set(currentX, cellContent.ordinal());
found = true;
break;
}
}
if (found) {
break;
}
}
checkState(found, errorMessage);
}

private boolean areAdjacentCellsSafe(List<List<Integer>> map, int x, int y, int width, int height) {
return isCellSafe(map.get(max(y - 1, 0)).get(x)) // up
&& isCellSafe(map.get(y).get(min(x + 1, width - 1))) // right
&& isCellSafe(map.get(min(y + 1, height - 1)).get(x)) // down
&& isCellSafe(map.get(y).get(max(x - 1, 0))); // left
}

private boolean isCellSafe(Integer ordinal) {
return CellContent.valueOf(ordinal).isSafe();
}

private List<List<Integer>> simpleMap(int width, int height) {
List<List<Integer>> map = newArrayList();

for (int i = 0; i < height; i++) {
List<Integer> row = simpleMapRow(width, i);
map.add(row);
}
return map;
}

private List<Integer> simpleMapRow(int width, int rowIndex) {
List<Integer> row = newArrayList();

for (int j = 0; j < width; j++) {
if ((rowIndex % 2 * j) % 2 == 0) {
row.add(PILL.ordinal());
} else {
row.add(WALL.ordinal());
}
}
return row;
}

public int getLevel() {
return level;
}

public String toString() {
StringBuilder builder = new StringBuilder(String.format("Level: %d\n", level));
for (List<Integer> row : map) {
for (Integer cellContent : row) {
builder.append(CellContent.valueOf(cellContent).toSymbol());
}
builder.append("\n");
}
return builder.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package de.uni_leipzig.life.icfp2014.main;

import org.junit.Test;

import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;

public class MazeMapTest {

@Test
public void testGenerateMapLevel1() {
MazeMap mazeMap = new MazeMap(1);
assertThat(mazeMap.toString(), containsString("/"));
assertThat(mazeMap.toString(), containsString("="));
assertThat(mazeMap.toString(), containsString("."));
}

@Test
public void testGenerateMapLevel20() {
MazeMap mazeMap = new MazeMap(20);
assertThat(mazeMap.toString(), containsString("/"));
assertThat(mazeMap.toString(), containsString("="));
assertThat(mazeMap.toString(), containsString("."));
}

}