ERLEDIGT
NEIN
NEIN
ANTWORTEN
0
0
ZUGRIFFE
1187
1187
EMPFEHLEN
-
Mein Lösungsansatz für Stufe 1 basiert auf einem Buchstaben-Index.
Zunächst wird ein Lösungswort aus dem Wörterbuch gewählt.
Anschließend wird buchstabenweise aus dem Index ein passender Wörterbucheintrag geholt.
Optional könnte man noch bereits verwendete Wörter aus dem Index entfernen, damit keine doppelten Worte benutzt werden.
An die höheren Stufen habe ich mich nicht rangemacht, bin aber gespannt auf die Lösungs-Ansätze
a sample dictionary file crossword.csvCode :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
/** * crossword generator, 13.08.2010 thilo * see [url]http://www.tutorials.de/diskussion/364656-quiz-17-woerter-kreuz-und-quer.html[/url] */ import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Hashtable; import java.util.List; import java.util.Map; /** * simple crossword generator * @author Thilo * */ public class Crossword { /** * @param args */ public static void main(String[] args) { // String curDir = System.getProperty("user.dir"); Crossword app = new Crossword(); if(app.loadFile("res/crossword.csv")) //see bottom of this file for a dictionary sample { app.generateCrossword(); } } public boolean loadFile(String filename) { //Open the file for reading try { BufferedReader br = new BufferedReader(new FileReader(filename)); String thisLine; while ((thisLine = br.readLine()) != null) { System.out.println(thisLine); if(thisLine.startsWith("#")) continue; int idx = thisLine.indexOf(';'); if(idx > 0) { String question = thisLine.substring(0, idx); String answer = thisLine.substring(idx+1, thisLine.length()).toUpperCase(); //TODO: perfect for multiple hint to the same answer: thisLine.split(";"); dict.add(new DictEntry(question, answer)); } } // end while } // end try catch (IOException e) { System.err.println("Error: " + e); return false; } System.out.println(); System.out.println("Dict size: "+dict.size()); return true; } void createLetterIndexForDictionary() { letterIndex.clear(); //generate the letter index from dict for(int i=0; i<dict.size(); i++) { String answer = dict.get(i).getAnswer(); for(int j=0; j<answer.length(); j++) { char ch = answer.charAt(j); if(false==letterIndex.containsKey(ch)) letterIndex.put(ch, new ArrayList<Integer>()); //add dict index for letter letterIndex.get(ch).add(i); } } } public void generateCrossword() { createLetterIndexForDictionary(); System.out.println("LetterIndex size: "+letterIndex.size()); System.out.println(); System.out.println("generated crossword"); //select a random index and word as solution int idxSolution = (int)(dict.size()*Math.random()); //enable line below for testing idxSolution = 0; //tutorials.de String solution = dict.get(idxSolution).getAnswer(); System.out.println("solution=\""+solution+"\" dict-index="+idxSolution); int[] idxHints = new int[solution.length()]; //-1 for no hint int[] offsetHints = new int[solution.length()]; //generate the crosswords solution hints for(int i=0; i<solution.length(); i++) { //build hint for solution i-th letter char ch = solution.charAt(i); //find an answer in the dictionary which contains letter ch if(letterIndex.get(ch)==null) { idxHints[i]=-1; } else { //clone the letter index list and remove all references to the solution List<Integer> chIndex = new ArrayList<Integer>(letterIndex.get(ch)); //TODO: remove indices of duplicate answers, change crossword.csv to format "answer;hint1;hint2;.." while(chIndex.remove(new Integer(idxSolution))) { //empty body } if(chIndex.size()<1) { //letter is only contained in solution, don't hint idxHints[i]=-1; } else { //use a random list element idxHints[i]=chIndex.get((int)(Math.random()*chIndex.size())); //enable for testing idxHints[i]=chIndex.get(0); //use first occurrence of letter ch offsetHints[i]=dict.get(idxHints[i]).getAnswer().indexOf(ch); } } } //print generated crossword System.out.println("crossword:"); //determine max offset for indentation int maxOffset = 0; for(int l : offsetHints) if(l>maxOffset) maxOffset=l; for(int i=0; i<solution.length(); i++) { System.out.printf("%2d: ",i); //indent space related to offset for(int j=0; j<maxOffset-offsetHints[i]; j++) System.out.print(" "); //write hint if(idxHints[i]<0) { // System.out.printf("[_]"); //write unknown System.out.printf("[%c]",solution.charAt(i)); //write solution char } else { for(int k=0; k<dict.get(idxHints[i]).getAnswer().length(); k++) System.out.printf("[%c]", dict.get(idxHints[i]).getAnswer().charAt(k)); } System.out.println(); } //print generated crossword hints System.out.println(); for(int i=0; i<solution.length(); i++) { if(idxHints[i]<0) continue; System.out.printf("%2d: %s", i, dict.get(idxHints[i]).getQuestion()); } } public Crossword() { dict = new ArrayList<DictEntry>(); letterIndex = new Hashtable<Character, List<Integer>>(); } /** inner class representing crossword dictionary entries */ class DictEntry { String question; String answer; DictEntry(String question, String answer) { this.question = question; this.answer = answer; } String getQuestion() { return question; } String getAnswer() { return answer; } } /** the crossword dictionary container */ List<DictEntry> dict; /** the crossword dictionary letter index * key - 'A' to 'Z' * value - list holding indices of occurrences in dict */ Map<Character,List<Integer>> letterIndex; }
Lösung, KonsolenausgabeCode :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#description;answer //CSV header Webseite für alles;tutorials.de Textauszeichnungssprache (Abk.);html Freies Betriebssystem;linux Programm zur Fotobearbeitung;Photoshop Ausstellungsort für Kunstwerke;9 Sammelbegriff für physische Bestandteile eines Rechners;Hardware 3D-Programm;8 Programmiersprache von Oracle;Java Datenbanksprache (Abk.);SQL Browser-Skriptsprache;10 Betriebssystem aus Redmond;Windows ... helfen ...n;User Standard-IDE für Java;eclipse Betriebssystem;Windows Online-Anleitung;Tutorial abc;Alphabet Tier mit 4 Beinen;Hund Tier mit 4 Beinen;Katze Tier mit 4 Beinen;Maus Tier mit 2 Beinen;Vogel Fliegendes Tier;Vogel
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
generated crossword solution="TUTORIALS.DE" dict-index=0 crossword: 0: [H][T][M][L] 1: [L][I][N][U][X] 2: [H][T][M][L] 3: [P][H][O][T][O][S][H][O][P] 4: [H][A][R][D][W][A][R][E] 5: [L][I][N][U][X] 6: [H][A][R][D][W][A][R][E] 7: [H][T][M][L] 8: [P][H][O][T][O][S][H][O][P] 9: [.] 10: [H][A][R][D][W][A][R][E] 11: [H][A][R][D][W][A][R][E] 0: Textauszeichnungssprache (Abk.) 1: Freies Betriebssystem 2: Textauszeichnungssprache (Abk.) 3: Programm zur Fotobearbeitung 4: Sammelbegriff für physische Bestandteile eines Rechners 5: Freies Betriebssystem 6: Sammelbegriff für physische Bestandteile eines Rechners 7: Textauszeichnungssprache (Abk.) 8: Programm zur Fotobearbeitung 10: Sammelbegriff für physische Bestandteile eines Rechners 11: Sammelbegriff für physische Bestandteile eines Rechners
Ähnliche Themen
-
Quiz XML mit Java DOM
Von Simon Berger im Forum XML TechnologienAntworten: 1Letzter Beitrag: 16.06.10, 08:20 -
[QUIZ#9]HonniCilest(Java)
Von HonniCilest im Forum ArchivAntworten: 2Letzter Beitrag: 05.08.09, 08:41 -
[QUIZ#04] zeja (Java)
Von zeja im Forum ArchivAntworten: 1Letzter Beitrag: 12.10.08, 18:38 -
[QUIZ#03] zeja (Java)
Von zeja im Forum ArchivAntworten: 0Letzter Beitrag: 05.10.08, 12:10 -
Quiz mit Java?
Von fraus im Forum Javascript & AjaxAntworten: 2Letzter Beitrag: 13.07.01, 10:22





Login





