Skip to content

Commit

Permalink
relocate random, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juhakaup committed Apr 16, 2021
1 parent 0528372 commit 5f3c20a
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 32 deletions.
17 changes: 17 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
id 'jacoco'
id 'checkstyle'
id 'org.openjfx.javafxplugin' version '0.0.9'
id "com.github.johnrengelman.shadow" version "6.1.0"
}

compileJava.options.encoding = "UTF-8"
Expand All @@ -20,6 +21,12 @@ dependencies {

mainClassName = 'WFC_dungeon_gen.Main'

jar {
manifest {
attributes 'Main-Class': 'WFC_dungeon_gen.Main'
}
}

run {
standardInput = System.in
}
Expand All @@ -29,6 +36,16 @@ jacocoTestReport {
xml.enabled = true
html.enabled = true
}

afterEvaluate {
classDirectories.from = files(classDirectories.files.collect {
fileTree(dir: it, exclude: [
'**/ui/*','**/WFC_dungeon_gen/*.*'


])
})
}
}

javafx {
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/WFC_dungeon_gen/domain/Solver.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package WFC_dungeon_gen.domain;

import WFC_dungeon_gen.util.MyRandom;
import static WFC_dungeon_gen.domain.Direction.*;
import WFC_dungeon_gen.util.TileQueue;
import java.util.Arrays;
Expand All @@ -19,13 +20,15 @@ public class Solver {
private final MyRandom random;
private TileQueue entropyQueue = null;
private TileQueue propagatorQueue = null;
private int rounds;

public Solver(int width, int depth, int numTiles, int[] weights, boolean[][][] rules) {
this.numPossibleTiles = numTiles;
this.tileWeights = weights;
this.adjacencyRules = rules;
this.width = width;
this.depth = depth;
this.rounds = 0;
this.random = new MyRandom(12346);
this.dungeonMap = null;
initMap();
Expand Down Expand Up @@ -58,6 +61,7 @@ public int[][] generateMap() {
propagate(nextTile);
}
}
this.rounds = 0;
return convertToTileIds(this.dungeonMap);
}

Expand Down Expand Up @@ -198,8 +202,9 @@ private void reduce(int row, int col, Direction dir, boolean[] propagatorTiles)

Boolean tileChanged = neighbour.setAvalableTiles(availableTiles);
if (tileChanged == null) {
System.out.println("Propagation error");
// TODO graceful handling of contradiction
System.out.print("Propagation error, retry round " + this.rounds++ + "\r");
initMap();
generateMap();
}
else if (tileChanged) {
this.propagatorQueue.add(neighbour);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/WFC_dungeon_gen/domain/Tile.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Tile {
private int sumOfPossibleWeights;

public Tile(double noise, int[] weights, int row, int col) {
this.finalValue = 6;
this.finalValue = -1;
this.randomNoise = noise;
this.tileWeights = weights;
this.row = row;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/WFC_dungeon_gen/ui/GeneratorUi.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
import WFC_dungeon_gen.dao.TileSetDao;
import WFC_dungeon_gen.dao.TileSetJsonDao;
import WFC_dungeon_gen.dao.TileSetTestData;
import WFC_dungeon_gen.domain.Solver;
import WFC_dungeon_gen.domain.TileSet;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -119,7 +120,11 @@ private void displayMap() {
for (int col = 0; col < this.mapWidth; col++) {
int tileNum = this.map[row][col];
for (int i = 0; i < this.tileWidth; i++) {
if (tileNum == -1) {
output += " ";
} else {
output += (tiles[tileNum][j][i]);
}
}
}
output += "\n";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package WFC_dungeon_gen.domain;
package WFC_dungeon_gen.util;

import java.util.Random;

Expand Down
87 changes: 59 additions & 28 deletions src/test/java/WFC_dungeon_gen/domain/SolverTest.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,79 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package WFC_dungeon_gen.domain;

import org.junit.After;
import org.junit.AfterClass;
import WFC_dungeon_gen.dao.TileSetTestData;
import java.io.FileNotFoundException;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
*
* @author scyti
* @author Juha Kauppinen
*/
public class SolverTest {


private Solver map;
private int width;
private int depth;
private TileSet tileSet;

public SolverTest() {
this.depth = 5;
this.width = 5;
try {
tileSet = new TileSetTestData().loadTileSet("hello");
} catch (FileNotFoundException ex) {
System.out.println("error loading data");
}
}

@BeforeClass
public static void setUpClass() {
}

@AfterClass
public static void tearDownClass() {
}


@Before
public void setUp() {
int numTiles = tileSet.getNumberOfTiles();
boolean[][][] rules = tileSet.getAdjacencyRules();
int[] weights = tileSet.getTileWeights();
this.map = new Solver(this.width, this.depth, numTiles, weights, rules);
}

@Test
public void mapInitializesToRightDimension() {
int[][] intMap = this.map.getMap();
assertEquals(this.width, intMap[0].length);
assertEquals(this.depth, intMap.length);
}

@After
public void tearDown() {

@Test
public void mapIsInitializedToMinusOne() {
this.map.initMap();
int[][] intMap = this.map.getMap();
for (int i = 0; i < this.depth; i++) {
for (int j = 0; j < this.width; j++) {
assertEquals(-1, intMap[i][j]);
}
}
}

/**
* Test of printMaze method, of class Solver.
*/
@Test
public void testPrintMaze() {
assertEquals(true, true);
public void tilesAreSelectedRandomly() {
int sum = 0;
int cycles = 1000;

for (int x = 0; x < cycles; x++) {
map.initMap();
map.step();
int[][] intMap = map.getMap();

for (int i = 0; i < depth; i++) {
for (int j = 0; j < width; j++) {
if (intMap[i][j] != -1) {
int value = i * depth + j;
sum += value;
}
}
}
}
int average = sum / cycles;
assertEquals(average, (width * depth) / 2, 2);
}

}

0 comments on commit 5f3c20a

Please sign in to comment.