working webapp

This commit is contained in:
foo 2016-12-18 07:43:01 +00:00
parent 865b8df406
commit f35ea4c246
6 changed files with 114 additions and 97 deletions

View File

@ -0,0 +1,3 @@
package uk.org.mafoo.wordsearch;
enum Direction { N, NE, E, SE, S, SW, W, NW; };

View File

@ -1,8 +1,9 @@
package uk.org.mafoo.wordsearch;
import java.io.*;
import java.util.*;
class GridFactory {
public class GridFactory {
static Random rnd = new Random();
static final int MAX_TRIES = 500;
@ -46,105 +47,19 @@ class GridFactory {
return chars[drng.getDistributedRandomNumber()];
}
public static void main(String[] args) {
public static void main(String[] args) throws IOException {
// Test invocation
char[][] g = makeGrid(new String[] {
"Aberdeenshire",
"Anglesey",
"Angus",
"Antrim",
"Argyll",
"Armagh",
"Ayrshire",
"Banffshire",
"Bedfordshire",
"Berkshire",
"Berwickshire",
"Brecknockshire",
"Buckinghamshire",
"Buteshire",
"Caernarfonshire",
"Caithness",
"Cambridgeshire",
"Cardiganshire",
"Carmarthenshire",
"Cheshire",
"Clackmannanshire",
"Cornwall",
"Cromartyshire",
"Cumberland",
"Denbighshire",
"Derbyshire",
"Devon",
"Dorset",
"Down",
"Dumbartonshire",
"Dumfriesshire",
"Durham",
"East Lothian",
"Essex",
"Fermanagh",
"Fife",
"Flintshire",
"Glamorgan",
"Gloucestershire",
"Hampshire",
"Herefordshire",
"Hertfordshire",
"Huntingdonshire",
"Invernessshire",
"Kent",
"Kincardineshire",
"Kirkcudbrightshire",
"Lanarkshire",
"Lancashire",
"Leicestershire",
"Lincolnshire",
"Londonderry",
"Merionethshire",
"Middlesex",
"Midlothian",
"Monmouthshire",
"Montgomeryshire",
"Morayshire",
"Nairnshire",
"Norfolk",
"Northamptonshire",
"Northumberland",
"Nottinghamshire",
"Orkney",
"Oxfordshire",
"Peeblesshire",
"Pembrokeshire",
"Perthshire",
"Radnorshire",
"Renfrewshire",
"Rossshire",
"Roxburghshire",
"Rutland",
"Selkirkshire",
"Shetland",
"Shropshire",
"Somerset",
"Staffordshire",
"Stirlingshire",
"Suffolk",
"Surrey",
"Sussex",
"Sutherland",
"Tyrone",
"Warwickshire",
"West Lothian",
"Westmorland",
"Wigtownshire",
"Wiltshire",
"Worcestershire",
"Yorkshire",
}, Integer.parseInt(args[0]), Integer.parseInt(args[1]));
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s;
List<String> words = new ArrayList<String>();
while ((s = in.readLine()) != null && s.length() != 0) {
words.add(s);
}
char[][] g = makeGrid(words, Integer.parseInt(args[0]), Integer.parseInt(args[1]));
dump2d(g);
}
private enum Direction { N, NE, E, SE, S, SW, W, 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() {
return directions[rnd.nextInt(directions.length-1)];
@ -236,7 +151,7 @@ class GridFactory {
return x;
}
public static char[][] makeGrid(String[] words, int height, int width) {
public static char[][] makeGrid(List<String> words, int height, int width) {
char[][] grid = new char[height][width];
// Place words at random?
@ -252,6 +167,8 @@ class GridFactory {
}
tries++;
// System.out.println("[" + word + "] Crossed over... retrying");
} finally {
// Nothing
}
}
}

13
war/WEB-INF/web.xml Executable file
View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Wordsearch creator</display-name>
<description>
Wordsearch building app
</description>
</web-app>

52
war/build.jsp Executable file
View File

@ -0,0 +1,52 @@
<html>
<head>
<title>Wordsearch builder</title>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
padding: 2px 4px;
}
</style>
</head>
<%@ page import="uk.org.mafoo.wordsearch.*" %>
<%@ page import="java.util.*" %>
<%@ page errorPage="error.jsp" %>
<%
int height = Integer.parseInt(request.getParameter("height"));
int width = Integer.parseInt(request.getParameter("width"));
if (request.getParameter("words").length() > 2048) { throw new Exception("Input too large"); }
List<String> words = new ArrayList<String>();
for ( String line : request.getParameter("words").split("\r\n")) {
words.add(line.trim());
}
char[][] grid = GridFactory.makeGrid(words, height, width);
%>
<body>
<h1>Wordsearch builder</h1>
<h2>Words</h2>
<ul>
<% for (String word : words) { %>
<li><%= word.trim() %></li>
<% } %>
</ul>
<h2>Grid</h2>
<table style="font-family: monospace;">
<% for(char[] row : grid) { %>
<tr>
<% for(char c : row) { %>
<td><%= c %></td>
<% } %>
</tr>
<% } %>
</table>
<footer>
Generated
</footer>
</body>
</html>

6
war/error.jsp Executable file
View File

@ -0,0 +1,6 @@
<HTML>
<BODY>
<%@ page isErrorPage="true" %>
Error: <%= exception %>
</BODY>
</HTML>

26
war/index.jsp Executable file
View File

@ -0,0 +1,26 @@
<html>
<head><title>Wordsearch builder</title></head>
<body>
<h1>Wordsearch builder</h1>
<h2>Words</h2>
<form action="build.jsp">
<textarea name="words" rows="10">
Kitchen
Lounge
Study
Ballroom
Conservatory
Billiard Room
Library
Hall
Dining Room
</textarea>
<br />
<input type="number" name="height" min="3" max="50" value="5" />
<input type="number" name="width" min="3" max="50" value="5" />
<input type="submit" value="Build!" />
</form>
</body>
</html>