Created
March 1, 2015 09:02
-
-
Save snarkbait/847fe9197310266c3616 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package philyboyd_studge; | |
public class MinecraftChallenge { | |
public static void main(String[] args) { | |
World world = new World(); | |
world.updateWorld(); | |
while (!world.isDead() || world.isComplete()) | |
{ | |
world.updateHero(); | |
// world.displaySlices(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package philyboyd_studge; | |
public class Node implements Comparable<Node> | |
{ | |
private int dist,x,y,z; | |
private World.Move move; | |
public Node(World.Move m, int mx, int my, int mz) | |
{ | |
move = m; | |
x = mx; | |
y = my; | |
z = mz; | |
dist = World.manhattanDistance(x,y,z); | |
} | |
public World.Move getMove() | |
{ | |
return move; | |
} | |
public int getDist() | |
{ | |
return dist; | |
} | |
public int getX() | |
{ | |
return x; | |
} | |
public int getY() | |
{ | |
return y; | |
} | |
public int getZ() | |
{ | |
return z; | |
} | |
@Override | |
public int compareTo(Node n) | |
{ | |
return this.dist - n.getDist(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package philyboyd_studge; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Random; | |
public class World { | |
public static final Random random = new Random(); | |
public enum Block { AIR, DIRT, SAND, LAVA, DIAMOND, HERO } | |
public enum Move { MOVE_NORTH, MOVE_EAST, MOVE_SOUTH, MOVE_WEST, JUMP_NORTH, JUMP_EAST, JUMP_SOUTH, JUMP_WEST, | |
DIG_NORTH, DIG_EAST, DIG_SOUTH, DIG_WEST, DIG_UP, DIG_DOWN, TAKE_DIAMOND } | |
/* // get random enum, skipping last two | |
public static <T extends Enum<?>>T randomEnum(Class<T> thisclass) | |
{ | |
int x = getRandomInt(1, 100); | |
int x = random.nextInt(thisclass.getEnumConstants().length - 2); | |
return thisclass.getEnumConstants()[x]; | |
}*/ | |
public static Block getRandomBlock() | |
{ | |
Block result = null; | |
int rand = random.nextInt(100); | |
if (rand < 10) result = Block.LAVA; | |
else if (rand < 25) result = Block.AIR; | |
else if (rand < 60) result = Block.SAND; | |
else result = Block.DIRT; | |
return result; | |
} | |
public static int getRandomInt(int low, int high) | |
{ | |
return random.nextInt(high - low + 1) + low; | |
} | |
private int heroX, heroY, heroZ; | |
private static int targetX, targetY, targetZ; | |
private boolean heroIsDead; | |
private boolean hasDiamond; | |
private boolean backToStart; | |
private ArrayList<Node> moveList; | |
private ArrayList<Node> path; | |
private Block map[][][]; | |
public World() | |
{ | |
map = new Block[100][100][10]; | |
targetX = 99; | |
targetY = 99; | |
targetZ = 9; | |
path = new ArrayList<>(); | |
System.out.println("Building World..."); | |
long start = System.currentTimeMillis(); | |
for (int mapZ = 0; mapZ < 10; mapZ++) | |
{ | |
for (int mapX = 0; mapX < 100; mapX++) | |
{ | |
for (int mapY = 0; mapY < 100; mapY++) | |
{ | |
if (mapX == 0 && mapY == 0 && mapZ == 0) map[mapX][mapY][mapZ] = Block.HERO; | |
else if (mapZ == 0) map[mapX][mapY][mapZ] = Block.AIR; | |
else if (mapZ == 1 && mapX == 0 && mapY == 0) map[mapX][mapY][mapZ] = Block.DIRT; | |
else if (mapX == 99 && mapY == 99 && mapZ == 9) map[mapX][mapY][mapZ] = Block.DIAMOND; | |
else map[mapX][mapY][mapZ] = getRandomBlock(); | |
} | |
} | |
} | |
System.out.println("Time elapsed:" + (System.currentTimeMillis() - start) + " milliseconds"); | |
} | |
public Block getMapBlock(int x, int y, int z) | |
{ | |
return map[x][y][z]; | |
} | |
public boolean isDead() | |
{ | |
return heroIsDead; | |
} | |
public boolean isComplete() | |
{ | |
return backToStart; | |
} | |
public static int manhattanDistance(int x, int y, int z) | |
{ | |
return (Math.abs(targetX - x)) + (Math.abs(targetY - y)) + (Math.abs(targetZ - z)); | |
} | |
// recursively check gravity on sand, player and lava | |
public void updateWorld() | |
{ | |
int heroDropCount = 0; | |
boolean changed = false; | |
// System.out.println("Checking gravity..."); | |
for (int z = 0; z < 9; z++) | |
{ | |
for (int x = 0; x < 100; x++) | |
{ | |
for (int y = 0; y < 100; y++) | |
{ | |
if (z !=0) | |
{ | |
if (map[x][y][z] == Block.SAND) | |
{ | |
if (map[x][y][z + 1] == Block.AIR) | |
{ | |
map[x][y][z] = Block.AIR; | |
map[x][y][z + 1] = Block.SAND; | |
changed = true; | |
} | |
} | |
if (map[x][y][z] == Block.LAVA) | |
{ | |
if (map[x][y][z + 1] == Block.AIR) | |
{ | |
map[x][y][z + 1] = Block.LAVA; | |
changed = true; | |
} | |
if (map[x][y][z + 1] == Block.HERO) | |
{ | |
heroIsDead = true; | |
System.out.println("lava on head"); | |
} | |
} | |
} | |
if (map[x][y][z] == Block.HERO) | |
{ | |
//System.out.println("Hero found"); | |
if (map[x][y][z + 1] == Block.AIR) | |
{ | |
System.out.println("Falling..."); | |
heroDropCount++; | |
if (heroDropCount >= 5) heroIsDead = true; | |
map[x][y][z] = Block.AIR; | |
map[x][y][z + 1] = Block.HERO; | |
heroX = x; | |
heroY = y; | |
heroZ = z + 1; | |
changed = true; | |
} | |
if (map[x][y][z + 1] == Block.LAVA) | |
{ | |
System.out.println("ouch. HOT"); | |
heroIsDead = true; | |
changed = true; | |
} | |
} | |
} | |
} | |
} | |
if (heroIsDead) | |
{ | |
System.out.println("YOU ARE DEDED"); | |
} | |
if (changed && !heroIsDead) updateWorld(); | |
} | |
public void moveHero(Node n) | |
{ | |
map[heroX][heroY][heroZ] = Block.AIR; | |
heroX = n.getX(); | |
heroY = n.getY(); | |
heroZ = n.getZ(); | |
if (map[heroX][heroY][heroZ]==Block.DIAMOND) | |
{ | |
System.out.println("GOT THE DIAMOND BABY"); | |
hasDiamond = true; | |
targetX = 0; | |
targetY = 0; | |
targetZ = 0; | |
displaySlices(); | |
} | |
map[heroX][heroY][heroZ] = Block.HERO; | |
path.add(n); | |
System.out.println(n.getMove() + ":Moving hero to:" + heroX + "," + heroY + "," + heroZ); | |
} | |
public ArrayList<Node> getMoves() | |
{ | |
ArrayList<Node> tempList = new ArrayList<>(); | |
for (Move m : Move.values()) | |
{ | |
switch (m) | |
{ | |
case MOVE_NORTH: | |
int y = heroY - 1; | |
if (y >= 0) | |
{ | |
if (canMoveTo(heroX,y,heroZ)) | |
{ | |
Node n = new Node(m, heroX, y, heroZ); | |
tempList.add(n); | |
} | |
} | |
break; | |
case MOVE_EAST: | |
int x = heroX + 1; | |
if (x < 100) | |
{ | |
if (canMoveTo(x,heroY,heroZ)) | |
{ | |
Node n = new Node(m, x, heroY, heroZ); | |
tempList.add(n); | |
} | |
} | |
break; | |
case MOVE_SOUTH: | |
y = heroY + 1; | |
if (y < 100) | |
{ | |
if (canMoveTo(heroX,y,heroZ)) | |
{ | |
Node n = new Node(m, heroX, y, heroZ); | |
tempList.add(n); | |
} | |
} | |
break; | |
case MOVE_WEST: | |
x = heroX - 1; | |
if (x >=0) | |
{ | |
if (canMoveTo(x, heroY, heroZ)) | |
{ | |
Node n = new Node(m, x, heroY, heroZ); | |
tempList.add(n); | |
} | |
} | |
break; | |
case JUMP_NORTH: | |
y = heroY - 1; | |
int z = heroZ - 1; | |
if (y >= 0 && z > 0) | |
{ | |
if (canJumpTo(heroX,y,z)) | |
{ | |
Node n = new Node(m, heroX, y, z); | |
tempList.add(n); | |
} | |
} | |
break; | |
case JUMP_EAST: | |
x = heroX + 1; | |
z = heroZ - 1; | |
if (x < 100 && z > 0) | |
{ | |
if (canJumpTo(x,heroY,z)) | |
{ | |
Node n = new Node(m, x, heroY, z); | |
tempList.add(n); | |
} | |
} | |
break; | |
case JUMP_SOUTH: | |
y = heroY + 1; | |
z = heroZ - 1; | |
if (y < 100 && z >0) | |
{ | |
if (canJumpTo(heroX,y,z)) | |
{ | |
Node n = new Node(m, heroX,y,z); | |
tempList.add(n); | |
} | |
} | |
break; | |
case JUMP_WEST: | |
x = heroX - 1; | |
z = heroZ - 1; | |
if (x >= 0 && z >0) | |
{ | |
if (canJumpTo(x,heroY,z)) | |
{ | |
Node n = new Node(m, x, heroY, z); | |
tempList.add(n); | |
} | |
} | |
break; | |
case DIG_NORTH: | |
y = heroY - 1; | |
if (y >=0) | |
{ | |
if (canDigTo(heroX,y,heroZ)) | |
{ | |
Node n = new Node(m,heroX, y, heroZ); | |
tempList.add(n); | |
} | |
} | |
break; | |
case DIG_EAST: | |
x = heroX + 1; | |
if (x < 100) | |
{ | |
if (canDigTo(x, heroY, heroZ)) | |
{ | |
Node n = new Node(m, x, heroY, heroZ); | |
tempList.add(n); | |
} | |
} | |
break; | |
case DIG_SOUTH: | |
y = heroY + 1; | |
if (y < 100) | |
{ | |
if (canDigTo(heroX,y,heroZ)) | |
{ | |
Node n = new Node(m,heroX,y,heroZ); | |
tempList.add(n); | |
} | |
} | |
break; | |
case DIG_WEST: | |
x = heroX - 1; | |
if (x >=0) | |
{ | |
if (canDigTo(x, heroY, heroZ)) | |
{ | |
Node n = new Node(m,x,heroY,heroZ); | |
tempList.add(n); | |
} | |
} | |
break; | |
case DIG_UP: | |
z = heroZ - 1; | |
if (z >=0) | |
{ | |
if (canDigTo(heroX,heroY,z)) | |
{ | |
Node n = new Node(m,heroX,heroY,z); | |
tempList.add(n); | |
} | |
} | |
break; | |
case DIG_DOWN: | |
z = heroZ + 1; | |
if (z < 10) | |
{ | |
if (canDigTo(heroX,heroY,z)) | |
{ | |
Node n = new Node(m, heroX, heroY, z); | |
tempList.add(n); | |
} | |
} | |
break; | |
case TAKE_DIAMOND: | |
if (manhattanDistance(heroX,heroY,heroZ) < 3) | |
{ | |
Node n = new Node(m, 99,99,9); | |
tempList.add(n); | |
} | |
break; | |
} | |
} | |
return tempList; | |
} | |
public void updateHero() | |
{ | |
moveList = getMoves(); | |
Collections.sort(moveList); | |
// for (Node current : moveList) | |
// { | |
// System.out.println(current.getMove() + ":" + current.getDist()); | |
// } | |
if (moveList.isEmpty()) | |
{ | |
System.out.println("No moves."); | |
System.exit(0); | |
} | |
if (moveList.size() > 0) | |
{ | |
if (moveList.size() == 4) | |
{ | |
int rand = random.nextInt(100); | |
if (rand < 10) moveHero(moveList.get(3)); | |
else if (rand < 25) moveHero(moveList.get(2)); | |
else if (rand < 60) moveHero(moveList.get(1)); | |
else moveHero(moveList.get(0)); | |
} | |
else | |
{ | |
moveHero(moveList.get(random.nextInt(moveList.size()))); | |
} | |
} | |
else moveHero(moveList.get(0)); | |
updateWorld(); | |
} | |
/* | |
* Test move functions | |
*/ | |
public boolean canMoveTo(int x, int y, int z) | |
{ | |
if (z < 9) return (map[x][y][z] == Block.AIR && map[x][y][z + 1] != Block.LAVA); | |
return map[x][y][z] == Block.AIR; | |
} | |
public boolean canJumpTo(int x, int y, int z) | |
{ | |
return ((map[x][y][z + 1] == Block.DIRT || map [x][y][z + 1] == Block.SAND) | |
&& (map[x][y][z] == Block.AIR && map[heroX][heroY][heroZ - 1] == Block.AIR)); | |
} | |
public boolean canDigTo(int x, int y, int z) | |
{ | |
if (map[x][y][z]==Block.DIRT || map[x][y][z]==Block.SAND) | |
{ | |
if (map[x][y][z - 1] != Block.LAVA) | |
{ | |
if (z <= 8) | |
{ | |
if ((map[x][y][z+1] != Block.LAVA) && checkVertical(x,y,z + 1)) return true; | |
} | |
else | |
{ return true; } | |
} | |
} | |
return false; | |
} | |
public boolean checkVertical(int x, int y, int z) | |
{ | |
for (int i = z; i < 9; i++) | |
{ | |
if (map[x][y][i]==Block.AIR) | |
{ | |
if (map[x][y][i + 1] == Block.LAVA) return false; | |
} | |
} | |
return true; | |
} | |
public void displaySlices() | |
{ | |
System.out.println("TOP============>"); | |
for (int y = ((heroY < 5) ? 0 : heroY - 5); y < ((heroY > 94) ? 99 : heroY + 5); y++) | |
{ | |
for (int x = ((heroX < 5) ? 0 : heroX - 5); x < ((heroX > 94) ? 99 : heroX + 5); x++) | |
{ | |
System.out.print("["+String.format("%-4s",getMapBlock(x,y,heroZ))+"] "); | |
} | |
System.out.print("\n"); | |
} | |
System.out.println("x side==============>"); | |
for (int z = 0; z < 10; z++) | |
{ | |
for (int x = ((heroX < 5) ? 0 : heroX - 5); x < ((heroX > 94) ? 99 : heroX + 5); x++) | |
{ | |
System.out.print("["+String.format("%-4s",getMapBlock(x,heroY,z))+"] "); | |
} | |
System.out.print("\n"); | |
} | |
System.out.println("y side==============>"); | |
for (int z = 0; z < 10; z++) | |
{ | |
for (int y = ((heroY < 5) ? 0 : heroY - 5); y < ((heroY > 94) ? 99 : heroY + 5); y++) | |
{ | |
System.out.print("["+String.format("%-4s",getMapBlock(heroX,y,z))+"] "); | |
} | |
System.out.print("\n"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment