support simple mode

This commit is contained in:
foo 2016-12-19 12:28:56 +00:00
parent 92391f3899
commit 1e327216f5
3 changed files with 27 additions and 11 deletions

View File

@ -61,9 +61,12 @@ public class GridFactory {
} }
private static Direction[] directions = new Direction[] { Direction.N, Direction.NE, Direction.E, Direction.SE, Direction.S, Direction.SW, Direction.W, Direction.NW }; private static Direction[] directions = new Direction[] { Direction.N, Direction.NE, Direction.E, Direction.SE, Direction.S, Direction.SW, Direction.W, Direction.NW };
private static Direction getDirection() { private static Direction getDirection(boolean simple) {
return directions[rnd.nextInt(directions.length-1)]; if(simple) {
// return rnd.nextBoolean() ? Direction.E : Direction.S; return rnd.nextBoolean() ? Direction.E : Direction.S;
} else {
return directions[rnd.nextInt(directions.length-1)];
}
} }
private static Bounds getBounds(int height, int width, Direction direction, int length) { private static Bounds getBounds(int height, int width, Direction direction, int length) {
@ -152,6 +155,10 @@ public class GridFactory {
} }
public static char[][] makeGrid(List<String> words, int height, int width) { public static char[][] makeGrid(List<String> words, int height, int width) {
return makeGrid(words, height, width, false);
}
public static char[][] makeGrid(List<String> words, int height, int width, boolean simple) {
char[][] grid = new char[height][width]; char[][] grid = new char[height][width];
// Place words at random? // Place words at random?
@ -159,7 +166,7 @@ public class GridFactory {
int tries = 0; int tries = 0;
while(true) { while(true) {
try { try {
grid = placeWord(word, grid); grid = placeWord(word, grid, simple);
break; break;
} catch (CouldNotPlaceWordException e) { } catch (CouldNotPlaceWordException e) {
if(tries > MAX_TRIES) { if(tries > MAX_TRIES) {
@ -185,8 +192,8 @@ public class GridFactory {
return grid; return grid;
} }
private static char[][] placeWord(String word, char[][] grid) throws CouldNotPlaceWordException { private static char[][] placeWord(String word, char[][] grid, boolean simple) throws CouldNotPlaceWordException {
Direction direction = getDirection(); Direction direction = getDirection(simple);
Bounds b = getBounds(grid.length, grid[0].length, direction, word.length()); Bounds b = getBounds(grid.length, grid[0].length, direction, word.length());
// System.out.println("[" + word + "] bounds: " + b); // System.out.println("[" + word + "] bounds: " + b);

View File

@ -24,16 +24,24 @@
float: right; float: right;
width: 200px; width: 200px;
} }
@media only print {
.noprint {
display: none;
}
}
</style> </style>
</head> </head>
<%@ page import="uk.org.mafoo.wordsearch.*" %> <%@ page import="uk.org.mafoo.wordsearch.*" %>
<%@ page import="java.util.*" %> <%@ page import="java.util.*" %>
<%@ page import="org.apache.commons.lang.StringUtils" %> <%@ page import="org.apache.commons.lang.StringUtils" %>
<%@ page errorPage="error.jsp" %> <%@ page import="org.apache.commons.lang.StringEscapeUtils" %>
<% <%
int height = Integer.parseInt(request.getParameter("height")); int height = Integer.parseInt(request.getParameter("height"));
int width = Integer.parseInt(request.getParameter("width")); int width = Integer.parseInt(request.getParameter("width"));
boolean simple = request.getParameter("simple") != null;
String name = StringEscapeUtils.escapeHtml(request.getParameter("name"));
if (request.getParameter("words").length() > 2048) { throw new Exception("Input too large"); } if (request.getParameter("words").length() > 2048) { throw new Exception("Input too large"); }
if (height > 100 || width > 100) { throw new Exception("Dimentions too large"); } if (height > 100 || width > 100) { throw new Exception("Dimentions too large"); }
@ -44,14 +52,13 @@
} }
Collections.sort(words); Collections.sort(words);
char[][] grid = GridFactory.makeGrid(words, height, width, simple);
char[][] grid = GridFactory.makeGrid(words, height, width);
String csv = ""; String csv = "";
%> %>
<body> <body>
<h1>Wordsearch builder</h1> <h1><%= name %></h1>
<a id='csvdownload'>Download CSV</a> <a id='csvdownload' class="noprint">Download CSV</a>
<script> <script>
var csv = '<%= csv %>'; var csv = '<%= csv %>';
var csvdownload = document.getElementById('csvdownload'); var csvdownload = document.getElementById('csvdownload');

View File

@ -6,6 +6,7 @@
<h2>Words</h2> <h2>Words</h2>
<form action="build.jsp" method="post"> <form action="build.jsp" method="post">
Name: <input type="text" size="25" name="name" value="Wordsearch"><br />
<textarea name="words" rows="10"> <textarea name="words" rows="10">
Kitchen Kitchen
Lounge Lounge
@ -20,6 +21,7 @@ Dining Room
<br /> <br />
<input type="number" name="height" min="3" max="50" value="15" /> <input type="number" name="height" min="3" max="50" value="15" />
<input type="number" name="width" min="3" max="50" value="15" /> <input type="number" name="width" min="3" max="50" value="15" />
<input type="checkbox" name="simple" value="yes">Simple?
<input type="submit" value="Build!" /> <input type="submit" value="Build!" />
</form> </form>