From f35ea4c246b1d4e23996bc8453e6e19e578c3395 Mon Sep 17 00:00:00 2001 From: foo Date: Sun, 18 Dec 2016 07:43:01 +0000 Subject: [PATCH] working webapp --- uk/org/mafoo/wordsearch/Direction.java | 3 + uk/org/mafoo/wordsearch/GridFactory.java | 111 +++-------------------- war/WEB-INF/web.xml | 13 +++ war/build.jsp | 52 +++++++++++ war/error.jsp | 6 ++ war/index.jsp | 26 ++++++ 6 files changed, 114 insertions(+), 97 deletions(-) create mode 100644 uk/org/mafoo/wordsearch/Direction.java create mode 100755 war/WEB-INF/web.xml create mode 100755 war/build.jsp create mode 100755 war/error.jsp create mode 100755 war/index.jsp diff --git a/uk/org/mafoo/wordsearch/Direction.java b/uk/org/mafoo/wordsearch/Direction.java new file mode 100644 index 0000000..ff00264 --- /dev/null +++ b/uk/org/mafoo/wordsearch/Direction.java @@ -0,0 +1,3 @@ +package uk.org.mafoo.wordsearch; + +enum Direction { N, NE, E, SE, S, SW, W, NW; }; diff --git a/uk/org/mafoo/wordsearch/GridFactory.java b/uk/org/mafoo/wordsearch/GridFactory.java index 469fbd6..32bef33 100755 --- a/uk/org/mafoo/wordsearch/GridFactory.java +++ b/uk/org/mafoo/wordsearch/GridFactory.java @@ -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 words = new ArrayList(); + + 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 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 } } } diff --git a/war/WEB-INF/web.xml b/war/WEB-INF/web.xml new file mode 100755 index 0000000..48b2678 --- /dev/null +++ b/war/WEB-INF/web.xml @@ -0,0 +1,13 @@ + + + + + Wordsearch creator + + Wordsearch building app + + + diff --git a/war/build.jsp b/war/build.jsp new file mode 100755 index 0000000..444d4c5 --- /dev/null +++ b/war/build.jsp @@ -0,0 +1,52 @@ + + + Wordsearch builder + + +<%@ 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 words = new ArrayList(); + for ( String line : request.getParameter("words").split("\r\n")) { + words.add(line.trim()); + } + + char[][] grid = GridFactory.makeGrid(words, height, width); +%> + +

Wordsearch builder

+

Words

+
    +<% for (String word : words) { %> +
  • <%= word.trim() %>
  • +<% } %> +
+ +

Grid

+ +<% for(char[] row : grid) { %> + + <% for(char c : row) { %> + + <% } %> + +<% } %> +
<%= c %>
+
+ Generated +
+ + diff --git a/war/error.jsp b/war/error.jsp new file mode 100755 index 0000000..e816df0 --- /dev/null +++ b/war/error.jsp @@ -0,0 +1,6 @@ + + +<%@ page isErrorPage="true" %> +Error: <%= exception %> + + diff --git a/war/index.jsp b/war/index.jsp new file mode 100755 index 0000000..a510f82 --- /dev/null +++ b/war/index.jsp @@ -0,0 +1,26 @@ + +Wordsearch builder + +

Wordsearch builder

+

Words

+
+ +
+ + + +
+ + + +