From 1e327216f5cbc1c954849bb936ff01688c157b83 Mon Sep 17 00:00:00 2001 From: foo Date: Mon, 19 Dec 2016 12:28:56 +0000 Subject: [PATCH] support simple mode --- uk/org/mafoo/wordsearch/GridFactory.java | 19 +++++++++++++------ war/build.jsp | 17 ++++++++++++----- war/index.jsp | 2 ++ 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/uk/org/mafoo/wordsearch/GridFactory.java b/uk/org/mafoo/wordsearch/GridFactory.java index 80ea03d..eb223ea 100755 --- a/uk/org/mafoo/wordsearch/GridFactory.java +++ b/uk/org/mafoo/wordsearch/GridFactory.java @@ -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 getDirection() { - return directions[rnd.nextInt(directions.length-1)]; - // return rnd.nextBoolean() ? Direction.E : Direction.S; + private static Direction getDirection(boolean simple) { + if(simple) { + 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) { @@ -152,6 +155,10 @@ public class GridFactory { } public static char[][] makeGrid(List words, int height, int width) { + return makeGrid(words, height, width, false); + } + + public static char[][] makeGrid(List words, int height, int width, boolean simple) { char[][] grid = new char[height][width]; // Place words at random? @@ -159,7 +166,7 @@ public class GridFactory { int tries = 0; while(true) { try { - grid = placeWord(word, grid); + grid = placeWord(word, grid, simple); break; } catch (CouldNotPlaceWordException e) { if(tries > MAX_TRIES) { @@ -185,8 +192,8 @@ public class GridFactory { return grid; } - private static char[][] placeWord(String word, char[][] grid) throws CouldNotPlaceWordException { - Direction direction = getDirection(); + private static char[][] placeWord(String word, char[][] grid, boolean simple) throws CouldNotPlaceWordException { + Direction direction = getDirection(simple); Bounds b = getBounds(grid.length, grid[0].length, direction, word.length()); // System.out.println("[" + word + "] bounds: " + b); diff --git a/war/build.jsp b/war/build.jsp index 299e845..8cc7ead 100755 --- a/war/build.jsp +++ b/war/build.jsp @@ -24,16 +24,24 @@ float: right; width: 200px; } + + @media only print { + .noprint { + display: none; + } + } <%@ page import="uk.org.mafoo.wordsearch.*" %> <%@ page import="java.util.*" %> <%@ 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 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 (height > 100 || width > 100) { throw new Exception("Dimentions too large"); } @@ -44,14 +52,13 @@ } Collections.sort(words); - - char[][] grid = GridFactory.makeGrid(words, height, width); + char[][] grid = GridFactory.makeGrid(words, height, width, simple); String csv = ""; %> -

Wordsearch builder

-Download CSV +

<%= name %>

+Download CSV